From 018fc433d016de20a3502b2e194ef0aaedba2231 Mon Sep 17 00:00:00 2001
From: pmouquet <pascal.mouquet@ird.fr>
Date: Mon, 12 Feb 2024 10:06:35 +0400
Subject: [PATCH] try to correct few possible issues during download

---
 sen2chain/download_eodag.py | 33 ++++++++++++++++++++++++---------
 sen2chain/products.py       |  6 +++++-
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/sen2chain/download_eodag.py b/sen2chain/download_eodag.py
index 4c4b8b8..2070734 100644
--- a/sen2chain/download_eodag.py
+++ b/sen2chain/download_eodag.py
@@ -69,17 +69,24 @@ class S2cEodag:
             end = end, 
             tileIdentifier = self.name,
         )
+
+        logger.info("{} - Searching online products, ref: {}".format(self.name, ref))
         logging.disable(level=logging.WARNING)
-        self.products = self.dag.search_all(
-            **default_search_criteria
-        )
-        logging.disable(logging.NOTSET)
+        try:
+            self.products = self.dag.search_all(
+                **default_search_criteria
+            )
+            logging.disable(logging.NOTSET)
+            
+        except simplejson.errors.JSONDecodeError:
+            logging.disable(logging.NOTSET)
+            logger.info("Issue with this tile search for now, pass")
+            pass            
         
         # if not isinstance(ref, list):
             # ref_string = ref
         # else:
             # ref_string = "specific cloudmask(s) and / or indice(s) product(s)"
-        logger.info("Searching online products, ref: {}".format(ref))
         
         fitered = self.products[:]
         for p in fitered:
@@ -285,6 +292,7 @@ class S2cEodag:
         outputs_prefix: str = None,
         extract: bool = True,
         delete_archive: bool = True,
+        remove_existing_zipfile: bool = True,
     ):
         if not outputs_prefix:
             if "L1C" in product_id.properties['title']:
@@ -294,11 +302,19 @@ class S2cEodag:
             outputs_prefix = str(Path(Config().get(root_path)) / self.name)
         
         setup_logging(verbose = 2)
-
+        
+        if remove_existing_zipfile:
+            zip_file = L1cProduct(product_id.properties["id"]).zipfile
+            if zip_file:
+                logger.info("Found existing zipfile {}".format(zip_file.name))
+                try:
+                    L1cProduct(product_id.properties["id"]).remove(zipfile = True)                                
+                except:
+                    logger.info("Issue with removing {}".format(zip_file))
+                    pass
         if product_id.properties["storageStatus"] == "ONLINE":
             # logging.disable(level=logging.WARNING)
             try:
-                L1cProduct(product_id.properties["id"]).remove(zipfile = True)                                
                 downloaded_path = self.dag.download(
                     product_id, 
                     outputs_prefix = outputs_prefix, 
@@ -308,9 +324,8 @@ class S2cEodag:
                     timeout=0,
                 )
             except:
+                logger.info("Issue with dowloading{}, removing corrupted zipfile".format(product_id.properties["id"]))
                 downloaded_path = None
-                logger.info("Issue with {}, removing corrupted zipfile".format(product_id.properties["id"]))
-                L1cProduct(product_id.properties["id"]).remove(zipfile = True)                                
                 pass
             if downloaded_path:
                 logger.info("dl_path {}".format(downloaded_path))
diff --git a/sen2chain/products.py b/sen2chain/products.py
index 46cf7de..6c36adb 100755
--- a/sen2chain/products.py
+++ b/sen2chain/products.py
@@ -427,7 +427,11 @@ class L1cProduct(Product):
     @property
     def zip_path(self) -> str:
         """Returns zip path"""
-        return self.path.parent / (self.path.stem + ".zip")
+        zip_path = self.path.parent / (self.path.stem + ".zip")
+        if zip_path.exists():
+            return zip_path
+        else:
+            return None
     
     # METADATA
     @property
-- 
GitLab