Commit 72cdee3d authored by Tristan Klempka's avatar Tristan Klempka
Browse files

ENH: separate python module from runnning snow detection

parent 67553865
......@@ -73,7 +73,7 @@ SET(BUILD_SHARED_LIBS ON)
add_subdirectory(src)
add_subdirectory(python)
add_subdirectory(app)
# Add Testing subdirectory
option(BUILD_TESTING "Build testing." OFF)
......
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/run_snow_detector.py DESTINATION ${CMAKE_BINARY_DIR}/app)
\ No newline at end of file
import sys
import json
from s2snow import snow_detector
VERSION="0.1"
def show_help():
"""Show help of the s2snow script"""
print "This script is used to compute snow mask using OTB applications on Spot/LandSat/Sentinel-2 products from theia platform"
print "Usage: s2snow.py param.json"
print "s2snow.py version to show version"
print "s2snow.py help to show help"
def show_version():
print VERSION
#----------------- MAIN ---------------------------------------------------
def main(argv):
""" main script of snow extraction procedure"""
json_file=argv[1]
#load json_file from json files
with open(json_file) as json_data_file:
data = json.load(json_data_file)
sd = snow_detector.snow_detector(data)
sd.detect_snow(2)
if __name__ == "__main__":
if len(sys.argv) != 2 :
show_help()
else:
if sys.argv[1] == "version":
show_version()
elif sys.argv[1] == "help":
show_help()
else:
main(sys.argv)
......@@ -11,13 +11,15 @@ import gdal
#TODO add qum
#fixme separate names and values formating
def format_files_name(snow_detector):
def format_ESA(snow_detector):
version = snow_detector.version
path_img = snow_detector.img
pout = snow_detector.path_tmp
xmltemplate = snow_detector.xmltemplate
mode = snow_detector.mode
zs = snow_detector.zs
ram = snow_detector.ram
#ID corresponding to the parent folder of the img
productID = op.basename(op.abspath(op.join(path_img, os.pardir)))
date = datetime.datetime.now()
......@@ -27,7 +29,7 @@ def format_files_name(snow_detector):
final_mask=op.join(pout, str_final_mask)
os.rename(op.join(pout, "final_mask.tif"), final_mask)
format_SEB_values(final_mask)
format_SEB_values(final_mask, ram)
str_final_mask_vec_shp=""
for f in glob.glob(op.join(pout, "final_mask_vec.*")):
......@@ -54,10 +56,10 @@ def format_files_name(snow_detector):
prj = ds.GetProjection()
root.find('mapProjection').text = str(prj)
tree.write(op.join(pout, "metadata.xml"))
def format_SEB_values(final_mask_path):
call(["otbcli_BandMath", "-il", final_mask_path, "-out", final_mask_path, "uint8", "-exp", "(im1b1==1)?100:(im1b1==2)?205:255"])
def format_SEB_values(final_mask_path, ram):
call(["otbcli_BandMath", "-il", final_mask_path, "-out", final_mask_path, "uint8", "-ram",str(ram), "-exp", "(im1b1==1)?100:(im1b1==2)?205:255"])
def format_SEB_VEC_values(final_mask_vec_path):
table = op.splitext(op.basename(final_mask_vec_path))[0]
......
......@@ -34,6 +34,7 @@ import histo_utils_ext
#Preprocessing an postprocessing script
import dem_builder
import format_output
VERSION="0.1"
#Build gdal option to generate maks of 1 byte using otb extended filename
......@@ -45,16 +46,6 @@ GDAL_OPT_2B="?&gdal:co:NBITS=2&gdal:co:COMPRESS=DEFLATE"
#TODO add temporaty directory
def show_help():
"""Show help of the s2snow script"""
print "This script is used to compute snow mask using OTB applications on Spot/LandSat/Sentinel-2 products from theia platform"
print "Usage: s2snow.py param.json"
print "s2snow.py version to show version"
print "s2snow.py help to show help"
def show_version():
print VERSION
def polygonize(input_img,input_mask,output_vec):
"""Helper function to polygonize raster mask using gdal polygonize"""
call(["gdal_polygonize.py",input_img,"-f","ESRI Shapefile","-mask",input_mask,output_vec])
......@@ -172,7 +163,7 @@ class snow_detector :
#External postprocessing
if self.do_postprocessing:
format_output.format_files_name(self)
format_output.format_ESA(self)
def pass0(self):
#Pass -1 : generate custom cloud mask
......@@ -245,12 +236,7 @@ class snow_detector :
#Generate polygons for pass2 (useful for quality check)
#TODO
polygonize(op.join(self.path_tmp,"pass2.tif"),op.join(self.path_tmp,"pass2.tif"),op.join(self.path_tmp,"pass2_vec.shp"))
#Fuse pass1 and pass2 (use 255 not 1 here because of bad handling of 1 byte tiff by otb)
#FIXME
condition_pass3= "(im1b1 == 255 or im2b1 == 255)"
call(["otbcli_BandMath","-il",self.ndsi_pass1_path,op.join(self.path_tmp,"pass2.tif"),"-out",op.join(self.path_tmp,"pass3.tif")+GDAL_OPT,"uint8","-ram",str(self.ram),"-exp",condition_pass3 + "?1:0"])
self.pass3()
generic_snow_path=op.join(self.path_tmp,"pass3.tif")
else:
#No zs elevation found, take result of pass1 in the output product
......@@ -272,13 +258,17 @@ class snow_detector :
#Build 8 bits snow_all tif
# 1st bit : pass1 snow
# 2nd bit : pass2 snow
# 3rd bit : pass3 snow
# 4th bit : cloud pass1
# 5th bit : cloud refine
expr = "(im1b1>0?0b00000001:0b00000000)|(im2b1>0?0b00000010:0b00000000)|(im3b1>0?0b00000100:0b00000000)|(im4b1>0?0b00001000:0b00000000)| (im5b1>0?0b00010000:0b00000000)"
# 3rd bit : cloud pass1
# 4th bit : cloud refine
expr = "(im1b1>0?0b00000001:0b00000000)|(im2b1>0?0b00000010:0b00000000)|(im3b1>0?0b00000100:0b00000000)|(im4b1>0?0b00001000:0b00000000)"
call(["otbcli_BandMathX", "-il", op.join(self.path_tmp,"pass1.tif"), op.join(self.path_tmp,"pass2.tif"), op.join(self.path_tmp,"cloud_pass1.tif"), op.join(self.path_tmp,"cloud_refine.tif"), "-out", op.join(self.path_tmp, "snow_all.tif"), "uint8","-ram",str(self.ram), "-exp", expr])
#TODO fix absolute path for OTB app
call(["/home/klempkat/OTB_5_2_1/build/OTB/build/bin/otbcli_BandMathX", "-il", op.join(self.path_tmp,"pass1.tif"), op.join(self.path_tmp,"pass2.tif"), op.join(self.path_tmp,"pass3.tif"), op.join(self.path_tmp,"cloud_pass1.tif"), op.join(self.path_tmp,"cloud_refine.tif"), "-out", op.join(self.path_tmp, "snow_all.tif"), "uint8", "-exp", expr])
def pass3(self):
#Fuse pass1 and pass2 (use 255 not 1 here because of bad handling of 1 byte tiff by otb)
#FIXME
condition_pass3= "(im1b1 == 255 or im2b1 == 255)"
call(["otbcli_BandMath","-il",self.ndsi_pass1_path,op.join(self.path_tmp,"pass2.tif"),"-out",op.join(self.path_tmp,"pass3.tif")+GDAL_OPT,"uint8","-ram",str(self.ram),"-exp",condition_pass3 + "?1:0"])
def sentinel_2_preprocessing(self):
#Handle Sentinel-2 case here. Sentinel-2 images are in 2 separates tif. R1
......@@ -336,30 +326,3 @@ class snow_detector :
#img variable is used later to compute snow mask
self.img=concat_s2
self.redBand_path=op.join(path_tmp,"red.tif")
#----------------- MAIN ---------------------------------------------------
#todo sentinel not working img var is not updated (local)
def main(argv):
""" main script of snow extraction procedure"""
json_file=argv[1]
#load json_file from json files
with open(json_file) as json_data_file:
data = json.load(json_data_file)
sd = snow_detector(data)
sd.detect_snow(2)
if __name__ == "__main__":
if len(sys.argv) != 2 :
show_help()
else:
if sys.argv[1] == "version":
show_version()
elif sys.argv[1] == "help":
show_help()
else:
main(sys.argv)
from distutils.core import setup
setup(name='s2snow',
version='',
package_dir={ '': '/home/klempkat/let-it-snow/let-it-snow/python' },
packages=['s2snow'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment