/** * Is the point (lat,lon) contained in the (row, col) rectangle ? * * @param wantLat lat of point * @param wantLon lon of point * @param rectIndex rectangle row, col, will be clipped to [0, nrows), [0, ncols) * @return true if contained */ private boolean containsOld(double wantLat, double wantLon, int[] rectIndex) { rectIndex[0] = Math.max( Math.min(rectIndex[0], nrows-1), 0); rectIndex[1] = Math.max( Math.min(rectIndex[1], ncols-1), 0); int row = rectIndex[0]; int col = rectIndex[1]; if (debug) System.out.printf(" (%d,%d) contains (%f,%f) in (lat=%f %f) (lon=%f %f) ?%n", rectIndex[0], rectIndex[1], wantLat, wantLon, latEdge.get(row, col), latEdge.get(row + 1, col), lonEdge.get(row, col), lonEdge.get(row, col + 1)); if (wantLat < latEdge.get(row, col)) return false; if (wantLat > latEdge.get(row + 1, col)) return false; if (wantLon < lonEdge.get(row, col)) return false; if (wantLon > lonEdge.get(row, col + 1)) return false; return true; }
/** * Is the point (lat,lon) contained in the (row, col) rectangle ? * * @param wantLat lat of point * @param wantLon lon of point * @param rectIndex rectangle row, col, will be clipped to [0, nrows), [0, ncols) * @return true if contained */ private boolean containsOld(double wantLat, double wantLon, int[] rectIndex) { rectIndex[0] = Math.max(Math.min(rectIndex[0], nrows - 1), 0); rectIndex[1] = Math.max(Math.min(rectIndex[1], ncols - 1), 0); int row = rectIndex[0]; int col = rectIndex[1]; if (debug) System.out.printf(" (%d,%d) contains (%f,%f) in (lat=%f %f) (lon=%f %f) ?%n", rectIndex[0], rectIndex[1], wantLat, wantLon, latEdge.get(row, col), latEdge.get(row + 1, col), lonEdge.get(row, col), lonEdge.get(row, col + 1)); if (wantLat < latEdge.get(row, col)) return false; if (wantLat > latEdge.get(row + 1, col)) return false; if (wantLon < lonEdge.get(row, col)) return false; if (wantLon > lonEdge.get(row, col + 1)) return false; return true; }
/** * Is the point (lat,lon) contained in the (row, col) rectangle ? * * @param wantLat lat of point * @param wantLon lon of point * @param rectIndex rectangle row, col, will be clipped to [0, nrows), [0, ncols) * @return true if contained */ private boolean containsOld(double wantLat, double wantLon, int[] rectIndex) { rectIndex[0] = Math.max( Math.min(rectIndex[0], nrows-1), 0); rectIndex[1] = Math.max( Math.min(rectIndex[1], ncols-1), 0); int row = rectIndex[0]; int col = rectIndex[1]; if (debug) System.out.printf(" (%d,%d) contains (%f,%f) in (lat=%f %f) (lon=%f %f) ?%n", rectIndex[0], rectIndex[1], wantLat, wantLon, latEdge.get(row, col), latEdge.get(row + 1, col), lonEdge.get(row, col), lonEdge.get(row, col + 1)); if (wantLat < latEdge.get(row, col)) return false; if (wantLat > latEdge.get(row + 1, col)) return false; if (wantLon < lonEdge.get(row, col)) return false; if (wantLon > lonEdge.get(row, col + 1)) return false; return true; }
/** * Normal case: do something reasonable in deciding on the edges when we have the midpoints of a 2D coordinate. * * @param midx x coordinates of midpoints * @return x coordinates of edges with shape (ny+1, nx+1) */ static public ArrayDouble.D2 makeXEdges(ArrayDouble.D2 midx) { int[] shape = midx.getShape(); int ny = shape[0]; int nx = shape[1]; ArrayDouble.D2 edgex = new ArrayDouble.D2(ny + 1, nx + 1); for (int y = 0; y < ny - 1; y++) { for (int x = 0; x < nx - 1; x++) { // the interior edges are the average of the 4 surrounding midpoints double xval = (midx.get(y, x) + midx.get(y, x + 1) + midx.get(y + 1, x) + midx.get(y + 1, x + 1)) / 4; edgex.set(y + 1, x + 1, xval); } // extrapolate to exterior points edgex.set(y + 1, 0, edgex.get(y + 1, 1) - (edgex.get(y + 1, 2) - edgex.get(y + 1, 1))); edgex.set(y + 1, nx, edgex.get(y + 1, nx - 1) + (edgex.get(y + 1, nx - 1) - edgex.get(y + 1, nx - 2))); } // extrapolate to the first and last row for (int x = 0; x < nx + 1; x++) { edgex.set(0, x, edgex.get(1, x) - (edgex.get(2, x) - edgex.get(1, x))); edgex.set(ny, x, edgex.get(ny - 1, x) + (edgex.get(ny - 1, x) - edgex.get(ny - 2, x))); } return edgex; }
/** * Is the point (lat,lon) contained in the (row, col) rectangle ? * * @param wantLat lat of point * @param wantLon lon of point * @param rectIndex rectangle row, col, will be clipped to [0, nrows), [0, ncols) * @return true if contained */ private boolean containsOld(double wantLat, double wantLon, int[] rectIndex) { rectIndex[0] = Math.max( Math.min(rectIndex[0], nrows-1), 0); rectIndex[1] = Math.max( Math.min(rectIndex[1], ncols-1), 0); int row = rectIndex[0]; int col = rectIndex[1]; if (debug) System.out.printf(" (%d,%d) contains (%f,%f) in (lat=%f %f) (lon=%f %f) ?%n", rectIndex[0], rectIndex[1], wantLat, wantLon, latEdge.get(row, col), latEdge.get(row + 1, col), lonEdge.get(row, col), lonEdge.get(row, col + 1)); if (wantLat < latEdge.get(row, col)) return false; if (wantLat > latEdge.get(row + 1, col)) return false; if (wantLon < lonEdge.get(row, col)) return false; if (wantLon > lonEdge.get(row, col + 1)) return false; return true; }
/** * Is the point (lat,lon) contained in the (row, col) rectangle ? * * @param wantLat lat of point * @param wantLon lon of point * @param rectIndex rectangle row, col, will be clipped to [0, nrows), [0, ncols) * @return true if contained */ private boolean containsOld(double wantLat, double wantLon, int[] rectIndex) { rectIndex[0] = Math.max( Math.min(rectIndex[0], nrows-1), 0); rectIndex[1] = Math.max( Math.min(rectIndex[1], ncols-1), 0); int row = rectIndex[0]; int col = rectIndex[1]; if (debug) System.out.printf(" (%d,%d) contains (%f,%f) in (lat=%f %f) (lon=%f %f) ?%n", rectIndex[0], rectIndex[1], wantLat, wantLon, latEdge.get(row, col), latEdge.get(row + 1, col), lonEdge.get(row, col), lonEdge.get(row, col + 1)); if (wantLat < latEdge.get(row, col)) return false; if (wantLat > latEdge.get(row + 1, col)) return false; if (wantLon < lonEdge.get(row, col)) return false; if (wantLon > lonEdge.get(row, col + 1)) return false; return true; }
private void makeConnectedLon(ArrayDouble.D2 mid) { int[] shape = mid.getShape(); int ny = shape[0]; int nx = shape[1]; // first row double connect = mid.get(0, 0); for (int i = 1; i < nx; i++) { connect = connectLon(connect, mid.get(0, i)); mid.set(0, i, connect); } // other rows for (int j = 1; j < ny; j++) { connect = mid.get(j - 1, 0); for (int i = 0; i < nx; i++) { connect = connectLon(connect, mid.get(j, i)); mid.set(j, i, connect); } } }
private boolean incr(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int col = rectIndex[1]; double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); if (Math.abs(diffLat) > Math.abs(diffLon)) { // try lat first rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } else { rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } // back to original, do box search rectIndex[0] = row; rectIndex[1] = col; return box9(wantLat, wantLon, rectIndex); }
private boolean incr(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int col = rectIndex[1]; double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); if (Math.abs(diffLat) > Math.abs(diffLon)) { // try lat first rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } else { rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } // back to original, do box search rectIndex[0] = row; rectIndex[1] = col; return box9(wantLat, wantLon, rectIndex); }
private boolean incr(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int col = rectIndex[1]; double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); if (Math.abs(diffLat) > Math.abs(diffLon)) { // try lat first rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } else { rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } // back to original, do box search rectIndex[0] = row; rectIndex[1] = col; return box9(wantLat, wantLon, rectIndex); }
private boolean incr(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int col = rectIndex[1]; double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); if (Math.abs(diffLat) > Math.abs(diffLon)) { // try lat first rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } else { rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } // back to original, do box search rectIndex[0] = row; rectIndex[1] = col; return box9(wantLat, wantLon, rectIndex); }
private boolean incr(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int col = rectIndex[1]; double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); if (Math.abs(diffLat) > Math.abs(diffLon)) { // try lat first rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } else { rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } // back to original, do box search rectIndex[0] = row; rectIndex[1] = col; return box9(wantLat, wantLon, rectIndex); }
private void makeConnectedLon(ArrayDouble.D2 mid) { int[] shape = mid.getShape(); int ny = shape[0]; int nx = shape[1]; // first row double connect = mid.get(0, 0); for (int i = 1; i < nx; i++) { connect = connectLon(connect, mid.get(0,i)); mid.set(0,i,connect); } // other rows for (int j = 1; j <ny; j++) { connect = mid.get(j-1, 0); for (int i = 0; i < nx; i++) { connect = connectLon(connect, mid.get(j,i)); mid.set(j,i,connect); } } }
private void makeConnectedLon(ArrayDouble.D2 mid) { int[] shape = mid.getShape(); int ny = shape[0]; int nx = shape[1]; // first row double connect = mid.get(0, 0); for (int i = 1; i < nx; i++) { connect = connectLon(connect, mid.get(0, i)); mid.set(0, i, connect); } // other rows for (int j = 1; j < ny; j++) { connect = mid.get(j - 1, 0); for (int i = 0; i < nx; i++) { connect = connectLon(connect, mid.get(j, i)); mid.set(j, i, connect); } } }
public void testSlice3D() throws InvalidRangeException, IOException { Array a = Array.makeArray(DataType.DOUBLE, 1000, 0.0, 1.0); Array a3 = a.reshape(new int[] {10,10,10}); logger.debug("{}", NCdumpW.toString(a3, "test a3", null)); Array a2 = a3.slice(0,1); logger.debug("{}", NCdumpW.toString(a2, "a3.slice(0,1)", null)); Array a1 = a2.slice(0,1); logger.debug("{}", NCdumpW.toString(a1, "a2.slice(0,1)", null)); ArrayDouble.D2 twoD = (ArrayDouble.D2) a2; System.out.printf("wrong= %f%n", a2.getDouble(0)); System.out.printf("right= %f%n", twoD.get(0, 0)); ArrayDouble.D1 oneD = (ArrayDouble.D1) a1; System.out.printf("wrong= %f%n", a1.getDouble(0)); System.out.printf("right= %f%n", oneD.get(0)); } }
private int findSingleHit(ArrayDouble.D2 boundsForRun, double target) { int hits = 0; int idxFound = -1; int n = boundsForRun.getShape()[0]; for (int i = 0; i < n; i++) { if (contains(target, boundsForRun.get(i,0), boundsForRun.get(i,1))) { hits++; idxFound = i; } } if (hits == 1) return idxFound; if (hits == 0) return -1; return -hits; }
private int findClosest(ArrayDouble.D2 boundsForRun , double target) { double minDiff = Double.MAX_VALUE; int idxFound = -1; int n = boundsForRun.getShape()[0]; for (int i = 0; i < n; i++) { double midpoint = (boundsForRun.get(i,0) + boundsForRun.get(i,1))/2.0; double diff = Math.abs(midpoint - target); if (diff < minDiff) { minDiff = diff; idxFound = i; } } return idxFound; }
/** * Get the coordinate value at the i, j index. * * @param i index 0 (fastest varying, right-most) * @param j index 1 * @return midpoint.get(j, i). */ public double getCoordValue(int j, int i) { if (coords == null) doRead(); return coords.get(j, i); }
/** * Get the coordinate value at the i, j index. * * @param i index 0 (fastest varying, right-most) * @param j index 1 * @return midpoint.get(j, i). */ public double getCoordValue(int j, int i) { if (coords == null) doRead(); return coords.get(j, i); }
/** Get the coordinate value at the i, j index. * @param i index 0 (fastest varying, right-most) * @param j index 1 * @return midpoint.get( j,i). */ public double getCoordValue(int j, int i) { if (midpoint == null) doRead(); return midpoint.get( j, i); }