From 8020c5b6db4f16f99bc257c5e6e864f2b2edc987 Mon Sep 17 00:00:00 2001 From: BRAUN REMI Date: Tue, 3 Dec 2024 12:08:16 +0100 Subject: [PATCH] FIX: Don't throw an error in case of missing cloud coverage, only a warning and set the cloud coverage to 0 #159 --- CHANGES.md | 1 + eoreader/products/optical/dimap_v2_product.py | 6 ++-- eoreader/products/optical/hls_product.py | 6 ++-- eoreader/products/optical/landsat_product.py | 6 ++-- eoreader/products/optical/maxar_product.py | 5 ++-- eoreader/products/optical/planet_product.py | 29 +++++++++++-------- eoreader/products/optical/s2_e84_product.py | 5 ++-- eoreader/products/optical/s2_product.py | 2 +- eoreader/products/optical/s2_theia_product.py | 8 ++--- 9 files changed, 38 insertions(+), 30 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 847b7914..fdb40f95 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ - FIX: Remove useless `_norm_diff` function `indices.py` - FIX: Add a fallback in case `map-overlay.kml` is not readable for `Sentinel-1` data ([#180](https://github.com/sertit/eoreader/discussions/180),[#182](https://github.com/sertit/eoreader/issues/182)) - FIX: Remove warning about Dask's lock and client +- FIX: Don't throw an error in case of missing cloud coverage, only a warning and set the cloud coverage to 0 [#159](https://github.com/sertit/eoreader/issues/159) - DOC: Update `conf.py` (remove useless hunks and set Sphinx 7 as base) - DOC: Added the [PAZ product guide](https://earth.esa.int/eogateway/documents/20142/37627/PAZ-Image-Products-Guide.pdf) to the PAZ Product documentation instead of the TerraSAR-X one - by @guillemc23 diff --git a/eoreader/products/optical/dimap_v2_product.py b/eoreader/products/optical/dimap_v2_product.py index 9645eced..2cde873c 100644 --- a/eoreader/products/optical/dimap_v2_product.py +++ b/eoreader/products/optical/dimap_v2_product.py @@ -1256,9 +1256,9 @@ def get_cloud_cover(self) -> float: # Get the cloud cover try: cc = float(root.findtext(".//CLOUD_COVERAGE")) - - except TypeError: - raise InvalidProductError("CLOUD_COVERAGE not found in metadata!") + except (InvalidProductError, TypeError): + LOGGER.warning("'CLOUD_COVERAGE' not found in metadata!") + cc = 0 return cc diff --git a/eoreader/products/optical/hls_product.py b/eoreader/products/optical/hls_product.py index fb0b9ae2..2517131c 100644 --- a/eoreader/products/optical/hls_product.py +++ b/eoreader/products/optical/hls_product.py @@ -1013,9 +1013,9 @@ def get_cloud_cover(self) -> float: # Get the cloud cover try: cc = float(root.findtext(".//cloud_coverage")) - - except TypeError: - raise InvalidProductError("'cloud_coverage' not found in metadata!") + except (InvalidProductError, TypeError): + LOGGER.warning("'cloud_coverage' not found in metadata!") + cc = 0 return cc diff --git a/eoreader/products/optical/landsat_product.py b/eoreader/products/optical/landsat_product.py index 5c00b2e0..1299f146 100644 --- a/eoreader/products/optical/landsat_product.py +++ b/eoreader/products/optical/landsat_product.py @@ -1820,9 +1820,9 @@ def get_cloud_cover(self) -> float: # Get the cloud cover try: cc = float(root.findtext(".//CLOUD_COVER")) - - except TypeError: - raise InvalidProductError("CLOUD_COVER not found in metadata!") + except (InvalidProductError, TypeError): + LOGGER.warning("'CLOUD_COVER' not found in metadata!") + cc = 0 return cc diff --git a/eoreader/products/optical/maxar_product.py b/eoreader/products/optical/maxar_product.py index 02c9b326..669fa6a9 100644 --- a/eoreader/products/optical/maxar_product.py +++ b/eoreader/products/optical/maxar_product.py @@ -1275,12 +1275,13 @@ def get_cloud_cover(self) -> float: cc = float(root.findtext(".//CLOUDCOVER")) except TypeError: - raise InvalidProductError("CLOUDCOVER not found in metadata!") + LOGGER.warning("'CLOUDCOVER' not found in metadata!") + cc = 0.0 # Manage the case with cloud_cover == -999.0 # i.e. 17APR05171409-M1BS_R1C1-000000010003_01_P001 (WV04) if cc < 0.0: - cc = None + cc = 0.0 return cc diff --git a/eoreader/products/optical/planet_product.py b/eoreader/products/optical/planet_product.py index 79b90308..7b60d992 100644 --- a/eoreader/products/optical/planet_product.py +++ b/eoreader/products/optical/planet_product.py @@ -1205,20 +1205,25 @@ def get_cloud_cover(self) -> float: root, nsmap = self.read_mtd() # Get the cloud cover - if self.constellation == Constellation.SKY: - try: - cc = float(root.findtext(".//cloud_percent")) + try: + if self.constellation == Constellation.SKY: + try: + cc = float(root.findtext(".//cloud_percent")) - except TypeError: - raise InvalidProductError("cloud_percent not found in metadata!") - else: - try: - cc = float(root.findtext(f".//{nsmap['opt']}cloudCoverPercentage")) + except TypeError: + raise InvalidProductError("'cloud_percent' not found in metadata!") + else: + try: + cc = float(root.findtext(f".//{nsmap['opt']}cloudCoverPercentage")) - except TypeError: - raise InvalidProductError( - "opt:cloudCoverPercentage not found in metadata!" - ) + except TypeError: + raise InvalidProductError( + "'opt:cloudCoverPercentage' not found in metadata!" + ) + + except (InvalidProductError, TypeError) as ex: + LOGGER.warning(ex) + cc = 0 return cc diff --git a/eoreader/products/optical/s2_e84_product.py b/eoreader/products/optical/s2_e84_product.py index ca8c7c9f..11157f6d 100644 --- a/eoreader/products/optical/s2_e84_product.py +++ b/eoreader/products/optical/s2_e84_product.py @@ -751,8 +751,9 @@ def get_cloud_cover(self) -> float: """ try: cc = self.stac_mtd["properties"]["eo:cloud_cover"] - except IndexError: - raise InvalidProductError("cloud_cover not found in metadata!") + except (InvalidProductError, TypeError): + LOGGER.warning("'cloud_cover' not found in metadata!") + cc = 0 return cc diff --git a/eoreader/products/optical/s2_product.py b/eoreader/products/optical/s2_product.py index 06aebd73..767a9e23 100644 --- a/eoreader/products/optical/s2_product.py +++ b/eoreader/products/optical/s2_product.py @@ -1626,7 +1626,7 @@ def get_cloud_cover(self) -> float: cc = float(root.findtext(".//CLOUDY_PIXEL_PERCENTAGE")) except (InvalidProductError, TypeError): LOGGER.warning( - "CLOUDY_PIXEL_PERCENTAGE not found in metadata! Cloud coverage set to 0." + "'CLOUDY_PIXEL_PERCENTAGE' not found in metadata! Cloud coverage set to 0." ) cc = 0 diff --git a/eoreader/products/optical/s2_theia_product.py b/eoreader/products/optical/s2_theia_product.py index dd0b2779..3cfcb3e2 100644 --- a/eoreader/products/optical/s2_theia_product.py +++ b/eoreader/products/optical/s2_theia_product.py @@ -869,10 +869,10 @@ def get_cloud_cover(self) -> float: # Get the cloud cover try: cc = float(root.findtext(".//QUALITY_INDEX[@name='CloudPercent']")) - - except TypeError: - raise InvalidProductError( - "QUALITY_INDEXQUALITY_INDEX name='CloudPercent' not found in metadata!" + except (InvalidProductError, TypeError): + LOGGER.warning( + "'QUALITY_INDEXQUALITY_INDEX name='CloudPercent'' not found in metadata!" ) + cc = 0 return cc