Example script for multiple operations?

@marpet Many thanks!
I want to use snappy for SAR interferogram, when I set parameters for TOPSAR-split, but I find it doesn’t work as expected.
could u help me to check it?

Set Params

params={
# TOPSAR-split
“subswath”: “IW2”,
“selectedPolarisations”: ‘VV’,
“bursts”: “1 to 2 (max number of bursts: 4)”, # not work
}

Parameters Assignments

parameters = HashMap()
for a in params:
# print(a)
parameters.put(a, params[a])

TOPSAR-Split

t1_spt = GPF.createProduct(“TOPSAR-Split”, parameters, product_t1)

Thanks in advance!

I found some help with “gpt TOPSAR-Split -h”

Parameter Options:
-PfirstBurstIndex= The first burst index
Valid interval is [1, *).
Default value is ‘1’.
-PlastBurstIndex= The last burst index
Valid interval is [1, *).
Default value is ‘9999’.
-PselectedPolarisations=<string,string,string,…> The list of polarisations
-Psubswath= The list of source bands.
-PwktAoi= WKT polygon to be used for selecting bursts

A ‘bursts’ parameter does not exist.
You need to use:

"firstBurstIndex":1
"lastBurstIndex":4

You can omit the burst indices. The they are calculated automatically.

Thanks, it works now for selecting bursts, but the selected polarisations are not expected, always ‘VV’.

gpt Path_to_graph.xml Path_to_source_product

And, how should I run graph.xml from python scripts? The same as command line?

It works well now for polarisations , thx.

In python you don’t need the XML. here you can use the output of one operator as in input to the next operator.
You can start command line sub-processes to start the processing with gpt, or execute the graph directly as in the following snippet:

import snappy
from snappy import jpy, ProgressMonitor

FileReader = jpy.get_type('java.io.FileReader')
GraphIO = jpy.get_type('org.esa.snap.core.gpf.graph.GraphIO')
Graph = jpy.get_type('org.esa.snap.core.gpf.graph.Graph')
GraphProcessor = jpy.get_type('org.esa.snap.core.gpf.graph.GraphProcessor')

graphFile = FileReader('Path-to-graph-file.xml')
graph = GraphIO.read(graphFile)
GraphProcessor.executeGraph(graph, ProgressMonitor.NULL)

I haven’t tested this code snippet. it is a translation from the java code. But I guess it will work.

If you want to get more progress output you can have a look at this thread, on how to do it:

2 Likes

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