Obtaining sigma (backscatter) from Sentinel 1 subset

I discovered that within the ‘Help’ for calibration operator, there’s a full name provided which we need to execute this plugin from Python.

However, I have an issue with starting my sigma0 calculation
My current code:

file1 = '/home/someone/S1A_IW_GRDH_1SDV_20160509T043538_20160509T043603_011177_010E0F_2700.zip'

import os.path
import snappy
from snappy import ProductIO
from snappy import Product
from snappy import ProductData
from snappy import ProductUtils

jpy = snappy.jpy

if os.path.exists(file1):
    # Read sourceProduct and get information needed to create target product:
    sourceProduct = snappy.ProductIO.readProduct(file1)
    width = sourceProduct.getSceneRasterWidth()
    height = sourceProduct.getSceneRasterHeight()
    #Create target product:
    targetProduct = Product('FLH_Product', 'FLH_Type', width, height)
    targetBand = targetProduct.addBand('FLH', ProductData.TYPE_FLOAT32)
    ProductUtils.copyGeoCoding(sourceProduct, targetProduct)
    # Use calibration operator - I've taken "org.esa.s1tbx.calibration.gpf.CalibrationOp" from the help window
    CalibrationOp = jpy.get_type("org.esa.s1tbx.calibration.gpf.CalibrationOp")
    CalOp = CalibrationOp()
    #### HERE I DO NOT KNOW HOW TO EXECUTE this operator
    #### When I try to execute: 'doExecute' or 'execute' methods, I receive an error message

1 Like

Have a look at the snappy_subset.py in the examples folder in the snappy directory. There you can see how to use an operator.
In general you don’t need to create the target product. It is created by the operator.
sub_product = op.getTargetProduct()

I adapt you code here. It is probably not fully correct but you will get the idea.

if os.path.exists(file1):
    # Read sourceProduct
    sourceProduct = snappy.ProductIO.readProduct(file1)
    # Use calibration operator - I've taken "org.esa.s1tbx.calibration.gpf.CalibrationOp" from the help window
    CalibrationOp = jpy.get_type("org.esa.s1tbx.calibration.gpf.CalibrationOp")
    CalOp = CalibrationOp()
    CalOp.setParameter('doSomethng', True)

    targetProduct = CalOp.getTargetProduct()
    snappy.ProductIO.writeProduct(result, '/path/toFile.dim', 'BEAM-DIMAP')

Instead of creating the operator instance directly it would be better to use GPF as I have written in the other post. Aggregation and Interpolation of Sentinel products - should I use snappy or GDAL tools?
You can also have a look at my other recent posts. They are also somehow related.

1 Like

@marpet Thanks. When I try to run your code, I get following message repeated numerous times:
java.lang.OutOfMemoryError: Java heap space

I’ve tried to repeat this commeand and after two
java.lang.OutOfMemoryError: Java heap space
I get:
RuntimeError: java.lang.OutOfMemoryError: GC overhead limit exceeded

I think you need to edit the snappy.ini as described here: ‘GC overhead limit exceeded’ runtime error using 'Terrain-Correction' in python

@marpet It doesn’t work for me.

I edited both snappy.ini in


and edited/uncommented following line (I have 8 GB of RAM):

java_max_mem: 6G

I even restart my computer, but it doesn’t help.

Each time when I try (I use code which I commited here, I receive:

java.lang.OutOfMemoryError: Java heap space

RuntimeError Traceback (most recent call last)
in ()
----> 1 getSigma(SentinelFile)

in getSigma(SentinelFile)
48 newFile=newFilepath(SentinelFile, “calibrated”)
49 print("Starting writing to the file: " + newFile)
—> 50 snappy.ProductIO.writeProduct(targetProduct, newFile, ‘BEAM-DIMAP’)
51 return newFile

RuntimeError: java.lang.OutOfMemoryError: Java heap space

I’m able to accomplish calibration in ESA SNAP desktop:

Might it be related with Java version?

I have:

java -showversion
openjdk version “1.8.0_91”
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~16.04.1-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

Your code looks good. Even though I would switch to GPF.createProduct().
Is the same operation working when you do it in the desktop app? I know that working with S1 data can be memory consuming.
Maybe one thing. When using the operator directly you should call op.setParameterDefaultValues(). This will ensure that parameters are initialised with default values, If they have default values. This is ensured by the framework if you use GPF.
However, this is probably not a problem in your case.

@marpet Yes, this operation works in desktop app - I edited my question to provide an example.

Regarding rewriting to GPF.createProduct() - could you provide me an example/suggestion. Will this help with mentioned errors?

@marpet It seems thatjava_max_mem settings from~/.snap/snap-python/snappy/snappy.ini does not work.

I typed ‘export _JAVA_OPTIONS=-Xmx4096m’ in my shell and then run my script.
Now I do not get a > java.lang.OutOfMemoryError exception.

1 Like

Hello Kedzior,
I’m a beginner to use snap+python,
I need to calibrate Sentinel-1 data.
Finally, do you have succeeded to run your script?
Please, can you share your your script?

you don’t need any script, just run the Calibration module (Menu > Radar > Radiometric > Calibration) on your Sentinel-1 data in order to convert them into Sigma0.

Thank you ABraun,
But I have many images to calibrate. So, I would link snap with a python (snappy) to do that in an iterative process.

that is an option, yes. But you can also simply create a graph file in SNAP (read > calibrate > write) and apply it on multiple images using the batch processing menu.

Here it turned out to be working.

Strange. However, finally you made it :+1:

Where we have to code this? In SNAP or somewhere else? Please also tell how to get backscatter values using python script?

it is basically just loading the data and applying the calibration operator.
Have you seen this topic: Snappy: Where to start?

Here is the main documentation: https://senbox.atlassian.net/wiki/spaces/SNAP/pages/50855941/Configure+Python+to+use+the+SNAP-Python+snappy+interface

I want python code to obtain backscatter values.

As I said above, you need to install the snappy library first and then load the data into python as explained in the documentation above.
How data can be loaded is described here: https://github.com/senbox-org/snap-examples/blob/master/snap-engine-python-scripts/src/main/python/snippets.py

The second step is to call the calibration tool from python.

While I am writing the code given above I am getting an error like: ImportError: cannot import name ProductIO. I have installed snappy. What should I have to do?

what happens when you type import snappy?

There is another python package called snappy, it is therefore important to install snappy with the procedure mentioned above. For example, running “pip install snappy” will load the wrong package.

Are you sure you installed the package in the correct python installation? Sometimes, there are several installed on the computer, e.g. when you use ArcGIS.

Maybe a colleague can help you to configure snappy.

This is what worked for me:

Please help me with the python script for Speckle Filtering of Sentinel-1A images.