Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #142: catch StopIteration and return InvalidProductError #143

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.21.2 (2024-mm-dd)

- FIX: `Sentinel-2` product with StopIteration error ([#142](https://github.com/sertit/eoreader/issues/142))
- FIX: Fix `Maxar` product with `QB02` satellite ID ([#140](https://github.com/sertit/eoreader/issues/140))
- ENH: `to_str` and `to_band`: add a `as_list` argument defaulting to `True`. When set as False, return a str from `to_str` and a band from `to_band` ([#138](https://github.com/sertit/eoreader/issues/138)). Thanks @jsetty!
- FIX: Fix issue with geocoding with unzipped `Sentinel-3 OLCI` product ([#137](https://github.com/sertit/eoreader/issues/137))
Expand Down
15 changes: 10 additions & 5 deletions eoreader/products/optical/landsat_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,11 +1007,16 @@ def _read_mtd(self, force_pd=False) -> (etree._Element, dict):
# FOR COLLECTION 1 AND 2
tar_ds = None
try:
mtd_path = next(self.path.glob(f"**/*{mtd_name}"))
except ValueError:
mtd_path = next(self.path.glob(f"*{mtd_name}"))

if not mtd_path.is_file():
try:
mtd_path = next(self.path.glob(f"**/*{mtd_name}"))
except ValueError:
mtd_path = next(self.path.glob(f"*{mtd_name}"))

if not mtd_path.is_file():
raise InvalidProductError(
f"No metadata file found in {self.name} !"
)
except StopIteration:
raise InvalidProductError(
f"No metadata file found in {self.name} !"
)
Expand Down
2 changes: 1 addition & 1 deletion eoreader/products/optical/s2_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def _get_name_constellation_specific(self) -> str:
next(self.path.glob("**/tileInfo.json")), print_file=False
)
name = tile_info["productName"]
except json.JSONDecodeError:
except (json.JSONDecodeError, StopIteration):
raise InvalidProductError(
f"Corrupted metadata and bad filename for {self.path}! "
f"Impossible to process this product."
Expand Down
7 changes: 6 additions & 1 deletion eoreader/products/optical/s3_olci_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,12 @@ def get_raw_band_paths(self, **kwargs) -> dict:
if self.is_archived:
raw_path = path.get_archived_path(self.path, f".*{filename}")
else:
raw_path = next(self.path.glob(f"*{filename}"))
try:
raw_path = next(self.path.glob(f"*{filename}"))
except StopIteration:
raise FileNotFoundError(
f"Non existing file {filename} in {self.path}"
)

raw_band_paths[band] = raw_path

Expand Down
5 changes: 4 additions & 1 deletion eoreader/products/optical/s3_slstr_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,10 @@ def _get_raw_band_path(self, band: BandNames, **kwargs) -> AnyPathType:
if self.is_archived:
raw_path = path.get_archived_path(self.path, f".*{filename}*")
else:
raw_path = next(self.path.glob(f"*{filename}*"))
try:
raw_path = next(self.path.glob(f"*{filename}*"))
except StopIteration:
raise FileNotFoundError(f"Non existing file {filename} in {self.path}")

return raw_path

Expand Down
5 changes: 4 additions & 1 deletion eoreader/products/optical/sv1_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ def footprint(self) -> gpd.GeoDataFrame:
if self.is_archived:
footprint = vectors.read(self.path, archive_regex=r".*\.shp")
else:
footprint = vectors.read(next(self.path.glob("*.shp")))
try:
footprint = vectors.read(next(self.path.glob("*.shp")))
except StopIteration:
raise FileNotFoundError(f"Non existing file *.shp in {self.path}")

return footprint.to_crs(self.crs())

Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/vhr_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ def _get_path(self, filename: str = "", extension: str = "") -> AnyPathType:
else:
prod_path = next(self.path.glob(f"{filename}*.{extension}"))

except (FileNotFoundError, IndexError):
LOGGER.warning(
except (FileNotFoundError, IndexError, StopIteration):
raise InvalidProductError(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise an exception instead of LOGGER. To confirm with @remi-braun

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lessgo 🚀

f"No file corresponding to *{filename}*.{extension} found in {self.path}"
)

Expand Down
21 changes: 18 additions & 3 deletions eoreader/products/sar/capella_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,22 @@ def _post_init(self, **kwargs) -> None:
(setting product-type, band names and so on)
"""
# Private attributes
self.snap_filename = str(next(self.path.glob("*CAPELLA*.json")).name)
try:
self.snap_filename = str(next(self.path.glob("*CAPELLA*.json")).name)
except StopIteration:
raise FileNotFoundError(f"Non existing file *CAPELLA*.json in {self.path}")
try:
self._raw_band_regex = str(next(self.path.glob(f"{self.name}.tif")).name)
except StopIteration:
# For SICD and SIDD
self._raw_band_regex = str(next(self.path.glob(f"{self.name}.ntf")).name)
try:
self._raw_band_regex = str(
next(self.path.glob(f"{self.name}.ntf")).name
)
except StopIteration:
raise FileNotFoundError(
f"Non existing file {self.name}.tif or {self.name}.ntf in {self.path}"
)

# Post init done by the super class
super()._post_init(**kwargs)
Expand All @@ -235,7 +245,12 @@ def wgs84_extent(self) -> gpd.GeoDataFrame:
gpd.GeoDataFrame: WGS84 extent as a gpd.GeoDataFrame
"""
if self._has_stac_mtd:
mtd_file = next(self.path.glob(f"{self.name}.json"))
try:
mtd_file = next(self.path.glob(f"{self.name}.json"))
except StopIteration:
raise FileNotFoundError(
f"Non existing file {self.name}.json in {self.path}"
)
extent = vectors.read(mtd_file)
else:
extent = None
Expand Down
17 changes: 11 additions & 6 deletions eoreader/products/sar/iceye_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,17 @@ def _post_init(self, **kwargs) -> None:
(setting product-type, band names and so on)
"""
# Private attributes
if self._use_slc:
self.snap_filename = str(next(self.path.glob("*ICEYE*SLC*.xml")).name)
self._raw_band_regex = "*ICEYE*SLC*.h5"
else:
self.snap_filename = str(next(self.path.glob("*ICEYE*GRD*.xml")).name)
self._raw_band_regex = "*ICEYE*GRD*.tif"
try:
if self._use_slc:
self.snap_filename = str(next(self.path.glob("*ICEYE*SLC*.xml")).name)
self._raw_band_regex = "*ICEYE*SLC*.h5"
else:
self.snap_filename = str(next(self.path.glob("*ICEYE*GRD*.xml")).name)
self._raw_band_regex = "*ICEYE*GRD*.tif"
except StopIteration:
raise FileNotFoundError(
f"Non existing file *ICEYE*SLC*.xml or *ICEYE*GRD*.xml in {self.path}"
)

# Post init done by the super class
super()._post_init(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion eoreader/products/sar/rs2_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def wgs84_extent(self) -> gpd.GeoDataFrame:
else:
extent_file = next(self.path.glob("*product.kml"))
product_kml = vectors.read(extent_file)
except IndexError as ex:
except (IndexError, StopIteration) as ex:
raise InvalidProductError(
f"Extent file (product.kml) not found in {self.path}"
) from ex
Expand Down
5 changes: 4 additions & 1 deletion eoreader/products/sar/s1_rtc_asf_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ def footprint(self) -> gpd.GeoDataFrame:
if self.is_archived:
footprint = vectors.read(self.path, archive_regex=r".*\.shp")
else:
footprint = vectors.read(next(self.path.glob("*.shp")))
try:
footprint = vectors.read(next(self.path.glob("*.shp")))
except StopIteration:
raise FileNotFoundError(f"Non existing file *.shp in {self.path}")

return footprint

Expand Down
7 changes: 5 additions & 2 deletions eoreader/products/sar/saocom_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def wgs84_extent(self) -> gpd.GeoDataFrame:
# else:
# try:
# extent_file = next(self.path.glob("**/Images/*.kml"))
# except IndexError as ex:
# except (IndexError, StopIteration) as ex:
# raise InvalidProductError(
# f"Extent file (products.kml) not found in {self.path}"
# ) from ex
Expand Down Expand Up @@ -286,7 +286,10 @@ def get_raw_band_paths(self, **kwargs) -> dict:
dict: Dictionary containing the path of every band existing in the raw products
"""
extended_fmt = _ExtendedFormatter()
cuss_file = next(self.path.glob("*.zip"))
try:
cuss_file = next(self.path.glob("*.zip"))
except StopIteration:
raise FileNotFoundError(f"Non existing file *.zip in {self.path}")
band_paths = {}
for band in SarBandNames.speckle_list():
band_regex = extended_fmt.format(self._raw_band_regex, band.value)
Expand Down
2 changes: 1 addition & 1 deletion eoreader/products/sar/tsx_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def wgs84_extent(self) -> gpd.GeoDataFrame:
# Open extent KML file
try:
extent_file = next(self.path.glob("**/*SUPPORT/GEARTH_POLY.kml"))
except IndexError as ex:
except (IndexError, StopIteration) as ex:
raise InvalidProductError(
f"Extent file (products.kml) not found in {self.path}"
) from ex
Expand Down