Sen2Cor and Snappy

I have installed the last release of Sen2Cor in my windows system , including it in the Anaconda environment so that now sen2cor is also set as a python package for python.
Is it possible to use it as a standard library and to import sen2cor as we do for snappy?
It would be useful to use it in a processing chain, and having a Product as an object, instead of the full name of the data product.

totally agree. could you explain how to use it as a standard library? I´m new to python and snappy.
I would like to generate a processing chain as well

If the Tool Adapter is installed for Sen2Cor it should be possible to use it via GPF

Just call

GPF.createProduct('Sen2Cor', ...)

as for other operators.
Maybe there are currently issues with the adapter, I’m not sure, but in general it should work.

Hello marpet,

I installed Snappy and it runs well when calling the functions ‘Resample’, ‘Subset’, ‘BiophysicalOp’.
I can’t have it run with ‘Sen2Cor’. What is the Tool Adapter you are talking about?

Thank you

Chloe

My code:
HashMap = snappy.jpy.get_type(‘java.util.HashMap’)
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()
parameters.put(‘resolution’, resolution_meters)
L2A_image = snappy.GPF.createProduct(‘Sen2Cor’, parameters, source_image)

error message:
L2A_image = snappy.GPF.createProduct(‘Sen2Cor’, parameters, input)
Traceback (most recent call last):
File “”, line 1, in
RuntimeError: org.esa.snap.core.gpf.OperatorException: Unknown operator ‘Sen2Cor’. Note that operator aliases are case sensitive.

Did you try to run sen2cor from command line ? it seems it is not installed in your Python …

Hi Antonio, thank you for your answer, how do I install it in my Python?
It is installed in my Snap software only at the moment.

Cheers

And it is install in my Python actually:
C:\Python34\Lib\site-packages\sen2cor-2.3.1-py3.4.egg

I understand…that could be good, but have you ever tried to run sen2cor from your command window?

If you use sen2core by calling GPF.createProduct() it must be properly configured in SNAP Desktop before.
So as antonio19812 already mentioned, can you invoke sen2core from the command line by a gpt call?
Or even try it first in SNAP Desktop and configure it at Tools \ Manage External Tools

Maybe this video tutorial is helpful for you:

Hi Antonio and Marco,

Thank you very much for your help.
I still have this error message and I don’t know what to do now. Do you have other ideas by any chance?

HashMap = snappy.jpy.get_type(‘java.util.HashMap’)
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()
parameters.put(‘resolution’, 60)
L2A_image = snappy.GPF.createProduct(‘Sen2Cor’, parameters, input)
Traceback (most recent call last):
File “C:\Users\chbr\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py”, line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
L2A_image = snappy.GPF.createProduct(‘Sen2Cor’, parameters, input)
RuntimeError: org.esa.snap.core.gpf.OperatorException: Unknown operator ‘Sen2Cor’. Note that operator aliases are case sensitive.

I can use Sen2Cor using the SNAP Desktop, it works well. I produce L2A data from L1C.

Yes I can invoke Sen2Cor from the command line typing ‘gpt -h Sen2Cor’. I got

"C:\Users\chbr\AppData\Local\Continuum\Anaconda2\Lib\site-packages\snappy\snappy>gpt -h Sen2Cor
INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
Usage:
gpt Sen2Cor [options]

Description:
Sen2Cor Atmospheric Correction Processor

Source Options:
-SsourceProduct= Sets source ‘sourceProduct’ to .
This is a mandatory source.

Parameter Options:
-PpostExecuteTemplate=
Default value is ‘Sen2Cor-post-template.vm’.
-Presolution= Sets parameter ‘resolution’ to .
Value must be one of ‘10’, ‘20’, ‘60’.
Default value is ‘60’.
-PsceneOnly= Scene Only
-PtargetProductFile= Output file

Graph XML Format:

 <graph id="someGraphId">
    <version>1.0</version>
    <node id="someNodeId">
      <operator>Sen2Cor</operator>
      <sources>
        <sourceProduct>${sourceProduct}</sourceProduct>
      </sources>
      <parameters>
        <targetProductFile>file</targetProductFile>
        <postExecuteTemplate>file</postExecuteTemplate>
        <sceneOnly>boolean</sceneOnly>
        <resolution>integer</resolution>
      </parameters>
    </node>
  </graph> 

Thank you

Chloe

I’ve tried it my self and it seems that sen2cor is not available as Operator from python.
I can use it in Desktop and with gpt but from python it is not in the list of available operators.
Maybe you can call sen2core directly without going the way through snap?

@kraftek or @ramona_manda do you have an idea why this is not working?

Another way to use Sen2Cor could be to work in the command line directly from Python, but it is a bit annoying maybe…isn’t it?

Yes thanks for your answers, my goal is to run Sen2Cor with python commands. I am scanning the forum to find a piece of python script for this. I didn’t find anything so far.

You can insert and run command lines directly from Python , this could be an uneasy but possible way …have you ever tried it?

I didn’t tried but I am going to. I am looking for examples of how to use the Sen2Cor function directly with python commands

This works:
cmd= ‘L2A_Process --resolution 60 D:\S2A_MSIL1C_20170224T162331_N0204_R040_T16QBE_20170224T163738.SAFE’

os.system(cmd)

This was exactly my hint :wink: what do you think about it @marpet ? but anyway I hope they will solve the problem to call directly sen2cor

Yes, this is the way you should use at the moment. Indeed the sen2cor processor should work as operator with GPF. But maybe it is more performant when using it directly and not using Java as mediator.

We are investigating this and come back when we have an answer (if it is possible and in which version). Thank you for the notice.

Hi @marpet, any movement on integrating sen2cor as as gpf?
thanks!