Skip to content

Commit

Permalink
refactor(dataset): extract common code into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wvenialbo committed Oct 25, 2024
1 parent 540750e commit 98df34a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 64 deletions.
39 changes: 39 additions & 0 deletions src/GOES_DL/dataset/base/locator_gg.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,42 @@ def timestamp_to_datetime(self, timestamp: str) -> datetime:
)

return file_date.astimezone(timezone.utc)

@staticmethod
def _validate_entity(
name: str, entity: str, available_entities: dict[str, str] | set[str]
) -> None:
if entity not in available_entities:
supported_entities: list[str] = sorted(available_entities)
raise ValueError(
f"Invalid {name} ID: '{entity}'. "
f"Available {name} IDs: {supported_entities}"
)

@classmethod
def _validate_instrument(
cls, instrument: str, available_instruments: dict[str, str]
) -> None:
cls._validate_entity("instrument", instrument, available_instruments)

@classmethod
def _validate_level(cls, level: str, available_levels: set[str]) -> None:
cls._validate_entity("level", level, available_levels)

@classmethod
def _validate_origin(
cls, origin: str, available_origins: dict[str, str]
) -> None:
cls._validate_entity("origin", origin, available_origins)

@classmethod
def _validate_product(
cls, name: str, available_products: dict[str, str]
) -> None:
cls._validate_entity("product", name, available_products)

@classmethod
def _validate_scene(
cls, scene: str, available_scenes: dict[str, str]
) -> None:
cls._validate_entity("scene", scene, available_scenes)
25 changes: 3 additions & 22 deletions src/GOES_DL/dataset/goes/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,28 +214,9 @@ def __init__(
# TODO: Too many positional arguments. Solve it by using
# the Builder or Factory methods, or patterns like
# Essence or Fluent.
if origin not in self.AVAILABLE_ORIGINS:
available_origins: list[str] = sorted(self.AVAILABLE_ORIGINS)
raise ValueError(
f"Invalid origin ID: '{origin}'. "
f"Available origin IDs: {available_origins}"
)

if instrument not in self.AVAILABLE_INSTRUMENTS:
available_instruments: list[str] = sorted(
self.AVAILABLE_INSTRUMENTS
)
raise ValueError(
f"Invalid instrument ID: '{instrument}'. "
f"Available instrument IDs: {available_instruments}"
)

if level not in self.AVAILABLE_LEVELS:
available_levels: list[str] = sorted(self.AVAILABLE_LEVELS)
raise ValueError(
f"Invalid level ID: '{level}'. "
f"Available level IDs: {available_levels}"
)
self._validate_origin(origin, self.AVAILABLE_ORIGINS)
self._validate_instrument(instrument, self.AVAILABLE_INSTRUMENTS)
self._validate_level(level, self.AVAILABLE_LEVELS)

self.name: str = name
self.level: str = level
Expand Down
8 changes: 2 additions & 6 deletions src/GOES_DL/dataset/goes/locator_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ def __init__(
# TODO: Too many positional arguments. Solve it by using
# the Builder or Factory methods, or patterns like
# Essence or Fluent.
if scene not in self.AVAILABLE_SCENES:
available_scenes: list[str] = sorted(self.AVAILABLE_SCENES)
raise ValueError(
f"Invalid scene ID: '{scene}'. "
f"Available scene IDs: {available_scenes}"
)
self._validate_scene(scene, self.AVAILABLE_SCENES)

scan_modes: list[str] = (
self.F_MODES if scene == self.FULL_DISK else self.CM_MODES
)
Expand Down
7 changes: 1 addition & 6 deletions src/GOES_DL/dataset/goes/locator_dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ def __init__(
ValueError
If the provided product name is invalid.
"""
if name not in self.AVAILABLE_PRODUCTS:
supported_products: list[str] = sorted(self.AVAILABLE_PRODUCTS)
raise ValueError(
f"Invalid product ID: '{name}'. "
f"Available product IDs: {supported_products}"
)
self._validate_product(name, self.AVAILABLE_PRODUCTS)

if isinstance(channels, str):
channels = [channels]
Expand Down
7 changes: 1 addition & 6 deletions src/GOES_DL/dataset/goes/locator_dp.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,7 @@ def __init__(self, name: str, scene: str, origin: str) -> None:
dataset directories are organised, only a single origin may
be provided.
"""
if name not in self.AVAILABLE_PRODUCTS:
supported_products: list[str] = sorted(self.AVAILABLE_PRODUCTS)
raise ValueError(
f"Invalid product ID: '{name}'. "
f"Available product IDs: {supported_products}"
)
self._validate_product(name, self.AVAILABLE_PRODUCTS)

only_in_segment: list[set[str]] = [
self.ONLY_CF_SCENE,
Expand Down
7 changes: 1 addition & 6 deletions src/GOES_DL/dataset/goes/locator_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ def __init__(self, name: str, origin: str) -> None:
ValueError
If the provided product name is invalid.
"""
if name not in self.AVAILABLE_PRODUCTS:
available_products: list[str] = sorted(self.AVAILABLE_PRODUCTS)
raise ValueError(
f"Invalid product ID: '{name}'. "
f"Available product IDs: {available_products}"
)
self._validate_product(name, self.AVAILABLE_PRODUCTS)

super().__init__(
name=name,
Expand Down
7 changes: 1 addition & 6 deletions src/GOES_DL/dataset/goes/locator_pm.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ def __init__(self, name: str, scene: str, origin: str) -> None:
dataset directories are organised, only a single origin may
be provided.
"""
if name not in self.AVAILABLE_PRODUCTS:
supported_products: list[str] = sorted(self.AVAILABLE_PRODUCTS)
raise ValueError(
f"Invalid product ID: '{name}'. "
f"Available product IDs: {supported_products}"
)
self._validate_product(name, self.AVAILABLE_PRODUCTS)

super().__init__(
name=name,
Expand Down
7 changes: 1 addition & 6 deletions src/GOES_DL/dataset/goes/locator_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ def __init__(
f"Supported channels: {supported_channels}"
)

if name not in self.AVAILABLE_PRODUCTS:
supported_products: list[str] = sorted(self.AVAILABLE_PRODUCTS)
raise ValueError(
f"Invalid product ID: '{name}'. "
f"Available product IDs: {supported_products}"
)
self._validate_product(name, self.AVAILABLE_PRODUCTS)

level: str = (
self.LEVEL_RAD if name == self.PRODUCT_RAD else self.LEVEL_NOT_RAD
Expand Down
7 changes: 1 addition & 6 deletions src/GOES_DL/dataset/gridsat/locator_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,7 @@ def __init__(
ValueError
If the provided origin, scene, or version is invalid.
"""
if scene not in self.AVAILABLE_SCENES:
available_scenes: list[str] = sorted(self.AVAILABLE_SCENES)
raise ValueError(
f"Invalid scene ID: '{scene}'. "
f"Available scene IDs: {available_scenes}"
)
self._validate_scene(scene, self.AVAILABLE_SCENES)

if isinstance(origins, str):
origins = [origins]
Expand Down

0 comments on commit 98df34a

Please sign in to comment.