From 36f180e4bd2f4255bcde06015bb917a3d33a3bd1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=C3=ABlle=20USSEGLIO?= <gaelle.usseglio@cnes.fr>
Date: Fri, 10 Jul 2020 13:44:14 +0000
Subject: [PATCH] ENH : Add protection on master Image check into SAR_MultiSlc
 chains

---
 python_src/SAR_MultiSlc.py     | 28 +++++++++++++++++++++++++++
 python_src/SAR_MultiSlc_IW.py  | 15 +++++++++++++++
 python_src/utils/func_utils.py | 35 ++++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+)

diff --git a/python_src/SAR_MultiSlc.py b/python_src/SAR_MultiSlc.py
index 808e4ae..edd43eb 100644
--- a/python_src/SAR_MultiSlc.py
+++ b/python_src/SAR_MultiSlc.py
@@ -124,13 +124,41 @@ if __name__ == "__main__":
             light_version = dict_Global['parameter']['clean']
     light_version = func_utils.str2bool(light_version)
 
+
+    func_utils.check_ifExist(dict_Global['in']['Input_Path'])
+
     if master_ext[0] == "h5" : # Cosmo case
         master_Image = func_utils.get_imgFromDir(dict_Global['in']['Master_Image'], dict_Global['in']['Input_Path'])
+
+        if not master_Image :
+            print(master_Image_base + " not found into given input path " + \
+                  dict_Global['in']['Input_Path'])
+            print("Please check your input path")
+            quit()
+        else :
+            correct = func_utils.check_image_pattern(master_Image_base, mode="Cosmo")
+            if not correct : 
+                print("Master image " + master_Image_base + " does not respect naming conventions for Cosmo")
+                quit()
+
         master_date = master_Image_base.split("_")[8][:8]
         pol = master_Image_base.split("_")[5]
         
     else : #S1 SM case
         master_Image = func_utils.get_imgFromSAFE(dict_Global['in']['Master_Image'], dict_Global['in']['Input_Path'])
+        
+        if not master_Image :
+            print(master_Image_base + " not found into given input path " + \
+                  dict_Global['in']['Input_Path'])
+            print("Please check your input path")
+            quit()
+        else :
+            correct = func_utils.check_image_pattern(master_Image_base, mode="S1SM")
+            if not correct : 
+                print("Master image " + master_Image_base + " does not respect naming conventions for S1SM")
+                quit()
+
+        
         master_date = master_Image_base.split("-")[4].split("t")[0]
         pol = master_Image_base.split("-")[3]
         manifest = master_Image.split("measurement")[0]+"/manifest.safe"
diff --git a/python_src/SAR_MultiSlc_IW.py b/python_src/SAR_MultiSlc_IW.py
index ef73fba..aa8e988 100644
--- a/python_src/SAR_MultiSlc_IW.py
+++ b/python_src/SAR_MultiSlc_IW.py
@@ -112,7 +112,22 @@ if __name__ == "__main__":
     start_time = int(dict_Global['in']['Start_Date'])
     end_time = int(dict_Global['in']['End_Date'])
     master_Image_base = dict_Global['in']['Master_Image']
+    
+    func_utils.check_ifExist(dict_Global['in']['Input_Path'])
+
     master_Image = func_utils.get_imgFromSAFE(dict_Global['in']['Master_Image'], dict_Global['in']['Input_Path'])
+
+    if not master_Image :
+        print(master_Image_base + " not found into given input path " + \
+              dict_Global['in']['Input_Path'])
+        print("Please check your input path")
+        quit()
+    else :
+        correct = func_utils.check_image_pattern(master_Image_base, mode="S1IW")
+        if not correct : 
+            print("Master image " + master_Image_base + " does not respect naming conventions for S1IW")
+            quit()
+
     master_date = master_Image_base.split("-")[4].split("t")[0]
     pol = master_Image_base.split("-")[3]
     iw = master_Image_base.split("-")[1]
diff --git a/python_src/utils/func_utils.py b/python_src/utils/func_utils.py
index c42e552..5c7d36b 100644
--- a/python_src/utils/func_utils.py
+++ b/python_src/utils/func_utils.py
@@ -249,6 +249,41 @@ def get_imgFromDir(arg, searchDir="."):
             img = str("".join(img))
             return img
 
+def check_image_pattern(img, mode="S1SM") :
+    """
+        Check pattern for current image. Must be cohetrent according to the sensor and mode
+    """
+
+    correct_pattern = False
+    
+    
+    if mode == "S1SM" :
+        # Mode S1 SM
+        pattern = "".join(["s1.", "-", '\w{1}', '\d', "-slc-", '\w{2}', "-", '\d{8}', "t", '\d{6}', "-", '\d{8}', "t", '\d{6}'])
+        
+        if re.match(pattern, img) :
+            correct_pattern = True
+
+    elif mode == "Cosmo":
+        # Mode Cosmo
+        pattern = "".join(["CSKS", '\d'])
+        
+        if re.match(pattern, img) :
+            pattern_dates = "".join(['\d{14}', "_", '\d{14}'])
+            dates = re.findall(pattern_dates, img)
+            
+            if len(dates) == 1 :
+                correct_pattern = True
+        
+    else :
+        # Mode S1 IW
+        pattern = "".join(["s1.", "-", '\w{2}', '\d', "-slc-", '\w{2}', "-", '\d{8}', "t", '\d{6}', "-", '\d{8}', "t", '\d{6}'])
+        
+        if re.match(pattern, img) :
+            correct_pattern = True
+
+    return correct_pattern
+
 
 def image_envelope(inTIF, outSHP):
     """ 
-- 
GitLab