Example script for multiple operations?


#21

It works well now for polarisations , thx.


#22

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:


#23

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


#24

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


#25

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

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

#26

Yes. It works perfectly!

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


#27

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!


#28

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

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


#29

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)


#30

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!


#31

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: