Skip to content

Commit 4ee9c2a

Browse files
authored
Fix opening STAC Assets with xarray:open_kwargs engine field (#18)
Update the kwargs merging logic to have cascading priority where `default_kwargs` is overridden by `open_kwargs` which is overridden by user provided `kwargs` . Includes a regression unit test that extends the existing simple_zarr test to ensure that the fix for duplicate keys works. Note that the `xarray:open_kwargs` field is a part of the [`xarray-assets`](https://github.com/stac-extensions/xarray-assets/tree/v1.0.0) STAC extension (in Proposal stage), xref #16 Fixes #17
1 parent 051d0ac commit 4ee9c2a

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ def simple_zarr() -> pystac.Asset:
4949
catalog = pystac_client.Client.open(STAC_URLS["PLANETARY-COMPUTER"])
5050
collection = catalog.get_collection("daymet-daily-hi")
5151
return collection.assets["zarr-abfs"]
52+
53+
54+
@pytest.fixture(scope="module")
55+
def complex_zarr(simple_zarr) -> pystac.Asset:
56+
simple_zarr.extra_fields["xarray:open_kwargs"]["engine"] = "zarr"
57+
return simple_zarr

tests/test_core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ def test_to_xarray_reference_file(simple_reference_file):
3030

3131
def test_to_xarray_zarr(simple_zarr):
3232
ds = to_xarray(simple_zarr)
33-
ds
33+
assert ds
34+
35+
36+
def test_to_xarray_zarr_with_open_kwargs_engine(complex_zarr):
37+
ds = to_xarray(complex_zarr)
38+
assert ds

xpystac/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ def _(obj: pystac.Asset, **kwargs) -> xarray.Dataset:
6464
else:
6565
default_kwargs = {}
6666

67-
ds = xarray.open_dataset(obj.href, **default_kwargs, **open_kwargs, **kwargs)
67+
ds = xarray.open_dataset(obj.href, **{**default_kwargs, **open_kwargs, **kwargs})
6868
return ds

0 commit comments

Comments
 (0)