Note: The following examples assume that you work with SNAP 10 and the new esa_snappy interface. Relevant differences to the previous snappy interface are outlined in the text or in comment lines in the Python code snippets.
Even though SNAP is based on the Java programming language it supports also the development of operator plugins written in Python. This is made possible by the esa_snappy module. It enables Python developers to write operators for SNAP. In the background, esa_snappy uses the library jpy which builds a bridge between Java and Python.
The intention of this page is to point out where writing an operator in Python is different from writing an operator in Java and what you need to consider. In general, the concepts presented in How to create a new operator, How to integrate an operator, and Operator Implementation Guidelines also apply here. This page will focus on pointing out where they are different. The RUT project provides a full example of a Python-based Operator in operational use (originally implemented with previous snappy, but a development branch has been updated and tested with the new esa_snappy). As you will use the Java API of SNAP Engine via esa_snappy, you should familiarize yourself with this API, or at least know where and how to look things up.
Table of Contents
In order to write a Python plugin for SNAP, you need the following tools:
In order to make the development easier, we recommend PyCharm as your IDE. Also, before starting to implement an operator you should make sure that you have followed the procedure described in Configure Python to use the new SNAP-Python (esa_snappy) interface (SNAP version 10+) for the most recent SNAP 10. (This description replaces Configure Python to use the SNAP-Python (snappy) interface (SNAP versions <= 9) for older SNAP versions.)
The recommended basic project structure differs slightly from the structure of a Java project. It looks as shown in the following code block.
The main differences are that you use (1) a Python file for your operator (obviously), (2) a dedicated .xml-file to set the operator metadata, and (3) a Python-specific
OperatorSpi-file to register the Operator in SNAP. These files will be explained below in detail. In the
pom.xml you only need to make the usual changes which are explained at How to integrate an operator. Special adaptations are not necessary for a python operator.
Operator implemented in Python needs to have three methods. The initialize() - method , one of the three methods to compute ( doExecute(), computeTile() or computeTileStack() ) and the dispose() method. Please have a look at the following skeleton of an operator.
def initialize(self, context):
# You need to either implement this, the computeTile method or the computeTileStack method
def doExecute(self, pm):
# You need to either implement this, the doExecute method or the computeTileStack method
def computeTile(self, context, band, tile):
# You either implement this, the doExecute method or the computeTile method
def computeTileStack(self, context, target_tiles, target_rectangle):
def dispose(self, context):
__init__() can be dropped if you find it is not necessary. The meaning and use of the other methods is discussed in How to create a new operator and Operator Implementation Guidelines.
context object you see as a parameter to all of the methods gives you to some general useful methods.
|Retrieves the source data from the specified raster data node and the specified rectangle. (see API Doc)|
context.getSourceTile(rasterDataNode, rectangle, broderExtender)
|Retrieves the source data from the specified raster data node and the specified rectangle. If the rectangle exceeds the bounds of the source the borderExtender defines how to handle this case. (see API Doc)|
|Retrieves the value of the named parameter. (see API Doc)|
|Get the source product which shall be processed. (see API Doc)|
|Get the named source product which shall be processed. (see API Doc)|
By extending the rectangle used with one of the getSourceTile() methods you can get data which is outside of the target region which is currently processed.
While for Java Operators the Metadata is set through Annotations, in Python an XML file serves this purpose. This file must start with the same name as the module in which the operator is implemented and must end with
-info.xml. The file is used to define processing parameters, the source product(s) and some more metadata about the operator like an operator alias name, description, version etc.
<!-- In esa_snappy, package org.esa.snap.python is replaced by org.esa.snap.snappy -->
<copyright>(C) 2016 ACME</copyright>
A short description of the operator
<description>Short description of the parameter</description>
In the following, the meaning and the usage of the tags are described.
|A unique identifier within SNAP. The identifier could be created by following the convention for the Java packages. E.g. |
|A user-friendly alias name to be used. This name should be easily usable on the command line. So it should be short but still distinguishable. Let's say your operator computes a cloud mask using neural nets. You could name it 'NNBasedCloudMaskOperator', but this is not handy. So maybe just name it 'CompanyNameClouds' or invent some other handy name. You can use the gpt tool to get a list of already existing operators and their names.|
|This must be always |
org.esa.snap.snappy.gpf.PyOperator. In esa_snappy, package
org.esa.snap.python is replaced by
|The version of the operator. It should follow the concept of Semantic Versioning|
|The names of the authors|
|The copyright notice|
|A short description of the operator shown on the command line|
|This section defines one or more source products. In the GUI currently, one product is supported. On the command line, it is possible to use the names along with the |
-S option to specify the source product, like
|In the parameters section, all the parameters needed by the operator are specified. Each parameter is surrounded by the <parameter> tag.|
The following table shows the possible tags to configure a parameter (inside the
|The name of the parameter.|
|The description of the parameter shown on the command line and as tool-tip in the GUI.|
|Used in the GUI only instead of the name. The label can have white spaces in contrast to the name.|
|The unit of the parameter. Shown in the user interface.|
|The data type of the parameter can be either |
|The value this parameter shall have by default.|
|<notNull>||The parameter must be provided if set to |
|<notEmpty>||The parameter must not be empty if set to |
true. The difference to
<notNull> is that even if the parameter tag present in a graph XML file it is not allowed to be empty. e.g.
|The valid interval for numeric parameters, e.g. |
[10,20): in the range 10 (inclusive) to 20 (exclusive)
Set of values which can be assigned to a parameter field. The value set is given as textual representations of the actual values.
A conditional expression which must return
true in order to indicate that the parameter value is valid, e.g.
value > 2.5
A regular expression pattern to which a textual parameter value must match in order to indicate a valid value, e.g.
A format string to which a textual parameter value must match in order to indicate a valid value, e.g.
The value can be
org.esa.snap.core.datamodel.Band or any other fully qualified name of the sub-classes of RasterDataNode. This ensures that the user can only specify bands, tie-point grids or mask of the source product for this parameter. For example, when the user shall select the bands which shall be processed or the shall select a mask from the input which defines the area to be processed.
Graphical UI for the Operator
Currently, it is not possible to define your own GUI in Python. To use the standard GUI see How to integrate an operator.
Like Java Operators, Python Operators need to be registered via the Java Service Provider Interface (SPI). In order to register the operator, developers need to create a file named
'org.esa.snap.snappy.gpf.PyOperatorSpi'. Again, in esa_snappy, package
org.esa.snap.python is replaced by
org.esa.snap.snappy. This file needs to be placed in the directory
resources/META-INF/services in the project structure (see above). This file must contain the python package path and name of the operator. See below for an example and a more detailed explanation.
# In order to publish the python operators in this plugin module to SNAP, they need to be listed in this file. On each
# line an operator class is specified by its fully qualified name.
# A fully specified name includes the Python package path and the class name both separated by dot characters ('.').
# For example:
# Following Python conventions, the last name in the package path must therefore either be subpackage or a Python
# file. For the 'water.mci.MciOp' entry above, the pysical representation could either be:
# (a) water/mci.py where mci.py defines the MciOp class
# (b) water/mci/__init__.py where __init__.py defines the MciOp class
Important issues found:
- On Windows, for using a Python operator, e.g. S2RutOp, we need to set environment variables PYTHONHOME, PYTHONPATH to current Python installation directory.
- Parameters for operators: for esa_snappy, primitive types (such as int) need to be represented by Java objects (such as java.lang.Integer). E.g., set in Python: Integer = jpy.get_type('java.lang.Integer'), parameters.put('targetResolution', Integer(10)) instead of parameters.put('targetResolution', 10). Again, see RUT tool development branch as example for complete implementation.