Example script for multiple operations?

Thanks for your great help. I’ll try right now.

Hi, @marpet
I tested this code snippet, but it occured an error.

INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
Traceback (most recent call last):
File “E:/Fogo Volcano 2014 Eruption/how_to_run_graph_with_python.py”, line 11, in
GraphProcessor.executeGraph(graph, ProgressMonitor.NULL)
RuntimeError: no matching Java method overloads found

Oh yes. I overlooked that this is not a static method.
You need to do:

graphProcessor = GraphProcessor()
graphProcessor.executeGraph(graph, ProgressMonitor.NULL)
1 Like

Yes. It works perfectly!

And, may I specify input products and output path for pre-built graph in python scripts? thx.

1 Like

I mean, could I change the pre-set source products and the write folder for a given graph.xml in python scripts?
cause I want to do batch processing by changing the input files and output files.
Thanks in advance!

Hi, @marpet Shall I use the code below for changing pre-set source products?

graph = GraphIO.read(Reader reader, Map<String,String> variables)

Thanks for your great help, I finally implement batch processing by runing graph from python.
and I set read and write nodes as follows:

graph.getNode(“read”).getConfiguration().getChild(0).setValue(String input1)
graph.getNode(“read(2)”).getConfiguration().getChild(0).setValue(String input2)
graph.getNode(‘write’).getConfiguration().getChild(0).setValue(String write)

3 Likes

Hi,
Thx for this useful snippet. I am not yet familiar with java and would like to include a Progress Monitor for my python code. So i implemented your code and created a .xml file from scratch. How can i include the file to the source product within this xml file?
I wonder how i change the follwoing line:

<sourceProducts>${SourceProducts}</sourceProducts>

to something like

<sourceProducts>${“Path to my source product”}</sourceProducts>

is this possible?
thx!

This is not possible in this way.
You can add a read operator in front of your graph and reference this node.
See this example:

<graph id="Graph">
  <version>1.0</version>
  <node id="Read">
    <operator>Read</operator>
    <sources/>
    <parameters>
      <file>G:\EOData\Meris\RR\MER_RR__1PNBCG20050709_101121_000001802038_00466_17554_0001.N1</file>
    </parameters>
  </node>
  <node id="BandMaths">
    <operator>BandMaths</operator>
    <sources>
      <source>Read</source>
    </sources>
...

However, I would not recommend it.
This makes the xml files not reusable, and this was actually the intention. Write one graph and let it run on thousands of products.
It is better to set up the graph by Python.
I’ll find several examples in the forum by searching for [GPF.createProduct(](https://forum.step.esa.int/search?q=GPF.createProduct()

Regarding ProgressMonitor, this might be of interest to you:


Where can i find the list of OperatorNames?

You can go to the command line and call gpt with the help option. Then all available operators are listed.

> gpt -h

In Python you can use the example code:

1 Like

Still i’m getting the same error.

Which error? This thread is not about an error :thinking:

pls how to set parameters to choise the decomposition : I tried parameters.put(‘Decomposition’,‘YAMAGUCHI_DECOMPOSITION’) … but doesn’t work, still giving the sinclair set as default

Try ‘decomposition’, with a small ‘d’.

This is what the help shows on the command line:

Usage:
  gpt Polarimetric-Decomposition [options]

Description:
  Perform Polarimetric decomposition of a given product


Source Options:
  -Ssource=<file>    Sets source 'source' to <filepath>.
                     This is a mandatory source.

Parameter Options:
  -Pdecomposition=<string>                  Sets parameter 'decomposition' to <string>.
                                            Value must be one of 'Sinclair Decomposition', 'Pauli Decomposition', 'Freeman-Durden Decomposition', 'Generalized Freeman-Durden Decomposition', 'Yamaguchi Decomposition', 'van Zyl Decomposition', 'H-A-Alpha Quad Pol Decomposition', 'H-Alpha Dual Pol Decomposition', 'Cloude Decomposition', 'Touzi Decomposition'.
                                            Default value is 'Sinclair Decomposition'.
  -PoutputAlpha123=<boolean>                Output alpha 1, 2, 3
                                            Default value is 'false'.
...

snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
ProductIO.getProductWriter(‘GeoTIFF’)
p = ProductIO.readProduct(‘D:\TESTS\product.xml’)
print(*p.getBandNames(), sep = “\n”)

HashMap = jpy.get_type(‘java.util.HashMap’)
parameters = HashMap()
parameters.put(‘Pdecomposition’,‘Yamaguchi Decomposition’)
my_pol_decomposition = GPF.createProduct(‘Polarimetric-Decomposition’, parameters, p)
ProductIO.writeProduct(my_pol_decomposition, ‘YAMAGUCHI_DECOMPOSITION.tif’, ‘GeoTIFF-BigTIFF’)

but still giving “sinclair decomposition”
did I miss something

Remove the ‘P’. This is just the indicator on the command line to tell gpt the next value is a parameter.

Just use:

parameters.put(‘decomposition’,‘Yamaguchi Decomposition’)
1 Like

thank you.
another question plz,
Can we use the operators like this:

polOP = snappy.jpy.get_type(‘org.csa.rstb.polarimetric.gpf.PolarimetricDecompositionOp’)
decom_type= polOP.YAMAGUCHI_DECOMPOSITION
GPF.SetDecomposition(decom_type)
#do the decomposition_
polOP.initialize()
#or GPF.PolarimetricDecompositionOp(source_pro, target_product,parameters)

##______ récupérer le résultat : produit résultant
result=polOP.getTargetProduct()
polOP.dispose() # free all resources previously allocated by the operator

It would only work with some corrections to the code. But I wouldn’t recommend it.
However, with the following changes it should work.

After getting the type an instance of this type needs to be created Then the default vaulues of all parameters should need to be set:

polOP = snappy.jpy.get_type(‘org.csa.rstb.polarimetric.gpf.PolarimetricDecompositionOp’)
pol_inst = polOp()
pol_inst.setParameterDefaultValues()

Instead of calling SetDecompostion on GPF, you need to use the newly created instance. And SetDecomposition can’t be used at all because it is not publicly accessible. You need to use the generic setParameter method.

pol_inst.setParameter('decomposition', polOP.YAMAGUCHI_DECOMPOSITION)

After setting also other parameters the resulting product can be retrieved.
result=pol_inst.getTargetProduct()

Calling dispose is usually not necessary, but if you want to do it then do it only if the product is written to disk or the data has been otherwise requested and is not used anymore. The reason is that the computation is only performed when the data is requested. After calling getTargetProduct only a proxy to the data is generated and data is not computed yet.

1 Like

Thank you very much Mister