Parse the type of Planet data from the directory path.
Parameters:
-
fpath
(pathlib.Path
)
–
The directory path to the Planet data.
Returns:
-
typing.Literal['orthotile', 'scene']
–
Literal["orthotile", "scene"]: The type of Planet data.
Raises:
-
ValueError
–
If the Planet data type cannot be parsed from the file path.
Source code in darts-acquisition/src/darts_acquisition/planet.py
| def parse_planet_type(fpath: Path) -> Literal["orthotile", "scene"]:
"""Parse the type of Planet data from the directory path.
Args:
fpath (Path): The directory path to the Planet data.
Returns:
Literal["orthotile", "scene"]: The type of Planet data.
Raises:
ValueError: If the Planet data type cannot be parsed from the file path.
"""
# Cases for Scenes:
# - YYYYMMDD_HHMMSS_NN_XXXX
# - YYYYMMDD_HHMMSS_XXXX
# Cases for Orthotiles:
# NNNNNNN/NNNNNNN_NNNNNNN_YYYY-MM-DD_XXXX
# NNNNNNN_NNNNNNN_YYYY-MM-DD_XXXX
assert fpath.is_dir(), "fpath must be the parent directory!"
ps_name_parts = fpath.stem.split("_")
if len(ps_name_parts) == 3:
# Must be scene or invalid
date, time, ident = ps_name_parts
if _is_valid_date(date, "%Y%m%d") and _is_valid_date(time, "%H%M%S") and len(ident) == 4:
return "scene"
if len(ps_name_parts) == 4:
# Assume scene
date, time, n, ident = ps_name_parts
if _is_valid_date(date, "%Y%m%d") and _is_valid_date(time, "%H%M%S") and n.isdigit() and len(ident) == 4:
return "scene"
# Is not scene, assume orthotile
chunkid, tileid, date, ident = ps_name_parts
if chunkid.isdigit() and tileid.isdigit() and _is_valid_date(date, "%Y-%m-%d") and len(ident) == 4:
return "orthotile"
raise ValueError(
f"Could not parse Planet data type from {fpath}."
f"Expected a format of YYYYMMDD_HHMMSS_NN_XXXX or YYYYMMDD_HHMMSS_XXXX for scene, "
"or NNNNNNN/NNNNNNN_NNNNNNN_YYYY-MM-DD_XXXX or NNNNNNN_NNNNNNN_YYYY-MM-DD_XXXX for orthotile."
f"Got {fpath.stem} instead."
"Please ensure that the parent directory of the file is used, instead of the file itself."
)
|