-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
Update the core if-then logic to only set the 'engine' keyword argument when it is not already set in the `open_kwargs` dict. Included a regression unit test that extends the existing simple_zarr test to ensure that the fix works.
xpystac/core.py
Outdated
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) |
There was a problem hiding this comment.
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:
ds = xarray.open_dataset(obj.href, **default_kwargs, **open_kwargs, **kwargs) | |
ds = xarray.open_dataset(obj.href, **{**default_kwargs, **open_kwargs, **kwargs}) |
There was a problem hiding this comment.
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) 😃
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
Order of priority from lowest to highest is default_kwargs -> open_kwargs -> kwargs. See https://peps.python.org/pep-0448. Co-Authored-By: Julia Signell <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening this!
Update the kwargs merging logic to have the 'engine' keyword argument from
default_kwarg
override the one set in theopen_kwargs
dict. Included 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 thexarray-assets
STAC extension (in Proposal stage), xref #16Fixes #17