GeoTiffUtils { public static File generateGeoTiffZipFromGrid(GridDataset gridDataset, List<String> gridVariableList, FeatureCollection<?, ?> featureCollection, boolean requireFullCoverage, Date dateTimeStart, Date dateTimeEnd, String destination) throws GeoTiffUtilException { Path destinationDirectory = Paths.get(destination); CalendarDateFormatter dateFormatter = new CalendarDateFormatter(FILE_DATE_FORMAT); String datasetURI = gridDataset.getLocationURI(); String dataId = datasetURI.replace(OPeNDAPUtils.OPENDAP_PROTO, ""); dataId = dataId.replace(" dataId = dataId.replace("/", "-"); Path workingDirectory; try { workingDirectory = Files.createTempDirectory(destinationDirectory, GEOTIFF_DIRECTORY_PREFIX); } catch (IOException e) { throw new GeoTiffUtilException(GeoTiffUtilExceptionID.GENERAL_EXCEPTION, "GeoTiffUtils", "generateGeoTiffZipFromGrid", "Unable to create GeoTiff working " + "directory at [" + destinationDirectory + "] Exception: " + e.getMessage()); } File result = null; String resultingZipFileName = ""; try { for (String gridVariable : gridVariableList) { GridDatatype parentGridDataType; try { parentGridDataType = gridDataset.findGridDatatype(gridVariable); } catch (Exception e) { throw new GeoTiffUtilException(GeoTiffUtilExceptionID.GENERAL_EXCEPTION, "GeoTiffUtils", "generateGeoTiffZipFromGrid", "Unable to generate Grid Data Type " + "for dataset [" + gridDataset.getLocationURI() + "] and variable [" + gridVariable + "]. Exception: " + e.getMessage()); } Range timeRange = TimeRangeUtil.generateTimeRange(parentGridDataType, dateTimeStart, dateTimeEnd); GridCoordSystem gridCoordSystem = parentGridDataType.getCoordinateSystem(); Range[] xyRanges; try { xyRanges = GridUtils.getXYRangesFromBoundingBox(featureCollection.getBounds(), gridCoordSystem, requireFullCoverage); } catch (Exception e) { throw new GeoTiffUtilException(GeoTiffUtilExceptionID.GENERAL_EXCEPTION, "GeoTiffUtils", "generateGeoTiffZipFromGrid", "Unable to generate XY Range set from Grid Data Type " + "for dataset [" + gridDataset.getLocationURI() + "] and variable [" + gridVariable + "]. Exception: " + e.getMessage()); } GridDatatype gridDataType; try { gridDataType = parentGridDataType.makeSubset(null, null, timeRange, null, xyRanges[1], xyRanges[0]); } catch (Exception e) { throw new GeoTiffUtilException(GeoTiffUtilExceptionID.GENERAL_EXCEPTION, "GeoTiffUtils", "generateGeoTiffZipFromGrid", "Unable to generate Grid Subset " + "for dataset [" + gridDataset.getLocationURI() + "] and variable [" + gridVariable + "]. Exception: " + e.getMessage()); } int delta = timeRange.last() - timeRange.first(); Range deltaRange = null; try { deltaRange = new Range(0, delta); } catch (InvalidRangeException e) { throw new GeoTiffUtilException(GeoTiffUtilExceptionID.GENERAL_EXCEPTION, "GeoTiffUtils", "generateGeoTiffZipFromGrid", "Unable to generate Subset time range " + "for dataset [" + gridDataset.getLocationURI() + "] and variable [" + gridVariable + "]. Exception: " + e.getMessage()); } Iterator iter = deltaRange.getIterator(); while (iter.hasNext()) { int tRange = iter.next(); CalendarDate date = TimeRangeUtil.getTimeFromRangeIndex(gridDataType, tRange); String dateString = dateFormatter.toString(date); String datasetName = dataId + FILE_DELIMETER + dateString; String absoluteFilePath = workingDirectory + File.separator + datasetName + FILE_TIFF_EXTENSION; GeoTiffUtils.createGeoTiffForGrid(gridDataset, gridDataType, tRange, absoluteFilePath); LOGGER.debug("testGeoTiffWriter(): Successfully wrote tiff file at: " + absoluteFilePath); } } resultingZipFileName = workingDirectory.toString() + FILE_ZIP_EXTENSION; try { FileHelper.zipDirectory(workingDirectory.toString(), resultingZipFileName); LOGGER.debug("GeoTiffUtils.generateGeoTiffZipFromGrid(): Directory [" + workingDirectory.toString() + "] successfully zipped."); } catch (Exception e) { throw new GeoTiffUtilException(GeoTiffUtilExceptionID.ZIP_EXCEPTION, "GeoTiffUtils", "generateGeoTiffZipFromGrid", "Unable to zip GeoTiff generated files for directory [" + workingDirectory.toString() + "]. Exception: " + e.getMessage()); } result = new File(resultingZipFileName); } finally { try { FileUtils.deleteDirectory(workingDirectory.toFile()); } catch (IOException e) { LOGGER.error("Unable to delete GeoTiff directory [" + workingDirectory.toString() + "]."); } } if (result == null || !result.exists()) { throw new GeoTiffUtilException(GeoTiffUtilExceptionID.GENERAL_EXCEPTION, "GeoTiffUtils", "generateGeoTiffZipFromGrid", "Unable to find resulting GeoTiff zipped file " + resultingZipFileName + "]."); } return result; }  static File generateGeoTiffZipFromGrid(GridDataset gridDataset, List<String> gridVariableList,
			FeatureCollection<?, ?> featureCollection, boolean requireFullCoverage, Date dateTimeStart,
			Date dateTimeEnd, String destination); static void createGeoTiffForGrid(GridDataset gridDataset, GridDatatype grid, int timeIndex, String filename); static final String GEOTIFF_DIRECTORY_PREFIX; static final String FILE_DATE_FORMAT; static final String FILE_DELIMETER; static final String FILE_TIFF_EXTENSION; static final String FILE_ZIP_EXTENSION; }
@Test public void writeSsebopetaGridToGeoTiffTest() { List<String> gridVariableList = Arrays.asList("et"); DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); Date startDate = null; try { startDate = format.parse("2000-01-01"); } catch (ParseException e) {} assertNotNull(startDate); Date endDate = null; try { endDate = format.parse("2014-12-01"); } catch (ParseException e) {} assertNotNull(endDate); File testFile = null; try { testFile = GeoTiffUtils.generateGeoTiffZipFromGrid(ssebopetaGridDataSet, gridVariableList, featureCollection, true, startDate, endDate, "."); } catch (GeoTiffUtilException e) { e.printStackTrace(); fail(); } assertTrue(testFile.exists()); testFile.delete(); }
@Test public void writePrismGridToGeoTiffTest() { List<String> gridVariableList = Arrays.asList("ppt"); DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); Date startDate = null; try { startDate = format.parse("2000-02-01"); } catch (ParseException e) {} assertNotNull(startDate); Date endDate = null; try { endDate = format.parse("2013-02-01"); } catch (ParseException e) {} assertNotNull(endDate); File testFile = null; try { testFile = GeoTiffUtils.generateGeoTiffZipFromGrid(prismGridDataSet, gridVariableList, featureCollection, true, startDate, endDate, "."); } catch (GeoTiffUtilException e) { e.printStackTrace(); fail(); } assertTrue(testFile.exists()); testFile.delete(); }
@Test public void writeDaymetGridToGeoTiffTest() { List<String> gridVariableList = Arrays.asList("prcp", "srad", "swe"); DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); Date startDate = null; try { startDate = format.parse("2006-10-01"); } catch (ParseException e) {} assertNotNull(startDate); Date endDate = null; try { endDate = format.parse("2007-10-01"); } catch (ParseException e) {} assertNotNull(endDate); File testFile = null; try { testFile = GeoTiffUtils.generateGeoTiffZipFromGrid(daymetGridDataSet, gridVariableList, featureCollection, false, startDate, endDate, "."); testFile.delete(); fail(); } catch (GeoTiffUtilException e) { assertTrue(e.getMessage().contains("Exception: Unsupported projection")); } }