public void testSingleDataset() throws IOException {
    InvCatalogImpl cat = TestTDSAll.open(null);

    InvDataset ds = cat.findDatasetByID("testSingleDataset");
    assert (ds != null) : "cant find dataset 'testSingleDataset'";
    assert ds.getDataType() == FeatureType.GRID;

    ThreddsDataFactory fac = new ThreddsDataFactory();

    ThreddsDataFactory.Result dataResult = fac.openFeatureDataset(ds, null);

    assert dataResult != null;
    assert !dataResult.fatalError;
    assert dataResult.featureDataset != null;

    GridDataset gds = (GridDataset) dataResult.featureDataset;
    GridDatatype grid = gds.findGridDatatype("Z_sfc");
    assert grid != null;
    GridCoordSystem gcs = grid.getCoordinateSystem();
    assert gcs != null;
    assert null == gcs.getVerticalAxis();

    CoordinateAxis1D time = gcs.getTimeAxis1D();
    assert time != null;
    assert time.getSize() == 1;
    assert 102840.0 == time.readScalarDouble();

    dataResult.featureDataset.close();
  }
Example #2
0
  @Test
  public void testFloatingPointCompare() throws Exception {
    String spec =
        TestDir.cdmUnitTestDir + "ft/fmrc/fp_precision/sediment_thickness_#yyMMddHHmm#.*\\.nc$";
    System.out.printf("%n====================FMRC dataset %s%n", spec);
    Formatter errlog = new Formatter();
    Fmrc fmrc = Fmrc.open(spec, errlog);
    assert (fmrc != null) : errlog;

    try (ucar.nc2.dt.GridDataset gridDs = fmrc.getDatasetBest()) {
      GridDatatype v = gridDs.findGridByShortName("thickness_of_sediment");
      assert v != null;
      GridCoordSystem gcs = v.getCoordinateSystem();
      CoordinateAxis1DTime time = gcs.getTimeAxis1D();

      Assert.assertEquals("hours since 2015-03-08 12:51:00.000 UTC", time.getUnitsString());
      Assert.assertEquals(74, time.getSize());
      Array data = time.read();
      System.out.printf("%s%n", NCdumpW.toString(data));

      for (CalendarDate cd : time.getCalendarDates()) {
        assert cd.getFieldValue(CalendarPeriod.Field.Minute) == 0 : System.out.printf("%s%n", cd);
      }
    }
  }
  public static DateRange extractDateRange(GridDataset gridDataset) {
    DateRange maxDateRange = null;

    for (GridDataset.Gridset gridset : gridDataset.getGridsets()) {
      GridCoordSystem gsys = gridset.getGeoCoordSystem();
      DateRange dateRange;

      CoordinateAxis1DTime time1D = gsys.getTimeAxis1D();
      if (time1D != null) {
        dateRange = time1D.getDateRange();
      } else {
        CoordinateAxis time = gsys.getTimeAxis();
        if (time == null) continue;

        try {
          DateUnit du = new DateUnit(time.getUnitsString());
          Date minDate = du.makeDate(time.getMinValue());
          Date maxDate = du.makeDate(time.getMaxValue());
          dateRange = new DateRange(minDate, maxDate);
        } catch (Exception e) {
          logger.warn("Illegal Date Unit " + time.getUnitsString());
          continue;
        }
      }

      if (maxDateRange == null) maxDateRange = dateRange;
      else maxDateRange.extend(dateRange);
    }

    return maxDateRange;
  }
  public static ThreddsMetadata.GeospatialCoverage extractGeospatial(GridDataset gridDataset) {
    ThreddsMetadata.GeospatialCoverage gc = new ThreddsMetadata.GeospatialCoverage();
    LatLonRect llbb = null;
    CoordinateAxis1D vaxis = null;

    for (GridDataset.Gridset gridset : gridDataset.getGridsets()) {
      GridCoordSystem gsys = gridset.getGeoCoordSystem();
      if (llbb == null) llbb = gsys.getLatLonBoundingBox();

      CoordinateAxis1D vaxis2 = gsys.getVerticalAxis();
      if (vaxis == null) vaxis = vaxis2;
      else if ((vaxis2 != null) && (vaxis2.getSize() > vaxis.getSize())) vaxis = vaxis2;
    }

    if (llbb != null) gc.setBoundingBox(llbb);
    if (vaxis != null) gc.setVertical(vaxis);
    return gc;
  }
Example #5
0
  void setField(GridDatatype field) {
    /*int idx = fieldChooser.setSelectedByName(field.toString());
    if (idx < 0)
      fieldChooser.setSelectedByIndex(0); */
    fieldChooser.setToolTipText(field.getName());

    GridCoordSystem gcs = field.getCoordinateSystem();

    // levels
    CoordinateAxis1D axis = gcs.getVerticalAxis();
    setChooserWanted("level", axis != null);
    if (axis != null) {
      List<NamedObject> levels = axis.getNames();
      levelChooser.setCollection(levels.iterator(), true);
      NamedObject no = levels.get(controller.getCurrentLevelIndex());
      levelChooser.setSelectedByName(no.getName());
    }

    // times
    if (gcs.hasTimeAxis()) {
      axis = gcs.hasTimeAxis1D() ? gcs.getTimeAxis1D() : gcs.getTimeAxisForRun(0);
      setChooserWanted("time", axis != null);
      if (axis != null) {
        List<NamedObject> names = axis.getNames();
        timeChooser.setCollection(names.iterator(), true);
        NamedObject no = names.get(controller.getCurrentTimeIndex());
        timeChooser.setSelectedByName(no.getName());
      }
    } else {
      setChooserWanted("time", false);
    }

    axis = gcs.getEnsembleAxis();
    setChooserWanted("ensemble", axis != null);
    if (axis != null) {
      List<NamedObject> names = axis.getNames();
      ensembleChooser.setCollection(names.iterator(), true);
      NamedObject no = names.get(controller.getCurrentEnsembleIndex());
      ensembleChooser.setSelectedByName(no.getName());
    }

    axis = gcs.getRunTimeAxis();
    setChooserWanted("runtime", axis != null);
    if (axis != null) {
      List<NamedObject> names = axis.getNames();
      runtimeChooser.setCollection(names.iterator(), true);
      NamedObject no = names.get(controller.getCurrentRunTimeIndex());
      runtimeChooser.setSelectedByName(no.getName());
    }

    setChoosers();

    colorScalePanel.setUnitString(field.getUnitsString());
  }
Example #6
0
  protected Element genLonLatEnvelope(GridCoordSystem gcs) {
    // <CoverageOfferingBrief>/lonLatEnvelope
    Element lonLatEnvelopeElem = new Element("lonLatEnvelope", wcsNS);
    lonLatEnvelopeElem.setAttribute("srsName", "urn:ogc:def:crs:OGC:1.3:CRS84");

    LatLonRect llbb = gcs.getLatLonBoundingBox();
    LatLonPoint llpt = llbb.getLowerLeftPoint();
    LatLonPoint urpt = llbb.getUpperRightPoint();

    // <CoverageOfferingBrief>/lonLatEnvelope/gml:pos
    String firstPosition = llpt.getLongitude() + " " + llpt.getLatitude();
    double lon = llpt.getLongitude() + llbb.getWidth();
    String secondPosition = lon + " " + urpt.getLatitude();
    // ToDo WCS 1.0Plus - Deal with conversion to meters. (Yikes!!)
    CoordinateAxis1D vertAxis = gcs.getVerticalAxis();
    if (vertAxis != null) {
      // See verAxis.getUnitsString()
      double zeroIndexValue = vertAxis.getCoordValue(0);
      double sizeIndexValue = vertAxis.getCoordValue(((int) vertAxis.getSize()) - 1);
      if (vertAxis.getPositive().equals(ucar.nc2.constants.CF.POSITIVE_UP)) {
        firstPosition += " " + zeroIndexValue;
        secondPosition += " " + sizeIndexValue;
      } else {
        firstPosition += " " + sizeIndexValue;
        secondPosition += " " + zeroIndexValue;
      }
    }

    lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(firstPosition));
    lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(secondPosition));

    // <CoverageOfferingBrief>/lonLatEnvelope/gml:timePostion [2]
    if (gcs.hasTimeAxis()) {
      lonLatEnvelopeElem.addContent(
          new Element("timePosition", gmlNS)
              .addContent(gcs.getCalendarDateRange().getStart().toString()));
      lonLatEnvelopeElem.addContent(
          new Element("timePosition", gmlNS)
              .addContent(gcs.getCalendarDateRange().getEnd().toString()));
    }

    return lonLatEnvelopeElem;
  }
Example #7
0
  protected Element genLonLatEnvelope(GridCoordSystem gcs) {
    // <CoverageOfferingBrief>/lonLatEnvelope
    Element lonLatEnvelopeElem = new Element("lonLatEnvelope", wcsNS);
    lonLatEnvelopeElem.setAttribute("srsName", "urn:ogc:def:crs:OGC:1.3:CRS84");

    LatLonRect llbb = gcs.getLatLonBoundingBox();
    LatLonPoint llpt = llbb.getLowerLeftPoint();
    LatLonPoint urpt = llbb.getUpperRightPoint();

    // <CoverageOfferingBrief>/lonLatEnvelope/gml:pos
    String firstPosition = llpt.getLongitude() + " " + llpt.getLatitude();
    double lon = llpt.getLongitude() + llbb.getWidth();
    String secondPosition = lon + " " + urpt.getLatitude();
    // ToDo WCS 1.0Plus - Add vertical (Deal with conversion to meters. Yikes!!)
    //    CoordinateAxis1D vertAxis = gcs.getVerticalAxis();
    //    if ( vertAxis != null )
    //    {
    //      // See verAxis.getUnitsString()
    //      firstPosition += " " + vertAxis.getCoordValue( 0);
    //      secondPostion += " " + vertAxis.getCoordValue( ((int)vertAxis.getSize()) - 1);
    //    }

    lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(firstPosition));
    lonLatEnvelopeElem.addContent(new Element("pos", gmlNS).addContent(secondPosition));

    // <CoverageOfferingBrief>/lonLatEnvelope/gml:timePostion [2]
    if (gcs.hasTimeAxis()) {
      DateRange dr = gcs.getDateRange();
      if (dr != null) {
        lonLatEnvelopeElem.addContent(
            new Element("timePosition", gmlNS).addContent(dr.getStart().toDateTimeStringISO()));
        lonLatEnvelopeElem.addContent(
            new Element("timePosition", gmlNS).addContent(dr.getEnd().toDateTimeStringISO()));
      }
    }

    return lonLatEnvelopeElem;
  }
  @Test
  @Category(NeedsCdmUnitTest.class)
  public void testCoordExtract() throws IOException {
    try (ucar.nc2.dt.grid.GridDataset dataset =
        GridDataset.open(
            TestDir.cdmUnitTestDir + "formats/grib2/coordExtract/TestCoordExtract.grib2")) {
      System.out.printf("%s%n", dataset.getLocation());

      GeoGrid grid = dataset.findGridByName("Convective_inhibition_surface");
      assert null != grid;
      GridCoordSystem gcs = grid.getCoordinateSystem();
      assert null != gcs;
      assert grid.getRank() == 3;

      System.out.printf("%s%n", gcs);

      int result[] = gcs.findXYindexFromLatLon(41.3669944444, -91.140575, null);

      System.out.printf("%d %d %n", result[0], result[1]);
      assert result[0] == 538;
      assert result[1] == 97;
    }
  }
  @Test
  public void bugReport() throws IOException {

    try (GridDataset dataset =
        GridDataset.open(
            "http://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3_0_run1_200001.nc")) {

      GridDatatype firstGridInfo = dataset.getGrids().get(0);
      System.out.println("Grid name =" + firstGridInfo.getName());
      GeoGrid firstGrid = (GeoGrid) dataset.getGrids().get(0);

      System.out.println("WHOLE GRID");
      GridCoordSystem gcs = firstGrid.getCoordinateSystem();
      System.out.println("is lat/lon system ? " + gcs.isLatLon());
      assert gcs.isLatLon();

      LatLonRect rect = gcs.getLatLonBoundingBox();
      System.out.println(
          "gcs bounding box : latmin="
              + rect.getLatMin()
              + " latmax="
              + rect.getLatMax()
              + " lonmin="
              + rect.getLonMin()
              + " lonmax="
              + rect.getLonMax());
      System.out.println("projection       : " + gcs.getProjection());
      System.out.println(
          "width =" + gcs.getXHorizAxis().getSize() + ", height=" + gcs.getYHorizAxis().getSize());
      System.out.println(
          "X is regular     ? " + ((CoordinateAxis1D) gcs.getXHorizAxis()).isRegular());
      System.out.println("X is contiguous  ? " + gcs.getXHorizAxis().isContiguous());
      System.out.println(
          "X start          : " + ((CoordinateAxis1D) gcs.getXHorizAxis()).getStart());
      System.out.println(
          "X increment      : " + ((CoordinateAxis1D) gcs.getXHorizAxis()).getIncrement());
      System.out.println(
          "Y is regular     ? " + ((CoordinateAxis1D) gcs.getYHorizAxis()).isRegular());
      System.out.println("Y is contiguous  ? " + gcs.getYHorizAxis().isContiguous());
      System.out.println(
          "Y start          : " + ((CoordinateAxis1D) gcs.getYHorizAxis()).getStart());
      System.out.println(
          "Y increment      : " + ((CoordinateAxis1D) gcs.getYHorizAxis()).getIncrement());

      LatLonPoint p = gcs.getLatLon(0, 0);
      System.out.println("index (0,0) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude());
      p = gcs.getLatLon(1, 1);
      System.out.println("index (1,1) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude());

      System.out.println("looking up lat=" + p.getLatitude() + "  lon=" + p.getLongitude());
      int[] xy = gcs.findXYindexFromLatLon(p.getLatitude(), p.getLongitude(), null);
      System.out.println("index= (" + xy[0] + ", " + xy[1] + ")");
      Assert.assertEquals(xy[0], 1);
      Assert.assertEquals(xy[1], 1);

      // --------------------------------------------------------------------------
      double latMin = -20.D, latMax = -10.D, lonMin = 35.D, lonMax = 45.D;
      System.out.println(
          "\nSUBGRID (latmin="
              + latMin
              + "  latmax="
              + latMax
              + "  lonmin="
              + lonMin
              + "  lonmax="
              + lonMax
              + ")");

      LatLonRect latLonRect =
          new LatLonRect(new LatLonPointImpl(latMin, lonMin), new LatLonPointImpl(latMax, lonMax));

      GeoGrid gridSubset = firstGrid.subset(null, null, latLonRect, 0, 1, 1);

      GridCoordSystem gcs2 = gridSubset.getCoordinateSystem();

      rect = gcs2.getLatLonBoundingBox();
      System.out.println("is lat/lon system ? " + gcs2.isLatLon());
      System.out.println(
          "gcs bounding box : latmin="
              + rect.getLatMin()
              + " latmax="
              + rect.getLatMax()
              + " lonmin="
              + rect.getLonMin()
              + " lonmax="
              + rect.getLonMax());
      System.out.println("projection       : " + gcs.getProjection());
      System.out.println(
          "width ="
              + gcs2.getXHorizAxis().getSize()
              + ", height="
              + gcs2.getYHorizAxis().getSize());
      System.out.println(
          "X is regular     ? " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).isRegular());
      System.out.println("X is contiguous  ? " + gcs2.getXHorizAxis().isContiguous());
      System.out.println(
          "X start          : " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).getStart());
      System.out.println(
          "X increment      : " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).getIncrement());
      System.out.println(
          "Y is regular     ? " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).isRegular());
      System.out.println("Y is contiguous  ? " + gcs2.getYHorizAxis().isContiguous());
      System.out.println(
          "Y start          : " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).getStart());
      System.out.println(
          "Y increment      : " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).getIncrement());

      p = gcs2.getLatLon(0, 0);
      System.out.println("index (0,0) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude());
      p = gcs2.getLatLon(1, 1);
      System.out.println("index (1,1) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude());

      System.out.println("looking up lat=" + p.getLatitude() + "  lon=" + p.getLongitude());
      xy = gcs2.findXYindexFromLatLon(p.getLatitude(), p.getLongitude(), null);
      System.out.println("index= (" + xy[0] + ", " + xy[1] + ")");
      Assert.assertEquals(xy[0], 1);
      Assert.assertEquals(xy[1], 1);

    } catch (IOException | InvalidRangeException e) {
      e.printStackTrace();
    }
  }