SpatialProductBinner { public static long processProduct(Product product, SpatialBinner spatialBinner, Map<Product, List<Band>> addedVariableBands, ProgressMonitor progressMonitor) throws IOException { if (product.getGeoCoding() == null) { throw new IllegalArgumentException("product.getGeoCoding() == null"); } BinningContext binningContext = spatialBinner.getBinningContext(); final VariableContext variableContext = binningContext.getVariableContext(); addVariablesToProduct(variableContext, product, addedVariableBands); PlanetaryGrid planetaryGrid = binningContext.getPlanetaryGrid(); CompositingType compositingType = binningContext.getCompositingType(); Geometry sourceProductGeometry = null; final MultiLevelImage maskImage; if (CompositingType.MOSAICKING.equals(compositingType)) { addMaskToProduct(variableContext.getValidMaskExpression(), product, addedVariableBands); PlateCarreeGrid plateCarreeGrid = (PlateCarreeGrid) planetaryGrid; sourceProductGeometry = plateCarreeGrid.computeProductGeometry(product); product = plateCarreeGrid.reprojectToPlateCareeGrid(product); maskImage = product.getBand(BINNING_MASK_NAME).getGeophysicalImage(); } else { maskImage = getMaskImage(product, variableContext.getValidMaskExpression()); } final MultiLevelImage[] varImages = getVariableImages(product, variableContext); final Rectangle[] sliceRectangles; if (CompositingType.MOSAICKING.equals(compositingType)) { PlateCarreeGrid plateCarreeGrid = (PlateCarreeGrid) planetaryGrid; Dimension tileSize = product.getPreferredTileSize(); sliceRectangles = plateCarreeGrid.getDataSliceRectangles(sourceProductGeometry, tileSize); } else { final Dimension defaultSliceDimension = computeDefaultSliceDimension(product); sliceRectangles = computeDataSliceRectangles(maskImage, varImages, defaultSliceDimension); } final float[] superSamplingSteps = getSuperSamplingSteps(binningContext.getSuperSampling()); long numObsTotal = 0; String productName = product.getName(); BinTracer binTracer = spatialBinner.getBinningContext().getBinManager().getBinTracer(); if (binTracer != null) { binTracer.setProductName(productName); } progressMonitor.beginTask("Spatially binning of " + productName, sliceRectangles.length); final Logger logger = BeamLogManager.getSystemLogger(); for (int idx = 0; idx < sliceRectangles.length; idx++) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); numObsTotal += processSlice(spatialBinner, progressMonitor, superSamplingSteps, maskImage, varImages, product, sliceRectangles[idx]); final String label = String.format("Processed slice %d of %d : ", idx + 1, sliceRectangles.length); stopWatch.stop(); logger.info(label + stopWatch.getTimeDiffString()); } spatialBinner.complete(); return numObsTotal; }  static long processProduct(Product product,
                                      SpatialBinner spatialBinner,
                                      Map<Product, List<Band>> addedVariableBands,
                                      ProgressMonitor progressMonitor);  }
@Test public void testThatProductMustHaveAGeoCoding() throws Exception { BinningContext ctx = createValidCtx(1, null); try { MySpatialBinConsumer mySpatialBinProcessor = new MySpatialBinConsumer(); SpatialProductBinner.processProduct(new Product("p", "t", 32, 256), new SpatialBinner(ctx, mySpatialBinProcessor), new HashMap<Product, List<Band>>(), null); Assert.fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { } }
@Test public void testProcessProduct() throws Exception { BinningContext ctx = createValidCtx(1, null); Product product = createProduct(); MySpatialBinConsumer mySpatialBinProcessor = new MySpatialBinConsumer(); SpatialProductBinner.processProduct(product, new SpatialBinner(ctx, mySpatialBinProcessor), new HashMap<Product, List<Band>>(), ProgressMonitor.NULL); Assert.assertEquals(32 * 256, mySpatialBinProcessor.numObs); }
@Test public void testProcessProductWithDataPeriod() throws Exception { DataPeriod testDataPeriod = new DataPeriod() { @Override public Membership getObservationMembership(double lon, double time) { return Membership.SUBSEQUENT_PERIODS; } }; BinningContext ctx = createValidCtx(1, testDataPeriod); Product product = createProduct(); MySpatialBinConsumer mySpatialBinProcessor = new MySpatialBinConsumer(); SpatialProductBinner.processProduct(product, new SpatialBinner(ctx, mySpatialBinProcessor), new HashMap<Product, List<Band>>(), ProgressMonitor.NULL); Assert.assertEquals(0, mySpatialBinProcessor.numObs); }
@Test public void testProcessProductWithSuperSampling() throws Exception { int superSampling = 3; BinningContext ctx = createValidCtx(superSampling, null); Product product = createProduct(); MySpatialBinConsumer mySpatialBinConsumer = new MySpatialBinConsumer(); SpatialProductBinner.processProduct(product, new SpatialBinner(ctx, mySpatialBinConsumer), new HashMap<Product, List<Band>>(), ProgressMonitor.NULL); Assert.assertEquals(32 * 256 * superSampling * superSampling, mySpatialBinConsumer.numObs); }
@Test public void testAddedBands() throws Exception { BinningContext ctx = createValidCtx(1, null); Product product = createProduct(); HashMap<Product, List<Band>> productBandListMap = new HashMap<Product, List<Band>>(); SpatialProductBinner.processProduct(product, new SpatialBinner(ctx, new MySpatialBinConsumer()), productBandListMap, ProgressMonitor.NULL); assertEquals(1, productBandListMap.size()); List<Band> bandList = productBandListMap.get(product); assertNotNull(bandList); VariableContext variableContext = ctx.getVariableContext(); assertEquals(variableContext.getVariableCount(), bandList.size()); for (int i = 0; i < bandList.size(); i++) { assertEquals(variableContext.getVariableName(i), bandList.get(i).getName()); } }
@Test public void testProcessProductWithMask() throws Exception { BinningContext ctx = createValidCtx(1, null); VariableContextImpl variableContext = (VariableContextImpl) ctx.getVariableContext(); variableContext.setMaskExpr("floor(X) % 2"); Product product = createProduct(); MySpatialBinConsumer mySpatialBinConsumer = new MySpatialBinConsumer(); SpatialBinner spatialBinner = new SpatialBinner(ctx, mySpatialBinConsumer); long numObservations = SpatialProductBinner.processProduct(product, spatialBinner, new HashMap<Product, List<Band>>(), ProgressMonitor.NULL); assertEquals(32 / 2 * 256, numObservations); assertEquals(numObservations, mySpatialBinConsumer.numObs); }
@Test public void testProcessProductWithMaskAndSuperSampling() throws Exception { int superSampling = 3; BinningContext ctx = createValidCtx(superSampling, null); VariableContextImpl variableContext = (VariableContextImpl) ctx.getVariableContext(); variableContext.setMaskExpr("floor(X) % 2"); Product product = createProduct(); MySpatialBinConsumer mySpatialBinConsumer = new MySpatialBinConsumer(); SpatialBinner spatialBinner = new SpatialBinner(ctx, mySpatialBinConsumer); long numObservations = SpatialProductBinner.processProduct(product, spatialBinner, new HashMap<Product, List<Band>>(), ProgressMonitor.NULL); assertEquals(32 / 2 * 256 * superSampling * superSampling, numObservations); assertEquals(numObservations, mySpatialBinConsumer.numObs); }