Where is snappy?

How do I setup snappy (the SNAP-Python bridge)? I don’t see all of it (e.g., snappy.py) in the SNAP 2.0 distribution. In my case, I want to call SNAP from Python. FYI, I’m on Mac OS X 10.8.5 … but possibly 10.9 or 10.10 soon.

dhcp210-15:snap ericrehm$ pwd
/Applications/snap

dhcp210-15:snap ericrehm$ find . -name *py* -print
...
./snap/config/Modules/org-esa-snap-snap-python.xml
./snap/modules/ext/org.esa.snap.snap-python
./snap/modules/ext/org.esa.snap.snap-python/org-jpy
./snap/modules/ext/org.esa.snap.snap-python/org-jpy/jpy.jar
./snap/modules/org-esa-snap-snap-python.jar
./snap/update_tracking/org-esa-snap-snap-python.xml
...

You can’t find it, because (1) the SNAP-Python bridge is not yet documented at all and (2) because snappy is generated. Sorry for the missing documentation, we’ll provide a description and a tutorial as soon as possible possible after the Summer gap.

To use the SNAP (Java) API from Python, you can force the generation of the snappy module as follows. Open a command-line shell and type:

> cd ${SNAP_HOME}/bin
> ./snap.sh --nogui --nosplash --python <your-python-interpreter-exe>  [<snappy-target-dir>]

where ${SNAP_HOME} points to SNAP’s installation directory.After successfuul configuration, the <snappy-target-dir> folder should contain the Python snappy module folder. If omitted, <snappy-target-dir> becomes ${USER_HOME}/.snap/snap-python/. Note that only Python 2.7, 3.3, and 3.4 are supported. In the snappy module folder you also find test and example code for the snappy module in Python.

If you use Python from Java, e.g. as a processor plugin, and snappy has not yet been generated, then snappy will configure itself to its default location. If you are interested, an example of a Python plugin processor is provided at https://github.com/senbox-org/snap-examples/tree/master/snap-engine-python-operator (it is the good, old NDVI algorithm for optical sensors).

Will snappy allow to make use of all functionality that is available through the GUI (I am especially interested in using the typical SAR tools like calibration and terrain correction)? In case you have any examples for these radar tools it would be great.

I am looking forward to the release of the documentation!

You can use the gpt tool to find out more about operators and their parameterisation.

> gpt -h
> gpt -h <op-alias>

Then data processors can be invoked from Python using the following programming model:

from snappy import ProductIO
from snappy import GPF
from snappy import HashMap

ifile = '<your-input-file>'
ofile = '<your-output-file>'

parameters = HashMap()
parameters.put('rmsThreshold', 1.5)
parameters.put('warpPolynomialOrder', 2)
parameters.put('saveDEM', True)

source ProductIO.readProduct(ifile)
target = GPF.createProduct('SARSim-Terrain-Correction', source, parameters)
# Do something with 'target' or just write it:
ProductIO.writeProduct(target, ofile, 'GeoTIFF')

Some operator classes also allow for direct instantiation and object parameterisation. Consult the Java source code / API docs in this case:

op = SubsetOp()
op.setSource(source)
op.setBandNames(['<your-band-1>', '<your-band-2>'])
op.setRegion(Rectangle(0, 0, 100, 100))
target = op.getTargetProduct()
1 Like

I am trying a bit with the example you provided, but the first problem already appears with the import of HashMap. The following error message is normally thrown if there are circular dependencies within several imports. Any idea on this?

C:\Anaconda3\python.exe D:/temp/Code/calibrate.py
Traceback (most recent call last):
File “D:/temp/Code/calibrate.py”, line 6, in
from snappy import HashMap
ImportError: cannot import name ‘HashMap’

Process finished with exit code 1

My 10 cents

Python code:

from snappy import HashMap

parameters = HashMap()

Becomes :

from snappy import jpy

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

Sorry, I’,ve just fixed that!

Note that since beta 8 you can use the bin/snappy-conf command-line tool to configure the snappy module:

Usage:

cd ${SNAP_HOME}/bin
./snappy-conf <your python executable> [<your snappy target dir>]

where <your snappy target dir> is the directory that will contain the snappy module and that that must be on Python’s sys.path. It is an optional parameter and defaults to ~/.snap/snap-python.

– Norman