Beispiel #1
0
 private void buildLocations() {
   guiLogger.info("Building Locations from ERDDAP Dataset: " + this.getTitle());
   try {
     ClientConfig clientConfig = new DefaultClientConfig();
     clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
     Client c = Client.create(clientConfig);
     guiLogger.info(
         "Request: "
             + this.getTabledap()
             + ".json?"
             + this.getX().getName()
             + ","
             + this.getY().getName()
             + "&distinct()");
     WebResource wr =
         c.resource(
             this.getTabledap()
                 + ".json?"
                 + this.getX().getName()
                 + ","
                 + this.getY().getName()
                 + "&distinct()");
     JSONObject result = wr.get(JSONObject.class);
     JSONArray ar = result.getJSONObject("table").getJSONArray("rows");
     locations = new ArrayList<SensorContainer>(ar.length());
     SensorContainer senc;
     double[] loc = new double[4];
     for (int i = 0; i < ar.length(); i++) {
       senc = new SensorContainer();
       loc[0] = ar.getJSONArray(i).getDouble(1);
       loc[1] = ar.getJSONArray(i).getDouble(0);
       loc[2] = ar.getJSONArray(i).getDouble(1);
       loc[3] = ar.getJSONArray(i).getDouble(0);
       senc.setNESW(loc.clone());
       senc.setName("");
       locations.add(senc);
     }
   } catch (Exception e) {
     guiLogger.error("Exception", e);
   }
 }
Beispiel #2
0
  @Override
  public void getObservations(File savePath) {

    double numSens = getSelectedSensorCount();
    double countSens = 0;
    String requestURL;
    ArrayList<String> filenames = new ArrayList<String>();

    // Are we loading an XSL?
    String schemaLoc = "/resources/schemas/ioos_gmlv061_to_arc.xsl";
    SAXBuilder xslBuilder = new SAXBuilder();
    Document xslDoc = null;
    try {
      pcs.firePropertyChange(
          "message", null, "Loading DIF to ERSI CSV XSL Schema from " + schemaLoc);
      xslDoc = xslBuilder.build(this.getClass().getResourceAsStream(schemaLoc));
    } catch (JDOMException e) {
      pcs.firePropertyChange("message", null, "XSL is not well-formed");
      return;
    } catch (IOException e) {
      pcs.firePropertyChange("message", null, "XSL at: " + schemaLoc + "; is inaccessible");
      return;
    }

    Timer stopwatch = new Timer();
    SAXBuilder difBuilder;
    Document difDoc;
    CsvProperties properties;
    Long filesize = Long.parseLong("0");

    for (SensorContainer sensor : selectedSensors) {
      properties = new CsvProperties();
      stopwatch.reset();
      stopwatch.start();
      pcs.firePropertyChange("message", null, "Sensor: " + sensor.getName());
      pcs.firePropertyChange("message", null, "- Building Request String");

      requestURL = buildRequest(sensor);

      difBuilder = new SAXBuilder(); // parameters control
      difDoc = null;
      try {
        pcs.firePropertyChange("message", null, "- Making Request (" + requestURL + ")");
        difDoc = difBuilder.build(requestURL);
      } catch (JDOMException e) {
        pcs.firePropertyChange("message", null, "- SOS at: " + requestURL + "; is not well-formed");
        continue;
      } catch (IOException e) {
        pcs.firePropertyChange("message", null, "- SOS at: " + sosURL + "; is inaccessible");
        continue;
      }

      try {
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        Writer writer = new StringWriter(4 * 10 ^ 7); // 40 mb
        outputter.output(difDoc, writer);
        float mysize = writer.toString().length();
        pcs.firePropertyChange(
            "message", null, "- Size of SOS Observations request (mB)= ~" + mysize / 1000000.0);
      } catch (IOException e) {
        pcs.firePropertyChange("message", null, "- Could not get the size of the xml document");
      }

      stopwatch.start();

      List<Text> myList = null;
      try {
        pcs.firePropertyChange("message", null, "- Transforming XML to CSV");
        myList = transform(difDoc, xslDoc);
        // These two tests are common error messages.
        if ((!myList.get(0).getText().trim().substring(0, 20).contains("No"))
            && (!myList.get(0).getText().trim().substring(0, 20).contains("Response format"))) {
          String filename = FileSaveUtils.chooseFilename(savePath, sensor.getName(), fileSuffix);
          properties.setPath(filename);
          properties.setSuffix(fileSuffix);
          properties.setIdHeader("Station");
          properties.setLatHeader("Latitude");
          properties.setLonHeader("Longitude");
          properties.setTimeHeader("DateTime");
          properties.setCdmFeatureType("timeSeries");
          File savedfile = new File(filename);
          Writer fstream = new FileWriter(savedfile);
          pcs.firePropertyChange("message", null, "- Streaming transformed results to file");
          BufferedWriter out = new BufferedWriter(fstream);
          out.write(myList.get(0).getText());
          out.close();
          filesize = Long.valueOf(savedfile.length());
          // Don't add empty files to the output path
          if (filesize > 0) {
            filenames.add(savedfile.getAbsolutePath());
            parseProperties(difDoc, properties);
            properties.writeFile();
          }
        } else {
          pcs.firePropertyChange(
              "message", null, "- No data returned at this station for selected parameters!");
        }
      } catch (IOException e) {
        pcs.firePropertyChange("message", null, "- Could not output transformed XML");
      } catch (JDOMException e) {
        pcs.firePropertyChange("message", null, "- JDOM tranform failed!");
      }
      countSens++;
      pcs.firePropertyChange(
          "message",
          null,
          "- Completed "
              + filesize.toString()
              + " bytes in "
              + stopwatch.elapsedTime()
              + " seconds");
      int prog = Double.valueOf(countSens / numSens * 100).intValue();
      pcs.firePropertyChange("progress", null, prog);
    } // End Sensor List
    pcs.firePropertyChange("progress", null, 100);
    if (!filenames.isEmpty()) {
      pcs.firePropertyChange("message", null, "Saved Files:");
      for (String s : filenames) {
        pcs.firePropertyChange("message", null, "- " + s);
      }
    }
    pcs.firePropertyChange("done", null, filenames.toString());
  }