Skip to content

Commit

Permalink
Revert "S1 extraction fixes - PR Re-opened"
Browse files Browse the repository at this point in the history
  • Loading branch information
HansVRP authored Sep 4, 2024
1 parent 0a6e65c commit 6202bbb
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 384 deletions.
110 changes: 70 additions & 40 deletions src/openeo_gfmap/fetching/generic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
""" Generic extraction of features, supporting VITO backend.
"""

from typing import Callable, Optional
from functools import partial
from typing import Callable

import openeo
from openeo.rest import OpenEoApiError

from openeo_gfmap.backend import Backend, BackendContext
from openeo_gfmap.fetching import CollectionFetcher, FetchType, _log
Expand All @@ -28,18 +28,15 @@
"vapour-pressure": "AGERA5-VAPOUR",
"wind-speed": "AGERA5-WIND",
}
KNOWN_UNTEMPORAL_COLLECTIONS = ["COPERNICUS_30"]


def _get_generic_fetcher(
collection_name: str, fetch_type: FetchType, backend: Backend
) -> Callable:
band_mapping: Optional[dict] = None

def _get_generic_fetcher(collection_name: str, fetch_type: FetchType) -> Callable:
if collection_name == "COPERNICUS_30":
band_mapping = BASE_DEM_MAPPING
BASE_MAPPING = BASE_DEM_MAPPING
elif collection_name == "AGERA5":
band_mapping = BASE_WEATHER_MAPPING
BASE_MAPPING = BASE_WEATHER_MAPPING
else:
raise Exception("Please choose a valid collection.")

def generic_default_fetcher(
connection: openeo.Connection,
Expand All @@ -48,34 +45,23 @@ def generic_default_fetcher(
bands: list,
**params,
) -> openeo.DataCube:
if band_mapping is not None:
bands = convert_band_names(bands, band_mapping)
bands = convert_band_names(bands, BASE_MAPPING)

if (collection_name in KNOWN_UNTEMPORAL_COLLECTIONS) and (
temporal_extent is not None
):
if (collection_name == "COPERNICUS_30") and (temporal_extent is not None):
_log.warning(
"Ignoring the temporal extent provided by the user as the collection %s is known to be untemporal.",
collection_name,
"User set-up non None temporal extent for DEM collection. Ignoring it."
)
temporal_extent = None

try:
cube = _load_collection(
connection,
bands,
collection_name,
spatial_extent,
temporal_extent,
fetch_type,
**params,
)
except OpenEoApiError as e:
if "CollectionNotFound" in str(e):
raise ValueError(
f"Collection {collection_name} not found in the selected backend {backend.value}."
) from e
raise e
cube = _load_collection(
connection,
bands,
collection_name,
spatial_extent,
temporal_extent,
fetch_type,
**params,
)

# # Apply if the collection is a GeoJSON Feature collection
# if isinstance(spatial_extent, GeoJSON):
Expand All @@ -90,11 +76,12 @@ def _get_generic_processor(collection_name: str, fetch_type: FetchType) -> Calla
"""Builds the preprocessing function from the collection name as it stored
in the target backend.
"""
band_mapping: Optional[dict] = None
if collection_name == "COPERNICUS_30":
band_mapping = BASE_DEM_MAPPING
BASE_MAPPING = BASE_DEM_MAPPING
elif collection_name == "AGERA5":
band_mapping = BASE_WEATHER_MAPPING
BASE_MAPPING = BASE_WEATHER_MAPPING
else:
raise Exception("Please choose a valid collection.")

def generic_default_processor(cube: openeo.DataCube, **params):
"""Default collection preprocessing method for generic datasets.
Expand All @@ -112,14 +99,51 @@ def generic_default_processor(cube: openeo.DataCube, **params):
if collection_name == "COPERNICUS_30":
cube = cube.min_time()

if band_mapping is not None:
cube = rename_bands(cube, band_mapping)
cube = rename_bands(cube, BASE_MAPPING)

return cube

return generic_default_processor


OTHER_BACKEND_MAP = {
"AGERA5": {
Backend.TERRASCOPE: {
"fetch": partial(_get_generic_fetcher, collection_name="AGERA5"),
"preprocessor": partial(_get_generic_processor, collection_name="AGERA5"),
},
Backend.CDSE: {
"fetch": partial(_get_generic_fetcher, collection_name="AGERA5"),
"preprocessor": partial(_get_generic_processor, collection_name="AGERA5"),
},
Backend.FED: {
"fetch": partial(_get_generic_fetcher, collection_name="AGERA5"),
"preprocessor": partial(_get_generic_processor, collection_name="AGERA5"),
},
},
"COPERNICUS_30": {
Backend.TERRASCOPE: {
"fetch": partial(_get_generic_fetcher, collection_name="COPERNICUS_30"),
"preprocessor": partial(
_get_generic_processor, collection_name="COPERNICUS_30"
),
},
Backend.CDSE: {
"fetch": partial(_get_generic_fetcher, collection_name="COPERNICUS_30"),
"preprocessor": partial(
_get_generic_processor, collection_name="COPERNICUS_30"
),
},
Backend.FED: {
"fetch": partial(_get_generic_fetcher, collection_name="COPERNICUS_30"),
"preprocessor": partial(
_get_generic_processor, collection_name="COPERNICUS_30"
),
},
},
}


def build_generic_extractor(
backend_context: BackendContext,
bands: list,
Expand All @@ -128,7 +152,13 @@ def build_generic_extractor(
**params,
) -> CollectionFetcher:
"""Creates a generic extractor adapted to the given backend. Currently only tested with VITO backend"""
fetcher = _get_generic_fetcher(collection_name, fetch_type, backend_context.backend)
preprocessor = _get_generic_processor(collection_name, fetch_type)
backend_functions = OTHER_BACKEND_MAP.get(collection_name).get(
backend_context.backend
)

fetcher, preprocessor = (
backend_functions["fetch"](fetch_type=fetch_type),
backend_functions["preprocessor"](fetch_type=fetch_type),
)

return CollectionFetcher(backend_context, bands, fetcher, preprocessor, **params)
4 changes: 3 additions & 1 deletion src/openeo_gfmap/fetching/s1.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ def s1_grd_fetch_default(
"""
bands = convert_band_names(bands, BASE_SENTINEL1_GRD_MAPPING)

load_collection_parameters = params.get("load_collection", {})

cube = _load_collection(
connection,
bands,
collection_name,
spatial_extent,
temporal_extent,
fetch_type,
**params,
**load_collection_parameters,
)

if fetch_type is not FetchType.POINT and isinstance(spatial_extent, GeoJSON):
Expand Down
Loading

0 comments on commit 6202bbb

Please sign in to comment.