SeaDASLevel3BinWriter extends AbstractBinWriter { @Override public void write(Map<String, String> metadataProperties, List<TemporalBin> temporalBins) throws IOException { final NetcdfFileWriteable netcdfFile = NetcdfFileWriteable.createNew(getTargetFilePath()); netcdfFile.setLargeFile(true); netcdfFile.addGlobalAttribute("title", "Level-3 Binned Data"); netcdfFile.addGlobalAttribute("super_sampling", binningContext.getSuperSampling()); if (getRegion() != null) { netcdfFile.addGlobalAttribute("region", getRegion().toText()); } writeGlobalTimeCoverageMetadata(netcdfFile); writeGlobalCommonMetadata(metadataProperties, netcdfFile); writeGlobalSEAGridMetadata(netcdfFile, planetaryGrid.getNumRows()); final Dimension binIndexDim = netcdfFile.addDimension("bin_index", planetaryGrid.getNumRows()); final Dimension binListDim = netcdfFile.addDimension("bin_list", temporalBins.size()); final Variable rowNumVar = netcdfFile.addVariable("bi_row_num", DataType.INT, new Dimension[]{binIndexDim}); rowNumVar.addAttribute(new Attribute("comment", "zero-based index of row corresponding to each 'bin_index' record.")); final Variable vsizeVar = netcdfFile.addVariable("bi_vsize", DataType.DOUBLE, new Dimension[]{binIndexDim}); vsizeVar.addAttribute(new Attribute("comment", "north-south extent (degrees latitude) of bins for each row.")); final Variable hsizeVar = netcdfFile.addVariable("bi_hsize", DataType.DOUBLE, new Dimension[]{binIndexDim}); hsizeVar.addAttribute(new Attribute("comment", "east-west extent (degrees longitude) of bins for each row;\n" + "ranges from 360/SEAGrid_bins for the two equatorial rows to 120 for the two polar rows.")); final Variable startNumVar = netcdfFile.addVariable("bi_start_num", DataType.INT, new Dimension[]{binIndexDim}); startNumVar.addAttribute(new Attribute("comment", "1-based bin number of first bin in the grid for each row (see bi_begin);\n" + "always the same set of values for the set of rows.")); startNumVar.addAttribute(new Attribute("missing_value", 0)); startNumVar.addAttribute(new Attribute("_FillValue", 0)); final Variable beginOffsetVar = netcdfFile.addVariable("bi_begin_offset", DataType.INT, new Dimension[]{binIndexDim}); beginOffsetVar.addAttribute(new Attribute("comment", "0-based offset of the first data-containing bin in for each row.")); beginOffsetVar.addAttribute(new Attribute("missing_value", -1)); beginOffsetVar.addAttribute(new Attribute("_FillValue", -1)); final Variable beginVar = netcdfFile.addVariable("bi_begin", DataType.INT, new Dimension[]{binIndexDim}); beginVar.addAttribute(new Attribute("comment", "1-based bin number of first data-containing bin for each row (see bi_start_num).")); beginVar.addAttribute(new Attribute("missing_value", 0)); beginVar.addAttribute(new Attribute("_FillValue", 0)); final Variable extendVar = netcdfFile.addVariable("bi_extent", DataType.INT, new Dimension[]{binIndexDim}); extendVar.addAttribute(new Attribute("comment", "number of bins actually stored (i.e. containing data for each row).")); final Variable maxVar = netcdfFile.addVariable("bi_max", DataType.INT, new Dimension[]{binIndexDim}); maxVar.addAttribute(new Attribute("comment", "the maximum number of bin for each row; " + "ranges from 3 for the two polar rows to SEAGrid_bins for the two equatorial bins.")); final Variable binNumVar = netcdfFile.addVariable("bl_bin_num", DataType.INT, new Dimension[]{binListDim}); final Variable numObsVar = netcdfFile.addVariable("bl_nobs", DataType.INT, new Dimension[]{binListDim}); final Variable numScenesVar = netcdfFile.addVariable("bl_nscenes", DataType.INT, new Dimension[]{binListDim}); final BinManager binManager = binningContext.getBinManager(); final String[] resultFeatureNames = binManager.getResultFeatureNames(); final ArrayList<Variable> featureVars = new ArrayList<Variable>(resultFeatureNames.length); for (String featureName : resultFeatureNames) { final Variable featureVar = netcdfFile.addVariable("bl_" + featureName, DataType.FLOAT, new Dimension[]{binListDim}); featureVar.addAttribute(new Attribute("_FillValue", Float.NaN)); featureVars.add(featureVar); } netcdfFile.create(); try { writeBinIndexVariables(netcdfFile, rowNumVar, vsizeVar, hsizeVar, startNumVar, maxVar); writeBinListVariables(netcdfFile, binNumVar, numObsVar, numScenesVar, featureVars, beginOffsetVar, beginVar, extendVar, temporalBins); } catch (InvalidRangeException e) { throw new IOException(e); } finally { netcdfFile.close(); } } SeaDASLevel3BinWriter(Geometry region, ProductData.UTC startTime, ProductData.UTC stopTime); @Override void setBinningContext(BinningContext binningContext); @Override void write(Map<String, String> metadataProperties, List<TemporalBin> temporalBins);  }
@Test public void testWriting() throws Exception { final HashMap<String, String> metadataProperties = createMetadataProperties(); final ArrayList<TemporalBin> temporalBins = createTemporalBins(); binWriter.write(metadataProperties, temporalBins); final NetcdfFile netcdfFile = NetcdfFileOpener.open(binWriter.getTargetFilePath()); assertNotNull(netcdfFile.findGlobalAttribute("title")); assertNotNull(netcdfFile.findGlobalAttribute("super_sampling")); assertEquals(numRows * 2, netcdfFile.findGlobalAttribute("SEAGrid_bins").getNumericValue()); assertEquals(numRows, netcdfFile.findDimension("bin_index").getLength()); assertEquals(temporalBins.size(), netcdfFile.findDimension("bin_list").getLength()); assertEquals(numRows, netcdfFile.findVariable("bi_row_num").getDimension(0).getLength()); Array bi_row_num = netcdfFile.findVariable("bi_row_num").read(); assertEquals(0, bi_row_num.getInt(0)); assertEquals(200, bi_row_num.getInt(200)); assertEquals(numRows, netcdfFile.findVariable("bi_vsize").getDimension(0).getLength()); Array bi_vsize = netcdfFile.findVariable("bi_vsize").read(); assertEquals(180.0 / numRows, bi_vsize.getDouble(numRows / 2), 1.0e-6); assertEquals(180.0 / numRows, bi_vsize.getDouble(0), 1.0e-6); assertEquals(180.0 / numRows, bi_vsize.getDouble(215), 1.0e-6); assertEquals(numRows, netcdfFile.findVariable("bi_hsize").getDimension(0).getLength()); Array bi_hsize = netcdfFile.findVariable("bi_hsize").read(); assertEquals(360.0 / (numRows * 2.0), bi_hsize.getDouble(numRows / 2), 1.0e-6); assertEquals(120.0, bi_hsize.getDouble(215), 1.0e-6); assertEquals(numRows, netcdfFile.findVariable("bi_start_num").getDimension(0).getLength()); Array bi_start_num = netcdfFile.findVariable("bi_start_num").read(); assertEquals(1, bi_start_num.getInt(215)); assertEquals(4, bi_start_num.getInt(214)); assertEquals(12315, bi_start_num.getInt(150)); assertEquals(59406, bi_start_num.getInt(0)); assertNotNull(netcdfFile.findVariable("bi_begin_offset")); assertEquals(numRows, netcdfFile.findVariable("bi_begin_offset").getDimension(0).getLength()); Array bi_begin_offset = netcdfFile.findVariable("bi_begin_offset").read(); assertEquals(0, bi_begin_offset.getInt(150)); assertEquals(-1, bi_begin_offset.getInt(214)); assertEquals(-1, bi_begin_offset.getInt(0)); assertEquals(numRows, netcdfFile.findVariable("bi_begin").getDimension(0).getLength()); Array bi_begin = netcdfFile.findVariable("bi_begin").read(); assertEquals(0, bi_begin.getInt(10)); assertEquals(46774, bi_begin.getInt(150)); assertEquals(0, bi_begin.getInt(215)); assertEquals(numRows, netcdfFile.findVariable("bi_extent").getDimension(0).getLength()); Array bi_extent = netcdfFile.findVariable("bi_extent").read(); assertEquals(0, bi_extent.getInt(10)); assertEquals(2, bi_extent.getInt(150)); assertEquals(0, bi_extent.getInt(215)); assertEquals(numRows, netcdfFile.findVariable("bi_max").getDimension(0).getLength()); Array bi_max = netcdfFile.findVariable("bi_max").read(); assertEquals(3, bi_max.getInt(0)); assertEquals(numRows * 2, bi_max.getInt(numRows / 2)); assertEquals(3, bi_max.getInt(215)); assertEquals(temporalBins.size(), netcdfFile.findVariable("bl_bin_num").getDimension(0).getLength()); Array bl_bin_num = netcdfFile.findVariable("bl_bin_num").read(); assertEquals(46774, bl_bin_num.getInt(0)); assertEquals(46775, bl_bin_num.getInt(1)); assertEquals(temporalBins.size(), netcdfFile.findVariable("bl_nobs").getDimension(0).getLength()); Array bl_nobs = netcdfFile.findVariable("bl_nobs").read(); assertEquals(1, bl_nobs.getInt(0)); assertEquals(2, bl_nobs.getInt(1)); assertEquals(temporalBins.size(), netcdfFile.findVariable("bl_nscenes").getDimension(0).getLength()); Array bl_nscenes = netcdfFile.findVariable("bl_nscenes").read(); assertEquals(1, bl_nscenes.getInt(0)); assertEquals(2, bl_nscenes.getInt(1)); assertEquals(temporalBins.size(), netcdfFile.findVariable("bl_test_min").getDimension(0).getLength()); Array bl_test_min = netcdfFile.findVariable("bl_test_min").read(); assertEquals(0.004f, bl_test_min.getFloat(0), 1.0e-6); assertEquals(0.398f, bl_test_min.getFloat(1), 1.0e-6); assertEquals(temporalBins.size(), netcdfFile.findVariable("bl_test_max").getDimension(0).getLength()); Array bl_test_max = netcdfFile.findVariable("bl_test_max").read(); assertEquals(0.14f, bl_test_max.getFloat(0), 1.0e-6); assertEquals(0.89f, bl_test_max.getFloat(1), 1.0e-6); }
@Test public void testWriting_startAndStopTimeMetadata() throws Exception { final HashMap<String, String> metadataProperties = createMetadataProperties(); final ArrayList<TemporalBin> temporalBins = createTemporalBins(); final Date startTime = new Date(500000000000L); final Date stopTime = new Date(510000000000L); final BinWriter binWriterWithUtc = createBinWriter(tempFile, numRows, ProductData.UTC.create(startTime, 0), ProductData.UTC.create(stopTime, 0)); binWriterWithUtc.write(metadataProperties, temporalBins); final NetcdfFile netcdfFile = NetcdfFileOpener.open(binWriterWithUtc.getTargetFilePath()); final Attribute startTimeAttribute = netcdfFile.findGlobalAttribute("start_time"); assertNotNull(startTimeAttribute); assertEquals("1985-11-05T00:53:20.000", startTimeAttribute.getStringValue()); final Attribute stopTimeAttribute = netcdfFile.findGlobalAttribute("stop_time"); assertNotNull(stopTimeAttribute); assertEquals("1986-02-28T18:40:00.000", stopTimeAttribute.getStringValue()); final Attribute startCoverageAttribute = netcdfFile.findGlobalAttribute("time_coverage_start"); assertNotNull(startCoverageAttribute); assertEquals("1985-11-05T00:53:20.000", startCoverageAttribute.getStringValue()); final Attribute endCoverageAttribute = netcdfFile.findGlobalAttribute("time_coverage_end"); assertNotNull(endCoverageAttribute); assertEquals("1986-02-28T18:40:00.000", endCoverageAttribute.getStringValue()); }