Python: sen2cor (Linux)

I made a python script to run sen2cor on a Virtual Machine (Linux). If I want to run it from the terminal I first need to call “source /home//sen2cor/L2A_Bashrc” and afterward “python run_sen2cor.py”.

Is it possible to implement command prompt into a python script? I tried:

cmd = ‘source /home//sen2cor/L2A_Bashrc’
os.system(cmd)

but it ignores the prompt and I get an error when calling:

cmd = 'L2A_Process --resolution 20 ’
os.system(cmd)

Error:

Traceback (most recent call last):
  File "/home/pi/anaconda2/bin/L2A_Process", line 9, in <module>
    load_entry_point('sen2cor==2.0.6', 'console_scripts', 'L2A_Process')()
  File "/home/pi/anaconda2/lib/python2.7/site-packages/sen2cor-2.0.6-py2.7.egg/sen2cor/L2A_Process.py", line 237, in main
    processor = L2A_Process(directory)
  File "/home/pi/anaconda2/lib/python2.7/site-packages/sen2cor-2.0.6-py2.7.egg/sen2cor/L2A_Process.py", line 21, in __init__
    self._config = L2A_Config(workdir)
  File "/home/pi/anaconda2/lib/python2.7/site-packages/sen2cor-2.0.6-py2.7.egg/sen2cor/L2A_Config.py", line 27, in __init__
    self._home = os.environ['SEN2COR_HOME'] + '/'
  File "/home/pi/anaconda2/lib/python2.7/UserDict.py", line 40, in __getitem__
    raise KeyError(key)
KeyError: 'SEN2COR_HOME'

Any idea how to solve it?

Hi! did you manage to run Sen2Cor with pyhton commands?
Thanks
Chloe

Hi Chloe,

I’m running it from python in this way:

 prod_path = "path/to/your/product. SAFE"
 p = subprocess.Popen(["L2A_Process {0} --resolution=10".format(prod_path)], 
 			shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 # Display output in console 		
 for line in iter(p.stdout.readline, ''): 
 	print line[:-1] # The -1 is just to remove the \n at the end of every line 

I think there is a better way, using something like
boa_product = GPF.createProduct('Sen2Cor', params, product)
but I have not managed to get that to work. (Should be possible though, as running gpt -h shows that there exists a Sen2Cor Operator)

Hi jlcontreras,

Thank you for your answer. I ran it this way eventually

cmd= ‘L2A_Process --resolution 60 D:\S2A_MSIL1C_20170224T162331_N0204_R040_T16QBE_20170224T163738.SAFE’
os.system(cmd)

As @marpet and @antonio19812 told me in this conversation Sen2Cor and Snappy the GPF.createProduct(‘Sen2Cor’,…) command is not working yet and we hope that the problem will be solved soon.

Cheers

Chloe

Hi Chloe,

out of curiousity. Why does this approach not work? I am running it exactly like that and have no trouble.

Is there an error message? Did you try to run it directly in the command window? If you run this code:

L2A_Process --help

Is there any error?

Cheers,
Andreas

Really? This works for you? I’ve also tried it and could not figure out why Sen2Cor is not usable.
Still to be investigated.

I have tried yesterday and I have the same problem of both of you

the command window approach works, it is the python approach that does not work :slight_smile:

Sorry maybe my message was not clear. I was saying that I ran Sen2Cor with the windows command line but with the snappy method it didn’t work.

Hi all,

I’m trying also to call L2A_Process.py in a python or even bash script and import some key arguments. Does anybody has some work on this? I read something from https://stackoverflow.com/questions/14155669/call-python-script-from-bash-with-argument but still a lot work to be done.

Is there any update in using GPF.createProduct(‘Sen2Cor’,…)?

Thanks!