Skip to content
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

Fix opening STAC Assets with xarray:open_kwargs engine field #18

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!



def test_to_xarray_zarr_with_open_kwargs_engine(complex_zarr):
ds = to_xarray(complex_zarr)
assert ds
16 changes: 8 additions & 8 deletions xpystac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ def _(obj: pystac.Asset, **kwargs) -> xarray.Dataset:
default_kwargs = {"engine": "zarr", "consolidated": False, "chunks": {}}
return xarray.open_dataset(mapper, **default_kwargs, **open_kwargs, **kwargs)

if obj.media_type == pystac.MediaType.COG:
_import_optional_dependency("rioxarray")
default_kwargs = {"engine": "rasterio"}
elif obj.media_type == "application/vnd+zarr":
_import_optional_dependency("zarr")
default_kwargs = {"engine": "zarr"}
else:
default_kwargs = {}
default_kwargs = {}
if open_kwargs.get("engine") is None:
if obj.media_type == pystac.MediaType.COG:
_import_optional_dependency("rioxarray")
default_kwargs["engine"] = "rasterio"
elif obj.media_type == "application/vnd+zarr":
_import_optional_dependency("zarr")
default_kwargs["engine"] = "zarr"

ds = xarray.open_dataset(obj.href, **default_kwargs, **open_kwargs, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making the changes above, we can just change this one line:

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

Copy link
Contributor Author

@weiji14 weiji14 Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's a nice and simple solution! I had to look up dictionary unpacking in https://peps.python.org/pep-0448, and check how the duplicate keys will be handled:

The keys in a dictionary remain in a right-to-left priority order, so {**{'a': 1}, 'a': 2, **{'a': 3}} evaluates to {'a': 3}. There is no restriction on the number or position of unpackings.

So the user-set kwargs should override the open_kwargs set in the STAC metadata, which in-turn should override the default_kwargs set by xpystac, following the logic mentioned in #16 (comment) 😃

return ds