|
18 | 18 | from collections.abc import Callable, Iterable, Sequence |
19 | 19 | from concurrent.futures import ThreadPoolExecutor |
20 | 20 | from datetime import datetime |
| 21 | +from functools import lru_cache |
| 22 | +from pathlib import Path |
21 | 23 | from threading import Thread |
22 | 24 | from typing import Any |
23 | 25 |
|
| 26 | +import yaml |
24 | 27 | from dateutil.parser import isoparse |
25 | 28 | from eodag import EOProduct |
26 | 29 | from fastapi import HTTPException, status |
@@ -145,6 +148,54 @@ def to_dt(dates: list[str]) -> list[datetime | None]: |
145 | 148 | return fixed_date_dt, start_date_dt, stop_date_dt |
146 | 149 |
|
147 | 150 |
|
| 151 | +@lru_cache |
| 152 | +def map_stac_platform() -> dict: |
| 153 | + """Function used to read and interpret from constellation.yaml""" |
| 154 | + with open(Path(__file__).parent.parent.parent / "config" / "constellation.yaml", encoding="utf-8") as cf: |
| 155 | + return yaml.safe_load(cf) |
| 156 | + |
| 157 | + |
| 158 | +def map_auxip_prip_mission(platform: str, constellation: str) -> tuple[str | None, str | None]: |
| 159 | + """ |
| 160 | + Custom function for ADGS/PRIP, to read constellation mapper and return propper |
| 161 | + values for platform and serial. |
| 162 | + Eodag maps this values to platformShortName, platformSerialIdentifier |
| 163 | +
|
| 164 | + Input: platform = sentinel-1a Output: sentinel-1, A |
| 165 | + Input: platform = sentinel-5P Output: sentinel-5p, None |
| 166 | + Input: constellation = sentinel-1 Output: sentinel-1, None |
| 167 | + """ |
| 168 | + data = map_stac_platform() |
| 169 | + platform_short_name: str | None = None |
| 170 | + platform_serial_identifier: str | None = None |
| 171 | + try: |
| 172 | + if platform: |
| 173 | + config = next(satellite[platform] for satellite in data["satellites"] if platform in satellite) |
| 174 | + platform_short_name = config.get("constellation", None) |
| 175 | + platform_serial_identifier = config.get("serialid", None) |
| 176 | + if constellation: |
| 177 | + if platform_short_name and platform_short_name != constellation: |
| 178 | + # Inconsistent combination of platform / constellation case |
| 179 | + raise HTTPException( |
| 180 | + status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, |
| 181 | + detail="Invalid combination of platform-constellation", |
| 182 | + ) |
| 183 | + if any( |
| 184 | + satellite[list(satellite.keys())[0]]["constellation"] == constellation |
| 185 | + for satellite in data["satellites"] |
| 186 | + ): |
| 187 | + platform_short_name = constellation |
| 188 | + platform_serial_identifier = None |
| 189 | + else: |
| 190 | + raise KeyError |
| 191 | + except (KeyError, IndexError, StopIteration) as exc: |
| 192 | + raise HTTPException( |
| 193 | + status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, |
| 194 | + detail="Cannot map platform/constellation", |
| 195 | + ) from exc |
| 196 | + return platform_short_name, platform_serial_identifier |
| 197 | + |
| 198 | + |
148 | 199 | def odata_to_stac( |
149 | 200 | feature_template: dict, |
150 | 201 | odata_dict: dict, |
|
0 commit comments