-
Notifications
You must be signed in to change notification settings - Fork 176
Add open_raw_zarr helper
#2668
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
Open
VeckoTheGecko
wants to merge
3
commits into
Parcels-code:main
Choose a base branch
from
VeckoTheGecko:open_raw_zarr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−0
Open
Add open_raw_zarr helper
#2668
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import xarray as xr | ||
| import zarr | ||
| import zarr.storage | ||
| from zarr.abc.store import Store | ||
|
|
||
|
|
||
| def _not_implemented(*args, **kwargs): | ||
| raise NotImplementedError("This function it not implemented") | ||
|
|
||
|
|
||
| def open_raw_zarr(store: Store): | ||
| """Open a Zarr dataset in an Xarray dataset, bypassing Dask.""" | ||
| with xr.open_zarr(store) as ds: | ||
| var_to_dims = {name: var.dims for name, var in ds.variables.items()} | ||
| var_to_attrs = {name: var.attrs for name, var in ds.variables.items()} | ||
| coords = {name: ds[name].variable.load() for name in ds.coords} | ||
| ds_attrs = ds.attrs | ||
|
|
||
| group = zarr.open(store, mode="r") | ||
| assert isinstance(group, zarr.Group) | ||
|
|
||
| data_vars = {} | ||
| for name, array in group.members(): | ||
| if not isinstance(array, zarr.Array): | ||
| raise ValueError("Discovered a zarr.Group in the root group. open_raw_zarr doesn't work with nested groups") | ||
| if name in coords: | ||
| continue | ||
|
|
||
| array.__array_function__ = _not_implemented # trick xarray to prevent coersion to a numpy array | ||
| data_vars[name] = xr.Variable(var_to_dims[name], array, attrs=var_to_attrs[name]) | ||
|
|
||
| return xr.Dataset(data_vars, coords, attrs=ds_attrs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import pytest | ||
| import xarray as xr | ||
| import zarr | ||
|
|
||
| from parcels import open_raw_zarr | ||
| from parcels._datasets.structured.generic import datasets | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("ds", [pytest.param(v, id=k) for k, v in datasets.items()]) | ||
| def test_open_raw_zarr(ds, tmp_path): | ||
| path = tmp_path / "ds.zarr" | ||
| ds.to_zarr(path) | ||
|
|
||
| result = open_raw_zarr(path) | ||
|
|
||
| for k in result.data_vars: | ||
| # tests that the internal representation within Xarray isn't coerced into a numpy array | ||
| assert isinstance(result[k]._variable._data, zarr.Array) | ||
|
|
||
| xr.testing.assert_identical(result.load(), ds) | ||
|
erikvansebille marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.