Skip to content

made load_stac nicer to mismatch in band names #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `load_stac` will now take over the requested band names if there is a mismatch/problem with the extracted collection metadata ([#752](https://github.com/Open-EO/openeo-python-client/issues/752))

### Removed

### Fixed
Expand Down
10 changes: 10 additions & 0 deletions openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ def rename_labels(self, target, source) -> Dimension:
def rename(self, name) -> Dimension:
return BandDimension(name=name, bands=self.bands)

def contains_band(self, band: Union[int, str]) -> bool:
"""
Check if the given band name or index is present in the dimension.
"""
try:
self.band_index(band)
return True
except ValueError:
return False


class GeometryDimension(Dimension):
# TODO: how to model/store labels of geometry dimension?
Expand Down
19 changes: 16 additions & 3 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,23 @@ def load_stac(
graph = PGNode("load_stac", arguments=arguments)
try:
metadata = metadata_from_stac(url)
# TODO: also apply spatial/temporal filters to metadata?

if isinstance(bands, list):
# TODO: also apply spatial/temporal filters to metadata?
metadata = metadata.filter_bands(band_names=bands)
except Exception:
if not metadata.has_band_dimension():
metadata = metadata.add_dimension(
name="bands",
type="bands",
label=None,
)
if all(metadata.band_dimension.contains_band(b) for b in bands):
metadata = metadata.filter_bands(band_names=bands)
else:
logging.warning(
"Some bands are not available in the collection metadata. Using requested bands as is."
)
metadata = metadata.rename_labels(dimension="bands", target=bands)
except Exception as e:
log.warning(f"Failed to extract cube metadata from STAC URL {url}", exc_info=True)
metadata = None
return cls(graph=graph, connection=connection, metadata=metadata)
Expand Down
14 changes: 13 additions & 1 deletion tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2957,7 +2957,7 @@ def test_load_stac_no_cube_extension_temporal_dimension(self, con120, tmp_path,
cube = con120.load_stac(str(stac_path))
assert cube.metadata.temporal_dimension == TemporalDimension(name="t", extent=dim_extent)

def test_load_stac_band_filtering(self, con120, tmp_path):
def test_load_stac_band_filtering(self, con120, tmp_path, caplog):
stac_path = tmp_path / "stac.json"
stac_data = StacDummyBuilder.collection(
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
Expand All @@ -2971,6 +2971,18 @@ def test_load_stac_band_filtering(self, con120, tmp_path):
cube = con120.load_stac(str(stac_path), bands=["B03", "B02"])
assert cube.metadata.band_names == ["B03", "B02"]

caplog.set_level(logging.WARNING)
# Test with non-existing bands in the collection metadata
cube = con120.load_stac(str(stac_path), bands=["B04"])
assert cube.metadata.band_names == ["B04"]
assert "Some bands are not available in the collection metadata. Using requested bands as is." in caplog.text
caplog.clear()

cube = con120.load_stac(str(stac_path), bands=["B03", "B04"])
assert cube.metadata.band_names == ["B03", "B04"]
assert "Some bands are not available in the collection metadata. Using requested bands as is." in caplog.text
caplog.clear()

@pytest.mark.parametrize(
"bands",
[
Expand Down
Loading