Sen2Cor and Snappy

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!

I’m not sure. There is the new version 2.4.0 which shall come today. Maybe along with the SNAP BETA 6 it might work.

I think @ramona_manda or @umwilm can say more about it .

Hello,

I have just tested the Sen2Cor with snappy, I have currently Sen2Cor 2.3.1 and SNAP 6.0 beta. For me it works, please find the code:

import datetime
import numpy
from snappy import ProductData
from snappy import GPF
from snappy import HashMap

def readProduct(path, formatName=None):
from snappy import (File, ProductIO)
productFile = File(path)
if formatName is None:
return ProductIO.readProduct(productFile)
else:
reader = ProductIO.getProductReader(formatName)
return reader.readProductNodes(productFile, None)

parameters = HashMap()
parameters.put(‘resolution’, “20”)
L2A_image = GPF.createProduct(‘Sen2Cor’, parameters, readProduct(“D:\temp\a.jp2”))

Let me know if you have any problems with this code.

This does not work anymore with Snap 7.0 and Sen2Cor 2.8
Everything done well, and tool manager set properly.
However it is not installed in python lib.

parameters = HashMap()
parameters.put(‘resolution’, ‘All’)
parameters.put(‘aerosol’, ‘MARITIME’)
parameters.put(‘ozone’, 0)
s2a_s2c = GPF.createProduct(‘Sen2Cor’, parameters, source)

RuntimeErrorTraceback (most recent call last)
in ()
7 parameters.put(‘aerosol’, ‘MARITIME’)
8 parameters.put(‘ozone’, 0)
----> 9 s2a_s2c = GPF.createProduct(‘Sen2Cor’, parameters, source)

RuntimeError: org.esa.snap.core.gpf.OperatorException: Unknown operator ‘Sen2Cor’. Is the name correctly spelled?

This is probably due to the existence of two versions 280 and 255. I tried different spelling like Sen2Cor280 but to no avail.

Otherwise gpt -h Sen2Cor280 works fine. (I only have 280 version installed). So we’re back to this previous solution of going through gpt command line.

milkabe’s code worked for me with minor tweaks to satisfy operator ToolAdapterOp

parameters = HashMap()
parameters.put(‘resolution’, ‘ALL') # was 'All'
parameters.put(‘aerosol’, ‘MARITIME’)
parameters.put(‘ozone’, '0') # was 0

I think the operator name in createProduct should be identical to Alias in Tools -> Manage External Tools -> Alias (in the SNAP GUI)
Edit: A list of all available opreators is available running gpt -h

# see ramona_manda's code above for a better implementations
source = snappy.ProductIO.readProduct(path_to_product) 
s2a_s2c = GPF.createProduct(‘Sen2Cor280’, parameters, source) # was Sen2Cor