public Object next() { return getCoordMidpoint(current++); } }
public LatLonPoint getLatLon(int yindex, int xindex) { if (isProjection) { double x = xAxis.getCoordMidpoint(xindex); double y = yAxis.getCoordMidpoint(xindex); ProjectionImpl proj = transform.getProjection(); return proj.projToLatLon(x, y); } else { double lat = latAxis.getCoordMidpoint(yindex); double lon = lonAxis.getCoordMidpoint(xindex); return new LatLonPointImpl(lat, lon); } }
private int findClosest(double target) { double minDiff = Double.MAX_VALUE; double useValue = Double.MIN_VALUE; int idxFound = -1; for (int i = 0; i < axis.getNcoords(); i++) { double coord = axis.getCoordMidpoint(i); double diff = Math.abs(coord - target); if (diff < minDiff || (diff == minDiff && coord > useValue)) { minDiff = diff; idxFound = i; useValue = coord; } } return idxFound; }
public Object getCoordObject(int index) { if (axisType == AxisType.RunTime) return makeDate(getCoordMidpoint(index)); if (isInterval()) return new double[]{getCoordEdge1(index), getCoordEdge2(index)}; return getCoordMidpoint(index); }
@Override public String getSummary() { if (axisType != AxisType.RunTime) return super.getSummary(); if (ncoords < 7) { Formatter f = new Formatter(); for (int i = 0; i < ncoords; i++) { CalendarDate cd = makeDate(getCoordMidpoint(i)); if (i > 0) f.format(", "); f.format("%s", cd); } return f.toString(); } Formatter f = new Formatter(); CalendarDate start = makeDate(getStartValue()); f.format("start=%s", start); CalendarDate end = makeDate(getEndValue()); f.format(", end=%s", end); f.format(" (npts=%d spacing=%s)", getNcoords(), getSpacing()); return f.toString(); }
private double[] makeValues(int index) { double[] subsetValues = null; // null for regular switch (axis.getSpacing()) { case irregularPoint: subsetValues = new double[1]; subsetValues[0] = axis.getCoordMidpoint(index); break; case discontiguousInterval: case contiguousInterval: subsetValues = new double[2]; subsetValues[0] = axis.getCoordEdge1(index); subsetValues[1] = axis.getCoordEdge2(index); break; } return subsetValues; }
@Nonnull private CoverageCoordAxisBuilder subsetValuesLatest() { int last = axis.getNcoords() - 1; double val = axis.getCoordMidpoint(last); CoverageCoordAxisBuilder builder = new CoverageCoordAxisBuilder(axis); builder.subset(1, val, val, 0.0, makeValues(last)); try { builder.setRange(new Range(last, last)); } catch (InvalidRangeException e) { throw new RuntimeException(e); // cant happen } return builder; }
@Override public Array getCoordsAsArray() { Array result; switch (dependenceType) { case scalar: result = Array.factory(getDataType(), new int[0]); break; default: result = Array.factory(getDataType(), new int[]{ncoords}); break; } for (int i = 0; i < ncoords; i++) result.setDouble(i, getCoordMidpoint(i)); return result; }
Optional<CoverageCoordAxisBuilder> subsetContaining(double want) { int index = findCoordElement(want, false); // not bounded, may not be valid index if (index < 0 || index >= axis.getNcoords()) return Optional.empty(String.format("value %f not in axis %s", want, axis.getName())); double val = axis.getCoordMidpoint(index); CoverageCoordAxisBuilder builder = new CoverageCoordAxisBuilder(axis); builder.subset(1, val, val, 0.0, makeValues(index)); try { builder.setRange(new Range(index, index)); } catch (InvalidRangeException e) { throw new RuntimeException(e); // cant happen } return Optional.of(builder); }
public List<NamedObject> getCoordValueNames() { loadValuesIfNeeded(); if (timeHelper != null) return timeHelper.getCoordValueNames(this); List<NamedObject> result = new ArrayList<>(); for (int i = 0; i < ncoords; i++) { Object value = null; switch (spacing) { case regularPoint: case irregularPoint: value = Format.d(getCoordMidpoint(i), 3); break; case regularInterval: case contiguousInterval: case discontiguousInterval: value = new CoordInterval(getCoordEdge1(i), getCoordEdge2(i), 3); break; } result.add(new NamedAnything(value, value + " " + getUnits())); } return result; }
@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; }
public List<NamedObject> getCoordValueNames(CoverageCoordAxis1D axis) { axis.loadValuesIfNeeded(); List<NamedObject> result = new ArrayList<>(); for (int i = 0; i < axis.getNcoords(); i++) { double value; switch (axis.getSpacing()) { case regularPoint: case irregularPoint: value = axis.getCoordMidpoint(i); result.add(new NamedAnything(makeDate(value), axis.getAxisType().toString())); break; case regularInterval: case contiguousInterval: case discontiguousInterval: CoordInterval coord = new CoordInterval(axis.getCoordEdge1(i), axis.getCoordEdge2(i), 3); result.add(new NamedAnything(coord, coord + " " + axis.getUnits())); break; } } return result; }
public Optional<CoordReturn> findXYindexFromCoord(double x, double y) { CoordReturn result = new CoordReturn(); if (isProjection) { CoordAxisHelper xhelper = new CoordAxisHelper(xAxis); CoordAxisHelper yhelper = new CoordAxisHelper(yAxis); result.x = xhelper.findCoordElement(x, false); result.y = yhelper.findCoordElement(y, false); if (result.x >= 0 && result.x < xAxis.getNcoords() && result.y >= 0 && result.y < yAxis.getNcoords()) { result.xcoord = xAxis.getCoordMidpoint(result.x); result.ycoord = yAxis.getCoordMidpoint(result.y); return Optional.of(result); } else { return Optional.empty("not in grid"); } } else { // 1D lat lon case CoordAxisHelper xhelper = new CoordAxisHelper(lonAxis); CoordAxisHelper yhelper = new CoordAxisHelper(latAxis); double lon = LatLonPointImpl.lonNormalFrom(x, lonAxis.getStartValue()); result.x = xhelper.findCoordElement(lon, false); result.y = yhelper.findCoordElement(y, false); if (result.x >= 0 && result.x < lonAxis.getNcoords() && result.y >= 0 && result.y < latAxis.getNcoords()) { result.xcoord = lonAxis.getCoordMidpoint(result.x); result.ycoord = latAxis.getCoordMidpoint(result.y); return Optional.of(result); } else { return Optional.empty("not in grid"); } } }
@Nonnull private CoverageCoordAxisBuilder subsetValuesClosest(double want) { int closest_index = findCoordElement(want, true); // bounded, always valid index CoverageCoordAxisBuilder builder = new CoverageCoordAxisBuilder(axis); if (axis.spacing == CoverageCoordAxis.Spacing.regularPoint) { double val = axis.getCoordMidpoint(closest_index); builder.subset(1, val, val, 0.0, null); } else if (axis.spacing == CoverageCoordAxis.Spacing.regularInterval) { double val1 = axis.getCoordEdge1(closest_index); double val2 = axis.getCoordEdge2(closest_index); builder.subset(1, val1, val2, val2-val1, null); } else { builder.subset(1, 0, 0, 0.0, makeValues(closest_index)); } try { builder.setRange(new Range(closest_index, closest_index)); } catch (InvalidRangeException e) { throw new RuntimeException(e); // cant happen } return builder; }
private void addAdjustedTimeCoords(SubsetParams result, CoverageCoordAxis1D axis, int coordIdx, CalendarDate runtime) { // this must be adjusted to be offset from the runtime. // adjust = end - start // axisCoordOffset + axis.reftime = offset + runtime // offset = axisCoordOffset + axis.reftime - runtime // offset = axisCoordOffset + adjust // offset = axisCoordOffset + end - start = axisCoordOffset + axis.reftime - runtime // therefore: end = reftime, start = runtime double adjust = axis.getOffsetInTimeUnits(runtime, axis.getRefDate()); if (axis.isInterval()) { double[] adjustVal = new double[] {axis.getCoordEdge1(coordIdx)+adjust, axis.getCoordEdge2(coordIdx)+adjust}; result.setTimeOffsetIntv(adjustVal); double mid = (adjustVal[0]+adjustVal[1]) / 2.0; result.set(SubsetParams.timeOffsetUnit, axis.makeDateInTimeUnits(runtime, mid)); // validation result.set(SubsetParams.timeOffsetUnit, axis.getCalendarDateUnit()); // validation } else { double adjustVal = axis.getCoordMidpoint(coordIdx) + adjust; result.setTimeOffset(adjustVal); result.set(SubsetParams.timeOffsetDate, axis.makeDateInTimeUnits(runtime, adjustVal)); // validation result.set(SubsetParams.timeOffsetUnit, axis.getCalendarDateUnit()); // validation } }
@Test public void testScalarRuntimeCoordinate() throws IOException { String filename = TestDir.cdmUnitTestDir + "ncss/GFS/CONUS_80km/GFS_CONUS_80km_20120227_0000.grib1.ncx4"; String gridName = "Pressure_surface"; try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(filename)) { 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); CoverageCoordSys csys = cov.getCoordSys(); Assert.assertNotNull("CoverageCoordSys", csys); CoverageCoordAxis1D runtime = (CoverageCoordAxis1D) csys.getAxis(AxisType.RunTime); Assert.assertNotNull(AxisType.RunTime.toString(), runtime); Assert.assertTrue(runtime.getClass().getName(), runtime instanceof CoverageCoordAxis1D); Assert.assertEquals(CoverageCoordAxis.Spacing.regularPoint, runtime.getSpacing()); Assert.assertEquals(CoverageCoordAxis.DependenceType.scalar, runtime.getDependenceType()); CalendarDate startDate = runtime.makeDate(runtime.getCoordMidpoint(0)); Assert.assertEquals(CalendarDate.parseISOformat(null, "2012-02-27T00:00:00Z"), startDate); } }
assertNotNull("time", time); Assert.assertEquals(1, time.getNcoords()); CalendarDate date = time.makeDate(time.getCoordMidpoint(0)); logger.debug("date = {}", date); Assert.assertEquals(date, CalendarDate.parseISOformat(null, "2012-04-19T00:00:00Z")); assertNotNull("vert", vert); Assert.assertEquals(1, vert.getNcoords()); double vertCoord = vert.getCoordMidpoint(0); logger.debug("date = {}", date); Assert2.assertNearlyEquals(800.0, vertCoord);
Assert.assertEquals(4, runtimeAxis.getNcoords()); Assert.assertEquals(CoverageCoordAxis.Spacing.regularPoint, runtimeAxis.getSpacing()); Assert2.assertNearlyEquals(0.0, runtimeAxis.getCoordMidpoint(0)); Assert2.assertNearlyEquals(6.0, runtimeAxis.getResolution()); Assert2.assertNearlyEquals(offsetVal, timeAxis1D.getCoordMidpoint(0));
CoverageCoordAxis1D runtimeAxis1D = (CoverageCoordAxis1D) runtimeAxis; if (runtime != null) Assert.assertEquals("runtime coord", runtime, runtimeAxis.makeDate(runtimeAxis1D.getCoordMidpoint(0))); Assert.assertTrue("time coord lower", !upper.isBefore(time)); // upper >= time } else { Assert.assertEquals("time coord", time, timeAxis1D.makeDate(timeAxis1D.getCoordMidpoint(0)));
Assert.assertEquals(1, runtimeAxis.getNcoords()); CoverageCoordAxis1D runtimeAxis1D = (CoverageCoordAxis1D) runtimeAxis; Assert.assertEquals("runtime coord", runtime, runtimeAxis.makeDate(runtimeAxis1D.getCoordMidpoint(0)));