@Override public VariableSimpleIF getDataVariable(String shortName) { for (CoverageCollection cc : covCollections) { VariableSimpleIF result = cc.findCoverage(shortName); if (result != null) return result; } return null; }
Coverage orgGrid = org.findCoverage(gridName); if (orgGrid == null) continue; orgCoverages.add(orgGrid); CoverageTransform t = org.findCoordTransform(tname); // these are truly immutable, so can use originals if (t != null) coordTransforms.add(t); return ucar.nc2.util.Optional.of(new CoverageCollection(org.getName(), org.getCoverageType(), new AttributeContainerHelper(org.getName(), org.getGlobalAttributes()), null, null, null, coordSys, coordTransforms, coordAxes, coverages, org.getReader())); // use org.reader -> subset always in coord space !
CdmrFeatureProto.CoverageDataset.Builder encodeHeader(CoverageCollection gridDataset, String location) { CdmrFeatureProto.CoverageDataset.Builder builder = CdmrFeatureProto.CoverageDataset.newBuilder(); builder.setName(location); builder.setCoverageType(convertCoverageType(gridDataset.getCoverageType())); builder.setDateRange(encodeDateRange(gridDataset.getCalendarDateRange())); if (gridDataset.getLatlonBoundingBox() != null) builder.setLatlonRect(encodeRectangle(gridDataset.getLatlonBoundingBox())); if (gridDataset.getProjBoundingBox() != null) builder.setProjRect(encodeRectangle(gridDataset.getProjBoundingBox())); for (Attribute att : gridDataset.getGlobalAttributes()) builder.addAtts(NcStream.encodeAtt(att)); for (CoverageCoordSys gcs : gridDataset.getCoordSys()) builder.addCoordSys(encodeCoordSys(gcs)); for (CoverageTransform gct : gridDataset.getCoordTransforms()) builder.addCoordTransforms(encodeCoordTransform(gct)); for (CoverageCoordAxis axis : gridDataset.getCoordAxes()) builder.addCoordAxes(encodeCoordAxis(axis)); for (Coverage grid : gridDataset.getCoverages()) builder.addGrids(encodeGrid(grid)); return builder; }
public FeatureDatasetCoverage(String location, Closeable closer, CoverageCollection covCollection) { this.location = location; this.gatts = new AttributeContainerHelper(location, covCollection.getGlobalAttributes()); this.closer = closer; this.covCollections = Lists.newArrayList(covCollection); this.featureType = covCollection.getCoverageType(); this.calendarDateRange = covCollection.getCalendarDateRange(); }
public FeatureDatasetCoverage(String location, AttributeContainer gatts, Closeable closer, List<CoverageCollection> covCollections) { this.location = location; this.gatts = gatts; this.closer = closer; this.covCollections = covCollections; CalendarDateRange cdr = null; FeatureType ft = null; for (CoverageCollection cc : covCollections) { FeatureType ftCheck = cc.getCoverageType(); if (ft == null) ft = ftCheck; else if (ftCheck != ft) ft = FeatureType.COVERAGE; CalendarDateRange cdrCheck = cc.getCalendarDateRange(); if (cdr == null) cdr = cdrCheck; else if (cdrCheck != null) cdr = cdr.extend( cdrCheck); } this.featureType = ft; this.calendarDateRange = cdr; }
@Test public void TestCoverageSize() throws IOException { String endpoint = TestDir.cdmUnitTestDir + "ncss/GFS/CONUS_80km/GFS_CONUS_80km_20120227_0000.grib1"; logger.info("open {}", endpoint); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { assert cc != null; Assert.assertEquals(1, cc.getCoverageCollections().size()); CoverageCollection gds = cc.getCoverageCollections().get(0); Assert.assertNotNull(endpoint, gds); Assert.assertEquals(FeatureType.GRID, gds.getCoverageType()); String covName = "Temperature_isobaric"; Coverage cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); long size = cover.getSizeInBytes(); Assert.assertEquals(25243920, size); covName = "Relative_humidity_layer_between_two_sigmas_layer"; cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); size = cover.getSizeInBytes(); Assert.assertEquals(870480, size); } }
Element rootElem = new Element("gridDataset"); Document doc = new Document(rootElem); rootElem.setAttribute("location", gcd.getName()); if (null != path) rootElem.setAttribute("path", path); for (CoverageCoordAxis axis : gcd.getCoordAxes()) { rootElem.addContent(writeAxis(axis)); for (CoordSysSet gridset : gcd.getCoverageSets()) { rootElem.addContent(writeCoverageSet(gridset.getCoordSys(), gridset.getCoverages(), gcd.getProjBoundingBox())); for (CoverageTransform ct : gcd.getCoordTransforms()) { rootElem.addContent(writeCoordTransform(ct)); LatLonRect bb = gcd.getLatlonBoundingBox(); if (bb != null) rootElem.addContent(writeBoundingBox(bb)); CalendarDateRange calDateRange = gcd.getCalendarDateRange(); if (calDateRange != null) { Element dateRange = new Element("TimeSpan");
@Test public void TestGribCurvilinear() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "ft/fmrc/rtofs/ofs.20091122/ofs_atl.t00z.F024.grb.grib2"; // GRIB Curvilinear logger.debug("open {}", endpoint); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { assert cc != null; Assert.assertEquals(1, cc.getCoverageCollections().size()); CoverageCollection gds = cc.getCoverageCollections().get(0); Assert.assertNotNull(endpoint, gds); Assert.assertEquals(FeatureType.CURVILINEAR, gds.getCoverageType()); Assert.assertEquals(7, gds.getCoverageCount()); HorizCoordSys hcs = gds.getHorizCoordSys(); Assert.assertNotNull(endpoint, hcs); Assert.assertTrue(endpoint, !hcs.isProjection()); Assert.assertNull(endpoint, hcs.getTransform()); String covName = "Mixed_layer_depth_surface"; Coverage cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); GeoReferencedArray geo = cover.readData(new SubsetParams()); TestCoverageSubsetTime.testGeoArray(geo, null, null, null); } }
CoverageCollection gds = cc.getCoverageCollections().get(0); Assert.assertNotNull(endpoint, gds); Assert.assertEquals(FeatureType.GRID, gds.getCoverageType()); Coverage cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); long size = cover.getSizeInBytes(); LatLonRect llbb = gds.getLatlonBoundingBox(); ProjectionRect projBB = gds.getProjBoundingBox(); ProjectionImpl proj = csys.getProjection(); ProjectionRect projBB2 = proj.latLonToProjBB(llbb); logger.info("ProjRect2 = {}", projBB2); SubsetParams subset = new SubsetParams().setLatLonBoundingBox(gds.getLatlonBoundingBox()); // should be the same! Optional<CoverageCoordSys> opt = csys.subset(subset); Assert.assertTrue(opt.isPresent());
@Test public void TestNetcdfCurvilinear() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "ft/coverage/Run_20091025_0000.nc"; // NetCDF has 2D and 1D logger.debug("open {}", endpoint); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { assert cc != null; Assert.assertEquals(1, cc.getCoverageCollections().size()); CoverageCollection gds = cc.getCoverageCollections().get(0); Assert.assertNotNull(endpoint, gds); Assert.assertEquals(FeatureType.CURVILINEAR, gds.getCoverageType()); Assert.assertEquals(20, gds.getCoverageCount()); String covName = "u"; Coverage cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); SubsetParams params = new SubsetParams().setVertCoord(-.05).set(SubsetParams.timePresent, true); GeoReferencedArray geo = cover.readData(params); Array data = geo.getData(); Index ima = data.getIndex(); int[] expectedShape = new int[] {1,1,22,12}; Assert.assertArrayEquals(expectedShape, data.getShape()); Assert2.assertNearlyEquals(0.0036624447, data.getDouble(ima.set(0, 0, 0, 0)), 1e-6); Assert2.assertNearlyEquals(0.20564626, data.getDouble(ima.set(0, 0, 21, 11)), 1e-6); } }
@Ignore("files not present") @Test public void testGridCoverageDatasetFmrc() throws IOException, InvalidRangeException { System.out.printf("%s%n", filename); try (FeatureDatasetCoverage fdc = CoverageDatasetFactory.open(filename)) { Assert.assertNotNull(filename, fdc); CoverageCollection cc = fdc.findCoverageDataset(FeatureType.GRID); Assert.assertNotNull(FeatureType.GRID.toString(), cc); System.out.printf(" %s type=%s%n", cc.getName(), cc.getCoverageType()); for (CoverageCoordSys coordSys : cc.getCoordSys()) { Assert.assertTrue( coordSys.isTime2D(coordSys.getAxis(AxisType.RunTime))); Assert.assertTrue( coordSys.isTime2D(coordSys.getTimeAxis())); } for (CoverageCoordAxis axis : cc.getCoordAxes()) { if (axis.getAxisType().isTime()) System.out.printf(" %12s %10s %5d %10s %s%n", axis.getName(), axis.getAxisType(), axis.getNcoords(), axis.getDependenceType(), axis.getSpacing()); } } } }
private void writeCoverageData(CoverageCollection gdsOrg, SubsetParams subsetParams, CoverageCollection subsetDataset, NetcdfFileWriter writer) throws IOException, InvalidRangeException { for (Coverage coverage : subsetDataset.getCoverages()) { // we need to call readData on the original Coverage coverageOrg = gdsOrg.findCoverage(coverage.getName()); GeoReferencedArray array = coverageOrg.readData(subsetParams); // test conform to whatever axis.getCoordsAsArray() returns checkConformance(coverage, array, gdsOrg.getName()); Variable v = writer.findVariable(coverage.getName()); if (show) System.out.printf("CFGridCoverageWriter2 write coverage %s%n", v.getNameAndDimensions()); writer.write(v, array.getData()); } }
CoverageCollection gds = cc.getCoverageCollections().get(0); Assert.assertNotNull(endpoint, gds); Assert.assertEquals(FeatureType.SWATH, gds.getCoverageType()); Coverage cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); LatLonRect llbb = gds.getLatlonBoundingBox(); Assert.assertNotNull("getLatlonBoundingBox", llbb); logger.debug("llbb={} ({})", llbb.toString2(), llbb); SubsetParams subset = new SubsetParams().setLatLonBoundingBox(gds.getLatlonBoundingBox()); // should be the same! Optional<CoverageCoordSys> opt = csys.subset(subset); Assert.assertTrue(opt.isPresent());
CoverageCollection gds = cc.getCoverageCollections().get(0); Assert.assertNotNull(endpoint, gds); Assert.assertEquals(FeatureType.CURVILINEAR, gds.getCoverageType()); HorizCoordSys hcs = gds.getHorizCoordSys(); Assert.assertNotNull(endpoint, hcs); Assert.assertTrue(endpoint, !hcs.isProjection()); Coverage coverage = gds.findCoverage(covName); Assert.assertNotNull(covName, coverage); CoverageCoordSys csys = coverage.getCoordSys();
private void addGlobalAttributes(CoverageCollection gds, NetcdfFileWriter writer) { // global attributes for (Attribute att : gds.getGlobalAttributes()) { if (att.getShortName().equals(CDM.FILE_FORMAT)) continue; if (att.getShortName().equals(_Coordinate._CoordSysBuilder)) continue; writer.addGroupAttribute(null, att); } Attribute att = gds.findAttributeIgnoreCase(CDM.CONVENTIONS); if (att == null || !att.getStringValue().startsWith("CF-")) // preserve prev version of CF Convention if exists writer.addGroupAttribute(null, new Attribute(CDM.CONVENTIONS, "CF-1.0")); writer.addGroupAttribute(null, new Attribute("History", "Translated to CF-1.0 Conventions by Netcdf-Java CDM (CFGridCoverageWriter2)\n" + "Original Dataset = " + gds.getName() + "; Translation Date = " + CalendarDate.present())); LatLonRect llbb = gds.getLatlonBoundingBox(); if (llbb != null) { // this will replace any existing writer.addGroupAttribute(null, new Attribute(ACDD.LAT_MIN, llbb.getLatMin())); writer.addGroupAttribute(null, new Attribute(ACDD.LAT_MAX, llbb.getLatMax())); writer.addGroupAttribute(null, new Attribute(ACDD.LON_MIN, llbb.getLonMin())); writer.addGroupAttribute(null, new Attribute(ACDD.LON_MAX, llbb.getLonMax())); } }
@Test public void testGridCoverageDatasetRdavm() throws IOException, InvalidRangeException { if (showDetails) System.out.printf("%s%n", filename); try (FeatureDatasetCoverage fdc = CoverageDatasetFactory.open(filename)) { Assert.assertNotNull(filename, fdc); for (CoverageCollection cc : fdc.getCoverageCollections()) { System.out.printf(" %s type=%s%n", cc.getName(), cc.getCoverageType()); //for (CoverageCoordSys coordSys : cc.getCoordSys()) { //Assert.assertTrue( coordSys.isTime2D(coordSys.getAxis(AxisType.RunTime))); //Assert.assertTrue( coordSys.isTime2D(coordSys.getTimeAxis())); //} if (showDetails) for (CoverageCoordAxis axis : cc.getCoordAxes()) { if (axis.getAxisType().isTime()) System.out.printf(" %12s %10s %5d %10s %s%n", axis.getName(), axis.getAxisType(), axis.getNcoords(), axis.getDependenceType(), axis.getSpacing()); } } } }
@Test public void testScalarRuntimeCoordinate() throws IOException { String filename = TestDir.cdmUnitTestDir + "ncss/GFS/CONUS_80km/GFS_CONUS_80km_20120227_0000.grib1.ncx4"; try (DtCoverageDataset gds = DtCoverageDataset.open(filename)) { Assert.assertNotNull(filename, gds); String gridName = "Pressure_surface"; DtCoverage grid = gds.findGridByShortName(gridName); Assert.assertNotNull(gridName, grid); DtCoverageCS gridCS = grid.getCoordinateSystem(); Assert.assertNotNull("Pressure_surface cs", gridCS); Assert.assertEquals("ucar.nc2.ft2.coverage.adapter.GridCS", gridCS.getClass().getName()); CoordinateAxis1DTime runAxis = gridCS.getRunTimeAxis(); Assert.assertNotNull("runtime axis", runAxis); assert runAxis.isScalar(); Formatter errlog = new Formatter(); try (FeatureDatasetCoverage cc = DtCoverageAdapter.factory(gds, errlog)) { Assert.assertNotNull(filename, cc); Assert.assertEquals(1, cc.getCoverageCollections().size()); CoverageCollection cd = cc.getCoverageCollections().get(0); Coverage cov = cd.findCoverage(gridName); Assert.assertNotNull(gridName, cov); CoverageCoordAxis cca = cd.findCoordAxis(runAxis.getShortName()); Assert.assertNotNull(runAxis.getShortName(), cca); Assert.assertEquals(CoverageCoordAxis.Spacing.regularPoint, cca.getSpacing()); } } }
@Test @Category(NeedsExternalResource.class) public void testDodsSubset() throws Exception { String filename = "dods://thredds.ucar.edu/thredds/dodsC/grib/NCEP/GFS/CONUS_80km/best"; System.out.printf("open %s%n", filename); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(filename)) { Assert.assertNotNull(filename, cc); CoverageCollection gcs = cc.findCoverageDataset(FeatureType.GRID); Assert.assertNotNull("gcs", gcs); String gribId = "Pressure_surface"; Coverage coverage = gcs.findCoverage(gribId); Assert.assertNotNull(gribId, coverage); CoverageCoordSys cs = coverage.getCoordSys(); Assert.assertNotNull("coordSys", cs); HorizCoordSys hcs = cs.getHorizCoordSys(); Assert.assertNotNull("HorizCoordSys", hcs); // Assert.assertArrayEquals(new int[]{65, 361, 720}, cs.getShape()); LatLonRect llbb = gcs.getLatlonBoundingBox(); LatLonRect llbb_subset = new LatLonRect(llbb.getLowerLeftPoint(), 20.0, llbb.getWidth() / 2); checkLatLonSubset(gcs, coverage, llbb_subset, new int[]{1, 35, 46}); } }
private static boolean readRandom(FeatureDatasetCoverage covDataset, PrintWriter pw ) throws IOException, InvalidRangeException { CoverageCollection cc = covDataset.getCoverageCollections().get(0); int ncov = cc.getCoverageCount(); Random r = new Random(System.currentTimeMillis()); int randomIdx = r.nextInt(ncov); int count = 0; Coverage randomCov = null; for (Coverage c : cc.getCoverages()) { if (count == randomIdx) { randomCov = c; break; } count++; } if (randomCov == null) { pw.format("Bad random coverage"); return false; } SubsetParams subset = new SubsetParams().setTimePresent(); GeoReferencedArray geo = randomCov.readData(subset); Array data = geo.getData(); System.out.printf(" read data from %s shape = %s%n", randomCov.getName(), Misc.showInts(data.getShape())); return true; }
@Override public List<VariableSimpleIF> getDataVariables() { List<VariableSimpleIF> result = new ArrayList<>(); for (CoverageCollection cc : covCollections) { for (Coverage cov : cc.getCoverages()) result.add(cov); } return result; }