@Test public void testWrite() throws IOException { String fileOut = tempFolder.newFile().getAbsolutePath(); try (GeoTiffWriter2 writer = new GeoTiffWriter2(fileOut)) { writer.writeGrid(filename, field, 0, 0, true, llbb); } try (GeoTiff geotiff = new GeoTiff(fileOut)) { geotiff.read(); System.out.println("geotiff read in = " + geotiff.showInfo()); } }
public void writeGrid(String gridDataset_filename, String gridName, int time, int level, boolean greyScale, LatLonRect pt) throws IOException { try (GridDataset dataset = ucar.nc2.dt.grid.GridDataset.open(gridDataset_filename)) { GridDatatype grid = dataset.findGridDatatype(gridName); if (grid == null) { throw new IllegalArgumentException("No grid named " + gridName + " in fileName"); } GridCoordSystem gcs = grid.getCoordinateSystem(); Projection proj = grid.getProjection(); if (!gcs.isRegularSpatial()) { Attribute att = dataset.attributes().findAttribute("datasetId"); if (att != null && att.getStringValue().contains("DMSP")) { writeSwathGrid(gridDataset_filename, gridName, time, level, greyScale, pt); return; } else { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } Array data = grid.readDataSlice(time, level, -1, -1); Array lon = xaxis.read(); Array lat = yaxis.read(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); lat = lat.flip(0); } if (gcs.isLatLon()) { data = geoShiftDataAtLon(data, lon); lon = geoShiftLon(lon); } LatLonPoint llp0 = pt.getLowerLeftPoint(); LatLonPoint llpn = pt.getUpperRightPoint(); double minLon = llp0.getLongitude(); double minLat = llp0.getLatitude(); double maxLon = llpn.getLongitude(); double maxLat = llpn.getLatitude(); int x1; int x2; int y1; int y2; double xStart; double yStart; if (!gcs.isLatLon()) { ProjectionPoint pjp0 = proj.latLonToProj(maxLat, minLon); x1 = getXIndex(lon, pjp0.getX(), 0); y1 = getYIndex(lat, pjp0.getY(), 0); yStart = pjp0.getY() * 1000.0; xStart = pjp0.getX() * 1000.0; ProjectionPoint pjpn = proj.latLonToProj(minLat, maxLon); x2 = getXIndex(lon, pjpn.getX(), 1); y2 = getYIndex(lat, pjpn.getY(), 1); } else { xStart = minLon; yStart = maxLat; x1 = getLonIndex(lon, minLon, 0); y1 = getLatIndex(lat, maxLat, 0); x2 = getLonIndex(lon, maxLon, 1); y2 = getLatIndex(lat, minLat, 1); } double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; Array data1 = getYXDataInBox(data, x1, x2, y1, y2); if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data1, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } }
GeoTiffWriter2 extends GeotiffWriter { public void writeGrid(String gridDataset_filename, String gridName, int time, int level, boolean greyScale, LatLonRect pt) throws IOException { try (GridDataset dataset = ucar.nc2.dt.grid.GridDataset.open(gridDataset_filename)) { GridDatatype grid = dataset.findGridDatatype(gridName); if (grid == null) { throw new IllegalArgumentException("No grid named " + gridName + " in fileName"); } GridCoordSystem gcs = grid.getCoordinateSystem(); Projection proj = grid.getProjection(); if (!gcs.isRegularSpatial()) { Attribute att = dataset.attributes().findAttribute("datasetId"); if (att != null && att.getStringValue().contains("DMSP")) { writeSwathGrid(gridDataset_filename, gridName, time, level, greyScale, pt); return; } else { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } Array data = grid.readDataSlice(time, level, -1, -1); Array lon = xaxis.read(); Array lat = yaxis.read(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); lat = lat.flip(0); } if (gcs.isLatLon()) { data = geoShiftDataAtLon(data, lon); lon = geoShiftLon(lon); } LatLonPoint llp0 = pt.getLowerLeftPoint(); LatLonPoint llpn = pt.getUpperRightPoint(); double minLon = llp0.getLongitude(); double minLat = llp0.getLatitude(); double maxLon = llpn.getLongitude(); double maxLat = llpn.getLatitude(); int x1; int x2; int y1; int y2; double xStart; double yStart; if (!gcs.isLatLon()) { ProjectionPoint pjp0 = proj.latLonToProj(maxLat, minLon); x1 = getXIndex(lon, pjp0.getX(), 0); y1 = getYIndex(lat, pjp0.getY(), 0); yStart = pjp0.getY() * 1000.0; xStart = pjp0.getX() * 1000.0; ProjectionPoint pjpn = proj.latLonToProj(minLat, maxLon); x2 = getXIndex(lon, pjpn.getX(), 1); y2 = getYIndex(lat, pjpn.getY(), 1); } else { xStart = minLon; yStart = maxLat; x1 = getLonIndex(lon, minLon, 0); y1 = getLatIndex(lat, maxLat, 0); x2 = getLonIndex(lon, maxLon, 1); y2 = getLatIndex(lat, minLat, 1); } double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; Array data1 = getYXDataInBox(data, x1, x2, y1, y2); if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data1, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } } }
GeoTiffWriter2 extends GeotiffWriter { public void writeGrid(String gridDataset_filename, String gridName, int time, int level, boolean greyScale, LatLonRect pt) throws IOException { try (GridDataset dataset = ucar.nc2.dt.grid.GridDataset.open(gridDataset_filename)) { GridDatatype grid = dataset.findGridDatatype(gridName); if (grid == null) { throw new IllegalArgumentException("No grid named " + gridName + " in fileName"); } GridCoordSystem gcs = grid.getCoordinateSystem(); Projection proj = grid.getProjection(); if (!gcs.isRegularSpatial()) { Attribute att = dataset.attributes().findAttribute("datasetId"); if (att != null && att.getStringValue().contains("DMSP")) { writeSwathGrid(gridDataset_filename, gridName, time, level, greyScale, pt); return; } else { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } Array data = grid.readDataSlice(time, level, -1, -1); Array lon = xaxis.read(); Array lat = yaxis.read(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); lat = lat.flip(0); } if (gcs.isLatLon()) { data = geoShiftDataAtLon(data, lon); lon = geoShiftLon(lon); } LatLonPoint llp0 = pt.getLowerLeftPoint(); LatLonPoint llpn = pt.getUpperRightPoint(); double minLon = llp0.getLongitude(); double minLat = llp0.getLatitude(); double maxLon = llpn.getLongitude(); double maxLat = llpn.getLatitude(); int x1; int x2; int y1; int y2; double xStart; double yStart; if (!gcs.isLatLon()) { ProjectionPoint pjp0 = proj.latLonToProj(maxLat, minLon); x1 = getXIndex(lon, pjp0.getX(), 0); y1 = getYIndex(lat, pjp0.getY(), 0); yStart = pjp0.getY() * 1000.0; xStart = pjp0.getX() * 1000.0; ProjectionPoint pjpn = proj.latLonToProj(minLat, maxLon); x2 = getXIndex(lon, pjpn.getX(), 1); y2 = getYIndex(lat, pjpn.getY(), 1); } else { xStart = minLon; yStart = maxLat; x1 = getLonIndex(lon, minLon, 0); y1 = getLatIndex(lat, maxLat, 0); x2 = getLonIndex(lon, maxLon, 1); y2 = getLatIndex(lat, minLat, 1); } double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; Array data1 = getYXDataInBox(data, x1, x2, y1, y2); if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data1, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } } GeoTiffWriter2(String fileOut); }
GeoTiffWriter2 extends GeotiffWriter { public void writeGrid(String gridDataset_filename, String gridName, int time, int level, boolean greyScale, LatLonRect pt) throws IOException { try (GridDataset dataset = ucar.nc2.dt.grid.GridDataset.open(gridDataset_filename)) { GridDatatype grid = dataset.findGridDatatype(gridName); if (grid == null) { throw new IllegalArgumentException("No grid named " + gridName + " in fileName"); } GridCoordSystem gcs = grid.getCoordinateSystem(); Projection proj = grid.getProjection(); if (!gcs.isRegularSpatial()) { Attribute att = dataset.attributes().findAttribute("datasetId"); if (att != null && att.getStringValue().contains("DMSP")) { writeSwathGrid(gridDataset_filename, gridName, time, level, greyScale, pt); return; } else { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } Array data = grid.readDataSlice(time, level, -1, -1); Array lon = xaxis.read(); Array lat = yaxis.read(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); lat = lat.flip(0); } if (gcs.isLatLon()) { data = geoShiftDataAtLon(data, lon); lon = geoShiftLon(lon); } LatLonPoint llp0 = pt.getLowerLeftPoint(); LatLonPoint llpn = pt.getUpperRightPoint(); double minLon = llp0.getLongitude(); double minLat = llp0.getLatitude(); double maxLon = llpn.getLongitude(); double maxLat = llpn.getLatitude(); int x1; int x2; int y1; int y2; double xStart; double yStart; if (!gcs.isLatLon()) { ProjectionPoint pjp0 = proj.latLonToProj(maxLat, minLon); x1 = getXIndex(lon, pjp0.getX(), 0); y1 = getYIndex(lat, pjp0.getY(), 0); yStart = pjp0.getY() * 1000.0; xStart = pjp0.getX() * 1000.0; ProjectionPoint pjpn = proj.latLonToProj(minLat, maxLon); x2 = getXIndex(lon, pjpn.getX(), 1); y2 = getYIndex(lat, pjpn.getY(), 1); } else { xStart = minLon; yStart = maxLat; x1 = getLonIndex(lon, minLon, 0); y1 = getLatIndex(lat, maxLat, 0); x2 = getLonIndex(lon, maxLon, 1); y2 = getLatIndex(lat, minLat, 1); } double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; Array data1 = getYXDataInBox(data, x1, x2, y1, y2); if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data1, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } } GeoTiffWriter2(String fileOut); void writeGrid(String gridDataset_filename, String gridName, int time, int level, boolean greyScale,
      LatLonRect pt); }
GeoTiffWriter2 extends GeotiffWriter { public void writeGrid(String gridDataset_filename, String gridName, int time, int level, boolean greyScale, LatLonRect pt) throws IOException { try (GridDataset dataset = ucar.nc2.dt.grid.GridDataset.open(gridDataset_filename)) { GridDatatype grid = dataset.findGridDatatype(gridName); if (grid == null) { throw new IllegalArgumentException("No grid named " + gridName + " in fileName"); } GridCoordSystem gcs = grid.getCoordinateSystem(); Projection proj = grid.getProjection(); if (!gcs.isRegularSpatial()) { Attribute att = dataset.attributes().findAttribute("datasetId"); if (att != null && att.getStringValue().contains("DMSP")) { writeSwathGrid(gridDataset_filename, gridName, time, level, greyScale, pt); return; } else { throw new IllegalArgumentException("Must have 1D x and y axes for " + grid.getFullName()); } } CoordinateAxis1D xaxis = (CoordinateAxis1D) gcs.getXHorizAxis(); CoordinateAxis1D yaxis = (CoordinateAxis1D) gcs.getYHorizAxis(); if (!xaxis.isRegular() || !yaxis.isRegular()) { throw new IllegalArgumentException("Must be evenly spaced grid = " + grid.getFullName()); } Array data = grid.readDataSlice(time, level, -1, -1); Array lon = xaxis.read(); Array lat = yaxis.read(); double scaler = (xaxis.getUnitsString().equalsIgnoreCase("km")) ? 1000.0 : 1.0; if (yaxis.getCoordValue(0) < yaxis.getCoordValue(1)) { data = data.flip(0); lat = lat.flip(0); } if (gcs.isLatLon()) { data = geoShiftDataAtLon(data, lon); lon = geoShiftLon(lon); } LatLonPoint llp0 = pt.getLowerLeftPoint(); LatLonPoint llpn = pt.getUpperRightPoint(); double minLon = llp0.getLongitude(); double minLat = llp0.getLatitude(); double maxLon = llpn.getLongitude(); double maxLat = llpn.getLatitude(); int x1; int x2; int y1; int y2; double xStart; double yStart; if (!gcs.isLatLon()) { ProjectionPoint pjp0 = proj.latLonToProj(maxLat, minLon); x1 = getXIndex(lon, pjp0.getX(), 0); y1 = getYIndex(lat, pjp0.getY(), 0); yStart = pjp0.getY() * 1000.0; xStart = pjp0.getX() * 1000.0; ProjectionPoint pjpn = proj.latLonToProj(minLat, maxLon); x2 = getXIndex(lon, pjpn.getX(), 1); y2 = getYIndex(lat, pjpn.getY(), 1); } else { xStart = minLon; yStart = maxLat; x1 = getLonIndex(lon, minLon, 0); y1 = getLatIndex(lat, maxLat, 0); x2 = getLonIndex(lon, maxLon, 1); y2 = getLatIndex(lat, minLat, 1); } double xInc = xaxis.getIncrement() * scaler; double yInc = Math.abs(yaxis.getIncrement()) * scaler; Array data1 = getYXDataInBox(data, x1, x2, y1, y2); if (pageNumber > 1) { geotiff.initTags(); } writeGrid(grid, data1, greyScale, xStart, yStart, xInc, yInc, pageNumber); pageNumber++; } } GeoTiffWriter2(String fileOut); void writeGrid(String gridDataset_filename, String gridName, int time, int level, boolean greyScale,
      LatLonRect pt);  }