Error with SARSim-Terrain-Correction within python

I’m trying to use SARSim-Terrain-Correction within python 2.7.11 in order to orthorectifying a RADARSAT-2 SLC image. It works in the GUI version on Windows OS.

But when running my python code I get the following error.

File “test.py”, line 29, in
target = GPF.createProduct(‘SARSim-Terrain-Correction’, parameters, source)
RuntimeError: org.esa.snap.core.gpf.OperatorException: SARSim-Terrain-Correction
: Metadata attribute ‘DEM resampling method’ not found

My python code is below. Any help would be much appreciated. Thanks.

from snappy import Product
from snappy import ProductData
from snappy import ProductIO
from snappy import ProductUtils
from snappy import GPF
from snappy import HashMap
import snappy
import jpy
ResamplingFactory = jpy.get_type(‘org.esa.snap.core.dataop.resamp.ResamplingFactory’)
ProductIOPlugInManager = snappy.jpy.get_type(‘org.esa.snap.core.dataio.ProductIOPlugInManager’)
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
source = ProductIO.readProduct(gdalsourcefile)

parameters = HashMap()
#parameters.put(‘PdemName’, ‘test’)
parameters.put(‘demResamplingMethod’, ‘BILINEAR_INTERPOLATION’)
#parameters.put(‘externalDEMFile’, dem_file)
#parameters.put(‘PsaveLayoverShadowMask’, 0)
parameters.put(‘sourceBands’, ‘Intensity_HH’)
testing = GPF.createProduct(‘SAR-Simulation’, parameters, source)

parameters = HashMap()
#parameters.put(‘rmsThreshold’, 1.5)
parameters.put(‘warpPolynomialOrder’, 2)
parameters.put(‘imgResamplingMethod’, ResamplingFactory.BILINEAR_INTERPOLATION_NAME)
parameters.put(‘mapProjection’, prj_wkt_main)
parameters.put(‘saveDEM’, True)
parameters.put('saveSigmaNought ', True)
target = GPF.createProduct(‘SARSim-Terrain-Correction’, parameters, source)

For the SAR-Simulation operator the names of two parameters are not correct. You should remove the leading ‘P’ from PdemName and PsaveLayoverShadowMask. Also the value for demName os not yet correct.
But these don’t cause the error I guess.

I think the reason is that you don’t use the right product as source. It does not have the required ‘DEM resampling method’ attribute in the metadata.
Maybe you want to use the outcome of the SAR-Simulation? So you need to put testing into the GPF call.

target = GPF.createProduct('SARSim-Terrain-Correction', parameters, testing)

Hi Marco
Thanks for your help, much appreciated.

I’m getting closer to what I need using snap python, but not quite there
yet…

‘Terrian-Correction’ seems to work to orthorectify my RADARSAT-2 images in
the GUI version.
Intensity_HH sigma0 values between 0 and 2.2; mean = 0.109

My python version generates an image but not in the desired Sigma0
intensity values.
It creates values between 1267 and 4000000; mean = 2400000

My python code is:
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
source = ProductIO.readProduct(gdalsourcefile)
parameters = HashMap()
parameters.put(‘sourceBands’, ‘Intensity_HH’)
parameters.put(‘pixelSpacingInMeter’, ‘12.5’)
parameters.put(‘saveDEM’, False)
parameters.put(‘saveSelectedSourceBand’, True)
parameters.put(‘saveSigmaNought’, True)
parameters.put(‘incidenceAngleForSigma0’, ‘Use incidence angle from
Ellipsoid’)
target = GPF.createProduct(‘Terrain-Correction’, parameters, source)
ProductIO.writeProduct(target, outfile_snappy, ‘GeoTIFF’)

Note, if I change ‘saveSelectedSourceBand’ to ‘false’ I get the following
error:
target = GPF.createProduct(‘Terrain-Correction’, parameters, source)
RuntimeError: org.esa.snap.core.gpf.OperatorException: Terrain-Correction:
Please select output band for terrain corrected image

The GUI version looks like:

Any help would be much appreciated.
Thanks
John

Oops, I forgot:
parameters.put(‘applyRadiometricNormalization’, True)
everything works now
Thanks
John

Hey folks!

Is there anyone has tried a python code for SAR orthorectification? I wanna perform it on RadarSat2, but not yet that success achieved. I truly appreciate if you share the code.Thanks again

This python works great to Orthorectify RADARSAT-2:

from snappy import Product
from snappy import ProductData
from snappy import ProductIO
from snappy import ProductUtils
from snappy import GPF
import snappy
import jpy
HashMap = jpy.get_type(‘java.util.HashMap’)
parameters = HashMap()
DEMFactory = jpy.get_type(‘org.esa.snap.dem.dataio.DEMFactory’)
Resampling = jpy.get_type(‘org.esa.snap.core.dataop.resamp.Resampling’)
ResamplingFactory = jpy.get_type(‘org.esa.snap.core.dataop.resamp.ResamplingFactory’)
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
ProductIOPlugInManager = snappy.jpy.get_type(‘org.esa.snap.core.dataio.ProductIOPlugInManager’)

def ortho_SLC(in_file, in_band, out_file, pixel_res, prj_wkt_main):
source = ProductIO.readProduct(in_file)
parameters = HashMap()
#parameters.put(‘demName’,‘SRTM 3Sec’)
parameters.put(‘demName’,‘GETASSE30’)
parameters.put(‘imgResamplingMethod’, ‘NEAREST_NEIGHBOUR’)
parameters.put(‘applyRadiometricNormalization’, True)
parameters.put(‘sourceBands’, in_band)
parameters.put(‘pixelSpacingInMeter’, pixel_res)
parameters.put(‘mapProjection’, prj_wkt_main)
parameters.put(‘saveDEM’, False)
parameters.put(‘saveSelectedSourceBand’, False)
parameters.put(‘saveSigmaNought’, True)
parameters.put(‘incidenceAngleForSigma0’, ‘Use projected local incidence angle from DEM’)
target = GPF.createProduct(‘Terrain-Correction’, parameters, source)
ProductIO.writeProduct(target, out_file, ‘GeoTIFF’)

3 Likes

How we can run this ortorectifying codes directly from python, Not snappy! Plz help!

What do you mean by “directly from python”. To use the functionality of SNAP within Python you need snappy.

1 Like

Thanks for your examples. Although I have some JAVA memory problems, I can get them running properly. I understand you use the ‘Terrain-Correction’ operator, but how to get info on how to use it (its parameters etc) or other operators which are available in the SNAP GUI (Calibration, Filtering, etc). So far searching in the http://step.esa.int/docs/v3.0/apidoc/engine/ documentation was not successful.

I mean how to import Snappy in python?

And also wondering whether we can have a free access to the SNAP python codes mainly related to the SAR data analysis (e.g., orthorectifying, multilooking, calibration,filtering,…) only through Snappy. If so, by importing the Snappy in python, I wont need to write the codes where all already has been provided by Snap. correct? Please correct me if I got it wrong.

Thanks again

Please have a look at this post:

Maybe an info for clarification, SNAP is not implemented in Python., so there are no python codes.
SNAP is implemented in Java, there is just a bridge to call from Python to Java.

To solve python/snappy memory problem:
You need to change snappy.ini in the snappy directory which you have probably copied into the site-packages directory.

Change the javamax_mem_ property to e.g. 6G. Also remove the ‘#’ from the beginning of the line.

2 Likes

Hello.

I am trying to run the Doppler Range Terrain Correction in python, and I get the error as follows. I do appreciate if you could assist me that (input: radarsat 2 SLC, External DEM: Lidar DEM.tiff)

:

import sys

sys.path.append(‘c:\users\myworkadmin\.snap\snap-python’)

from snappy import Product
from snappy import ProductData
from snappy import ProductIO
from snappy import ProductUtils
from snappy import GPF
import snappy
import jpy
HashMap = jpy.get_type(‘java.util.HashMap’)
parameters = HashMap()
DEMFactory = jpy.get_type(‘org.esa.snap.dem.dataio.DEMFactory’)
Resampling = jpy.get_type(‘org.esa.snap.core.dataop.resamp.Resampling’)
ResamplingFactory = jpy.get_type(‘org.esa.snap.core.dataop.resamp.ResamplingFactory’)
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
ProductIOPlugInManager = snappy.jpy.get_type(‘org.esa.snap.core.dataio.ProductIOPlugInManager’)

in_file=“C:\Sam\20140201_011537_HH_SLC\20140201_011537_HH_SLC\product.xml”

dem_file=‘C:\Sam\LIDAR_DEM\SS_LiDAR_DEM_Mosaic.tif’
in_band=“Intensity_HH” # “3” # “band_1” # three options i_HH
out_file=“20140201_011537_HH_SLC” #“C:\Sam\output_tc\”
pixel_res=3.0
prj_wkt_main=“wgs84(DD)”

def ortho_SLC(in_file, in_band, out_file, pixel_res, prj_wkt_main):
source = ProductIO.readProduct(in_file)
parameters = HashMap()
#parameters.put(‘demName’, ‘SRTM 3Sec’) #
#parameters.put(‘demName’,‘GETASSE30’)
parameters.put(‘externalDEMFile’, dem_file)
parameters.put(‘imgResamplingMethod’, ‘NEAREST_NEIGHBOUR’)
parameters.put(‘applyRadiometricNormalization’, True)
parameters.put(‘sourceBands’, ‘Intensity_HH’) # in_band)
parameters.put(‘pixelSpacingInMeter’, pixel_res)
parameters.put(‘mapProjection’, prj_wkt_main)
parameters.put(‘saveDEM’, False)
parameters.put(‘saveSelectedSourceBand’, True)
parameters.put(‘saveSigmaNought’, True)
parameters.put(‘incidenceAngleForSigma0’, ‘Use projected local incidence angle from DEM’)
target = GPF.createProduct(‘Terrain-Correction’, parameters, source)
ProductIO.writeProduct(target, out_file, ‘GeoTIFF’)

ortho_SLC(in_file, in_band, out_file, pixel_res, prj_wkt_main)

Hi All

I have copied the code proposed by jhonb, and it showed me out this error:

Traceback (most recent call last):
File “D:\ToBackup\Documents\Scripts\Python\Snap_Process\Attempt1.py”, line 27, in
ProductIO.writeProduct(target, outfile_snappy, ‘GeoTIFF’)
RuntimeError: java.lang.IllegalStateException: File size too big. TIFF file size is limited to [4294967296] bytes!

I have modified the java_mav_memory to 6G, but it didn’t work.

I will appreciate your help.

Thanks
Andrés

This error is not related to the memory but to the files size.
That’s why changing the properties doesn’t help.
You need to use a different target format, GeoTiff has a limit of 4GB. Please see my post
Snappy against a large geotiff file.

1 Like