From a8caa921f711c43c2ab3f776b6f93377309f1524 Mon Sep 17 00:00:00 2001
From: impact <pascal.mouquet@ird.fr>
Date: Thu, 27 Oct 2022 16:35:07 +0400
Subject: [PATCH] cloudcover filtering for l1c download

---
 sen2chain/download_eodag.py | 57 ++++++++++++++++++++++++++-----------
 sen2chain/tiles.py          |  4 ++-
 2 files changed, 44 insertions(+), 17 deletions(-)

diff --git a/sen2chain/download_eodag.py b/sen2chain/download_eodag.py
index 0a05c95..63cbf67 100644
--- a/sen2chain/download_eodag.py
+++ b/sen2chain/download_eodag.py
@@ -72,7 +72,8 @@ class S2cEodag:
         start: str = "2015-01-01",
         end: str = "9999-12-31",
         ref: str = "l1c",
-    
+        min_cloudcover: int = 0,
+        max_cloudcover: int = 100,    
     ):
         ######### ici faudrait virer le self.productType, qui ne doit pas être global pour le download... 
         if productType == "L1C":
@@ -100,23 +101,47 @@ class S2cEodag:
         for p in fitered:
             if self.name not in p.properties["title"]:
                 self.products.remove(p)
-                logger.info("{} - wrong Tile - filtering".format(p.properties["title"]))        
-        
-        local = self.products[:]
-        for p in local:
-            if ref == "l1c":
-                if (outputs_prefix / (p.properties["title"] + ".SAFE")).exists():
-                    # p.location = "file://" + str(outputs_prefix / (p.properties["title"] + ".SAFE"))
-                    logger.info("{} - remote {} - local l1c PRESENT - filtering".format(p.properties["title"], p.properties["storageStatus"]))
-                    self.products.remove(p)
-                else:
-                    logger.info("{} - remote {} - local l1c ABSENT".format(p.properties["title"], p.properties["storageStatus"]))
-            elif ref == "l2a":
-                if (Path(Config().get("l2a_path")) / self.name / (p.properties["title"].replace("L1C_", "L2A_").replace("__OPER__", "_USER_") + ".SAFE")).exists():
-                    logger.info("{} - remote {} - local l2a PRESENT - filtering".format(p.properties["title"], p.properties["storageStatus"]))
+                logger.info("{} - wrong Tile - filtering".format(p.properties["title"]))
+            else:
+                if not(min_cloudcover <= int(p.properties["cloudCover"]) <= max_cloudcover):
                     self.products.remove(p)
+                    logger.info("{} - wrong cloud cover ({}%) - filtering".format(p.properties["title"], int(p.properties["cloudCover"])))
                 else:
-                    logger.info("{} - remote {} - local l2a ABSENT".format(p.properties["title"], p.properties["storageStatus"]))
+                    if ref == "l1c":
+                        if (outputs_prefix / (p.properties["title"] + ".SAFE")).exists():
+                            # p.location = "file://" + str(outputs_prefix / (p.properties["title"] + ".SAFE"))
+                            logger.info("{} - remote {} - local l1c PRESENT - filtering".format(p.properties["title"], p.properties["storageStatus"]))
+                            self.products.remove(p)
+                        else:
+                            logger.info("{} - remote {} - local l1c ABSENT".format(p.properties["title"], p.properties["storageStatus"]))
+                    elif ref == "l2a":
+                        if (Path(Config().get("l2a_path")) / self.name / (p.properties["title"].replace("L1C_", "L2A_").replace("__OPER__", "_USER_") + ".SAFE")).exists():
+                            logger.info("{} - remote {} - local l2a PRESENT - filtering".format(p.properties["title"], p.properties["storageStatus"]))
+                            self.products.remove(p)
+                        else:
+                            logger.info("{} - remote {} - local l2a ABSENT".format(p.properties["title"], p.properties["storageStatus"]))
+                    
+                    
+                      
+        # clouds = self.products[:]
+        # for p in clouds:
+            # if not(min_cloudcover <= int(p.properties["cloudCover"]) <= max_cloudcover):
+                # self.products.remove(p)
+                # logger.info("{} - wrong cloud cover ({}%) - filtering".format(p.properties["title"], int(p.properties["cloudCover"])))        
+        # local = self.products[:]
+        # for p in local:
+            # if ref == "l1c":
+                # if (outputs_prefix / (p.properties["title"] + ".SAFE")).exists():
+                    # logger.info("{} - remote {} - local l1c PRESENT - filtering".format(p.properties["title"], p.properties["storageStatus"]))
+                    # self.products.remove(p)
+                # else:
+                    # logger.info("{} - remote {} - local l1c ABSENT".format(p.properties["title"], p.properties["storageStatus"]))
+            # elif ref == "l2a":
+                # if (Path(Config().get("l2a_path")) / self.name / (p.properties["title"].replace("L1C_", "L2A_").replace("__OPER__", "_USER_") + ".SAFE")).exists():
+                    # logger.info("{} - remote {} - local l2a PRESENT - filtering".format(p.properties["title"], p.properties["storageStatus"]))
+                    # self.products.remove(p)
+                # else:
+                    # logger.info("{} - remote {} - local l2a ABSENT".format(p.properties["title"], p.properties["storageStatus"]))
         logger.info("Search returned {} new product(s) to download".format(len(self.products))) ####### rajouter ici "- dont xx ABSENT - dont xx ONLINE / xx STAGING"
             
     def download(
diff --git a/sen2chain/tiles.py b/sen2chain/tiles.py
index 6b1bcfa..c67a057 100644
--- a/sen2chain/tiles.py
+++ b/sen2chain/tiles.py
@@ -730,13 +730,15 @@ class Tile:
         end: str = "9999-12-31",
         new: bool = False,
         ref: str = "l1c",
+        min_cloudcover: int = 0,
+        max_cloudcover: int = 100,
         order_offline: bool = False,
     ):
         dag = S2cEodag(self.name, provider = provider)
         if new:
             if self.l1c:
                 start = datetime_to_str(self.l1c.last.date + timedelta(days=1), date_format = 'ymd')
-        dag.search(start = start, end = end, ref = ref)
+        dag.search(start = start, end = end, ref = ref, min_cloudcover = min_cloudcover, max_cloudcover = max_cloudcover)
         
         ##############  virer ici de la liste des téléchargements les produits qui ont déjà des l2a ou indices (pour ne pas retélécharger des produits l1c à chaque fois)
         # if ref == "l2a":
-- 
GitLab