Skip to content

Commit

Permalink
Fix #142: catch StopIteration in several products
Browse files Browse the repository at this point in the history
  • Loading branch information
jteulade committed May 13, 2024
1 parent d9a85a7 commit 21f21ba
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 23 deletions.
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
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
2 changes: 1 addition & 1 deletion eoreader/products/optical/vhr_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def _get_path(self, filename: str = "", extension: str = "") -> AnyPathType:
else:
prod_path = next(self.path.glob(f"{filename}*.{extension}"))

except (FileNotFoundError, IndexError):
except (FileNotFoundError, IndexError, StopIteration):
LOGGER.warning(
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

0 comments on commit 21f21ba

Please sign in to comment.