Sen2cor via snappy

Hello,

i try to run sen2cor via snappy.
Sen2cor is installed in the snap desktop aplication (as Plugin). I use Python 2.7.
The newest versions of SNAP and Sen2cor are installed.
The code i am trying to run is:

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

s=ProductIO.readProduct(‘path to source product’)
HashMap = snappy.jpy.get_type(‘java.util.HashMap’)
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()
parameters.put(‘resolution’, 10)
GPF.createProduct(‘Sen2Cor’, “parameters”, s)

I receive the following error message:
Traceback (most recent call last):
File , line 13, in
GPF.createProduct(‘Sen2Cor’, “parameters”, s)
RuntimeError: no matching Java method overloads found

The last line is line 13.

Can anybody tell me what that error is about.
Thank you really much in advance.

Probably it will work if you remove the quotes from “parameters”.

I was running it before without quotes and recieved the following error message:

GPF.createProduct(‘Sen2Cor’, parameters, s)
RuntimeError: org.esa.snap.core.gpf.OperatorException: Operator ‘ToolAdapterOp’: Value for ‘Resolution’ must be of type ‘String’.

In this case, you need to use quotes for the 10 -> ‘10’. :slight_smile:
In your code, it is an integer, but sen2core requires a string.

3rd try :slight_smile: next error message.

GPF.createProduct(‘Sen2Cor’, parameters, s)
RuntimeError: java.lang.NullPointerException

This is difficult.
Could be that the product is not correctly read.
Try to print the variable s and see what the output is.

By the way, you don’t need the loadOperatorSpis() line any more.

s={product} org.esa.snap.core.datamodel.Product[name=S2B_MSIL1C_20171016T101009_N0205_R022_T33UVU_20171016T101010]

Is it possible that something is not working correctly with the HashMap?

This looks like a valid product.
Is the resolution parameter the only one sen2cor requires?
@obarrilero could you have a look?

I have executed the script and it is working here. Please, @Leddow11, could you try to execute Sen2Cor directly from SNAP Desktop to be sure that everything is properly installed?

It finnaly works. But the L2A product is just avo ihave to run the process first in 20m and then 10m. Or is this done automaticly like in the desktop aplication?
I recieve the following message in the end of the process:
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: Progress[%]: 49.20 : PID-4492, L2A_Tables: band VIS exported, elapsed time[s]: 1.015, total: 0:19:31.651000
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: Process L2A_ProcessTile-1:
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: Traceback (most recent call last):
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: File “C:\Users\Niels.snap\auxdata\Sen2Cor-02.05.05-win64\lib\multiprocessing\process.py”, line 258, in _bootstrap
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: self.run()
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: File “C:\Users\Niels.snap\auxdata\Sen2Cor-02.05.05-win64\Lib\site-packages\sen2cor\L2A_ProcessTile.py”, line 136, in run
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: if not self.process_10():
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: File “C:\Users\Niels.snap\auxdata\Sen2Cor-02.05.05-win64\Lib\site-packages\sen2cor\L2A_ProcessTile.py”, line 179, in process_10
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: if not self.process_20():
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: File “C:\Users\Niels.snap\auxdata\Sen2Cor-02.05.05-win64\Lib\site-packages\sen2cor\L2A_ProcessTile.py”, line 158, in process_20
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: return self.process()
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: File “C:\Users\Niels.snap\auxdata\Sen2Cor-02.05.05-win64\Lib\site-packages\sen2cor\L2A_ProcessTile.py”, line 229, in process
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: if self.postprocess() == False:
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: File “C:\Users\Niels.snap\auxdata\Sen2Cor-02.05.05-win64\Lib\site-packages\sen2cor\L2A_ProcessTile.py”, line 311, in postprocess
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: if not self.tables.exportBandList():
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: File “C:\Users\Niels.snap\auxdata\Sen2Cor-02.05.05-win64\Lib\site-packages\sen2cor\L2A_Tables.py”, line 2307, in exportBandList
INFO: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: fn = os.path.basename(self._L2A_Tile_PVI_File)
SEVERE: org.esa.snap.core.gpf.operators.tooladapter.DefaultOutputConsumer: AttributeError: ‘L2A_Tables’ object has no attribute ‘_L2A_Tile_PVI_File’
INFO: org.hsqldb.persist.Logger: Database closed

Hi,

the workaround for this error is to execute sen2cor in all resolutions (as in SNAP Desktop).
It should work the following line in your script:

parameters.put(resolution, ‘ALL’)

Thank you @obarrilero that solved the Problem.

That is my code to run sen2cor via snappy (python 2.7, sen2cor2.05.05, SNAP 6.0):
import snappy
from snappy import ProductIO
from snappy import GPF
from snappy import jpy
from snappy import HashMap

Sen2cor processor L1C to L2A

source_image = ProductIO.readProduct(‘path_to_L1C_product’)
parameters = HashMap()

all resolutions are necessary to get a L2A product with 10 m resolution

parameters.put(‘resolution’, ‘ALL,’)
HashMap = snappy.jpy.get_type(‘java.util.HashMap’)

standalone installation from sen2cor

GPF.createProduct(‘Sen2Cor’, parameters, source_image)

I try to run a other operator (SaviOp):
import snappy
from snappy import ProductIO
from snappy import GPF
from snappy import jpy
from snappy import HashMap

source_image = ProductIO.readProduct(‘path_to_L1C_product’)
parameters = HashMap()
parameters.put(‘redSourceBand’, ‘B4’, ‘nirSourceBand’, ‘B8’)
HashMap = snappy.jpy.get_type(‘java.util.HashMap’)

GPF.createProduct(‘SaviOp’, parameters, source_image)

Is it in general possible to run operators this way. Is the Band alias wrong?

I recieve the error message:

parameters.put(‘redSourceBand’, ‘B4’, ‘nirSourceBand’, ‘B8’)
RuntimeError: no matching Java method overloads found

Hi,

you should put one parameter at a time:

parameters.put(‘redSourceBand’, ‘B4’)
parameters.put(‘nirSourceBand’, ‘B8’)

You need to do this in two lines:

parameters.put(‘redSourceBand’, ‘B4’)
parameters.put(‘nirSourceBand’, ‘B8’)

That solved the problem.

A new one occured. Do i have to define the directory were the GPF.createproduct is creating a folder for my file. For Sen2cor it was working automaticly.

Running the script know i recieve the following error:

WARNING: org.esa.s2tbx.dataio.s2.ortho.Sentinel2OrthoProductReader: Warning: missing file C:\Users\Niels\S2B_MSIL2A_20171016T101009_N0205_R022_T33UVU_20171016T101010.SAFE\GRANULE\L2A_T33UVU_A003194_20171016T101010\QI_DATA\L2A_T33UVU_20171016T101009_DDV_20m.jp2

WARNING: org.esa.s2tbx.dataio.s2.ortho.Sentinel2OrthoProductReader: Warning: no image files found for band quality_dense_dark_vegetation

Process finished with exit code 0

Actually GPF.createproductdoes not write to the disk automatically. Only if you use it with the write operator. Writing to disk is also done if you use ProductIO.writeProduct()
For the latter one it depends on the concrete writer implementation. But in general it would be good if the directory already exists.

1 Like