|
2 | 2 | import importlib.resources |
3 | 3 | import json |
4 | 4 | import re |
5 | | -from typing import Dict, List, Optional, Set |
| 5 | +from typing import Dict, Iterable, List, Optional, Set |
6 | 6 |
|
7 | 7 | from openeo import BaseOpenEoException |
8 | | -from openeo.metadata import CollectionMetadata |
9 | 8 | from openeo.processes import ProcessBuilder, array_create, array_modify |
10 | 9 | from openeo.rest.datacube import DataCube |
11 | 10 |
|
@@ -211,6 +210,14 @@ def _callback( |
211 | 210 | return array_create(data=index_values) |
212 | 211 |
|
213 | 212 |
|
| 213 | +def _extract_collection_ids(cube: DataCube) -> Iterable[str]: |
| 214 | + """Determine the collection ids used in load_collection nodes in the process graph.""" |
| 215 | + for node in cube.result_node().walk_nodes(): |
| 216 | + if node.process_id == "load_collection" and isinstance(node.arguments.get("id"), str): |
| 217 | + yield node.arguments["id"] |
| 218 | + # TODO: Possible to determing/guess collection id or name from `load_stac` url (without requesting it)? |
| 219 | + |
| 220 | + |
214 | 221 | def compute_and_rescale_indices( |
215 | 222 | datacube: DataCube, |
216 | 223 | index_dict: dict, |
@@ -270,10 +277,14 @@ def compute_and_rescale_indices( |
270 | 277 | # Automatic band mapping |
271 | 278 | band_mapping = _BandMapping() |
272 | 279 | if platform is None: |
273 | | - if isinstance(datacube.metadata, CollectionMetadata) and datacube.metadata.get("id"): |
274 | | - platform = band_mapping.guess_platform(name=datacube.metadata.get("id")) |
| 280 | + collection_ids = set(_extract_collection_ids(cube=datacube)) |
| 281 | + platform_guesses = set(band_mapping.guess_platform(name=cid) for cid in collection_ids) |
| 282 | + if len(platform_guesses) == 1: |
| 283 | + [platform] = platform_guesses |
275 | 284 | else: |
276 | | - raise BandMappingException("Unable to determine satellite platform from data cube metadata") |
| 285 | + raise BandMappingException( |
| 286 | + f"Unable to guess satellite platform from {collection_ids=} ({platform_guesses=})" |
| 287 | + ) |
277 | 288 | band_to_var = band_mapping.actual_band_name_to_variable_map( |
278 | 289 | platform=platform, band_names=datacube.metadata.band_names |
279 | 290 | ) |
|
0 commit comments