public CoverageAsPoint(CoverageCollection gcd, List<String> varNames, SubsetParams subset) throws IOException { this.gcd = gcd; this.subset = subset; latLonPoint = (LatLonPointImpl) subset.get(SubsetParams.latlonPoint); if (latLonPoint == null) throw new IllegalArgumentException("No latlon point"); varData = new ArrayList<>(varNames.size()); for (String varName : varNames) { Coverage cov = gcd.findCoverage(varName); // LOOK we should read all at once if possible if (cov != null) { varData.add(new VarData(cov)); if (dateUnit == null) { // assume all have the same time unit, just use the first one CoverageCoordSys csys = cov.getCoordSys(); CoverageCoordAxis timeAxis = csys.getTimeAxis(); this.dateUnit = timeAxis.getCalendarDateUnit(); } } } }
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()); } }
@Override public int getRank() { return getShape().length; }
public Coverage(Coverage from, CoverageCoordSys coordSysSubset) { this.name = from.getName(); this.dataType = from.getDataType(); this.atts = from.atts; this.units = from.getUnitsString(); this.description = from.getDescription(); this.coordSysName = (coordSysSubset != null) ? coordSysSubset.getName() : from.coordSysName; this.reader = from.reader; this.user = from.user; }
private void addCoverages(CoverageCollection subsetDataset, NetcdfFileWriter writer) { for (Coverage grid : subsetDataset.getCoverages()) { Variable v = writer.addVariable(null, grid.getName(), grid.getDataType(), grid.getIndependentAxisNamesOrdered()); addVariableAttributes(v, grid.getAttributes()); } }
CdmrFeatureProto.Coverage.Builder encodeGrid(Coverage grid) { CdmrFeatureProto.Coverage.Builder builder = CdmrFeatureProto.Coverage.newBuilder(); builder.setName(grid.getName()); builder.setDataType(NcStream.convertDataType(grid.getDataType())); for (Attribute att : grid.getAttributes()) builder.addAtts(NcStream.encodeAtt(att)); builder.setUnits(grid.getUnitsString()); builder.setDescription(grid.getDescription()); builder.setCoordSys(grid.getCoordSysName()); return builder; }
@Test @Category(NeedsExternalResource.class) public void testCdmRemoteCoverage() throws Exception { String ds = "http://thredds-test.unidata.ucar.edu/thredds/catalog/grib/NCEP/GFS/Global_0p25deg_ana/latest.xml"; try (DataFactory.Result result = new DataFactory().openFeatureDataset("thredds:resolve:" + ds, null)) { logger.debug("result errlog = {}", result.errLog); assert !result.fatalError; assert result.featureType == FeatureType.GRID; assert result.featureDataset != null; String gridName = "Temperature_isobaric"; FeatureDatasetCoverage dataset = (FeatureDatasetCoverage) result.featureDataset; Assert.assertNotNull(gridName, dataset.getDataVariable(gridName)); Assert.assertEquals(1, dataset.getCoverageCollections().size()); CoverageCollection cc = dataset.getCoverageCollections().get(0); Coverage grid = cc.findCoverage(gridName); Assert.assertNotNull(gridName, grid); CoverageCoordSys gcs = grid.getCoordSys(); Assert.assertNotNull("CoverageCoordSys", gcs); Assert.assertEquals("CoverageCoordSys rank", 5, gcs.getAxes().size()); SubsetParams params = new SubsetParams().set(SubsetParams.timePresent, true).setHorizStride(3); GeoReferencedArray geo = grid.readData(params); CoverageCoordSys geoCoordsys = geo.getCoordSysForData(); Assert.assertNotNull("geoCoordsys", geoCoordsys); int[] shape = geoCoordsys.getShape(); logger.debug("grid_section.getShape = {}", new Section(shape)); int[] expectShape = new int[] {1, 31, 241, 480}; Assert.assertArrayEquals("subset shape", expectShape, shape); } }
@Override public GeoReferencedArray readData(Coverage coverage, SubsetParams params, boolean canonicalOrder) throws IOException, InvalidRangeException { DtCoverage grid = (DtCoverage) coverage.getUserObject(); CoverageCoordSys orgCoordSys = coverage.getCoordSys(); ucar.nc2.util.Optional<CoverageCoordSys> opt = orgCoordSys.subset(params); if (!opt.isPresent()) return new GeoReferencedArray(coverage.getName(), coverage.getDataType(), data, subsetCoordSys); Array result = Array.factory(coverage.getDataType(), subsetCoordSys.getShape()); System.out.printf(" read %s result shape=%s%n", coverage.getName(), Misc.showInts(result.getShape())); int[] origin = new int[result.getRank()]; // all zeroes return new GeoReferencedArray(coverage.getName(), coverage.getDataType(), result, subsetCoordSys);
Assert.assertArrayEquals(expectShape, cov.getShape()); CoverageCoordSys gcs = cov.getCoordSys(); Assert.assertNotNull(gcs);
public VarData(Coverage cov) throws IOException { this.cov = cov; try { this.array = cov.readData(subset); if (debug) System.out.printf(" Coverage %s data shape = %s%n", cov.getName(), Misc.showInts(array.getData().getShape())); } catch (InvalidRangeException e) { e.printStackTrace(); } } }
@Test public void testNcml() throws IOException, InvalidRangeException { String catalog = "/catalog/hioos/model/wav/swan/oahu/catalog.xml"; Catalog cat = TdsLocalCatalog.open(catalog); String id = "hioos/model/wav/swan/oahu/SWAN_Oahu_Regional_Wave_Model_(500m)_best.ncd"; Dataset ds = cat.findDatasetByID(id); assert (ds != null) : "cant find dataset id="+id; assert ds.getFeatureType() == FeatureType.GRID; DataFactory fac = new DataFactory(); try (DataFactory.Result dataResult = fac.openFeatureDataset( ds, null)) { assert !dataResult.fatalError; assert dataResult.featureDataset != null; FeatureDatasetCoverage gds = (FeatureDatasetCoverage) dataResult.featureDataset; String gridName = "salt"; VariableSimpleIF vs = gds.getDataVariable(gridName); Assert.assertNotNull(gridName, vs); Assert.assertEquals(1, gds.getCoverageCollections().size()); CoverageCollection cc = gds.getCoverageCollections().get(0); Coverage grid = cc.findCoverage(gridName); Assert.assertNotNull(gridName, grid); CoverageCoordSys gcs = grid.getCoordSys(); Assert.assertNotNull(gcs); int[] expectShape = new int[] {65, 30, 194, 294}; Assert.assertArrayEquals(expectShape, grid.getShape()); Attribute att = grid.findAttributeIgnoreCase("_FillValue"); assert att != null; assert att.getDataType() == DataType.FLOAT; assert Float.isNaN((Float) att.getNumericValue()); } }
private void addCFAnnotations(CoverageCollection gds, NetcdfFileWriter writer, boolean shouldAddLatLon2D) { for (Coverage grid : gds.getCoverages()) { CoverageCoordSys gcs = grid.getCoordSys(); Variable newV = writer.findVariable(grid.getName()); if (newV == null) { logger.error("CFGridCoverageWriter2 cant find " + grid.getName() + " in writer "); continue; for (String axisName : grid.getCoordSys().getAxisNames()) { coordsAttribValFormatter.format("%s ", axisName);
Assert.assertNotNull(covName, cover); int[] shape = cover.getShape(); logger.debug("{}", Misc.showInts(shape)); Assert.assertArrayEquals(new int[]{1631, 1465}, shape); long size = cover.getSizeInBytes(); Assert.assertEquals(1631 * 1465 * DataType.USHORT.getSize(), size); CoverageCoordSys csys = cover.getCoordSys(); LatLonRect llbb = gds.getLatlonBoundingBox(); Assert.assertNotNull("getLatlonBoundingBox", llbb); Assert.assertEquals(csys.getYAxis().getNcoords(), csyss.getYAxis().getNcoords()); GeoReferencedArray geo = cover.readData(subset); Array data = geo.getData(); logger.debug("{}", Misc.showInts(data.getShape()));
@Test public void TestCFWriterCoverageRead() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "ncss/test/GFS_CONUS_80km_20120227_0000.grib1.nc4"; 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.FMRC, gds.getCoverageType()); String covName = "Temperature_isobaric"; Coverage cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); long size = cover.getSizeInBytes(); Assert.assertEquals(25243920, size); SubsetParams subset = new SubsetParams().setVertCoord(300.0).setTimeOffset(42.0); GeoReferencedArray geo = cover.readData(subset); Array data = geo.getData(); logger.info("{}", Misc.showInts(data.getShape())); } } }
@Override public String getShortName() { return getName(); }
private Element writeGrid(Coverage grid) { Element varElem = new Element("grid"); varElem.setAttribute("name", grid.getName()); String desc = grid.getDescription() != null ? grid.getDescription() : "No description"; varElem.setAttribute("desc", desc); /* StringBuilder buff = new StringBuilder(); List dims = grid.getDimensions(); for (int i = 0; i < dims.size(); i++) { Dimension dim = (Dimension) dims.get(i); if (i > 0) buff.append(" "); if (dim.isShared()) buff.append(dim.getShortName()); else buff.append(dim.getLength()); } if (buff.length() > 0) varElem.setAttribute("shape", buff.toString()); */ DataType dt = grid.getDataType(); if (dt != null) varElem.setAttribute("type", dt.toString()); //GridCoordSystem cs = grid.getCoordinateSystem(); //varElem.setAttribute("coordSys", cs.getName()); // attributes for (ucar.nc2.Attribute att : grid.getAttributes()) { varElem.addContent(ncmlWriter.makeAttributeElement(att)); } return varElem; }
if (orgGrid == null) continue; orgCoverages.add(orgGrid); CoverageCoordSys cs = orgGrid.getCoordSys(); orgCoordSys.put(cs.getName(), cs); for (String tname : cs.getTransformNames()) CoverageCoordSys subsetCs = subsetCFCoordSys.get(orgCov.getCoordSysName()); coverages.add( new Coverage(orgCov, subsetCs)); // must use a copy, because of setCoordSys()
Coverage cover = gds.findCoverage(covName); Assert.assertNotNull(covName, cover); long size = cover.getSizeInBytes(); Assert.assertEquals(6433128, size); CoverageCoordSys csys = cover.getCoordSys(); LatLonRect llbb = gds.getLatlonBoundingBox(); ProjectionRect projBB = gds.getProjBoundingBox();
private List<CoordSysSet> wireObjectsTogether(List<Coverage> coverages) { for (CoverageCoordAxis axis : coordAxes) axisMap.put(axis.getName(), axis); for (CoverageCoordAxis axis : coordAxes) axis.setDataset(this); // wire dependencies Map<String, CoordSysSet> map = new HashMap<>(); for (Coverage coverage : coverages) { coverageMap.put(coverage.getName(), coverage); CoordSysSet gset = map.get(coverage.getCoordSysName()); // duplicates get eliminated here if (gset == null) { CoverageCoordSys ccsys = findCoordSys(coverage.getCoordSysName()); if (ccsys == null) { throw new IllegalStateException("Cant find "+coverage.getCoordSysName()); } gset = new CoordSysSet(ccsys); // must use findByName because objects arent wired up yet map.put(coverage.getCoordSysName(), gset); gset.getCoordSys().setDataset(this); // wire dataset into coordSys } gset.addCoverage(coverage); coverage.setCoordSys(gset.getCoordSys()); // wire coordSys into coverage } // sort the coordsys sets List<CoordSysSet> csets = new ArrayList<>(map.values()); Collections.sort(csets, (o1, o2) -> o1.getCoordSys().getName().compareTo(o2.getCoordSys().getName())); return csets; }
@Override public PointFeature next() { double obsTime = timeAxis.getCoordMidpoint(curr); StructureDataScalar coords = new StructureDataScalar("Coords"); for (VarIter vi : varIters) { coords.addMember(vi.cov.getName(), null, null, vi.cov.getDataType(), (Number) vi.dataIter.getObjectNext()); } curr++; PointFeature pf = new MyPointFeature(MyStationFeature.this, obsTime, 0.0, timeUnit, coords); calcBounds(pf); return pf; }