SNAP pytohn api rayleigh correction help

hello ,

I want to to use SNAP python API using Rayleigh correction tools .but i takes errors.
First i need help how to define inputs parameters to use nappy.GPF.createProduct('RayleighCorrection', parameters, s) i don’t find some manual about this

here my code :

outfile_snappy='foldername'
s=ProductIO.readProduct('/folder_inp_name.SAFE')
HashMap = snappy.jpy.get_type('java.util.HashMap')
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()

parameters.put('ComputeRayleighopticalthicknessbands', True)
parameters.put('ComputebottomofRayleighreflecncebands', True)
parameters.put('ComputegaseousabsorptioncorrectedTOAreflectancebands', True)
parameters.put('ComputeTOAreflectancebands', True)
parameters.put('Addairmass', True)
parameters.put('imageresoluoninmintargetproduct.Resamplingisonlyifsourceproductisnotresampledyet(S2MSIonly)', '10')

parameters.put('SealevelpressureinhPa(S@MSIonly)', '1000')
parameters.put('OzoneinDU(SSIonly)', '300.0')
parameters.put('Sourcebands', 'B4')
target=snappy.GPF.createProduct('RayleighCorrection', parameters, s)
ProductIO.writeProduct(target, outfile_snappy, 'GeoTIFF')

and i take this error :

target=snappy.GPF.createProduct('RayleighCorrection', parameters, s)
RuntimeError: org.esa.snap.core.gpf.OperatorException: Please select at least one source band.

e.g. i want to use this for all bands .

thanks you

Use: gpt RayleighCorrection -h

Snappy is less efficient than using gpt directly. You may want to use subprocess.run() from python if you are processing a list of files.

yes i use this command and i take this help in select bands :

-PsourceBandNames=<string,string,string,...> The source bands for the computation.

but if i try to follow define this parametre :

parameters.put('PsourceBandNames', 'B01','B02','B03','B04','B05','B06','B07','B08','B09','B10','B11','B12')

i have this error :

parameters.put('sourceBandNames', 'B01','B02','B03','B04','B05','B06','B07','B08','B09','B10','B11','B12') RuntimeError: no matching Java method overloads found

any idea how to fix it ?

I would try putting the list of source band names into a Python list:

parameters.put('sourceBandNames', ['B01', 'B02', 'B03','B04', 'B05', 'B06', 'B07', 'B08', 'B09','B10', 'B11', 'B12'])

I tried this using python list but i take error : ValueError: cannot convert a Python 'list' to a Java 'java.lang.Object' , and if i use () like this ('B01','B02','B03','B04','B05','B06','B07','B08','B09','B10','B11','B12') i take error : ValueError: cannot convert a Python 'tuple' to a Java 'java.lang.Object'

Hi, please use a single string containing the comma sperated band names.
‘B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12’

1 Like

some error if i use ‘B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12’ :

RuntimeError: org.esa.snap.core.gpf.OperatorException: Please select at least one source band.

Can you show us the full parameters.put(...) line?

1st try :
parameters.put('PsourceBandNames','B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12')

error 1 :

target=snappy.GPF.createProduct('RayleighCorrection', parameters, s) RuntimeError: org.esa.snap.core.gpf.OperatorException: Please select at least one source band.

2st try :
parameters.put('PsourceBandNames', ['B01', 'B02', 'B03','B04', 'B05', 'B06', 'B07', 'B08', 'B09','B10', 'B11', 'B12'])

Error 2 :

ValueError: cannot convert a Python 'list' to a Java 'java.lang.Object

3st try :

parameters.put('PsourceBandNames', 'B01','B02','B03','B04','B05','B06','B07','B08','B09','B10','B11','B12')

error 3 :
ValueError: cannot convert a Python 'tuple' to a Java 'java.lang.Object'

4st try :
parameters.put('PsourceBandNames', 'B01', 'B02', 'B03','B04', 'B05', 'B06', 'B07', 'B08', 'B09','B10', 'B11', 'B12')

error 4 :

RuntimeError: no matching Java method overloads found

5st try :
parameters.put('PsourceBandNames','B01_B02_B03_B04_B05_B06_B07_B08_B09_B10_B11_B12')

error 5 :
RuntimeError: org.esa.snap.core.gpf.OperatorException: Please select at least one source band.

i will be use os commands to run RayleighCorrection if i have some option to define export path and name folder

'PsourceBandNames' should be just 'sourceBandNames', which explains the error message. If you read the gpt help carefully you will see that -P<name> is used to indicate a setting for the <name> parameter. Another way to check for the way parameters are set is with the SNAP GUI Graph Builder tool – just save and view the .xml file for the graph.

is the some errors is i use sourceBandNames.

1st
parameters.put('sourceBandNames','B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12')

error 1 :
target=snappy.GPF.createProduct('RayleighCorrection', parameters, s) RuntimeError: org.esa.snap.core.gpf.OperatorException: Operator 'RayleighCorrectionOp': Value for 'Source bands' is invalid: 'B01'

2st

parameters.put('sourceBandNames',['B01', 'B02', 'B03','B04', 'B05', 'B06', 'B07', 'B08', 'B09','B10', 'B11', 'B12'])

error 2 :

ValueError: cannot convert a Python 'list' to a Java 'java.lang.Object'

3st
parameters.put('sourceBandNames',('B01', 'B02', 'B03','B04', 'B05', 'B06', 'B07', 'B08', 'B09','B10', 'B11', 'B12'))

error 3:
parameters.put('sourceBandNames',('B01', 'B02', 'B03','B04', 'B05', 'B06', 'B07', 'B08', 'B09','B10', 'B11', 'B12')) ValueError: cannot convert a Python 'tuple' to a Java 'java.lang.Object'

4st
parameters.put('sourceBandNames','B01','B02','B03','B04','B05','B06','B07','B08','B09','B10','B11','B12')

error 4 :

`parameters.put('sourceBandNames','B01','B02','B03','B04','B05','B06','B07','B08','B09','B10','B11','B12')RuntimeError: no matching Java method overloads found`

5st
parameters.put('sourceBandNames','B01_B02_B03_B04_B05_B06_B07_B08_B09_B10_B11_B12')
error 5 :
RuntimeError: org.esa.snap.core.gpf.OperatorException: Operator 'RayleighCorrectionOp': Value for 'Source bands' is invalid: 'B01_B02_B03_B04_B05_B06_B07_B08_B09_B10_B11_B12'

Have a look at %USERPROFILE%\.snap\snap-python\snappy\examples\snappy_bmaths.py. At the bottom of the file there is a note that future versions of snappy will use a Python dict object to set parameters.

I tried the example from the note – apparently the future has not yet arrived.