int search(double want) { if (axis.getNcoords() == 1) { return Misc.nearlyEquals(want, axis.getStartValue()) ? 0 : -1; } if (axis.isRegular()) { double fval = (want - axis.getStartValue()) / axis.getResolution(); double ival = Math.rint(fval); return Misc.nearlyEquals(fval, ival) ? (int) ival : (int) -ival - 1; // LOOK } // otherwise do a binary search return Arrays.binarySearch(axis.getValues(), want); } }
private Optional<CoverageCoordAxis> subsetLon(LatLonRect llbb, int stride) throws InvalidRangeException { double wantMin = LatLonPointImpl.lonNormalFrom(llbb.getLonMin(), lonAxis.getStartValue()); double wantMax = LatLonPointImpl.lonNormalFrom(llbb.getLonMax(), lonAxis.getStartValue()); double start = lonAxis.getStartValue(); double end = lonAxis.getEndValue(); // use MAMath.MinMax as a container for two values, min and max List<MAMath.MinMax> lonIntvs = subsetLonIntervals(wantMin, wantMax, start, end); if (lonIntvs.size() == 0) return Optional.empty(String.format( "longitude want [%f,%f] does not intersect lon axis [%f,%f]", wantMin, wantMax, start, end)); if (lonIntvs.size() == 1) { MAMath.MinMax lonIntv = lonIntvs.get(0); return lonAxis.subset(lonIntv.min, lonIntv.max, stride); } // this is the seam crossing case return lonAxis.subsetByIntervals(lonIntvs, stride); }
@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 Optional<CoverageCoordAxisBuilder> subsetValues(double minValue, double maxValue, int stride) { if (axis.getSpacing() == CoverageCoordAxis.Spacing.discontiguousInterval) return subsetValuesDiscontinuous(minValue, maxValue, stride); double lower = axis.isAscending() ? Math.min(minValue, maxValue) : Math.max(minValue, maxValue); double upper = axis.isAscending() ? Math.max(minValue, maxValue) : Math.min(minValue, maxValue); int minIndex = findCoordElement(lower, false); int maxIndex = findCoordElement(upper, false); if (minIndex >= axis.getNcoords()) return Optional.empty(String.format("no points in subset: lower %f > end %f", lower, axis.getEndValue())); if (maxIndex < 0) return Optional.empty(String.format("no points in subset: upper %f < start %f", upper, axis.getStartValue())); if (minIndex < 0) minIndex = 0; if (maxIndex >= axis.getNcoords()) maxIndex = axis.getNcoords() - 1; int count = maxIndex - minIndex + 1; if (count <= 0) throw new IllegalArgumentException("no points in subset"); try { return Optional.of(subsetByIndex(new Range(minIndex, maxIndex, stride))); } catch (InvalidRangeException e) { return Optional.empty(e.getMessage()); } }
public Optional<RangeIterator> makeRange(double minValue, double maxValue, int stride) { //if (axis.getSpacing() == CoverageCoordAxis.Spacing.discontiguousInterval) // return subsetValuesDiscontinuous(minValue, maxValue, stride); double lower = axis.isAscending() ? Math.min(minValue, maxValue) : Math.max(minValue, maxValue); double upper = axis.isAscending() ? Math.max(minValue, maxValue) : Math.min(minValue, maxValue); int minIndex = findCoordElement(lower, false); int maxIndex = findCoordElement(upper, false); if (minIndex >= axis.getNcoords()) return Optional.empty(String.format("no points in subset: lower %f > end %f", lower, axis.getEndValue())); if (maxIndex < 0) return Optional.empty(String.format("no points in subset: upper %f < start %f", upper, axis.getStartValue())); if (minIndex < 0) minIndex = 0; if (maxIndex >= axis.getNcoords()) maxIndex = axis.getNcoords() - 1; int count = maxIndex - minIndex + 1; if (count <= 0) return Optional.empty("no points in subset"); try { return Optional.of(new Range(minIndex, maxIndex, stride)); } catch (InvalidRangeException e) { return Optional.empty(e.getMessage()); } }
CoordAxisHelper yhelper = new CoordAxisHelper(latAxis); double lonNormal = LatLonPointImpl.lonNormalFrom(latlon.getLongitude(), lonAxis.getStartValue()); optb = xhelper.subsetContaining(lonNormal); if (optb.isPresent()) lonaxisSubset = new CoverageCoordAxis1D(optb.get());
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"); } } }
case regularInterval: case regularPoint: builder.startValue = timeAxisSubset.getStartValue() + offset; builder.endValue = timeAxisSubset.getEndValue() + offset; break;
case regularInterval: case regularPoint: builder.startValue = timeAxisSubset.getStartValue() + offset; builder.endValue = timeAxisSubset.getEndValue() + offset; break;
Assert.assertEquals(92, timeAxis.getNcoords()); Assert.assertEquals(CoverageCoordAxis.Spacing.discontiguousInterval, timeAxis.getSpacing()); Assert2.assertNearlyEquals(0.0, timeAxis.getStartValue()); Assert2.assertNearlyEquals(384.0, timeAxis.getEndValue());