@Test @Ignore("takes a few seconds") public void testStitchMet_tx() throws IOException, PDUStitchingException, InvalidRangeException, URISyntaxException { final String ncFileName = "met_tx.nc"; final ImageSize targetImageSize = new ImageSize("in", 21687, 64, 6000, 130); final ImageSize[] imageSizes = new ImageSize[3]; imageSizes[0] = new ImageSize("in", 21687, 64, 2000, 130); imageSizes[1] = new ImageSize("in", 23687, 64, 2000, 130); imageSizes[2] = new ImageSize("in", 25687, 64, 2000, 130); final Date now = Calendar.getInstance().getTime(); final File[] ncFiles = getNcFiles(ncFileName); final File stitchedFile = NcFileStitcher.stitchNcFiles(ncFileName, targetDirectory, now, ncFiles, targetImageSize, imageSizes); assert (stitchedFile != null); assert (stitchedFile.exists()); assertEquals(ncFileName, stitchedFile.getName()); netcdfFile = NetcdfFileOpener.open(stitchedFile); assertNotNull(netcdfFile); final List<Variable> variables = netcdfFile.getVariables(); assertEquals(28, variables.size()); assertEquals("t_single", variables.get(0).getFullName()); assertEquals(DataType.SHORT, variables.get(0).getDataType()); assertEquals("sea_ice_fraction_tx", variables.get(9).getFullName()); assertEquals(DataType.FLOAT, variables.get(9).getDataType()); assertEquals("u_wind_tx", variables.get(10).getFullName()); assertEquals(DataType.FLOAT, variables.get(10).getDataType()); assertEquals("snow_depth_tx", variables.get(27).getFullName()); assertEquals(DataType.FLOAT, variables.get(27).getDataType()); List<Attribute> snowLiquidAttributes = variables.get(27).getAttributes(); assertEquals("_ChunkSize", snowLiquidAttributes.get(0).getFullName()); assertEquals("1 600 130 ", snowLiquidAttributes.get(0).getValues().toString()); assertEquals("long_name", snowLiquidAttributes.get(1).getFullName()); assertEquals("Snow liquid water equivalent depth", snowLiquidAttributes.get(1).getStringValue()); assertEquals("standard_name", snowLiquidAttributes.get(2).getFullName()); assertEquals("lwe_thickness_of_surface_snow_amount", snowLiquidAttributes.get(2).getStringValue()); assertEquals("units", snowLiquidAttributes.get(3).getFullName()); assertEquals("metre", snowLiquidAttributes.get(3).getStringValue()); assertEquals("model", snowLiquidAttributes.get(4).getFullName()); assertEquals("ECMWF_F", snowLiquidAttributes.get(4).getStringValue()); assertEquals("parameter", snowLiquidAttributes.get(5).getFullName()); assertEquals("141", snowLiquidAttributes.get(5).getStringValue()); final List<Variable>[] inputFileVariables = new ArrayList[3]; for (int i = 0; i < 3; i++) { final NetcdfFile ncFile = NetcdfFileOpener.open(ncFiles[i]); assertNotNull(ncFile); inputFileVariables[i] = ncFile.getVariables(); } for (int i = 0; i < variables.size(); i++) { Variable variable = variables.get(i); for (int j = 0; j < inputFileVariables.length; j++) { int rowOffset = j * 2000; final Variable inputFileVariable = inputFileVariables[j].get(i + 1); final List<Dimension> inputFileVariableDimensions = inputFileVariable.getDimensions(); int[] origin = new int[inputFileVariableDimensions.size()]; int[] shape = new int[inputFileVariableDimensions.size()]; for (int k = 0; k < inputFileVariableDimensions.size(); k++) { if (inputFileVariableDimensions.get(k).getFullName().equals("rows")) { origin[k] = rowOffset; shape[k] = 2000; } else { origin[k] = 0; shape[k] = inputFileVariableDimensions.get(k).getLength(); } } final Section section = new Section(origin, shape); final Array stitchedArrayPart = variable.read(section); final Array fileArray = inputFileVariable.read(); if (variable.getDataType() == DataType.SHORT) { final short[] expectedArray = (short[]) fileArray.copyTo1DJavaArray(); final short[] actualArray = (short[]) stitchedArrayPart.copyTo1DJavaArray(); assertArrayEquals(expectedArray, actualArray); } else { final float[] expectedArray = (float[]) fileArray.copyTo1DJavaArray(); final float[] actualArray = (float[]) stitchedArrayPart.copyTo1DJavaArray(); assertArrayEquals(expectedArray, actualArray, 1e-8f); } } } }
static File stitchNcFiles(String fileName, File targetDirectory, Date creationDate, File[] ncFiles, ImageSize targetImageSize, ImageSize[] imageSizes) throws IOException, PDUStitchingException { NetcdfFile[] inputFiles = new NetcdfFile[ncFiles.length]; List<Attribute>[] globalAttributes = new ArrayList[ncFiles.length]; List<Dimension>[] dimensions = new List[ncFiles.length]; List<Variable>[] variables = new List[ncFiles.length]; for (int i = 0; i < ncFiles.length; i++) { inputFiles[i] = NetcdfFileOpener.open(ncFiles[i]); globalAttributes[i] = inputFiles[i].getGlobalAttributes(); dimensions[i] = inputFiles[i].getDimensions(); variables[i] = inputFiles[i].getVariables(); } final File file = new File(targetDirectory, fileName); final SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); setGlobalAttributes(netcdfWriteable, globalAttributes, targetDirectory.getName(), creationDate); setDimensions(netcdfWriteable, dimensions, targetImageSize, variables); final Map<String, Array> variableToArrayMap = defineVariables(netcdfWriteable, variables, targetImageSize, imageSizes); netcdfWriteable.create(); for (String variableName : variableToArrayMap.keySet()) { netcdfWriteable.findVariable(variableName).writeFullyInSections(variableToArrayMap.get(variableName)); } netcdfWriteable.close(); for (NetcdfFile inputFile : inputFiles) { inputFile.close(); } return file; }
NcFileStitcher { static File stitchNcFiles(String fileName, File targetDirectory, Date creationDate, File[] ncFiles, ImageSize targetImageSize, ImageSize[] imageSizes) throws IOException, PDUStitchingException { NetcdfFile[] inputFiles = new NetcdfFile[ncFiles.length]; List<Attribute>[] globalAttributes = new ArrayList[ncFiles.length]; List<Dimension>[] dimensions = new List[ncFiles.length]; List<Variable>[] variables = new List[ncFiles.length]; for (int i = 0; i < ncFiles.length; i++) { inputFiles[i] = NetcdfFileOpener.open(ncFiles[i]); globalAttributes[i] = inputFiles[i].getGlobalAttributes(); dimensions[i] = inputFiles[i].getDimensions(); variables[i] = inputFiles[i].getVariables(); } final File file = new File(targetDirectory, fileName); final SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); setGlobalAttributes(netcdfWriteable, globalAttributes, targetDirectory.getName(), creationDate); setDimensions(netcdfWriteable, dimensions, targetImageSize, variables); final Map<String, Array> variableToArrayMap = defineVariables(netcdfWriteable, variables, targetImageSize, imageSizes); netcdfWriteable.create(); for (String variableName : variableToArrayMap.keySet()) { netcdfWriteable.findVariable(variableName).writeFullyInSections(variableToArrayMap.get(variableName)); } netcdfWriteable.close(); for (NetcdfFile inputFile : inputFiles) { inputFile.close(); } return file; } }
NcFileStitcher { static File stitchNcFiles(String fileName, File targetDirectory, Date creationDate, File[] ncFiles, ImageSize targetImageSize, ImageSize[] imageSizes) throws IOException, PDUStitchingException { NetcdfFile[] inputFiles = new NetcdfFile[ncFiles.length]; List<Attribute>[] globalAttributes = new ArrayList[ncFiles.length]; List<Dimension>[] dimensions = new List[ncFiles.length]; List<Variable>[] variables = new List[ncFiles.length]; for (int i = 0; i < ncFiles.length; i++) { inputFiles[i] = NetcdfFileOpener.open(ncFiles[i]); globalAttributes[i] = inputFiles[i].getGlobalAttributes(); dimensions[i] = inputFiles[i].getDimensions(); variables[i] = inputFiles[i].getVariables(); } final File file = new File(targetDirectory, fileName); final SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); setGlobalAttributes(netcdfWriteable, globalAttributes, targetDirectory.getName(), creationDate); setDimensions(netcdfWriteable, dimensions, targetImageSize, variables); final Map<String, Array> variableToArrayMap = defineVariables(netcdfWriteable, variables, targetImageSize, imageSizes); netcdfWriteable.create(); for (String variableName : variableToArrayMap.keySet()) { netcdfWriteable.findVariable(variableName).writeFullyInSections(variableToArrayMap.get(variableName)); } netcdfWriteable.close(); for (NetcdfFile inputFile : inputFiles) { inputFile.close(); } return file; }  }
NcFileStitcher { static File stitchNcFiles(String fileName, File targetDirectory, Date creationDate, File[] ncFiles, ImageSize targetImageSize, ImageSize[] imageSizes) throws IOException, PDUStitchingException { NetcdfFile[] inputFiles = new NetcdfFile[ncFiles.length]; List<Attribute>[] globalAttributes = new ArrayList[ncFiles.length]; List<Dimension>[] dimensions = new List[ncFiles.length]; List<Variable>[] variables = new List[ncFiles.length]; for (int i = 0; i < ncFiles.length; i++) { inputFiles[i] = NetcdfFileOpener.open(ncFiles[i]); globalAttributes[i] = inputFiles[i].getGlobalAttributes(); dimensions[i] = inputFiles[i].getDimensions(); variables[i] = inputFiles[i].getVariables(); } final File file = new File(targetDirectory, fileName); final SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); setGlobalAttributes(netcdfWriteable, globalAttributes, targetDirectory.getName(), creationDate); setDimensions(netcdfWriteable, dimensions, targetImageSize, variables); final Map<String, Array> variableToArrayMap = defineVariables(netcdfWriteable, variables, targetImageSize, imageSizes); netcdfWriteable.create(); for (String variableName : variableToArrayMap.keySet()) { netcdfWriteable.findVariable(variableName).writeFullyInSections(variableToArrayMap.get(variableName)); } netcdfWriteable.close(); for (NetcdfFile inputFile : inputFiles) { inputFile.close(); } return file; }   }
NcFileStitcher { static File stitchNcFiles(String fileName, File targetDirectory, Date creationDate, File[] ncFiles, ImageSize targetImageSize, ImageSize[] imageSizes) throws IOException, PDUStitchingException { NetcdfFile[] inputFiles = new NetcdfFile[ncFiles.length]; List<Attribute>[] globalAttributes = new ArrayList[ncFiles.length]; List<Dimension>[] dimensions = new List[ncFiles.length]; List<Variable>[] variables = new List[ncFiles.length]; for (int i = 0; i < ncFiles.length; i++) { inputFiles[i] = NetcdfFileOpener.open(ncFiles[i]); globalAttributes[i] = inputFiles[i].getGlobalAttributes(); dimensions[i] = inputFiles[i].getDimensions(); variables[i] = inputFiles[i].getVariables(); } final File file = new File(targetDirectory, fileName); final SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); setGlobalAttributes(netcdfWriteable, globalAttributes, targetDirectory.getName(), creationDate); setDimensions(netcdfWriteable, dimensions, targetImageSize, variables); final Map<String, Array> variableToArrayMap = defineVariables(netcdfWriteable, variables, targetImageSize, imageSizes); netcdfWriteable.create(); for (String variableName : variableToArrayMap.keySet()) { netcdfWriteable.findVariable(variableName).writeFullyInSections(variableToArrayMap.get(variableName)); } netcdfWriteable.close(); for (NetcdfFile inputFile : inputFiles) { inputFile.close(); } return file; }    }