How to create Aggregator for Binning?

Hi,

I would like to use Binning from python. I have found, that I can use this:

GPF.createProduct(‘Binning’, parameters, products)

But how can I create a aggregator to use it in parameters? I have found this class:

org.esa.snap.core.gpf.common.resample.AggregatorFactory

where is this method:

createAggregator(AggregationType aggregationType, int dataBufferType)

to set aggregationType I can use:

org.esa.snap.core.gpf.common.resample.AggregationType

But what about dataBufferType?

Or is there some example how to use Binning from python?

Or is the aggregators parameter just another HashMap?

The code you found is actually referring to other forms of aggregators, so you cannot use them here.

You have two options: The first is to create a xml graph, define your aggregators there (see binningGraphExample.xml (1.5 KB) for a nonsense example ) and use this to run the operator.
The other option is to create the AggregatorConfigs in Python. The configs are

  • org.esa.snap.binning.aggregators.AggregatorAverage.Config
  • org.esa.snap.binning.aggregators.AggregatorAverageML.Config
  • org.esa.snap.binning.aggregators.AggregatorAverageOutlierAware.Config
  • org.esa.snap.binning.aggregators.AggregatorMinMax.Config
  • org.esa.snap.binning.aggregators.AggregatorAverageML.Config
  • org.esa.snap.binning.aggregators.AggregatorPercentile.Config

Each of these has a constructor with which you can fully define the aggregator. You then can add an array of aggregatorConfigs to the parameters map.

1 Like

Thanks @TonioF for reply.

A have tried using aggregators from python, but I can’t create classes you mentioned. A can get just aggregator class itself.

After:

bin = jpy.get_type(‘org.esa.snap.binning.aggregators.AggregatorAverage.Config’)

I get:

ValueError: Java class ‘org.esa.snap.binning.aggregators.AggregatorAverage.Config’ not found

This works fine:

bin = jpy.get_type(‘org.esa.snap.binning.aggregators.AggregatorAverage’)

But I can not initialize the new object:

bin()

RuntimeError: no matching Java method overloads found

I have looked at a java documentation of AggregatorAverage class, but I have no experiences with Java. In that class, if I understand right, there are two constructors. Each of them has as first input parameter VariableContext varCtx . But it looks like that the VariableContext class has no constructor.

Anyway, I have tried set rest of parameters (String varName, double weightCoeff), but with the same error.

bin(varName=‘radiance_1’, weightCoeff=0.0)

RuntimeError: no matching Java method overloads found

So, how can I create a AverrageConfig object?

Ah, that’s right, you’re using Python. As the Config here is an inner class of the AggregatorAverage, jpy expects a slightly different notation:

aggregator_average_config = jpy.get_type(‘org.esa.snap.binning.aggregators.AggregatorAverage$Config’)

1 Like

thanks a lot! now it works fine :slight_smile:

My final code looks like this:

import snappy
from snappy import ProductIO

input_filename = ‘S3A_SL_2_LST____20180829T031813_20180829T045912_20180830T094318_6059_035_118______LN2_O_NT_003_T3.dim’

product = ProductIO.readProduct(input_filename )

aggregator_average_config = snappy.jpy.get_type(‘org.esa.snap.binning.aggregators.AggregatorAverage$Config’)

agg_avg_lst = aggregator_average_config(‘LST’)

HashMap = snappy.jpy.get_type(‘java.util.HashMap’)
parameters = HashMap()

aggregators = snappy.jpy.array(‘org.esa.snap.binning.aggregators.AggregatorAverage$Config’, 1)
aggregators[0] = agg_avg_lst

parameters.put(‘outputFile’, ‘level-3_py.dim’)
parameters.put(‘numRows’, 13360) # to have about 1.5km spatial resolution
parameters.put(‘aggregators’, aggregators)

result = snappy.GPF.createProduct(‘Binning’, parameters, product)

Maybe some more questions. It is possible to set target format? By default it’s ‘BEAM-DIMAP’, even if I set another extension, it adds a ‘.dim’. If I want another format, I need to save result. But the ‘BEAM-DIMAP’ is already created.

And it’s possible to set spatial resolution in km, not in pixels, like in SNAP Desktop?

There is a parameter outputFormat to which you can pass any valid output format. You can get the valid formats via:

manager = jpy.get_type(‘org.esa.snap.core.dataio.ProductIOPlugInManager’)
manager.getInstance().getAllProductWriterFormatStrings()

You cannot set the spatial resolution in km. The conversion is pretty simple, though. See here how it is done in SNAP Desktop: https://github.com/senbox-org/snap-desktop/blob/14ba164298c2bc7e8a5123964a676ed709f42f6a/snap-binning-ui/src/main/java/org/esa/snap/binning/operator/ui/BinningConfigurationPanel.java#L196 .