Skip to content
Snippets Groups Projects
Commit e8e37102 authored by Gaëlle USSEGLIO's avatar Gaëlle USSEGLIO
Browse files

ENH : Improve generateConfigFile with color

parent fc70b8f4
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@
"""
import os
import sys
import re
import json
import readline, glob
......@@ -20,79 +21,109 @@ import func_utils
def complete(text, state):
return (glob.glob(text+'*')+[None])[state]
# Print with colors
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
def prYellow(skk): print("\033[93m {}\033[00m" .format(skk))
def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk))
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
# Input with colors
def genericInput(skk, color) :
try:
return input(color .format(skk))
except KeyboardInterrupt :
prRed("Generation Aborted, Ctrl-C detected")
sys.exit(1)
except :
prRed("Generation Aborted")
sys.exit(1)
def inRed(skk): return genericInput(skk, "\033[91m {}\033[00m")
def inGreen(skk): return genericInput(skk, "\033[92m {}\033[00m")
def inYellow(skk): return genericInput(skk, "\033[93m {}\033[00m")
def inLightPurple(skk): return genericInput(skk, "\033[94m {}\033[00m")
def inPurple(skk): return genericInput(skk, "\033[95m {}\033[00m")
def inCyan(skk): return genericInput(skk, "\033[96m {}\033[00m")
def inLightGray(skk): return genericInput(skk, "\033[97m {}\033[00m")
def inBlack(skk): return genericInput(skk, "\033[98m {}\033[00m")
# Questions to user for SAR_MultiSlc* chains
def askForMultiSlc(dataConfig) :
# Select sensor if SAR_MultiSlc
sensor = "S1IW"
if (response == "SAR_MultiSlc") :
sensor = input("Please, select the wanted sensor S1SM " \
sensor = inLightPurple("Please, select the wanted sensor S1SM " \
"(for Sentinel-1 StripMap mode) or Cosmo " \
"(for Cosmo-Skymed Spotligth and StriMap mode) : " )
if sensor not in ["S1SM", "Cosmo"] :
print("Unknown sensor, please choose between S1SM or Cosmo")
prRed("Unknown sensor, please choose between S1SM or Cosmo")
quit()
# SRTM_Shapefile
SRTM_Shapefile = os.path.realpath(input("Please, enter your path to srtm shp : " ))
SRTM_Shapefile = os.path.realpath(inPurple("Please, enter your path to srtm shp : " ))
func_utils.check_ifExist(SRTM_Shapefile)
# SRTM_Path
SRTM_Path = os.path.realpath(input("Please, enter your path to srtm hgt files : " ))
SRTM_Path = os.path.realpath(inPurple("Please, enter your path to srtm hgt files : " ))
func_utils.check_ifDir(SRTM_Path)
# Input/Output Paths
Input_Path = os.path.realpath(input("Please, enter your path to input images : " ))
Input_Path = os.path.realpath(inPurple("Please, enter your path to input images : " ))
func_utils.check_ifDir(Input_Path)
Output_Path = os.path.realpath(input("Where would you like to store the output results : " ))
Output_Path = os.path.realpath(inPurple("Where would you like to store the output results : " ))
#func_utils.check_ifDir(os.path.dirname(Output_Path))
func_utils.check_ifDir(Output_Path)
# reference image (must be into Input_Path)
reference_image = input("Which image is your reference : " )
reference_image = inPurple("Which image is your reference : " )
func_utils.check_ifExist(reference_image)
reference_image = os.path.basename(reference_image)
if not func_utils.get_imgFromDir(reference_image, Input_Path) :
print(reference_image + " not found into given input path " + \
prRed(reference_image + " not found into given input path " + \
"Input_Path")
print("Please check your input path")
prRed("Please check your input path")
quit()
else :
correct = func_utils.check_image_pattern(reference_image, mode=sensor)
if not correct :
print("Reference image " + reference_image + " does not respect naming conventions for the " \
prRed("Reference image " + reference_image + " does not respect naming conventions for the " \
"selected sensor")
quit()
# Geoid file
res_geoid = input("Would you like to add a geoid file (yes/no) : ")
res_geoid = inPurple("Would you like to add a geoid file (yes/no) : ")
Geoid = None
if res_geoid == "yes" :
Geoid = os.path.realpath(input("Please, enter your path to your geoid file : "))
Geoid = os.path.realpath(inLightPurple("Please, enter your path to your geoid file : "))
func_utils.check_ifExist(Geoid)
else :
Geoid = os.getenv('OTB_GEOID_FILE')
# Start/End date for image selection
res_date = input("Would you like to specify a start and end date for image selection (yes/no) : ")
res_date = inLightPurple("Would you like to specify a start and end date for image selection (yes/no) : ")
# Dummy dates to select by default all images into Input_Path
start_date = "19000101"
end_date = "29000101"
pattern = "".join(['\d{8}'])
if res_date == "yes" :
start_date = input("Please, indicate a start date with YYYYMMDD format : ")
start_date = inPurple("Please, indicate a start date with YYYYMMDD format : ")
if not re.match(pattern, start_date) :
print("start_date " + start_date + " does not respect the expected format YYYYMMDD")
prRed("start_date " + start_date + " does not respect the expected format YYYYMMDD")
quit()
end_date = input("Please, indicate a end date with YYYYMMDD format : ")
end_date = inPurple("Please, indicate a end date with YYYYMMDD format : ")
if not re.match(pattern, end_date) :
print("end_date " + end_date + " does not respect the expected format YYYYMMDD")
prRed("end_date " + end_date + " does not respect the expected format YYYYMMDD")
quit()
# Indicate to user, all selected images with given dates, polarisation and input_path
......@@ -117,24 +148,24 @@ def askForMultiSlc(dataConfig) :
tiff_dates = func_utils.avoidDuplicates(tiff_dates)
tiff_dates.remove(reference_image)
print("For your information, the selected images for processings will be : ")
print("As reference : " + reference_image)
print("As secondaries : " + str(tiff_dates))
prYellow("For your information, the selected images for processings will be : ")
prYellow("As reference : " + reference_image)
prYellow("As secondaries : " + str(tiff_dates))
# Ask to continue if selection OK
res_continue = input("Do you agree to continue with this selection (yes/no) : ")
res_continue = inLightPurple("Do you agree to continue with this selection (yes/no) : ")
if res_continue != "yes" :
print("Previous selection does not correspond to your expectations, you can relaunch this script with new inputs")
prRed("Previous selection does not fullfill your expectations, you can relaunch this script with new inputs")
quit()
# EOF file
EOF_Path = None
if sensor != "Cosmo" :
res_eof = input("Would you like to indicate fine orbits (yes/no) : ")
res_eof = inLightPurple("Would you like to indicate fine orbits (yes/no) : ")
if res_eof == "yes" :
EOF_Path = os.path.realpath(input("Please, enter your path to .EOF files : " ))
EOF_Path = os.path.realpath(inPurple("Please, enter your path to .EOF files : " ))
func_utils.check_ifDir(EOF_Path)
......@@ -160,53 +191,53 @@ def askForDiapOTB(dataConfig) :
# Select sensor if diapOTB
sensor = "S1IW"
if (response == "diapOTB") :
sensor = input("Please, select the wanted sensor S1SM " \
sensor = inLightPurple("Please, select the wanted sensor S1SM " \
"(for Sentinel-1 StripMap mode) or Cosmo " \
"(for Cosmo-Skymed Spotligth and StriMap mode) : " )
if sensor not in ["S1SM", "Cosmo"] :
print("Unknown sensor, please choose between S1SM or Cosmo")
prRed("Unknown sensor, please choose between S1SM or Cosmo")
quit()
# reference image (path to image)
reference_image = os.path.realpath(input("Which image is your reference : " ))
reference_image = os.path.realpath(inPurple("Which image is your reference : " ))
func_utils.check_ifExist(reference_image)
reference_image_base = os.path.basename(reference_image)
correct = func_utils.check_image_pattern(reference_image_base, mode=sensor)
if not correct :
print("Reference image " + reference_image_base + " does not respect naming conventions for the " \
prRed("Reference image " + reference_image_base + " does not respect naming conventions for the " \
"selected sensor")
quit()
# reference image (path to image)
secondary_image = os.path.realpath(input("Which image is secondary : " ))
secondary_image = os.path.realpath(inPurple("Which image is secondary : " ))
func_utils.check_ifExist(secondary_image)
secondary_image_base = os.path.basename(secondary_image)
correct = func_utils.check_image_pattern(secondary_image_base, mode=sensor)
if not correct :
print("Reference image " + secondary_image_base + " does not respect naming conventions for the " \
prRed("Reference image " + secondary_image_base + " does not respect naming conventions for the " \
"selected sensor")
quit()
# DEM Path
DEM_Path = os.path.realpath(input("Please, enter your path to your DEM : "))
DEM_Path = os.path.realpath(inPurple("Please, enter your path to your DEM : "))
func_utils.check_ifExist(DEM_Path)
# Output Path
Output_Path = os.path.realpath(input("Where would you like to store the output results : " ))
Output_Path = os.path.realpath(inPurple("Where would you like to store the output results : " ))
func_utils.check_ifDir(os.path.dirname(Output_Path))
# EOF file
EOF_Path = None
if sensor != "Cosmo" :
res_eof = input("Would you like to indicate fine orbits (yes/no) : ")
res_eof = inLightPurple("Would you like to indicate fine orbits (yes/no) : ")
if res_eof == "yes" :
EOF_Path = os.path.realpath(input("Please, enter your path to .EOF files : " ))
EOF_Path = os.path.realpath(inPurple("Please, enter your path to .EOF files : " ))
func_utils.check_ifDir(EOF_Path)
......@@ -227,19 +258,18 @@ def askForDiapOTB(dataConfig) :
if __name__ == "__main__":
######### Introduction prints #########
print("Welcome to DiapOTB remote module !")
print("You can generate configuration files for the four available processing chains : diapOTB," \
prCyan("Welcome to DiapOTB remote module !")
prCyan("You can generate configuration files for the four available processing chains : diapOTB," \
"diapOTB_S1IW, SAR_MultiSlc and SAR_MultiSlc_IW")
######### Load the example for prepare the configuration file according to user's choice #########
# First choice for user : the selected chain
response = input("Please, choose your porcessing chain (diapOTB, diapOTB_S1IW, SAR_MultiSlc and " \
response = inLightGray("Please, choose your processing chain (diapOTB, diapOTB_S1IW, SAR_MultiSlc and " \
"SAR_MultiSlc_IW) : ")
if response not in ['diapOTB', 'diapOTB_S1IW', 'SAR_MultiSlc', 'SAR_MultiSlc_IW'] :
print("Wrong chain, please choose between available chains")
prRed("Wrong chain, please choose between available chains")
quit()
# Load examples according to the selected chain => Init a dictionnary with default parameters
......@@ -277,29 +307,36 @@ if __name__ == "__main__":
########## Ask to user for generic fields #########
# SAR_MultiSlc* chains
if (response == "SAR_MultiSlc" or response == "SAR_MultiSlc_IW") :
askForMultiSlc(dataConfig)
try:
askForMultiSlc(dataConfig)
except :
prRed("Generation Aborted")
sys.exit(1)
if (response == "diapOTB" or response == "diapOTB_S1IW") :
askForDiapOTB(dataConfig)
try :
askForDiapOTB(dataConfig)
except :
prRed("Generation Aborted")
sys.exit(1)
# Dump dataConfig with the new fields and the default parameters
res_json = os.path.realpath(input("Where do you want store your configuration file : "))
res_json = os.path.realpath(inLightGray("Where do you want store your configuration file : "))
# if directory
if os.path.isdir(res_json) :
res_json_name = input("Please, enter a name for your configuration " \
"file (with .json extension) : ")
res_json_name = inLightGray("Please, enter a name for your configuration " \
"file (with .json extension) : ")
if os.path.exists(os.path.join(res_json, res_json_name)) :
res_json_overwrite = input("Would you like to overwrite the file " +
res_json_name + " (yes/no) : ")
res_json_overwrite = inLightGray("Would you like to overwrite the file " +
res_json_name + " (yes/no) : ")
if res_json_overwrite == "yes" :
with open(os.path.join(res_json, res_json_name), 'w') as f:
json.dump(dataConfig, f, indent=2, sort_keys=False)
else :
print("Generation Aborted")
prRed("Generation Aborted")
quit()
else :
......@@ -311,26 +348,26 @@ if __name__ == "__main__":
if os.path.isdir(os.path.dirname(res_json)) :
if os.path.exists(res_json) :
res_json_overwrite = input("Would you like to overwrite the file " +
res_json + " (yes/no) : ")
res_json_overwrite = inLightGray("Would you like to overwrite the file " +
res_json + " (yes/no) : ")
if res_json_overwrite == "yes":
with open(res_json, 'w') as f:
json.dump(dataConfig, f, indent=2, sort_keys=False)
else :
print("Generation Aborted")
prRed("Generation Aborted")
quit()
else :
with open(res_json, 'w') as f:
json.dump(dataConfig, f, indent=2, sort_keys=False)
else :
print("Wrong path for the configuration file, Generation Aborted")
prRed("Wrong path for the configuration file, Generation Aborted")
######### Conclusion prints #########
print("The configuration file was generated !")
print("You can modify the parameters in it and launch the processing chain with this file as only argument")
print("You can find further information on https://gitlab.orfeo-toolbox.org/remote_modules/diapotb/-/wikis/")
prGreen("The configuration file was generated !")
prCyan("You can modify the parameters in it and launch the processing chain with this file as only argument")
prCyan("You can find further information on https://gitlab.orfeo-toolbox.org/remote_modules/diapotb/-/wikis/")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment