Skip to content

Commit

Permalink
Fix opening STAC Assets with xarray:open_kwargs engine field (#18)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
weiji14 authored Mar 29, 2023
1 parent 051d0ac commit 4ee9c2a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ def simple_zarr() -> pystac.Asset:
catalog = pystac_client.Client.open(STAC_URLS["PLANETARY-COMPUTER"])
collection = catalog.get_collection("daymet-daily-hi")
return collection.assets["zarr-abfs"]


@pytest.fixture(scope="module")
def complex_zarr(simple_zarr) -> pystac.Asset:
simple_zarr.extra_fields["xarray:open_kwargs"]["engine"] = "zarr"
return simple_zarr
7 changes: 6 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ def test_to_xarray_reference_file(simple_reference_file):

def test_to_xarray_zarr(simple_zarr):
ds = to_xarray(simple_zarr)
ds
assert ds


def test_to_xarray_zarr_with_open_kwargs_engine(complex_zarr):
ds = to_xarray(complex_zarr)
assert ds
2 changes: 1 addition & 1 deletion xpystac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ def _(obj: pystac.Asset, **kwargs) -> xarray.Dataset:
else:
default_kwargs = {}

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

0 comments on commit 4ee9c2a

Please sign in to comment.