/**
   * Add information from the InvDataset to the NetcdfDataset.
   *
   * @param ds get info from here
   * @param ncDataset add to here
   */
  public static void annotate(InvDataset ds, NetcdfDataset ncDataset) {
    ncDataset.setTitle(ds.getName());
    ncDataset.setId(ds.getID());

    // add properties as global attributes
    for (InvProperty p : ds.getProperties()) {
      String name = p.getName();
      if (null == ncDataset.findGlobalAttribute(name)) {
        ncDataset.addAttribute(null, new Attribute(name, p.getValue()));
      }
    }

    /* ThreddsMetadata.GeospatialCoverage geoCoverage = ds.getGeospatialCoverage();
    if (geoCoverage != null) {
      if ( null != geoCoverage.getNorthSouthRange()) {
        ncDataset.addAttribute(null, new Attribute("geospatial_lat_min", new Double(geoCoverage.getLatSouth())));
        ncDataset.addAttribute(null, new Attribute("geospatial_lat_max", new Double(geoCoverage.getLatNorth())));
      }
      if ( null != geoCoverage.getEastWestRange()) {
        ncDataset.addAttribute(null, new Attribute("geospatial_lon_min", new Double(geoCoverage.getLonWest())));
        ncDataset.addAttribute(null, new Attribute("geospatial_lon_max", new Double(geoCoverage.getLonEast())));
      }
      if ( null != geoCoverage.getUpDownRange()) {
        ncDataset.addAttribute(null, new Attribute("geospatial_vertical_min", new Double(geoCoverage.getHeightStart())));
        ncDataset.addAttribute(null, new Attribute("geospatial_vertical_max", new Double(geoCoverage.getHeightStart() + geoCoverage.getHeightExtent())));
      }
    }

    DateRange timeCoverage = ds.getTimeCoverage();
    if (timeCoverage != null) {
      ncDataset.addAttribute(null, new Attribute("time_coverage_start", timeCoverage.getStart().toDateTimeStringISO()));
      ncDataset.addAttribute(null, new Attribute("time_coverage_end", timeCoverage.getEnd().toDateTimeStringISO()));
    } */

    ncDataset.finish();
  }
  public static NetcdfDataset buildTrajectory(List<IObservationGroup> obsGroups) {
    //        IObservationGroup.DataType tdt = IObservationGroup.DataType.values()[0];
    //        IObservationGroup.DataType lldt = IObservationGroup.DataType.values()[0];
    //        IObservationGroup.DataType ddt = IObservationGroup.DataType.values()[0];
    //        for(IObservationGroup og : obsGroups) {
    //            tdt = (og.getTimeDataType().compareTo(tdt) > 0) ? og.getTimeDataType() : tdt;
    //            lldt = (og.getLatLonDataType().compareTo(lldt) > 0) ? og.getLatLonDataType() :
    // lldt;
    //            ddt = (og.getDepthDataType().compareTo(ddt) > 0) ? og.getDepthDataType() : ddt;
    //        }

    NetcdfDataset ncds = null;
    File temp = null;
    try {
      /* Instantiate an empty NetcdfDataset object from the template ncml */
      temp = File.createTempFile("ooi", ".ncml");
      ncds = getNcdsFromTemplate(temp, "trajectory.ncml");

      int nobs = obsGroups.size();
      List<Number> allDepths = new ArrayList<Number>();
      List<VariableParams> allDn = new ArrayList<VariableParams>();
      int nt = nobs;
      int nd = 0;
      for (IObservationGroup og : obsGroups) {
        nd = Math.max(nd, og.getDepths().length);
        for (Number d : og.getDepths()) {
          if (!allDepths.contains(d)) {
            allDepths.add(d);
          }
        }
        for (VariableParams dn : og.getDataNames()) {
          if (!allDn.contains(dn)) {
            allDn.add(dn);
          }
        }
      }

      /* Do the trajectory ID */

      /* Do the times */
      Number[] times = obsGroups.get(0).getTimes();
      IObservationGroup.DataType tdt = obsGroups.get(0).getTimeDataType();
      DataType ncdtTime = getNcDataType(tdt);
      ncds.findDimension("time").setLength(nt);
      Array tarr = Array.factory(ncdtTime, new int[] {nt});
      IndexIterator tii = tarr.getIndexIterator();
      Variable tvar = ncds.findVariable("time");
      tvar.setDataType(getNcDataType(tdt));
      tvar.setCachedData(tarr);

      /* Do the lats */
      IObservationGroup.DataType lldt = obsGroups.get(0).getLatLonDataType();
      DataType ncdtLl = getNcDataType(lldt);
      Array laarr = Array.factory(ncdtLl, new int[] {nt});
      IndexIterator laii = laarr.getIndexIterator();
      Variable lavar = ncds.findVariable("lat");
      lavar.setDataType(ncdtLl);
      lavar.setCachedData(laarr);

      /* Do the lons */
      Array loarr = Array.factory(ncdtLl, new int[] {nt});
      IndexIterator loii = loarr.getIndexIterator();
      Variable lovar = ncds.findVariable("lon");
      lovar.setDataType(ncdtLl);
      lovar.setCachedData(loarr);

      /* Iterate over the observation groups and fill the data */
      Map<String, String> allAttributes = new HashMap<String, String>();
      IObservationGroup og;
      HashMap<String, IndexIterator> darrs = new HashMap<String, IndexIterator>();
      Number time;
      Number depth = allDepths.get(0);
      for (int obs = 0; obs < nobs; obs++) {
        og = obsGroups.get(obs);
        time = og.getTimes()[0];
        putArrayData(tii, ncdtTime, time);
        putArrayData(loii, ncdtLl, og.getLon());
        putArrayData(laii, ncdtLl, og.getLat());

        for (VariableParams dn : allDn) {
          if (og.getDataNames().contains(dn)) {
            DataType ncdtData = getNcDataType(dn.getDataType());
            VariableDS dvar = (VariableDS) ncds.findVariable(dn.getShortName());
            if (dvar == null) {
              dvar =
                  new VariableDS(
                      ncds,
                      null,
                      null,
                      dn.getShortName(),
                      ncdtData,
                      "time",
                      dn.getUnits(),
                      dn.getDescription());
              dvar.addAttribute(new Attribute(CF.COORDINATES, "time lon lat"));
              //                            dvar.addAttribute(new Attribute("missing_value",
              // missingData));
              dvar.addAttribute(new Attribute(CF.STANDARD_NAME, dn.getStandardName()));
              Array darr = Array.factory(ncdtData, new int[] {nt});
              dvar.setCachedData(darr);

              darrs.put(dn.getStandardName(), darr.getIndexIterator());
              ncds.addVariable(null, dvar);
            }
            putArrayData(darrs.get(dn.getStandardName()), ncdtData, og.getData(dn, time, depth));

          } else {
            /*
             * station doesn't have this variable - don't believe
             * this can even happen...
             *
             * NOTE: This would indicate a problem with the above processing where the data-name list
             * has been modified prior to contains-checking
             */
          }
        }
      }

      /* Add global attributes */
      for (String key : allAttributes.keySet()) {
        ncds.addAttribute(null, new Attribute(key, allAttributes.get(key)));
      }
    } catch (FileNotFoundException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      if (temp != null) {
        if (!temp.delete()) {
          temp.deleteOnExit();
        }
      }
      if (ncds != null) {
        ncds.finish();
      }
    }

    return ncds;
  }
  public static NetcdfDataset buildStation(IObservationGroup obsGroup) {
    if (obsGroup.getDepths().length > 1) {
      return buildStationProfile(obsGroup);
    }
    NetcdfDataset ncds = null;
    File temp = null;
    try {
      /* Instantiate an empty NetcdfDataset object from the template ncml */
      temp = File.createTempFile("ooi", ".ncml");
      ncds = getNcdsFromTemplate(temp, "station.ncml");

      Map<String, String> allAttributes = new HashMap<String, String>();
      allAttributes.putAll(obsGroup.getAttributes());

      /* Do the station Id */
      ArrayInt.D0 aid = new ArrayInt.D0();
      aid.set(obsGroup.getId());
      ncds.findVariable("stnId").setCachedData(aid);

      /* Do the times */
      Number[] times = obsGroup.getTimes();
      IObservationGroup.DataType tdt = obsGroup.getTimeDataType();
      ncds.findDimension("time").setLength(times.length);
      Variable tvar = ncds.findVariable("time");
      tvar.setDataType(getNcDataType(tdt));
      tvar.setCachedData(getNcArray(times, tdt));

      /* Do the lat & lon */
      IObservationGroup.DataType lldt = obsGroup.getLatLonDataType();
      DataType ncdtLl = getNcDataType(lldt);
      Variable laVar = ncds.findVariable("lat");
      laVar.setDataType(ncdtLl);
      laVar.setCachedData(getNcScalar(obsGroup.getLat(), lldt));
      Variable loVar = ncds.findVariable("lon");
      loVar.setDataType(ncdtLl);
      loVar.setCachedData(getNcScalar(obsGroup.getLon(), lldt));

      /* Do the depth */
      IObservationGroup.DataType ddt = obsGroup.getDepthDataType();
      VariableDS zVar =
          new VariableDS(
              ncds, null, null, "stnDepth", getNcDataType(ddt), "", "m", "station depth");
      zVar.addAttribute(new Attribute("positive", "down"));
      ncds.addVariable(null, zVar);
      Number depth = obsGroup.getDepths()[0];
      zVar.setCachedData(getNcScalar(obsGroup.getDepths()[0], ddt));

      /* Do the data variables */
      for (VariableParams dn : obsGroup.getDataNames()) {
        DataType ncdtData = getNcDataType(dn.getDataType());
        VariableDS dvar =
            new VariableDS(
                ncds,
                null,
                null,
                dn.getShortName(),
                ncdtData,
                "time",
                dn.getUnits(),
                dn.getDescription());
        dvar.addAttribute(new Attribute(CF.COORDINATES, "time lon lat"));
        dvar.addAttribute(new Attribute(CF.STANDARD_NAME, dn.getStandardName()));
        Array adata = Array.factory(ncdtData, new int[] {times.length});
        IndexIterator aii = adata.getIndexIterator();
        dvar.setCachedData(adata);
        ncds.addVariable(null, dvar);

        for (int ti = 0; ti < times.length; ti++) {
          putArrayData(aii, ncdtData, obsGroup.getData(dn, times[ti], depth));
        }
      }

      /* Add global attributes */
      for (String key : allAttributes.keySet()) {
        ncds.addAttribute(null, new Attribute(key, allAttributes.get(key)));
      }

    } catch (FileNotFoundException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      if (temp != null) {
        if (!temp.delete()) {
          temp.deleteOnExit();
        }
      }
      if (ncds != null) {
        ncds.finish();
      }
    }

    return ncds;
  }
Beispiel #4
0
  public void augmentDataset(NetcdfDataset ds, CancelTask cancelTask) throws IOException {
    ds.addAttribute(null, new Attribute("FeatureType", FeatureType.IMAGE.toString())); // LOOK

    Group vhrr = ds.findGroup("VHRR");
    Group loc = vhrr.findGroup("Geo-Location");
    Variable lat = loc.findVariable("Latitude");
    lat.addAttribute(new Attribute(CDM.UNITS, "degrees_north"));
    lat.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
    Variable lon = loc.findVariable("Longitude");
    lon.addAttribute(new Attribute(CDM.UNITS, "degrees_east"));
    lon.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));

    int[] shape = lat.getShape();
    assert shape.length == 2;
    Dimension scan = new Dimension("scan", shape[0]);
    Dimension xscan = new Dimension("xscan", shape[1]);
    vhrr.addDimension(scan);
    vhrr.addDimension(xscan);

    lat.setDimensions("scan xscan");
    lon.setDimensions("scan xscan");

    Group data = vhrr.findGroup("Image Data");
    for (Variable v : data.getVariables()) {
      int[] vs = v.getShape();
      if ((vs.length == 2) && (vs[0] == shape[0]) && (vs[1] == shape[1])) {
        v.setDimensions("scan xscan");
        v.addAttribute(new Attribute(_Coordinate.Axes, "lat lon time"));
      }
    }

    /* Group PRODUCT_METADATA {

    Group PRODUCT_DETAILS {
      PRODUCT_DETAILS:UNIQUE_ID = "3AVHR_22NOV2007_0902";
      PRODUCT_DETAILS:PRODUCT_TYPE = "STANDARD(FULL DISK) ";
      PRODUCT_DETAILS:PROCESSING_SOFTWARE = "InPGS_XXXXXXXXXXXXXX";
      PRODUCT_DETAILS:SPACECRAFT_ID = "INSAT-3A  ";
      PRODUCT_DETAILS:SENSOR_ID = "VHR";
      PRODUCT_DETAILS:ACQUISITION_TYPE = "FULL FRAME     ";
      PRODUCT_DETAILS:ACQUISITION_DATE = "22NOV2007";
      PRODUCT_DETAILS:ACQUISITION_TIME_IN_GMT = "0902";
      PRODUCT_DETAILS:PRODUCT_NAME = "      FULL DISK";
      PRODUCT_DETAILS:PROCESSING_LEVEL = "L1B";
      PRODUCT_DETAILS:BAND_COMBINATION = "Visible(VIS),Thermal Infrared(TIR),Water Vapour(WV)";
      PRODUCT_DETAILS:PRODUCT_CODE = "ST00001HD";
    } */

    Group info = ds.findGroup("PRODUCT_METADATA/PRODUCT_DETAILS");
    String dateS = info.findAttribute("ACQUISITION_DATE").getStringValue();
    String timeS = info.findAttribute("ACQUISITION_TIME_IN_GMT").getStringValue();

    SimpleDateFormat format = new SimpleDateFormat("ddMMMyyyyHHmm");
    format.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
    try {
      Date d = format.parse(dateS + timeS);
      VariableDS time =
          new VariableDS(
              ds,
              vhrr,
              null,
              "time",
              DataType.LONG,
              "",
              "seconds since 1970-01-01 00:00",
              "time generated from PRODUCT_METADATA/PRODUCT_DETAILS");

      time.addAttribute(
          new Attribute(
              _Coordinate.AxisType,
              AxisType.Time.toString())); // // LOOK : cant handle scalar coordinates yet ??
      time.addAttribute(new Attribute("IsoDate", new DateFormatter().toDateTimeStringISO(d)));
      CoordinateAxis1D timeAxis = new CoordinateAxis1D(ds, time);
      ds.addVariable(vhrr, timeAxis);
      ArrayLong.D0 timeData = new ArrayLong.D0();
      timeData.set(d.getTime() / 1000);
      time.setCachedData(timeData, true);

    } catch (ParseException e) {
      e.printStackTrace();
      throw new IOException(e.getMessage());
    }

    ds.finish();
  }