NcFileStitcher { static void setDimensions(SlstrNFileWritable nFileWriteable, List<Dimension>[] dimensionLists, ImageSize targetImageSize, List<Variable>[] variableLists) throws PDUStitchingException, IOException { List<String> namesOfAddedDimensions = new ArrayList<>(); for (int i = 0; i < dimensionLists.length; i++) { List<Dimension> dimensions = dimensionLists[i]; for (Dimension dimension : dimensions) { final String dimensionName = dimension.getFullName(); if (!namesOfAddedDimensions.contains(dimensionName)) { switch (dimensionName) { case "rows": nFileWriteable.addDimension("rows", targetImageSize.getRows()); break; case "columns": nFileWriteable.addDimension("columns", targetImageSize.getColumns()); break; default: checkWhetherEquallyNamedVariablesContainEqualValues(dimensionName, i, variableLists); checkWhetherDimensionLengthIsEqualAcrossAllEquallyNamedDimensions(dimension, i, dimensionLists); nFileWriteable.addDimension(dimensionName, dimension.getLength()); break; } namesOfAddedDimensions.add(dimensionName); } } } }    }
@Test public void testSetDimensions() throws IOException, PDUStitchingException { List<Dimension>[] dimensionLists = new ArrayList[2]; List<Variable>[] variableLists = new ArrayList[2]; final File inputFile1 = new File(targetDirectory, "input_1.nc"); SlstrNFileWritable inputWriteable1 = new SlstrNFileWritable(inputFile1.getAbsolutePath()); inputWriteable1.addDimension("rows", 5); inputWriteable1.addDimension("columns", 7); inputWriteable1.addDimension("the_twilight_zone", 12); final SlstrN4Variable abVariable1 = inputWriteable1.addVariable("ab", DataType.BYTE, true, "rows columns"); final SlstrN4Variable cdVariable1 = inputWriteable1.addVariable("cd", DataType.LONG, false, "columns the_twilight_zone"); inputWriteable1.create(); abVariable1.writeFullyInSections(new ArrayByte(new int[]{5, 7}, false)); cdVariable1.writeFullyInSections(new ArrayLong(new int[]{7, 12}, false)); inputWriteable1.close(); final NetcdfFile inputnc1 = NetcdfFileOpener.open(inputFile1); assertNotNull(inputnc1); dimensionLists[0] = inputnc1.getDimensions(); variableLists[0] = inputnc1.getVariables(); inputnc1.close(); final File inputFile2 = new File(targetDirectory, "input_2.nc"); final NFileWriteable inputWriteable2 = NWritableFactory.create(inputFile2.getAbsolutePath(), "netcdf4"); inputWriteable2.addDimension("rows", 5); inputWriteable2.addDimension("columns", 7); inputWriteable2.addDimension("outer_limits", 25); final NVariable abVariable2 = inputWriteable2.addVariable("ab", DataType.BYTE, true, null, "rows columns"); final NVariable cdVariable2 = inputWriteable2.addVariable("ef", DataType.LONG, false, null, "columns outer_limits"); inputWriteable2.create(); abVariable2.writeFully(new ArrayByte(new int[]{5, 7}, false)); cdVariable2.writeFully(new ArrayLong(new int[]{7, 25}, false)); inputWriteable2.close(); final NetcdfFile inputnc2 = NetcdfFileOpener.open(inputFile2); assertNotNull(inputnc2); dimensionLists[1] = inputnc2.getDimensions(); variableLists[1] = inputnc2.getVariables(); inputnc2.close(); final File file = new File(targetDirectory, "something.nc"); SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); ImageSize targetImageSize = new ImageSize("id", 10, 20, 10, 20); NcFileStitcher.setDimensions(netcdfWriteable, dimensionLists, targetImageSize, variableLists); netcdfWriteable.create(); netcdfWriteable.close(); netcdfFile = NetcdfFileOpener.open(file); assertNotNull(netcdfFile); final List<Dimension> dimensions = netcdfFile.getDimensions(); assertEquals(4, dimensions.size()); Map<String, Integer> expectedDimensions = new HashMap<>(); expectedDimensions.put("rows", 10); expectedDimensions.put("columns", 20); expectedDimensions.put("the_twilight_zone", 12); expectedDimensions.put("outer_limits", 25); for (Dimension dimension : dimensions) { assertTrue(expectedDimensions.containsKey(dimension.getFullName())); assertEquals(expectedDimensions.get(dimension.getFullName()).intValue(), dimension.getLength()); } }
@Test public void testSetDimensions_VaryingDimensionLengths() throws IOException { List<Dimension>[] dimensionLists = new ArrayList[2]; List<Variable>[] variableLists = new ArrayList[2]; final File inputFile1 = new File(targetDirectory, "input_1.nc"); final NFileWriteable inputWriteable1 = NWritableFactory.create(inputFile1.getAbsolutePath(), "netcdf4"); inputWriteable1.addDimension("rows", 5); inputWriteable1.addDimension("columns", 7); inputWriteable1.addDimension("the_twilight_zone", 12); final NVariable abVariable1 = inputWriteable1.addVariable("ab", DataType.BYTE, true, null, "rows columns"); final NVariable cdVariable1 = inputWriteable1.addVariable("cd", DataType.LONG, false, null, "columns the_twilight_zone"); inputWriteable1.create(); abVariable1.writeFully(new ArrayByte(new int[]{5, 7}, false)); cdVariable1.writeFully(new ArrayLong(new int[]{7, 12}, false)); inputWriteable1.close(); final NetcdfFile inputnc1 = NetcdfFileOpener.open(inputFile1); assertNotNull(inputnc1); dimensionLists[0] = inputnc1.getDimensions(); variableLists[0] = inputnc1.getVariables(); inputnc1.close(); final File inputFile2 = new File(targetDirectory, "input_2.nc"); final NFileWriteable inputWriteable2 = NWritableFactory.create(inputFile2.getAbsolutePath(), "netcdf4"); inputWriteable2.addDimension("rows", 5); inputWriteable2.addDimension("columns", 7); inputWriteable2.addDimension("the_twilight_zone", 25); final NVariable abVariable2 = inputWriteable2.addVariable("ab", DataType.BYTE, true, null, "rows columns"); final NVariable cdVariable2 = inputWriteable2.addVariable("ef", DataType.LONG, false, null, "columns the_twilight_zone"); inputWriteable2.create(); abVariable2.writeFully(new ArrayByte(new int[]{5, 7}, false)); cdVariable2.writeFully(new ArrayLong(new int[]{7, 25}, false)); inputWriteable2.close(); final NetcdfFile inputnc2 = NetcdfFileOpener.open(inputFile2); assertNotNull(inputnc2); dimensionLists[1] = inputnc2.getDimensions(); variableLists[1] = inputnc2.getVariables(); inputnc2.close(); final File file = new File(targetDirectory, "something.nc"); SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); ImageSize targetImageSize = new ImageSize("id", 10, 20, 10, 20); try { NcFileStitcher.setDimensions(netcdfWriteable, dimensionLists, targetImageSize, variableLists); fail("Exception expected"); } catch (Exception e) { assertEquals("Dimension the_twilight_zone has different lengths across input files", e.getMessage()); } finally { netcdfWriteable.create(); netcdfWriteable.close(); } }
@Test public void testSetDimensions_VaryingValues() throws IOException { List<Dimension>[] dimensionLists = new ArrayList[2]; List<Variable>[] variableLists = new ArrayList[2]; final File inputFile1 = new File(targetDirectory, "input_1.nc"); final NFileWriteable inputWriteable1 = NWritableFactory.create(inputFile1.getAbsolutePath(), "netcdf4"); inputWriteable1.addDimension("rows", 5); inputWriteable1.addDimension("columns", 7); inputWriteable1.addDimension("cd", 12); final NVariable abVariable1 = inputWriteable1.addVariable("ab", DataType.BYTE, true, null, "rows columns"); final NVariable cdVariable1 = inputWriteable1.addVariable("cd", DataType.LONG, false, null, "cd"); inputWriteable1.create(); abVariable1.writeFully(new ArrayByte(new int[]{5, 7}, false)); cdVariable1.writeFully(new ArrayLong(new int[]{12}, false)); inputWriteable1.close(); final NetcdfFile inputnc1 = NetcdfFileOpener.open(inputFile1); assertNotNull(inputnc1); dimensionLists[0] = inputnc1.getDimensions(); variableLists[0] = inputnc1.getVariables(); final File inputFile2 = new File(targetDirectory, "input_2.nc"); final NFileWriteable inputWriteable2 = NWritableFactory.create(inputFile2.getAbsolutePath(), "netcdf4"); inputWriteable2.addDimension("rows", 5); inputWriteable2.addDimension("columns", 7); inputWriteable2.addDimension("cd", 12); final NVariable abVariable2 = inputWriteable2.addVariable("ab", DataType.BYTE, true, null, "rows columns"); final NVariable cdVariable2 = inputWriteable2.addVariable("cd", DataType.LONG, false, null, "cd"); inputWriteable2.create(); abVariable2.writeFully(new ArrayByte(new int[]{5, 7}, false)); final ArrayLong values = new ArrayLong(new int[]{12}, false); values.setLong(10, 10); cdVariable2.writeFully(values); inputWriteable2.close(); final NetcdfFile inputnc2 = NetcdfFileOpener.open(inputFile2); assertNotNull(inputnc2); dimensionLists[1] = inputnc2.getDimensions(); variableLists[1] = inputnc2.getVariables(); final File file = new File(targetDirectory, "something.nc"); SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); ImageSize targetImageSize = new ImageSize("id", 10, 20, 10, 20); try { NcFileStitcher.setDimensions(netcdfWriteable, dimensionLists, targetImageSize, variableLists); fail("Exception expected"); } catch (Exception e) { assertEquals("Values for cd are different across input files", e.getMessage()); } finally { netcdfWriteable.create(); inputnc1.close(); inputnc2.close(); netcdfWriteable.close(); } }