Python snappy execute snap graphs

hello

i want to learn how to can execute some graphs from ESA SNAP using python snappy and .xml file .

here the test graphs :

<!--
>> C:\Progra~1\snap\bin\gpt "C:\Users\Marco\Desktop\SplitS2_Graph.xml" -PsourceFile="G:\S2\newS2Sample\S2A_MSIL1C_20160914T074612_N0204_R135_T36JTT_20160914T081456.SAFE\MTD_MSIL1C.xml" -PtargetbasePath="G:\EOData\temp\S2A_MS
IL1C_20160914T074612\S2A_MSIL1C_20160914T074612_N0204_R135_T36JTT_20160914T081456"
-->
<graph id="Graph">
  <version>1.0</version>
  <node id="Read">
    <operator>Read</operator>
    <sources/>
    <parameters>
      <file>${sourceFile11}</file>
    </parameters>
  </node>
  <node id="Resample">
    <operator>Resample</operator>
    <sources>
      <sourceProduct refid="Read"/>
    </sources>
    <parameters>
      <referenceBand>B4</referenceBand>
      <targetWidth/>
      <targetHeight/>
      <targetResolution/>
      <upsampling>Nearest</upsampling>
      <downsampling>First</downsampling>
      <flagDownsampling>First</flagDownsampling>
      <resampleOnPyramidLevels>true</resampleOnPyramidLevels>
    </parameters>
  </node>
  <node id="BandSelect">
    <operator>BandSelect</operator>
    <sources>
      <sourceProduct refid="Resample"/>
    </sources>
    <parameters>
      <selectedPolarisations/>
      <sourceBands>B1</sourceBands>
      <bandNamePattern/>
    </parameters>
  </node>
  <node id="Write">
    <operator>Write</operator>
    <sources>
      <sourceProduct refid="BandSelect"/>
    </sources>
    <parameters>
      <file>${targetbasePath}_B1.tif</file>
      <formatName>GeoTiff</formatName>
    </parameters>
  </node>
  <node id="BandSelect(2)">
    <operator>BandSelect</operator>
    <sources>
      <sourceProduct refid="Resample"/>
    </sources>
    <parameters>
      <selectedPolarisations/>
      <sourceBands>B2</sourceBands>
      <bandNamePattern/>
    </parameters>
  </node>
  <node id="Write(2)">
    <operator>Write</operator>
    <sources>
      <sourceProduct refid="BandSelect(2)"/>
    </sources>
    <parameters>
      <file>${targetbasePath}_B2.tif</file>
      <formatName>GeoTiff</formatName>
    </parameters>
  </node>
</graph>

and here the python code :

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('/home/saran/PycharmProjects/pythonProject/grapths/SplitS2_Graph.xml')
graph = GraphIO.read(graphFile)

graph.getNode("read").getConfiguration().getChild(0).setValue('/home/files/S2A_path.SAFE/GRANULE/L1C_T34TFL/IMG_DATA/myname_B02.jp2')
graph.getNode("Write").getConfiguration().getChild(0).setValue('g1.tiff')
graph.getNode("Write(2)").getConfiguration().getChild(0).setValue('g2.tiff')
#GraphProcessor.executeGraph(graph, ProgressMonitor.NULL)

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

and i have error :

 [NodeId: Resample] Operator 'ResamplingOp': Value for 'Reference band' is invalid: 'B4'

and idea where i have wrong ?can i work with python snappy and graphs with specific paths ?

thank you

You appear to be using linux, so you could consider snapista/. Note that snapista is packaged for anaconda python, so you will need anaconda3 or miniconda3. The primary contribution of snapista is the ability to create graphs using Python. Since you already have a graph, you might want to just copy the snapista code that runs gpt using Python subprocess.Popen().

1 Like