Ejemplo n.º 1
0
  private TemporalBinList doTemporalBinning(SpatialBinCollection spatialBinMap) throws IOException {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    long numberOfBins = spatialBinMap.size();
    final TemporalBinner temporalBinner = new TemporalBinner(binningContext);
    final CellProcessorChain cellChain = new CellProcessorChain(binningContext);
    final TemporalBinList temporalBins = new TemporalBinList((int) numberOfBins);
    Iterable<List<SpatialBin>> spatialBinListCollection = spatialBinMap.getBinCollection();
    int binCounter = 0;
    int percentCounter = 0;
    long hundredthOfNumBins = numberOfBins / 100;
    for (List<SpatialBin> spatialBinList : spatialBinListCollection) {
      binCounter += spatialBinList.size();

      SpatialBin spatialBin = spatialBinList.get(0);
      long spatialBinIndex = spatialBin.getIndex();
      TemporalBin temporalBin = temporalBinner.processSpatialBins(spatialBinIndex, spatialBinList);

      temporalBin = temporalBinner.computeOutput(spatialBinIndex, temporalBin);
      temporalBin = cellChain.process(temporalBin);

      temporalBins.add(temporalBin);
      if (binCounter >= hundredthOfNumBins) {
        binCounter = 0;
        getLogger().info(String.format("Finished %d%% of temporal bins", ++percentCounter));
      }
    }
    stopWatch.stop();
    getLogger()
        .info(String.format("Temporal binning of %d bins done, took %s", numberOfBins, stopWatch));

    return temporalBins;
  }
Ejemplo n.º 2
0
  private void processSource(Product sourceProduct, SpatialBinner spatialBinner)
      throws IOException {
    final StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    updateDateRangeUtc(sourceProduct);
    metadataAggregator.aggregateMetadata(sourceProduct);

    final String productName = sourceProduct.getName();
    getLogger().info(String.format("Spatial binning of product '%s'...", productName));
    getLogger().fine(String.format("Product start time: '%s'", sourceProduct.getStartTime()));
    getLogger().fine(String.format("Product end time:   '%s'", sourceProduct.getEndTime()));
    if (region != null) {
      SubsetOp subsetOp = new SubsetOp();
      subsetOp.setSourceProduct(sourceProduct);
      subsetOp.setGeoRegion(region);
      sourceProduct = subsetOp.getTargetProduct();
      // TODO mz/nf/mp 2013-11-06: avoid creation of subset products
      //  - replace subset with rectangle as parameter to SpatialProductBinner
      //  - grow rectangle by binSize in pixel units (see lc-tools of LC-CCI project)
    }
    final long numObs =
        SpatialProductBinner.processProduct(
            sourceProduct, spatialBinner, addedVariableBands, ProgressMonitor.NULL);
    stopWatch.stop();

    getLogger()
        .info(
            String.format(
                "Spatial binning of product '%s' done, %d observations seen, took %s",
                productName, numObs, stopWatch));

    if (region == null && regionArea != null) {
      for (GeneralPath generalPath : ProductUtils.createGeoBoundaryPaths(sourceProduct)) {
        try {
          Area area = new Area(generalPath);
          regionArea.add(area);
        } catch (Throwable e) {
          getLogger()
              .log(
                  Level.SEVERE,
                  String.format("Failed to handle product boundary: %s", e.getMessage()),
                  e);
          // sometimes the Area constructor throw an "java.lang.InternalError: Odd number of new
          // curves!"
          // then just ignore this geometry
        }
      }
    }

    ++numProductsAggregated;
  }
Ejemplo n.º 3
0
  private void writeOutput(
      List<TemporalBin> temporalBins, ProductData.UTC startTime, ProductData.UTC stopTime)
      throws Exception {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    initMetadataProperties();

    if (outputBinnedData) {
      try {
        writeNetCDFBinFile(temporalBins, startTime, stopTime);
      } catch (Exception e) {
        getLogger()
            .log(Level.SEVERE, String.format("Failed to write binned data: %s", e.getMessage()), e);
      }
    }

    if (outputTargetProduct) {
      getLogger()
          .info(String.format("Writing mapped product '%s'...", formatterConfig.getOutputFile()));
      final MetadataElement processingGraphMetadata = getProcessingGraphMetadata();
      Formatter.format(
          binningContext.getPlanetaryGrid(),
          getTemporalBinSource(temporalBins),
          binningContext.getBinManager().getResultFeatureNames(),
          formatterConfig,
          region,
          startTime,
          stopTime,
          processingGraphMetadata);
      stopWatch.stop();

      String msgPattern = "Writing mapped product '%s' done, took %s";
      getLogger().info(String.format(msgPattern, formatterConfig.getOutputFile(), stopWatch));

      if (outputType.equalsIgnoreCase("Product")) {
        final File writtenProductFile = new File(outputFile);
        String format = Formatter.getOutputFormat(formatterConfig, writtenProductFile);
        writtenProduct = ProductIO.readProduct(writtenProductFile, format);
        this.targetProduct = copyProduct(writtenProduct);
      } else {
        this.targetProduct = new Product("Dummy", "t", 10, 10);
      }
    } else {
      this.targetProduct = new Product("Dummy", "t", 10, 10);
    }
  }
Ejemplo n.º 4
0
  /**
   * Processes all source products and writes the output file. The target product represents the
   * written output file
   *
   * @throws org.esa.snap.framework.gpf.OperatorException If a processing error occurs.
   */
  @Override
  public void initialize() throws OperatorException {
    formatterConfig = new FormatterConfig();
    formatterConfig.setBandConfigurations(bandConfigurations);
    formatterConfig.setOutputFile(outputFile);
    formatterConfig.setOutputFormat(outputFormat);
    formatterConfig.setOutputType(outputType);
    formatterConfig.setProductCustomizerConfig(productCustomizerConfig);

    validateInput();

    ProductData.UTC startDateUtc = null;
    ProductData.UTC endDateUtc = null;
    if (startDateTime != null) {
      startDateUtc = parseStartDateUtc(startDateTime);
      double startMJD = startDateUtc.getMJD();
      double endMJD = startMJD + periodDuration;
      endDateUtc = new ProductData.UTC(endMJD);
    }

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    if (region == null) {
      // TODO use JTS directly (nf 2013-11-06)
      regionArea = new Area();
    }

    final BinningConfig binningConfig = createConfig();

    binningContext = binningConfig.createBinningContext(region, startDateUtc, periodDuration);

    BinningProductFilter productFilter =
        createSourceProductFilter(binningContext.getDataPeriod(), startDateUtc, endDateUtc, region);

    metadataAggregator = MetadataAggregatorFactory.create(metadataAggregatorName);
    numProductsAggregated = 0;

    try {
      // Step 1: Spatial binning - creates time-series of spatial bins for each bin ID ordered by
      // ID. The tree map structure is <ID, time-series>
      SpatialBinCollection spatialBinMap = doSpatialBinning(productFilter);
      if (!spatialBinMap.isEmpty()) {
        // update region
        if (region == null && regionArea != null) {
          region = JTS.shapeToGeometry(regionArea, new GeometryFactory());
        }
        // Step 2: Temporal binning - creates a list of temporal bins, sorted by bin ID
        TemporalBinList temporalBins = doTemporalBinning(spatialBinMap);
        // Step 3: Formatting
        try {
          if (startDateTime != null) {
            writeOutput(temporalBins, startDateUtc, endDateUtc);
          } else {
            writeOutput(temporalBins, minDateUtc, maxDateUtc);
          }
        } finally {
          temporalBins.close();
        }
      } else {
        getLogger().warning("No bins have been generated, no output has been written");
      }
    } catch (OperatorException e) {
      throw e;
    } catch (Exception e) {
      throw new OperatorException(e);
    } finally {
      cleanSourceProducts();
    }

    stopWatch.stopAndTrace(
        String.format("Total time for binning %d product(s)", numProductsAggregated));

    globalMetadata.processMetadataTemplates(metadataTemplateDir, this, targetProduct, getLogger());
  }