List of usage examples for org.apache.commons.io FileUtils writeByteArrayToFile
public static void writeByteArrayToFile(File file, byte[] data) throws IOException
From source file:org.geoserver.wcs2_0.WCSNetCDFMosaicTest.java
@Test public void testRequestNetCDF4() throws Exception { boolean isNC4Available = NetCDFUtilities.isNC4CAvailable(); if (!isNC4Available && LOGGER.isLoggable(Level.INFO)) { LOGGER.info("NetCDF C library not found. NetCDF4 output will not be created"); }/*from w w w. j a v a2s . c o m*/ // http response from the request inside the string MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageId=wcs__dummyView&format=application/x-netcdf4&subset=http://www.opengis.net/def/axis/OGC/0/time(\"2013-01-08T00:00:00.000Z\")"); assertNotNull(response); assertEquals((isNC4Available ? "application/x-netcdf4" : "application/xml"), response.getContentType()); if (isNC4Available) { byte[] netcdfOut = getBinary(response); File file = File.createTempFile("netcdf", "out.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, netcdfOut); NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath()); assertNotNull(dataset); dataset.close(); } }
From source file:org.geoserver.wcs2_0.WCSNetCDFMosaicTest.java
@Test public void testRequestNetCDFUomConversion() throws Exception { // http response from the request inside the string CoverageInfo info = getCatalog().getCoverageByName(new NameImpl("wcs", "visibilityCF")); assertTrue(info.getDimensions().get(0).getUnit().equalsIgnoreCase(ORIGINAL_UNIT)); MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageId=wcs__visibilityCF&format=application/x-netcdf"); assertNotNull(response);/* w w w .ja v a2 s . co m*/ byte[] netcdfOut = getBinary(response); File file = File.createTempFile("netcdf", "outCF.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, netcdfOut); NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath()); Variable var = dataset.findVariable(STANDARD_NAME); assertNotNull(var); // Check the unit has been converted to meter String unit = var.getUnitsString(); assertEquals(CANONICAL_UNIT, unit); Array readData = var.read(NETCDF_SECTION); assertEquals(DataType.FLOAT, readData.getDataType()); float data = readData.getFloat(0); // Data have been converted to canonical unit (m) from km. // Data value is bigger assertEquals(data, (ORIGINAL_PIXEL_VALUE) * 1000, DELTA); Attribute fillValue = var.findAttribute(NetCDFUtilities.FILL_VALUE); Attribute standardName = var.findAttribute(NetCDFUtilities.STANDARD_NAME); assertNotNull(standardName); assertEquals(STANDARD_NAME, standardName.getStringValue()); assertNotNull(fillValue); assertEquals(ORIGINAL_FILL_VALUE, fillValue.getNumericValue().doubleValue(), DELTA); // Check global attributes have been added Attribute attribute = dataset.findGlobalAttribute("custom_attribute"); assertNotNull(attribute); assertEquals("testing WCS", attribute.getStringValue()); dataset.close(); }
From source file:org.geoserver.wcs2_0.WCSNetCDFMosaicTest.java
@Test public void testRequestNetCDFCompression() throws Exception { boolean isNC4Available = NetCDFUtilities.isNC4CAvailable(); if (!isNC4Available && LOGGER.isLoggable(Level.INFO)) { LOGGER.info("NetCDF C library not found. NetCDF4 output will not be created"); }/*w w w. j a v a2s . c o m*/ // http response from the request inside the string MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageId=wcs__visibilityCompressed&format=application/x-netcdf4"); assertNotNull(response); assertEquals((isNC4Available ? "application/x-netcdf4" : "application/xml"), response.getContentType()); if (isNC4Available) { byte[] netcdfOut = getBinary(response); File file = File.createTempFile("netcdf", "outCompressed.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, netcdfOut); NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath()); assertNotNull(dataset); Variable var = dataset.findVariable(STANDARD_NAME); assertNotNull(var); final long varByteSize = var.getSize() * var.getDataType().getSize(); // The output file is smaller than the size of the underlying variable. // Compression successfully occurred assertTrue(netcdfOut.length < varByteSize); dataset.close(); } }
From source file:org.geoserver.wcs2_0.WCSNetCDFMosaicTest.java
@Test public void testRequestNetCDFDataPacking() throws Exception { // http response from the request inside the string MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageId=wcs__visibilityPacked&format=application/x-netcdf"); assertNotNull(response);// www.j a v a2 s .c om byte[] netcdfOut = getBinary(response); File file = File.createTempFile("netcdf", "outPK.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, netcdfOut); NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath()); Variable var = dataset.findVariable(STANDARD_NAME); assertNotNull(var); // Check the unit hasn't been converted String unit = var.getUnitsString(); assertEquals(ORIGINAL_UNIT, unit); Attribute fillValue = var.findAttribute(NetCDFUtilities.FILL_VALUE); assertNotNull(fillValue); // There is dataPacking, therefore, fillValue should have been changed assertEquals(PACKED_FILL_VALUE, fillValue.getNumericValue().doubleValue(), 1E-6); Attribute addOffsetAttr = var.findAttribute(DataPacking.ADD_OFFSET); assertNotNull(addOffsetAttr); Attribute scaleFactorAttr = var.findAttribute(DataPacking.SCALE_FACTOR); assertNotNull(scaleFactorAttr); double scaleFactor = scaleFactorAttr.getNumericValue().doubleValue(); double addOffset = addOffsetAttr.getNumericValue().doubleValue(); Array readData = var.read(NETCDF_SECTION); assertEquals(DataType.SHORT, readData.getDataType()); short data = readData.getShort(0); // Data has been packed to short double packedData = (ORIGINAL_PIXEL_VALUE - addOffset) / scaleFactor; assertEquals((short) (packedData + 0.5), data, DELTA); dataset.close(); }
From source file:org.geoserver.wcs2_0.WCSNetCDFMosaicTest.java
@Test public void testRequestNetCDFCFDataPacking() throws Exception { // http response from the request inside the string MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageId=wcs__visibilityCFPacked&format=application/x-netcdf"); assertNotNull(response);//from w ww . ja v a 2s. co m byte[] netcdfOut = getBinary(response); File file = File.createTempFile("netcdf", "outCFPK.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, netcdfOut); NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath()); Variable var = dataset.findVariable(STANDARD_NAME); assertNotNull(var); // Check the unit has been converted to meter String unit = var.getUnitsString(); assertEquals(CANONICAL_UNIT, unit); Attribute addOffsetAttr = var.findAttribute(DataPacking.ADD_OFFSET); assertNotNull(addOffsetAttr); Attribute scaleFactorAttr = var.findAttribute(DataPacking.SCALE_FACTOR); assertNotNull(scaleFactorAttr); double scaleFactor = scaleFactorAttr.getNumericValue().doubleValue(); double addOffset = addOffsetAttr.getNumericValue().doubleValue(); Array readData = var.read(NETCDF_SECTION); assertEquals(DataType.SHORT, readData.getDataType()); short data = readData.getShort(0); // Data has been packed to short // Going from original unit to canonical, then packing double packedData = ((ORIGINAL_PIXEL_VALUE * 1000) - addOffset) / scaleFactor; assertEquals((short) (packedData + 0.5), data, DELTA); Attribute fillValue = var.findAttribute(NetCDFUtilities.FILL_VALUE); assertNotNull(fillValue); // There is dataPacking, therefore, fillValue should have been changed assertEquals(PACKED_FILL_VALUE, fillValue.getNumericValue().doubleValue(), DELTA); // Check global attributes have been added Attribute attribute = dataset.findGlobalAttribute("custom_attribute"); assertNotNull(attribute); assertEquals("testing WCS", attribute.getStringValue()); dataset.close(); }
From source file:org.geoserver.wcs2_0.WCSNetCDFTest.java
/** * Test NetCDF output from a NetCDF file with a rotated pole projection. *///from ww w. ja va 2 s . c o m @Test public void testNetcdfRotatedPole() throws Exception { MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageid=wcs__Temperature_surface_NetCDF&format=application/x-netcdf"); assertEquals(200, response.getStatus()); assertEquals("application/x-netcdf", response.getContentType()); byte[] responseBytes = getBinary(response); File file = File.createTempFile("netcdf-rotated-pole-", "-wcs__Temperature_surface_NetCDF.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, responseBytes); try (NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath())) { assertNotNull(dataset); // check dimensions Dimension rlonDim = dataset.findDimension("rlon"); assertNotNull(rlonDim); assertEquals(7, rlonDim.getLength()); Dimension rlatDim = dataset.findDimension("rlat"); assertNotNull(rlatDim); assertEquals(5, rlatDim.getLength()); // check coordinate variables Variable rlonVar = dataset.findVariable("rlon"); assertNotNull(rlonVar); assertEquals(1, rlonVar.getDimensions().size()); assertEquals(rlonDim, rlonVar.getDimensions().get(0)); assertEquals("grid_longitude", rlonVar.findAttribute("long_name").getStringValue()); assertEquals("grid_longitude", rlonVar.findAttribute("standard_name").getStringValue()); assertEquals("degrees", rlonVar.findAttribute("units").getStringValue()); assertArrayEquals(new float[] { -30, -20, -10, 0, 10, 20, 30 }, (float[]) rlonVar.read().copyTo1DJavaArray(), (float) DELTA); Variable rlatVar = dataset.findVariable("rlat"); assertNotNull(rlatVar); assertEquals(1, rlatVar.getDimensions().size()); assertEquals(rlatDim, rlatVar.getDimensions().get(0)); assertEquals("grid_latitude", rlatVar.findAttribute("long_name").getStringValue()); assertEquals("grid_latitude", rlatVar.findAttribute("standard_name").getStringValue()); assertEquals("degrees", rlatVar.findAttribute("units").getStringValue()); assertArrayEquals(new float[] { -20, -10, 0, 10, 20 }, (float[]) rlatVar.read().copyTo1DJavaArray(), (float) DELTA); // check projection variable Variable projVar = dataset.findVariable("rotated_latitude_longitude"); assertNotNull(projVar); assertEquals("rotated_latitude_longitude", projVar.findAttribute("grid_mapping_name").getStringValue()); assertEquals(74.0, projVar.findAttribute("grid_north_pole_longitude").getNumericValue().doubleValue(), DELTA); assertEquals(36.0, projVar.findAttribute("grid_north_pole_latitude").getNumericValue().doubleValue(), DELTA); // check data variable Variable tempVar = dataset.findVariable("Temperature_surface_NetCDF"); assertNotNull(tempVar); assertEquals("rotated_latitude_longitude", tempVar.findAttribute("grid_mapping").getStringValue()); assertEquals("K", tempVar.findAttribute("units").getStringValue()); assertEquals(2, tempVar.getDimensions().size()); assertEquals(rlatDim, tempVar.getDimensions().get(0)); assertEquals(rlonDim, tempVar.getDimensions().get(1)); assertArrayEquals(new float[] { 300, 299, 298, 297, 296, 295, 294, 299, 300, 299, 298, 297, 296, 295, 298, 299, 300, 299, 298, 297, 296, 297, 298, 299, 300, 299, 298, 297, 296, 297, 298, 299, 300, 299, 298 }, (float[]) tempVar.read().copyTo1DJavaArray(), (float) DELTA); } finally { FileUtils.deleteQuietly(file); } }
From source file:org.geoserver.wcs2_0.WCSNetCDFTest.java
/** * Test NetCDF output from an RAP native GRIB2 file with a GDS template 32769 rotated pole projection. *//*w w w. java 2 s . c o m*/ @Test public void testRapNativeGribRotatedPole() throws Exception { MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageid=wcs__Temperature_surface&format=application/x-netcdf"); assertEquals(200, response.getStatus()); assertEquals("application/x-netcdf", response.getContentType()); byte[] responseBytes = getBinary(response); File file = File.createTempFile("rap-native-grib-rotated-pole-", "-wcs__Temperature_surface.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, responseBytes); try (NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath())) { assertNotNull(dataset); // check dimensions Dimension rlonDim = dataset.findDimension("rlon"); assertNotNull(rlonDim); assertEquals(7, rlonDim.getLength()); Dimension rlatDim = dataset.findDimension("rlat"); assertNotNull(rlatDim); assertEquals(5, rlatDim.getLength()); // check coordinate variables Variable rlonVar = dataset.findVariable("rlon"); assertNotNull(rlonVar); assertEquals(1, rlonVar.getDimensions().size()); assertEquals(rlonDim, rlonVar.getDimensions().get(0)); assertEquals("grid_longitude", rlonVar.findAttribute("long_name").getStringValue()); assertEquals("grid_longitude", rlonVar.findAttribute("standard_name").getStringValue()); assertEquals("degrees", rlonVar.findAttribute("units").getStringValue()); assertArrayEquals(new float[] { -30, -20, -10, 0, 10, 20, 30 }, (float[]) rlonVar.read().copyTo1DJavaArray(), (float) DELTA); Variable rlatVar = dataset.findVariable("rlat"); assertNotNull(rlatVar); assertEquals(1, rlatVar.getDimensions().size()); assertEquals(rlatDim, rlatVar.getDimensions().get(0)); assertEquals("grid_latitude", rlatVar.findAttribute("long_name").getStringValue()); assertEquals("grid_latitude", rlatVar.findAttribute("standard_name").getStringValue()); assertEquals("degrees", rlatVar.findAttribute("units").getStringValue()); assertArrayEquals(new float[] { -20, -10, 0, 10, 20 }, (float[]) rlatVar.read().copyTo1DJavaArray(), (float) DELTA); // check projection variable Variable projVar = dataset.findVariable("rotated_latitude_longitude"); assertNotNull(projVar); assertEquals("rotated_latitude_longitude", projVar.findAttribute("grid_mapping_name").getStringValue()); assertEquals(74.0, projVar.findAttribute("grid_north_pole_longitude").getNumericValue().doubleValue(), DELTA); assertEquals(36.0, projVar.findAttribute("grid_north_pole_latitude").getNumericValue().doubleValue(), DELTA); // check data variable Variable dataVar = dataset.findVariable("Temperature_surface"); assertNotNull(dataVar); assertEquals("rotated_latitude_longitude", dataVar.findAttribute("grid_mapping").getStringValue()); assertEquals("K", dataVar.findAttribute("units").getStringValue()); assertEquals(2, dataVar.getDimensions().size()); assertEquals(rlatDim, dataVar.getDimensions().get(0)); assertEquals(rlonDim, dataVar.getDimensions().get(1)); assertArrayEquals(new float[] { 300, 299, 298, 297, 296, 295, 294, 299, 300, 299, 298, 297, 296, 295, 298, 299, 300, 299, 298, 297, 296, 297, 298, 299, 300, 299, 298, 297, 296, 297, 298, 299, 300, 299, 298 }, (float[]) dataVar.read().copyTo1DJavaArray(), (float) DELTA); } finally { FileUtils.deleteQuietly(file); } }
From source file:org.geoserver.wcs2_0.WCSNetCDFTest.java
/** * Test NetCDF output from a COSMO EU GRIB2 file with a GDS template 1 rotated pole projection. *//* www . ja v a 2 s .c o m*/ @Test public void testCosmoEuGribRotatedPole() throws Exception { MockHttpServletResponse response = getAsServletResponse("ows?request=GetCoverage&service=WCS&version=2.0.1" + "&coverageid=wcs__Snow_depth_water_equivalent_surface&format=application/x-netcdf"); assertEquals(200, response.getStatus()); assertEquals("application/x-netcdf", response.getContentType()); byte[] responseBytes = getBinary(response); File file = File.createTempFile("cosmo-eu-grib-rotated-pole-", "-wcs__Snow_depth_water_equivalent_surface.nc", new File("./target")); FileUtils.writeByteArrayToFile(file, responseBytes); try (NetcdfDataset dataset = NetcdfDataset.openDataset(file.getAbsolutePath())) { assertNotNull(dataset); // check dimensions Dimension rlonDim = dataset.findDimension("rlon"); assertNotNull(rlonDim); assertEquals(5, rlonDim.getLength()); Dimension rlatDim = dataset.findDimension("rlat"); assertNotNull(rlatDim); assertEquals(5, rlatDim.getLength()); // check coordinate variables Variable rlonVar = dataset.findVariable("rlon"); assertNotNull(rlonVar); assertEquals(1, rlonVar.getDimensions().size()); assertEquals(rlonDim, rlonVar.getDimensions().get(0)); assertEquals("grid_longitude", rlonVar.findAttribute("long_name").getStringValue()); assertEquals("grid_longitude", rlonVar.findAttribute("standard_name").getStringValue()); assertEquals("degrees", rlonVar.findAttribute("units").getStringValue()); assertArrayEquals(new float[] { -18, -8, 2, 12, 22 }, (float[]) rlonVar.read().copyTo1DJavaArray(), (float) DELTA); Variable rlatVar = dataset.findVariable("rlat"); assertNotNull(rlatVar); assertEquals(1, rlatVar.getDimensions().size()); assertEquals(rlatDim, rlatVar.getDimensions().get(0)); assertEquals("grid_latitude", rlatVar.findAttribute("long_name").getStringValue()); assertEquals("grid_latitude", rlatVar.findAttribute("standard_name").getStringValue()); assertEquals("degrees", rlatVar.findAttribute("units").getStringValue()); assertArrayEquals(new float[] { -20, -10, 0, 10, 20 }, (float[]) rlatVar.read().copyTo1DJavaArray(), (float) DELTA); // check projection variable Variable projVar = dataset.findVariable("rotated_latitude_longitude"); assertNotNull(projVar); assertEquals("rotated_latitude_longitude", projVar.findAttribute("grid_mapping_name").getStringValue()); assertEquals(-170.0, projVar.findAttribute("grid_north_pole_longitude").getNumericValue().doubleValue(), DELTA); assertEquals(40.0, projVar.findAttribute("grid_north_pole_latitude").getNumericValue().doubleValue(), DELTA); // check data variable Variable dataVar = dataset.findVariable("Snow_depth_water_equivalent_surface"); assertNotNull(dataVar); assertEquals("rotated_latitude_longitude", dataVar.findAttribute("grid_mapping").getStringValue()); assertEquals(2, dataVar.getDimensions().size()); assertEquals(rlatDim, dataVar.getDimensions().get(0)); assertEquals(rlonDim, dataVar.getDimensions().get(1)); assertArrayEquals( new float[] { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124 }, (float[]) dataVar.read().copyTo1DJavaArray(), (float) DELTA); } finally { FileUtils.deleteQuietly(file); } }
From source file:org.geoserver.wps.gs.download.DownloadAnimationProcessTest.java
@Test public void testAnimateBmTime() throws Exception { String xml = IOUtils.toString(getClass().getResourceAsStream("animateBlueMarble.xml")); MockHttpServletResponse response = postAsServletResponse("wps", xml); assertEquals("video/mp4", response.getContentType()); // JCodec API works off files only... File testFile = new File("target/animateBmTime.mp4"); FileUtils.writeByteArrayToFile(testFile, response.getContentAsByteArray()); // check frames and duration Format f = JCodecUtil.detectFormat(testFile); Demuxer d = JCodecUtil.createDemuxer(f, testFile); DemuxerTrack vt = d.getVideoTracks().get(0); DemuxerTrackMeta dtm = vt.getMeta(); assertEquals(4, dtm.getTotalFrames()); assertEquals(8, dtm.getTotalDuration(), 0d); // grab frames for checking File source = new File("src/test/resources/org/geoserver/wps/gs/download/bm_time.zip"); FrameGrab grabber = FrameGrab.createFrameGrab(NIOUtils.readableChannel(testFile)); // first//from w w w .j a v a 2 s . com BufferedImage frame1 = AWTUtil.toBufferedImage(grabber.getNativeFrame()); BufferedImage expected1 = grabImageFromZip(source, "world.200402.3x5400x2700.tiff"); ImageAssert.assertEquals(expected1, frame1, 100); // second BufferedImage frame2 = AWTUtil.toBufferedImage(grabber.getNativeFrame()); BufferedImage expected2 = grabImageFromZip(source, "world.200403.3x5400x2700.tiff"); ImageAssert.assertEquals(expected2, frame2, 100); // third BufferedImage frame3 = AWTUtil.toBufferedImage(grabber.getNativeFrame()); BufferedImage expected3 = grabImageFromZip(source, "world.200404.3x5400x2700.tiff"); ImageAssert.assertEquals(expected3, frame3, 100); // fourth BufferedImage frame4 = AWTUtil.toBufferedImage(grabber.getNativeFrame()); BufferedImage expected4 = grabImageFromZip(source, "world.200405.3x5400x2700.tiff"); ImageAssert.assertEquals(expected4, frame4, 100); }
From source file:org.geoserver.wps.gs.download.DownloadAnimationProcessTest.java
@Test public void testAnimateDecoration() throws Exception { String xml = IOUtils.toString(getClass().getResourceAsStream("animateDecoration.xml")); MockHttpServletResponse response = postAsServletResponse("wps", xml); assertEquals("video/mp4", response.getContentType()); // JCodec API works off files only... File testFile = new File("target/animateWaterDecoration.mp4"); FileUtils.writeByteArrayToFile(testFile, response.getContentAsByteArray()); // check frames and duration Format f = JCodecUtil.detectFormat(testFile); Demuxer d = JCodecUtil.createDemuxer(f, testFile); DemuxerTrack vt = d.getVideoTracks().get(0); DemuxerTrackMeta dtm = vt.getMeta(); assertEquals(2, dtm.getTotalFrames()); assertEquals(2, dtm.getTotalDuration(), 0d); // grab first frame for test FrameGrab grabber = FrameGrab.createFrameGrab(NIOUtils.readableChannel(testFile)); BufferedImage frame1 = AWTUtil.toBufferedImage(grabber.getNativeFrame()); ImageAssert.assertEquals(new File(SAMPLES + "animateDecorateFirstFrame.png"), frame1, 100); }