TimeseriesIterator() { varIters = new ArrayList<>(); for (VarData vd : varData) { Array data = vd.array.getData(); if (debug) System.out.printf("%s shape=%s%n", vd.cov.getName(), Misc.showInts(data.getShape())); varIters.add(new VarIter(vd.cov, vd.array, data.getIndexIterator())); nvalues = (int) data.getSize(); if (timeAxis == null) { // assume they are all the same (!) CoverageCoordSys csys = vd.array.getCoordSysForData(); timeAxis = (CoverageCoordAxis1D) csys.getTimeAxis(); // LOOK may not be right } } }
public GeoReferencedArray readData(CoverageDataResponse dataResponse, GeoArrayResponse arrayResponse, InputStream is) throws IOException { int sizeIn = NcStream.readVInt(is); // not used ? if (arrayResponse.deflate) { is = new InflaterInputStream(is); float ratio = (sizeIn == 0) ? 0.0f : ((float) arrayResponse.uncompressedSize) / sizeIn; if (showCompression) System.out.printf(" readData data message compress= %d decompress=%d compress=%f%n", sizeIn, arrayResponse.uncompressedSize, ratio); } byte[] datab = new byte[(int) arrayResponse.uncompressedSize]; NcStream.readFully(is, datab); Array data = Array.factory(arrayResponse.dataType, arrayResponse.shape, ByteBuffer.wrap(datab)); CoverageCoordSys csys = dataResponse.findCoordSys( arrayResponse.coordSysName); if (csys == null) throw new IOException("Misformed response - no coordsys"); return new GeoReferencedArray(arrayResponse.coverageName, arrayResponse.dataType, data, csys); }
public CdmrFeatureProto.GeoReferencedArray.Builder encodeGeoReferencedArray(GeoReferencedArray geoArray, boolean deflate) { CdmrFeatureProto.GeoReferencedArray.Builder builder = CdmrFeatureProto.GeoReferencedArray.newBuilder(); builder.setCoverageName(geoArray.getCoverageName()); builder.setDataType(NcStream.convertDataType(geoArray.getDataType())); builder.setVersion(3); // set to >= 3 for proto3 builder.setBigend(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN); int shape[] = geoArray.getData().getShape(); for (int aShape : shape) builder.addShape(aShape); CoverageCoordSys csys = geoArray.getCoordSysForData(); for (String axisName : csys.getAxisNames()) // geoArray.getAxisNames()) // LOOK could use csys.getAxisNames(), but order may be incorrect, must match shape builder.addAxisName(axisName); builder.setCoordSysName(csys.getName()); if (deflate) { builder.setCompress(NcStreamProto.Compress.DEFLATE); long uncompressedSize = geoArray.getData().getSizeBytes(); builder.setUncompressedSize(uncompressedSize); } else { builder.setPrimdata(NcStreamDataCol.copyArrayToByteString( geoArray.getData())); } return builder; }
CoverageCoordSys gcs = array.getCoordSysForData(); if (!gcs.isRegularSpatial()) throw new IllegalArgumentException("Must have 1D x and y axes for " + array.getCoverageName()); double yInc = Math.abs(yaxis.getResolution()) * scaler; Array data = array.getData().reduce(); if (yaxis.getCoordMidpoint(0) < yaxis.getCoordMidpoint(1)) { data = data.flip(0);
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; }
@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); } }
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()); } }
CoverageCoordSys geoCs = geo.getCoordSysForData();
private void checkConformance(Coverage gridSubset, GeoReferencedArray geo, String where) { CoverageCoordSys csys = gridSubset.getCoordSys(); CoverageCoordSys csysData = geo.getCoordSysForData(); //System.out.printf(" csys=%s%n", csys); //System.out.printf("csysData=%s%n", csysData); Section s = new Section(csys.getShape()); Section so = new Section(csysData.getShape()); boolean ok = s.conformal(so); int[] dataShape = geo.getData().getShape(); //System.out.printf("dataShape=%s%n", Misc.showInts(dataShape)); Section sdata = new Section(dataShape); boolean ok2 = s.conformal(sdata); if (!ok || !ok2) logger.warn("CFGridCoverageWriter2 checkConformance fails " +where); } }
static int timeDataRead2(String remote, int stride) throws IOException, InvalidRangeException { logger.debug("--Coverage Read {} stride={} ", remote, stride); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(remote)) { CoverageCollection gcs = cc.getSingleCoverageCollection(); String gridName = "10_metre_V_wind_component_surface"; Coverage grid = gcs.findCoverage(gridName); Assert.assertNotNull(gridName, grid); SubsetParams params = new SubsetParams().setTimePresent().setHorizStride(stride); long start = System.currentTimeMillis(); GeoReferencedArray geo = grid.readData(params); long took = System.currentTimeMillis() - start; logger.debug("took={} size={}", took, geo.getData().getSize()); } return 1; } }
CoverageCoordSys geoCs = geo.getCoordSysForData();
public GeoReferencedArray decodeGeoReferencedArray(CoverageDataResponse dataResponse, CdmrFeatureProto.GeoReferencedArray parray) { DataType dataType = NcStream.convertDataType(parray.getDataType()); ByteOrder byteOrder = parray.getBigend() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; boolean deflate = parray.getCompress() == NcStreamProto.Compress.DEFLATE; long uncompressedSize = parray.getUncompressedSize(); int[] shape = new int[parray.getShapeCount()]; for (int i=0; i< parray.getShapeCount(); i++) shape[i] = parray.getShape(i); ByteBuffer bb = parray.getPrimdata().asReadOnlyByteBuffer(); bb.order(byteOrder); Array data = Array.factory(dataType, shape, bb); CoverageCoordSys csys = dataResponse.findCoordSys( parray.getCoordSysName()); if (csys == null) throw new IllegalStateException("Misformed response - no coordsys"); return new GeoReferencedArray(parray.getCoverageName(), dataType, data, csys); }
private void checkLatLonSubset(CoverageCollection gcs, Coverage coverage, LatLonRect bbox, int[] expectedShape) throws Exception { System.out.printf(" coverage llbb = %s width=%f%n", gcs.getLatlonBoundingBox().toString2(), gcs.getLatlonBoundingBox().getWidth()); System.out.printf(" constrain bbox= %s width=%f%n", bbox.toString2(), bbox.getWidth()); SubsetParams params = new SubsetParams().setLatLonBoundingBox(bbox).setTimePresent(); GeoReferencedArray geo = coverage.readData(params); CoverageCoordSys gcs2 = geo.getCoordSysForData(); Assert.assertNotNull("CoordSysForData", gcs2); System.out.printf(" data cs shape=%s%n", Misc.showInts(gcs2.getShape())); System.out.printf(" data shape=%s%n", Misc.showInts(geo.getData().getShape())); Assert.assertArrayEquals("CoordSys=Data shape", gcs2.getShape(), geo.getData().getShape()); Assert.assertArrayEquals("expected data shape", expectedShape, geo.getData().getShape()); }
CompareNetcdf.compareData(dt_array, gc_array.getData()); } catch (Throwable t) { try {
CoverageCoordSys geoCs = geo.getCoordSysForData();
Array data = dataReader.readData2(coordIter, yxRange.get(0), yxRange.get(1)); return new GeoReferencedArray(coverage.getName(), coverage.getDataType(), data, subsetCoordSys);
@Test public void TestGribCurvilinearSubset() 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 coverage = gds.findCoverage(covName); Assert.assertNotNull(covName, coverage); LatLonRect bbox = new LatLonRect(new LatLonPointImpl(64.0, -61.), new LatLonPointImpl(59.0, -52.)); SubsetParams params = new SubsetParams().set(SubsetParams.timePresent, true).set(SubsetParams.latlonBB, bbox); GeoReferencedArray geo = coverage.readData(params); logger.debug("csys shape={}", Misc.showInts(geo.getCoordSysForData().getShape())); Array data = geo.getData(); logger.debug("data shape={}", Misc.showInts(data.getShape())); Assert.assertArrayEquals(geo.getCoordSysForData().getShape(), data.getShape()); int[] expectedShape = new int[] {1, 165, 161}; Assert.assertArrayEquals(expectedShape, data.getShape()); } }
@Test public void testBestTimeOffsetCoord() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "gribCollections/gfs_2p5deg/gfs_2p5deg.ncx4"; String covName = "Temperature_altitude_above_msl"; logger.debug("testBestPresent Dataset {} coverage {}", endpoint, covName); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { Assert.assertNotNull(endpoint, cc); CoverageCollection gcs = cc.findCoverageDataset(FeatureType.GRID); Assert.assertNotNull("gcs", gcs); Coverage cover = gcs.findCoverage(covName); Assert.assertNotNull(covName, cover); SubsetParams params = new SubsetParams(); params.setTimeOffset(48.0); params.setVertCoord(3658.0); logger.debug(" subset {}", params); GeoReferencedArray geo = cover.readData(params); // should not be missing ! assert !Float.isNaN(geo.getData().getFloat(0)); Array data = geo.getData(); Index ai = data.getIndex(); float testValue = data.getFloat(ai.set(0, 0, 3, 0)); Assert2.assertNearlyEquals(250.5, testValue); } }
return new GeoReferencedArray(coverage.getName(), coverage.getDataType(), data, subsetCoordSys); return new GeoReferencedArray(coverage.getName(), coverage.getDataType(), result, subsetCoordSys);
@Test public void testSrcTimePresent() throws IOException, InvalidRangeException { String endpoint = TestDir.cdmUnitTestDir + "ncss/GFS/CONUS_80km/GFS_CONUS_80km_20120227_0000.grib1"; String covName = "Temperature_isobaric"; logger.debug("testSrcTimePresent Dataset {} coverage {}", endpoint, covName); try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(endpoint)) { Assert.assertNotNull(endpoint, cc); CoverageCollection gcs = cc.findCoverageDataset(FeatureType.GRID); Assert.assertNotNull("gcs", gcs); Coverage cover = gcs.findCoverage(covName); Assert.assertNotNull(covName, cover); CoverageCoordSys cs = cover.getCoordSys(); CoverageCoordAxis timeAxis = cs.getAxis(AxisType.Time); Assert.assertNotNull("timeoffset axis", timeAxis); Assert.assertEquals(36, timeAxis.getNcoords()); SubsetParams params = new SubsetParams(); params.set(SubsetParams.timePresent, true); logger.debug(" subset {}", params); GeoReferencedArray geo = cover.readData(params); int[] resultShape = geo.getData().getShape(); int[] expectShape = new int[] {1, 29, 65, 93}; Assert.assertArrayEquals("shape", expectShape, resultShape); CoverageCoordSys geocs = geo.getCoordSysForData(); CoverageCoordAxis toAxis2 = geocs.getAxis(AxisType.Time); Assert.assertNotNull("timeoffset axis", toAxis2); Assert.assertEquals(1, toAxis2.getNcoords()); } }