/* * NOSA HEADER START * * The contents of this file are subject to the terms of the NASA Open * Source Agreement (NOSA), Version 1.3 only (the "Agreement"). You may * not use this file except in compliance with the Agreement. * * You can obtain a copy of the agreement at * docs/NASA_Open_Source_Agreement_1.3.txt * or * http://cdaweb.gsfc.nasa.gov/WebServices/NASA_Open_Source_Agreement_1.3.txt. * * See the Agreement for the specific language governing permissions * and limitations under the Agreement. * * When distributing Covered Code, include this NOSA HEADER in each * file and include the Agreement file at * docs/NASA_Open_Source_Agreement_1.3.txt. If applicable, add the * following below this NOSA HEADER, with the fields enclosed by * brackets "[]" replaced with your own identifying information: * Portions Copyright [yyyy] [name of copyright owner] * * NOSA HEADER END * * Copyright (c) 2003-2006 United States Government as represented by the * National Aeronautics and Space Administration. No copyright is claimed * in the United States under Title 17, U.S.Code. All Other Rights Reserved. * * $Id: WsExample.java,v 1.29 2007/08/02 11:53:13 bharris Exp $ */ package gov.nasa.gsfc.spdf.cdas.test; import java.io.*; import java.net.URL; import java.util.Date; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.SimpleTimeZone; import java.text.SimpleDateFormat; import java.text.ParsePosition; import javax.xml.rpc.Stub; import gov.nasa.gsfc.spdf.cdas.CDASWS_Impl; import gov.nasa.gsfc.spdf.cdas.CoordinatedDataAnalysisSystem; import gov.nasa.gsfc.spdf.cdas.DatasetDescription; import gov.nasa.gsfc.spdf.cdas.FileDescription; import gov.nasa.gsfc.spdf.cdas.GraphVariableDescription; import gov.nasa.gsfc.spdf.cdas.ResultDescription; import gov.nasa.gsfc.spdf.cdas.ViewDescription; public class WsExample { /** * Object for formatting Date values. Note that this instance * is NOT thread-safe. */ private SimpleDateFormat dateFormat = null; /** * The Coordinated Data Analysis System. */ private CoordinatedDataAnalysisSystem cdas = null; /** * A UTC TimeZone. */ private static final SimpleTimeZone UTC_TIME_ZONE = new SimpleTimeZone(0, "UTC"); public WsExample(String cdasUrl) { dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); dateFormat.setTimeZone(UTC_TIME_ZONE); cdas = getConnection(cdasUrl); } public static void main(String args[]) throws Exception { WsExample example = new WsExample(args[0]); example.execute(); } public static CoordinatedDataAnalysisSystem getConnection( String url) { // ---------- CDAS Web Services code ---------- System.setProperty("http.agent", "WsExample (" + System.getProperty("os.name") + " " + System.getProperty("os.arch") + ")"); Stub stub = (Stub) (new CDASWS_Impl().getCoordinatedDataAnalysisSystemPort()); stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, url); return (CoordinatedDataAnalysisSystem)stub; // ---------- end CDAS Web Services code ---------- } public void execute() throws Exception { Date t1 = new Date(); // ---------- CDAS Web Services code ---------- ViewDescription[] views = cdas.getAllViewDescriptions(); // ---------- end CDAS Web Services code ---------- Date t2 = new Date(); int istpIndex = -1; System.out.println("Mission Data Views:"); for (int i = 0; i < views.length; i++) { System.out.println(" " + views[i].id + " Title: " + views[i].title); System.out.println(" EndpointAddress: " + views[i].endpointAddress); System.out.println(" SubTitle: " + views[i].subtitle); System.out.println(" Overview: " + views[i].overview); System.out.println(" UnderConstruction: " + views[i].underConstruction); System.out.println(" Notice: " + views[i].noticeUrl); System.out.println(" Public Access: " + views[i].publicAccess); if (views[i].id.equals("istp_public")) { istpIndex = i; } }; long etGetAllSources = t2.getTime() - t1.getTime(); System.out.println("Time to execute getViewDescriptions = " + (double)etGetAllSources / 1000.0 + "seconds"); int testViewIndex = istpIndex > -1 ? istpIndex : 0; String testView = views[testViewIndex].id; String testUrl = views[testViewIndex].endpointAddress; System.out.println("Connecting to " + testUrl); CoordinatedDataAnalysisSystem testCdas = getConnection(testUrl); t1 = new Date(); // ---------- CDAS Web Services code ---------- String[] missionGroups = testCdas.getAllMissionGroups(); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nMission Groups for " + testView + " :"); for (int i = 0; i < missionGroups.length; i++) { System.out.println(" " + missionGroups[i]); }; long etGetAllMissionGroups = t2.getTime() - t1.getTime(); System.out.println("Time to execute getAllMissionGroups = " + (double)etGetAllMissionGroups / 1000.0 + "seconds"); String[] testGroups = null; if (missionGroups != null && missionGroups.length > 0) { testGroups = new String[] {missionGroups[0]}; } t1 = new Date(); // ---------- CDAS Web Services code ---------- String[] instrumentTypes = testCdas.getAllInstrumentTypes(); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nInstrument Types for " + testView + " :"); for (int i = 0; i < instrumentTypes.length; i++) { System.out.println(" " + instrumentTypes[i]); }; long etGetAllInstrumentTypes = t2.getTime() - t1.getTime(); System.out.println("Time to execute getAllInstrumentTypes = " + (double)etGetAllInstrumentTypes / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- String[][] instruments = testCdas.getAllInstruments(); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nInstruments for " + testView + " :"); for (int i = 0; i < instruments.length; i++) { for (int j = 0; j < instruments[i].length; j++) { System.out.print(" " + instruments[i][j]); } System.out.println(); }; long etGetAllInstruments = t2.getTime() - t1.getTime(); System.out.println("Time to execute getAllInstruments = " + (double)etGetAllInstruments / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- String[][] sources = testCdas.getSources(null); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nAll data sources for " + testView + " :"); for (int i = 0; i < sources.length; i++) { System.out.println(" " + sources[i][0] + " " + sources[i][1]); }; long etGetSources = t2.getTime() - t1.getTime(); System.out.println("Time to execute getSources = " + (double)etGetSources / 1000.0 + "seconds"); String[] selectInstrTypes = null; if (instrumentTypes.length > 0) { selectInstrTypes = new String[] {instrumentTypes[0]}; } t1 = new Date(); // ---------- CDAS Web Services code ---------- sources = testCdas.getSources(selectInstrTypes); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nAll data sources for " + testView + ", " + selectInstrTypes[0] + " :"); for (int i = 0; i < sources.length; i++) { System.out.println(" " + sources[i][0] + " " + sources[i][1]); }; etGetSources = t2.getTime() - t1.getTime(); System.out.println("Time to execute getSources = " + (double)etGetSources / 1000.0 + "seconds"); String[] testSources = null; if (sources != null && sources.length > 0) { testSources = new String[] {sources[0][0]}; } String[] selectInstrument = null; String[] testInstruments = null; if (instruments.length > 0) { selectInstrument = new String[] {instruments[0][0]}; testInstruments = new String[] {instruments[0][0]}; } t1 = new Date(); // ---------- CDAS Web Services code ---------- sources = testCdas.getSourcesByInstrument(selectInstrument); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nAll data sources for " + testView + ", " + selectInstrument[0] + " :"); for (int i = 0; i < sources.length; i++) { System.out.println(" " + sources[i][0] + " " + sources[i][1]); }; long etGetSourcesByInstrument = t2.getTime() - t1.getTime(); System.out.println("Time to execute getSourcesByInstrument = " + (double)etGetSourcesByInstrument / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- instrumentTypes = testCdas.getInstrumentTypes(testSources); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nInstrument Types for " + testView + ", " + (testSources != null ? testSources[0] : null) + " :"); for (int i = 0; i < instrumentTypes.length; i++) { System.out.println(" " + instrumentTypes[i]); }; long etGetInstrumentTypes = t2.getTime() - t1.getTime(); System.out.println("Time to execute getInstrumentTypes = " + (double)etGetInstrumentTypes / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- instruments = testCdas.getInstruments(testSources); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nInstruments for " + testView + ", " + (testSources != null ? testSources[0] : null) + " :"); for (int i = 0; i < instruments.length; i++) { for (int j = 0; j < instruments[i].length; j++) { System.out.print(" " + instruments[i][j]); } System.out.println(); }; long etGetInstruments = t2.getTime() - t1.getTime(); System.out.println("Time to execute getInstruments = " + (double)etGetInstruments / 1000.0 + "seconds"); String[] testInstrumentTypes = null; if (instrumentTypes != null && instrumentTypes.length > 0) { testInstrumentTypes = new String[] {instrumentTypes[0]}; } t1 = new Date(); // ---------- CDAS Web Services code ---------- DatasetDescription[] datasets = testCdas.getDatasets(testGroups, null); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nDatasets for " + testView + ", " + (testGroups != null ? testGroups[0] : null) + ", null: "); for (int i = 0; i < datasets.length; i++) { System.out.println(" " + datasets[i].id + " " + datasets[i].label); System.out.println(" PI: " + datasets[i].piName + " PI Affiliation: " + datasets[i].piAffiliation); System.out.println(" Time range: " + dateFormat.format(datasets[i].startTime) + " " + dateFormat.format(datasets[i].endTime)); System.out.println(" Notes: " + datasets[i].notesUrl); if (datasets[i].links == null) { System.out.println(" Links: null"); } else { System.out.println(" Links: "); for (int j = 0; j < datasets[i].links.length; j++) { System.out.println(" [" + j + "] " + datasets[i].links[j].text); System.out.println(" " + datasets[i].links[j].title); System.out.println(" " + datasets[i].links[j].url); } } }; long etGetDatasets = t2.getTime() - t1.getTime(); System.out.println("Time to execute getDatasets = " + (double)etGetDatasets / 1000.0 + "seconds"); /* t1 = new Date(); // ---------- CDAS Web Services code ---------- datasets = testCdas.getDatasetsBySource(testSources, testInstrumentTypes); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nDatasets for " + testView + ", " + (testSources != null ? testSources[0] : null) + ", " + (testInstrumentTypes != null ? testInstrumentTypes[0] : null) + " :"); for (int i = 0; i < datasets.length; i++) { System.out.println(" " + datasets[i].id + " " + datasets[i].label); System.out.println(" PI: " + datasets[i].piName + " PI Affiliation: " + datasets[i].piAffiliation); System.out.println(" Time range: " + dateFormat.format(datasets[i].startTime) + " " + dateFormat.format(datasets[i].endTime)); System.out.println(" Notes: " + datasets[i].notesUrl); }; long etGetDatasetsBySource = t2.getTime() - t1.getTime(); System.out.println("Time to execute getDatasetsBySource = " + (double)etGetDatasetsBySource / 1000.0 + "seconds"); */ String testDataset = null; Date testDatasetStart = null; Date testDatasetEnd = null; if (datasets != null && datasets.length > 0) { testDataset = datasets[0].id; testDatasetStart = datasets[0].startTime; testDatasetEnd = datasets[0].endTime; } else { System.out.println("\nNo datasets to complete tests"); return; } t1 = new Date(); // ---------- CDAS Web Services code ---------- String[][] variables = testCdas.getDatasetVariables(testDataset); // ---------- end CDAS Web Services code ---------- t2 = new Date(); if (variables != null && variables.length > 0) { System.out.println("\nVaribles for " + testView + ", " + testDataset + ": "); for (int i = 0; i < variables.length; i++) { for (int j = 0; j < variables[i].length; j++) { System.out.print(" " + variables[i][j]); } System.out.println(); } } else { System.out.println("\nNo values returned by getDatasetVariables"); } long etGetDatasetVariables = t2.getTime() - t1.getTime(); System.out.println("Time to execute getDatasetVariables = " + (double)etGetDatasetVariables / 1000.0 + "seconds"); Calendar calendar = GregorianCalendar.getInstance(); calendar.setTime(testDatasetStart); calendar.add(Calendar.DATE, 2); Date testEndTime = calendar.getTime(); t1 = new Date(); // ---------- CDAS Web Services code ---------- FileDescription[] cdfFiles = testCdas.getDataFiles(testDataset, testDatasetStart, testEndTime); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nData Files for " + testView + ", " + testDataset + ": "); System.out.println( " " + dateFormat.format(testDatasetStart) + " - " + dateFormat.format(testEndTime)); printFileDescriptions ("", cdfFiles); long etGetDataCdfFiles = t2.getTime() - t1.getTime(); System.out.println("Time to execute getDataFiles = " + (double)etGetDataCdfFiles / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- String[] cdfUrls = testCdas.getDataUrls(testDataset, testDatasetStart, testEndTime); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nData URLs for " + testView + ", " + testDataset + ": "); System.out.println(" " + dateFormat.format(testDatasetStart) + " - " + dateFormat.format(testEndTime)); if (cdfUrls != null && cdfUrls.length > 0) { for (int i = 0; i < cdfUrls.length; i++) { System.out.println(" " + cdfUrls[i]); } } else { System.out.println("\nNo values returned by getDataUrls"); } long etGetDataCdfUrl = t2.getTime() - t1.getTime(); System.out.println("Time to execute getDataCdfUrl = " + (double)etGetDataCdfUrl / 1000.0 + "seconds"); String aceDataset = "AC_H0_MFI"; Date aceStart = dateFormat.parse("2005/01/25 00:00:00"); Date aceEnd = dateFormat.parse("2005/01/25 02:00:00"); String[] aceVars = new String[] {"Magnitude"}; /* t1 = new Date(); // ---------- CDAS Web Services code ---------- cdfFiles = testCdas.getDataFiles(aceDataset, aceStart, aceEnd, aceVars); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nData Files for " + testView + ", " + aceDataset + ": "); System.out.println( " 2005/01/25 00:00:00 - 2005/01/25 02:00:00 " + "Magnitude"); printFileDescriptions("", cdfFiles); long etGetDataUrl2 = t2.getTime() - t1.getTime(); System.out.println("Time to execute getDataFiles = " + (double)etGetDataUrl2 / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- cdfUrls = testCdas.getCdfmlDataUrls(aceDataset, aceStart, aceEnd, aceVars, 1); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nData URLs for " + testView + ", " + aceDataset + ": "); System.out.println(" 2005/01/25 00:00:00 - 2005/01/25 02:00:00 " + "Magnitude"); if (cdfUrls != null && cdfUrls.length > 0) { for (int i = 0; i < cdfUrls.length; i++) { System.out.println(" " + cdfUrls[i]); } } else { System.out.println("\nNo values returned by getCdfmlDataUrls2"); } long etGetCdfmlDataUrl2 = t2.getTime() - t1.getTime(); System.out.println("Time to execute getCdfmlDataUrls2 = " + (double)etGetCdfmlDataUrl2 / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- String[] cdfmlUrls = testCdas.getCdfmlDataUrls(testDataset, testDatasetStart, testEndTime, 2); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nData URLs for " + testView + ", " + testDataset + ": "); System.out.println(" " + dateFormat.format(testDatasetStart) + " - " + dateFormat.format(testEndTime)); if (cdfmlUrls != null && cdfmlUrls.length > 0) { for (int i = 0; i < cdfmlUrls.length; i++) { System.out.println(" " + cdfmlUrls[i]); } } else { System.out.println("\nNo values returned by getCdfmlDataUrls"); } long etGetCdfmlDataCdfUrl = t2.getTime() - t1.getTime(); System.out.println("Time to execute getCdfmlDataUrls3 = " + (double)etGetCdfmlDataCdfUrl / 1000.0 + "seconds"); t1 = new Date(); // ---------- CDAS Web Services code ---------- datasets = testCdas.getDatasetsByInstrument(testGroups, testInstruments); // datasets = testCdas.getDatasetsByInstrument(new String[] {"Polar"}, // new String[] {"UVI"}); // ---------- end CDAS Web Services code ---------- t2 = new Date(); System.out.println("\nDatasets for " + testView + ", " + (testGroups != null ? testGroups[0] : null) + ", " + (testInstruments != null ? testInstruments[0] : null) + " :"); for (int i = 0; i < datasets.length; i++) { System.out.println(" " + datasets[i].id + " " + datasets[i].label); System.out.println(" PI: " + datasets[i].piName + " PI Affiliation: " + datasets[i].piAffiliation); System.out.println(" Time range: " + dateFormat.format(datasets[i].startTime) + " " + dateFormat.format(datasets[i].endTime)); System.out.println(" Notes: " + datasets[i].notesUrl); }; long etGetDatasetsByInstrument = t2.getTime() - t1.getTime(); System.out.println("Time to execute getDatasetsByInstrument = " + (double)etGetDatasetsByInstrument / 1000.0 + "seconds"); */ t1 = new Date(); // ---------- CDAS Web Services code ---------- ResultDescription result = testCdas.getDataAsText(aceDataset, dateFormat.parse("2006/01/30 00:00:00"), dateFormat.parse("2006/01/30 00:30:00"), aceVars); // ---------- end CDAS Web Services code ---------- t2 = new Date(); printResult("getDataAsText", result); long etGetDataAsText = t2.getTime() - t1.getTime(); System.out.println("\nTime to execute getDataAsText = " + (double)etGetDataAsText / 1000.0 + "seconds"); GraphVariableDescription[] graphVars = { new GraphVariableDescription(aceDataset, aceVars) }; t1 = new Date(); // ---------- CDAS Web Services code ---------- result = testCdas.getDataGraph(aceStart, aceEnd, graphVars, 0x3, new String[] {"GIF", "PS", "PDF"}); // ---------- end CDAS Web Services code ---------- t2 = new Date(); printResult("getDataGraph", result); long etGetDataGraph = t2.getTime() - t1.getTime(); System.out.println("\nTime to execute getDataGraph = " + (double)etGetDataGraph / 1000.0 + "seconds"); } private void printResult(String title, ResultDescription result) throws IOException { System.out.println("\n" + title + " Results: "); System.out.println(" Messages:"); for (int i = 0; result.messages != null && i < result.messages.length; i++) { System.out.println(" " + result.messages[i]); } System.out.println(" Warnings:"); for (int i = 0; result.warnings != null && i < result.warnings.length; i++) { System.out.println(" " + result.warnings[i]); } System.out.println(" Statuses:"); for (int i = 0; result.statuses != null && i < result.statuses.length; i++) { System.out.println(" " + result.statuses[i]); } System.out.println(" Errors:"); for (int i = 0; result.errors != null && i < result.errors.length; i++) { System.out.println(" " + result.errors[i]); } System.out.println(" URLs:"); for (int i = 0; result.urls != null && i < result.urls.length; i++) { System.out.println(" " + result.urls[i]); if (result.urls[i].endsWith(".txt")) { System.out.println(" file contents:\n"); URL url = new URL(result.urls[i]); BufferedReader reader = new BufferedReader(new InputStreamReader( url.openStream())); String line = null; // line of input while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } } private void printFileDescriptions ( String title, FileDescription[] files) { System.out.println(title + " FileDescriptions:"); if (files != null && files.length > 0) { for (int i = 0; i < files.length; i++) { FileDescription file = files[i]; // i'th file System.out.println(" " + file.getName() + " " + file.getMimeType() + " " + dateFormat.format(file.getStartTime()) + " - " + dateFormat.format(file.getEndTime()) + " " + file.getLength() + "B " + dateFormat.format(file.getLastModified())); } } else { System.out.println(" None"); } } }