AbstractGaussianGrid implements PlanetaryGrid { static int findNearest(double[] values, double value) { int l = 0; int h = values.length - 1; while (l <= h) { final int m = (h + l) >> 1; if (values[m] >= value) { l = m + 1; } else { h = m - 1; } } if (h == -1) { return 0; } else if (h == values.length - 1) { return h; } else if (values[h] - value < value - values[h + 1]) { return h; } else { return h + 1; } } protected  AbstractGaussianGrid(int numRows); @Override final long getBinIndex(double lat, double lon); @Override final int getNumRows(); @Override final int getNumCols(int rowIndex); @Override final long getFirstBinIndex(int rowIndex); @Override final double getCenterLat(int rowIndex); @Override final double[] getCenterLatLon(long binIndex);  }
@Test public void testFindNearest() throws Exception { final double[] values = {175, 50, -50, -175}; assertEquals(0, AbstractGaussianGrid.findNearest(values, 178)); assertEquals(0, AbstractGaussianGrid.findNearest(values, 173)); assertEquals(1, AbstractGaussianGrid.findNearest(values, 60)); assertEquals(2, AbstractGaussianGrid.findNearest(values, -30)); assertEquals(3, AbstractGaussianGrid.findNearest(values, -160)); assertEquals(3, AbstractGaussianGrid.findNearest(values, -179)); }