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

Conversation

weiji14
Copy link
Contributor

@weiji14 weiji14 commented Mar 27, 2023

Update the kwargs merging logic to have the 'engine' keyword argument from default_kwarg override the one set in the open_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 the xarray-assets STAC extension (in Proposal stage), xref #16

Fixes #17

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.
@weiji14 weiji14 marked this pull request as ready for review March 27, 2023 03:19
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)
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) 😃

@@ -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!

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]>
Copy link
Member

@jsignell jsignell left a 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!

@jsignell jsignell merged commit 4ee9c2a into stac-utils:main Mar 29, 2023
@weiji14 weiji14 deleted the fix/duplicate_engine_kwarg branch March 29, 2023 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: xarray.backends.api.open_dataset() got multiple values for keyword argument 'engine'
2 participants