Skip to content

Commit 6f9476a

Browse files
committed
re-adding the multiscale induced bands to stac_discovery/assets (again), they got lost during the refactor, also adding/modifing tests to catch the new additin of metadta
1 parent 1b41632 commit 6f9476a

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎src/eopf_geozarr/s2_optimization/s2_converter.py‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,13 @@ def simple_root_consolidation(
349349

350350
if dt_input and dt_input.attrs:
351351
# this can be used to add multiscale paths to the stac attributes
352-
# wether we want that or not has to be discussed
353-
# -> For now this data is not added, as we dont want to expose the additional multiscale arrays for users in the stac assets, this comes at the possibility of confusion for users, but we accept that risk
354-
# as users wont need the multiscale, but they are just used for visualisation
355-
# the code is currently commented out, as this discussion is not 100% final yet and changes might apply
356-
357-
# updated_stac_attrs = add_multiscale_pyramids_to_stac_metadata(datasets, dt_input.attrs)
358-
# utils.write_store_root_stac_metadata(
359-
# output_path,
360-
# root_attrs=cast("dict[str, dict[str, Any]]", updated_stac_attrs),
361-
# )
362-
363-
# addition of measurements as its own stac asset in root -> will needto be verified and tested
364-
# likely triggErs addtionial modifications in eopf-stac -> cannot be tested here as eopf-stac is out of scope from this repo
365-
root_attrs = cast("dict[str, dict[str, Any]]", dt_input.attrs)
352+
updated_stac_attrs = add_multiscale_pyramids_to_stac_metadata(datasets, dt_input.attrs)
353+
366354
# Reference the pyramid root group, not the individual levels. That
367355
# group carries the `multiscales` attribute, and the
368356
# `profile=multiscales` media-type parameter tells a consumer to look
369357
# for it there and resolve the levels from the convention itself.
370-
stac = root_attrs.get("stac_discovery")
358+
stac = updated_stac_attrs.get("stac_discovery")
371359
if stac is not None:
372360
reflectance_asset: dict[str, Any] = {
373361
"href": "/measurements/reflectance",
@@ -401,7 +389,9 @@ def simple_root_consolidation(
401389
def add_multiscale_pyramids_to_stac_metadata(
402390
datasets: Mapping[str, object], dt_attributes: dict[Hashable, Any]
403391
) -> dict[Hashable, Any]:
404-
stac_attrs = dt_attributes["stac_discovery"]["assets"]
392+
stac_attrs = dt_attributes.get("stac_discovery", {}).get("assets")
393+
if not stac_attrs:
394+
return dt_attributes
405395

406396
# a bit messy but effective split to get group parent from stac attrs
407397
existing_group_paths = {"/".join(v["href"].split("/")[:-1]) for v in stac_attrs.values()}
@@ -425,8 +415,6 @@ def add_multiscale_pyramids_to_stac_metadata(
425415
else:
426416
log.warning("Found non-dataset object in datasets!", dataset=ds)
427417

428-
# replace attrs
429-
dt_attributes["stac_discovery"]["assets"] = stac_attrs
430418
return dt_attributes
431419

432420

‎tests/test_s2_converter_simplified.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from eopf_geozarr.s2_optimization.s2_converter import (
1717
_validate_s2_input,
18+
add_multiscale_pyramids_to_stac_metadata,
1819
convert_s2_optimized,
1920
initialize_crs_from_dataset,
2021
simple_root_consolidation,
@@ -494,5 +495,44 @@ def test_simple_root_consolidation_adds_reflectance_stac_asset(tmp_path: Path) -
494495
}
495496

496497

498+
def test_add_multiscale_pyramids_to_stac_metadata_adds_missing_levels() -> None:
499+
"""Pyramid levels absent from existing STAC assets get one asset per data variable."""
500+
existing_asset = {"href": "/measurements/reflectance/r10m/b02", "title": "B02"}
501+
attrs: dict = {"stac_discovery": {"assets": {"b02": dict(existing_asset)}}}
502+
datasets = {
503+
# already referenced by an existing asset -> must not be duplicated
504+
"/measurements/reflectance/r10m": xr.Dataset({"b02": (["y", "x"], np.zeros((4, 4)))}),
505+
# new pyramid level -> assets added, spatial_ref skipped
506+
"/measurements/reflectance/r20m": xr.Dataset(
507+
{
508+
"b02": (["y", "x"], np.zeros((2, 2))),
509+
"b05": (["y", "x"], np.zeros((2, 2))),
510+
"spatial_ref": ((), 0),
511+
}
512+
),
513+
"/measurements/reflectance/r60m": None,
514+
}
515+
516+
result = add_multiscale_pyramids_to_stac_metadata(datasets, attrs)
517+
518+
assert isinstance(result, dict)
519+
assert result["stac_discovery"]["assets"] == {
520+
"b02": existing_asset,
521+
"b02_r20m": {"href": "/measurements/reflectance/r20m/b02", "title": "b02_r20m"},
522+
"b05_r20m": {"href": "/measurements/reflectance/r20m/b05", "title": "b05_r20m"},
523+
}
524+
525+
526+
@pytest.mark.parametrize("attrs", [{}, {"stac_discovery": {}}, {"stac_discovery": {"assets": {}}}])
527+
def test_add_multiscale_pyramids_to_stac_metadata_without_assets_is_noop(attrs: dict) -> None:
528+
"""Missing stac_discovery/assets returns the attributes unchanged instead of raising."""
529+
datasets = {
530+
"/measurements/reflectance/r20m": xr.Dataset({"b02": (["y", "x"], np.zeros((2, 2)))})
531+
}
532+
expected = json.loads(json.dumps(attrs))
533+
534+
assert add_multiscale_pyramids_to_stac_metadata(datasets, attrs) == expected
535+
536+
497537
if __name__ == "__main__":
498538
pytest.main([__file__])

0 commit comments

Comments
 (0)