Perform Atmospheric Correction to Sentinel 2 Data with Sen2Cor Automatically

Sen2Cor is a processor for Sentinel-2 Level 2A product generation and formatting. It performs the atmospheric, terrain and cirrus correction of Top-Of- Atmosphere Level 1C input data. Sen2Cor creates Bottom-Of-Atmosphere, optionally terrain and cirrus corrected reflectance images; additional, Aerosol Optical Thickness, Water Vapor, Scene Classification Maps and Quality Indicators for cloud and snow probabilities. Its output product format is equivalent to the Level 1C User Product in JPEG 2000 format images,  in three different resolutions, 60, 20 and 10 m.

Sen2Cor installation packages are provided in two forms: as standalone installers (which are recommended by the author) and in the form of the classic installer (which will need an Anaconda installation as a prerequisite). You can find more for Sen2Cor installation here.

The software bellow is has been created for the automatic atmospheric correction of Sentinel 2 Data using Sen2Cor algorithm given freely by E.S.A. It works with anaconda and Sen2Cor algorithm, so they must be installed in your computer.

The script starts from the directory that it’s saved, searches into all the sub-directories for Sentinel 2 data and performs atmospheric correction. For more information check on Github.


"""
This Software is created for the automatic atmospheric correction of Sentinel 2 Data using Sen2Cor algorithm given freely by E.S.A under the GNU General Public License v3.0. This script works with anaconda and Sen2Cor algorithm, so they must be installed in your computer.
Created by : Falagas Alexandros
E-mail : alek.falagas@gmail.com
Begin : S.R.S.E - N.T.U.A, 16/06/2017
Version : Version 0.0.3

Run this script under the umbrella of Anaconda.
Known issues:
1.Spaces cause problem to Sen2Cor algorithm. Please avoid using spaces in the absolute path to images.
2.The processor is able to perform a 10 m image without background pixels (a full scene) with a memory of about 6 GB. Below the value of 6GB it might work, but this is dependent on the amount of existing non background pixels. The reason for this huge memory consumption is the fact, that for some of the atmospheric calculations (AOT) a collection of full arrays of size 10.000 x 10.000 must be kept in memory, as otherwise artifacts due to tiling will occur.
"""

import os
from os import walk
import fnmatch
import subprocess
import time

cwd=os.getcwd()
print 'Current Working Directory:',cwd
username=os.system("whoami")
dir=[]
im=0
starttime=time.time()
for (dirpath, dirnames, filenames) in walk(cwd):
dir.extend(dirnames)
for dir in dir:
if fnmatch.fnmatch(dir, '*.SAFE'):
im=im+1
print "Sentinel 2 Data found:",dir
abspath=os.getcwd()
cmd=str("L2A_Process "+str(abspath)+"/"+str(dir)) # You can set Sen2Cor resolution by adding after L2A_Process --resolution=60 for 60m or 20 for 20m or 10 for 10m. By default resolution is all.
print "Running...", cmd
os.system("export SEN2COR_HOME=/home/{}/sen2cor".format(username))
os.system("export SEN2COR_BIN=/home/{}/anaconda2/lib/python2.7/site-packages/sen2cor-2.3.1-py2.7.egg/sen2cor".format(username))
os.system("export GDAL_DATA=/home/{}/anaconda2/lib/python2.7/site-packages/sen2cor-2.3.1-py2.7.egg/sen2cor/cfg/gdal_data".format(username))
os.system(cmd)
elapsedtime=time.time()-starttime
mins, secs = divmod(elapsedtime, 60)
hours, mins = divmod(mins, 60)
image='image'
if im>1:
image="images"
print "S.A.C algorithm completed for:", im, image, "in: {}:{}:{}".format(int(hours), int(mins), round(secs,2))

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s