@Nonnull private CoverageCoordAxisBuilder subsetValuesClosest(double[] want) { int closest_index = findCoordElement(want, true); // bounded, always valid index if (closest_index < 0) findCoordElement(want, true); CoverageCoordAxisBuilder builder = new CoverageCoordAxisBuilder(axis); 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; }
public CoverageCoordAxis1D getTimeAxisForRun(CalendarDate rundate) { double rundateTarget = runCoord.convert(rundate); int run_index = new CoordAxisHelper(runCoord).findCoordElement(rundateTarget, false); // LOOK not Bounded return (run_index < 0 || run_index >= runCoord.getNcoords()) ? null : getTimeAxisForRun(run_index); }
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()); } }
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()); } }
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 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; }
@Override public Optional<CoverageCoordAxis> subset(SubsetParams params) { if (params == null) return Optional.of(new TimeAxis2DFmrc(new CoverageCoordAxisBuilder(this))); CalendarDate rundate = (CalendarDate) params.get(SubsetParams.runtime); boolean runtimeAll = (Boolean) params.get(SubsetParams.runtimeAll); boolean latest = (rundate == null) && !runtimeAll; // default is latest int run_index = -1; if (latest) { run_index = runCoord.getNcoords() - 1; } else if (rundate != null){ double rundateTarget = runCoord.convert(rundate); CoordAxisHelper helper = new CoordAxisHelper(runCoord); run_index = helper.findCoordElement(rundateTarget, true); // LOOK Bounded } if (run_index >= 0) { CoverageCoordAxis1D time1D = getTimeAxisForRun(run_index); return time1D.subset(params); } // no subsetting needed return Optional.of(new TimeAxis2DFmrc(new CoverageCoordAxisBuilder(this))); }