Skip to content

Commit

Permalink
Merge pull request #149 from Open-EO/s1-extraction-fixes-PR2
Browse files Browse the repository at this point in the history
S1 extraction fixes - PR Re-opened
  • Loading branch information
HansVRP authored Sep 4, 2024
2 parents 1110e4a + 386aebf commit 0a6e65c
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 185 deletions.
110 changes: 40 additions & 70 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 functools import partial
from typing import Callable
from typing import Callable, Optional

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,15 +28,18 @@
"vapour-pressure": "AGERA5-VAPOUR",
"wind-speed": "AGERA5-WIND",
}
KNOWN_UNTEMPORAL_COLLECTIONS = ["COPERNICUS_30"]


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

if collection_name == "COPERNICUS_30":
BASE_MAPPING = BASE_DEM_MAPPING
band_mapping = BASE_DEM_MAPPING
elif collection_name == "AGERA5":
BASE_MAPPING = BASE_WEATHER_MAPPING
else:
raise Exception("Please choose a valid collection.")
band_mapping = BASE_WEATHER_MAPPING

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

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

cube = _load_collection(
connection,
bands,
collection_name,
spatial_extent,
temporal_extent,
fetch_type,
**params,
)
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

# # Apply if the collection is a GeoJSON Feature collection
# if isinstance(spatial_extent, GeoJSON):
Expand All @@ -76,12 +90,11 @@ 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":
BASE_MAPPING = BASE_DEM_MAPPING
band_mapping = BASE_DEM_MAPPING
elif collection_name == "AGERA5":
BASE_MAPPING = BASE_WEATHER_MAPPING
else:
raise Exception("Please choose a valid collection.")
band_mapping = BASE_WEATHER_MAPPING

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

cube = rename_bands(cube, BASE_MAPPING)
if band_mapping is not None:
cube = rename_bands(cube, band_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 @@ -152,13 +128,7 @@ def build_generic_extractor(
**params,
) -> CollectionFetcher:
"""Creates a generic extractor adapted to the given backend. Currently only tested with VITO backend"""
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),
)
fetcher = _get_generic_fetcher(collection_name, fetch_type, backend_context.backend)
preprocessor = _get_generic_processor(collection_name, fetch_type)

return CollectionFetcher(backend_context, bands, fetcher, preprocessor, **params)
4 changes: 1 addition & 3 deletions src/openeo_gfmap/fetching/s1.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ 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,
**load_collection_parameters,
**params,
)

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

0 comments on commit 0a6e65c

Please sign in to comment.