Skip to content

Commit

Permalink
FIX: By default, try to assign a constellation (in a pure dummy way) …
Browse files Browse the repository at this point in the history
…to any `Product` created

FIX: Add ways of knowing if a constellation is a real one or not (i.e. CUSTOM or template such as Maxar)
  • Loading branch information
remi-braun committed Nov 4, 2024
1 parent 3a2d3ff commit dbe4784
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- FIX: Handle ICEYE products with missing quicklook
- FIX: Fix Sentinel-1 name with weird PDFs names (i.e. ending with `.SAFE-report...`)
- FIX: By default, try to assign a constellation (in a pure dummy way) to any `Product` created
- FIX: Add ways of knowing if a constellation is a real one or not (i.e. CUSTOM or template such as Maxar)

## 0.21.6 (2024-10-17)

Expand Down
10 changes: 10 additions & 0 deletions CI/SCRIPTS/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,13 @@ def test_deprecation():
# Check deprecation for resolution keyword
with pytest.deprecated_call():
prod_green1.load(SWIR_1, resolution=2.0, window=window)


def test_constellations():
real_const = Constellation.get_real_constellations()
assert Constellation.SPOT45 not in real_const
assert Constellation.MAXAR not in real_const
assert Constellation.CUSTOM not in real_const

assert Constellation.is_real_constellation(Constellation.S2)
assert not Constellation.is_real_constellation(Constellation.MAXAR)
31 changes: 27 additions & 4 deletions eoreader/products/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ def __init__(
self._mask_false = 0
self._mask_nodata = 255

self.constellation = kwargs.get("constellation")
self.constellation = kwargs.get(
"constellation", self._get_constellation_dummy()
)
"""Product constellation, such as Sentinel-2"""

# Set the resolution, needs to be done when knowing the product type
Expand Down Expand Up @@ -257,6 +259,11 @@ def __init__(
# Constellation and satellite ID
if not self.constellation:
self.constellation = self._get_constellation()
if self.constellation is None:
raise InvalidProductError(
f"Impossible to set a constellation to the given product! {self.name}"
)

self.constellation_id = (
self.constellation
if isinstance(self.constellation, str)
Expand Down Expand Up @@ -418,9 +425,25 @@ def _set_instrument(self) -> None:

@classmethod
def _get_constellation(cls) -> Constellation:
class_module = cls.__module__.split(".")[-1]
constellation_id = class_module.replace("_product", "").upper()
return getattr(Constellation, constellation_id)
return cls._get_constellation_dummy(raise_ex=True)

@classmethod
def _get_constellation_dummy(cls, raise_ex: bool = False) -> Constellation:
try:
class_module = cls.__module__.split(".")[-1]
constellation_id = class_module.replace("_product", "").upper()
const = getattr(Constellation, constellation_id)
except AttributeError as ex:
if raise_ex:
raise ex
else:
const = None

# In Dummy, don't set generic constellations!
if const not in Constellation.get_real_constellations():
const = None

return const

def _get_name(self) -> str:
"""
Expand Down
21 changes: 21 additions & 0 deletions eoreader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,27 @@ class Constellation(ListEnum):
CUSTOM = "CUSTOM"
"""Custom stack"""

@classmethod
def get_real_constellations(cls):
"""
Get only constellations of existing satellite (discard CUSTOM, templates etc)
"""
not_real = [cls.MAXAR, cls.SPOT45, cls.CUSTOM]
return {const for const in cls.__members__.values() if const not in not_real}

@classmethod
def is_real_constellation(cls, const: Constellation):
"""
Is the given constellation a real one?
Args:
const (Constellation): Constellation to check
Returns:
"""
return cls.convert_from(const)[0] in cls.get_real_constellations()


CONSTELLATION_REGEX = {
Constellation.S1: r"S1[ABCD]_(IW|EW|SM|WV|S\d)_(RAW|SLC|GRD|OCN)[FHM_]_[0-2]S[SD][HV]_\d{8}T\d{6}_\d{8}T\d{6}_\d{6}_.{11}(_COG|)",
Expand Down

0 comments on commit dbe4784

Please sign in to comment.