-
Notifications
You must be signed in to change notification settings - Fork 4
Pp add warning and tests #147
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eed755f
add a user warning when data is not lazy
valeriupredoi f13b642
add a test module for zarrs
valeriupredoi 3f7cf4a
add tiny Zarr sample data
valeriupredoi 91ce068
add test
valeriupredoi edd6c36
make warning more robust
valeriupredoi 105031f
add Zarr3 test data
valeriupredoi 16fb588
rm erroneous file
valeriupredoi f6c2766
add zarr3 test
valeriupredoi 7846f77
make the warning better
valeriupredoi 2d2c661
full test suite
valeriupredoi 1796100
more general search meth
valeriupredoi 3bcf54f
Add zarr to test dependencies.
pp-mo 2ea08de
Add aiohttp to test deps.
pp-mo 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
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,118 @@ | ||
| """Test conversion of remote and local Zarr store to iris Cube.""" | ||
| from importlib.resources import files as importlib_files | ||
| from pathlib import Path | ||
|
|
||
| import iris | ||
| import pytest | ||
| import xarray as xr | ||
| import ncdata | ||
| import ncdata.iris_xarray | ||
| import zarr | ||
|
|
||
|
|
||
| def _return_kwargs(): | ||
| time_coder = xr.coders.CFDatetimeCoder(use_cftime=True) | ||
| xr_kwargs = { | ||
| "consolidated": True, | ||
| "decode_times": time_coder, | ||
| "engine": "zarr", | ||
| "chunks": {}, | ||
| "backend_kwargs": {}, | ||
| } | ||
|
|
||
| return xr_kwargs | ||
|
|
||
|
|
||
| def _run_checks(cube): | ||
| """Run some standard checks.""" | ||
| assert cube.var_name == "q" | ||
| assert cube.standard_name == "specific_humidity" | ||
| assert cube.long_name is None | ||
| coords = cube.coords() | ||
| coord_names = [coord.standard_name for coord in coords] | ||
| assert "longitude" in coord_names | ||
| assert "latitude" in coord_names | ||
|
|
||
|
|
||
| def test_load_zarr2_local(): | ||
| """Test loading a Zarr2 store from local FS.""" | ||
| zarr_path = ( | ||
| Path(importlib_files("tests")) | ||
| / "zarr-sample-data" | ||
| / "example_field_0.zarr2" | ||
| ) | ||
|
|
||
| xr_kwargs = _return_kwargs() | ||
| zarr_xr = xr.open_dataset(zarr_path, **xr_kwargs) | ||
| zarr_xr.unify_chunks() | ||
|
|
||
| conversion_func = ncdata.iris_xarray.cubes_from_xarray | ||
| cubes = conversion_func(zarr_xr) | ||
|
|
||
| assert len(cubes) == 1 | ||
| cube = cubes[0] | ||
| _run_checks(cube) | ||
|
|
||
|
|
||
| def test_load_zarr3_local(): | ||
| """Test loading a Zarr3 store from local FS.""" | ||
| zarr_path = ( | ||
| Path(importlib_files("tests")) | ||
| / "zarr-sample-data" | ||
| / "example_field_0.zarr3" | ||
| ) | ||
|
|
||
| xr_kwargs = _return_kwargs() | ||
| zarr_xr = xr.open_dataset(zarr_path, **xr_kwargs) | ||
| zarr_xr.unify_chunks() | ||
|
|
||
| conversion_func = ncdata.iris_xarray.cubes_from_xarray | ||
| cubes = conversion_func(zarr_xr) | ||
|
|
||
| assert len(cubes) == 1 | ||
| cube = cubes[0] | ||
| _run_checks(cube) | ||
|
|
||
|
|
||
| def test_load_remote_zarr(): | ||
| """Test loading a remote Zarr store. | ||
|
|
||
| This is a ~250MB compressed Zarr in an S3 bucket. | ||
| Conversion is done fully lazily, by passing chunks={} | ||
| to Xarray loader. Test takes ~3-4s and needs ~400MB res mem. | ||
| """ | ||
| zarr_path = ( | ||
| "https://uor-aces-o.s3-ext.jc.rl.ac.uk/" | ||
| "esmvaltool-zarr/pr_Amon_CNRM-ESM2-1_02Kpd-11_r1i1p2f2_gr_200601-220112.zarr3" | ||
| ) | ||
|
|
||
| xr_kwargs = _return_kwargs() | ||
| zarr_xr = xr.open_dataset(zarr_path, **xr_kwargs) | ||
| zarr_xr.unify_chunks() | ||
|
|
||
| conversion_func = ncdata.iris_xarray.cubes_from_xarray | ||
| cubes = conversion_func(zarr_xr) | ||
|
|
||
| assert isinstance(cubes, iris.cube.CubeList) | ||
| assert len(cubes) == 1 | ||
| assert cubes[0].has_lazy_data() | ||
|
|
||
|
|
||
| def test_load_remote_zarr_realized_data(): | ||
| """Test with the same remote Zarr store but chunks=None.""" | ||
| zarr_path = ( | ||
| "https://uor-aces-o.s3-ext.jc.rl.ac.uk/" | ||
| "esmvaltool-zarr/pr_Amon_CNRM-ESM2-1_02Kpd-11_r1i1p2f2_gr_200601-220112.zarr3" | ||
| ) | ||
|
|
||
| xr_kwargs = _return_kwargs() | ||
| xr_kwargs["chunks"] = None | ||
| zarr_xr = xr.open_dataset(zarr_path, **xr_kwargs) | ||
|
|
||
| conversion_func = ncdata.iris_xarray.cubes_from_xarray | ||
| msg = ( | ||
| "has fully realized data, if you need lazy data, " | ||
| "then add chunks={} as argument to Xarray open_dataset." | ||
| ) | ||
| with pytest.warns(UserWarning, match=msg) as w: | ||
| cubes = conversion_func(zarr_xr) |
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,3 @@ | ||
| { | ||
| "Conventions": "CF-1.12" | ||
| } |
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,3 @@ | ||
| { | ||
| "zarr_format": 2 | ||
| } |
171 changes: 171 additions & 0 deletions
171
tests/zarr-sample-data/example_field_0.zarr2/.zmetadata
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,171 @@ | ||
| { | ||
| "metadata": { | ||
| ".zattrs": { | ||
| "Conventions": "CF-1.12" | ||
| }, | ||
| ".zgroup": { | ||
| "zarr_format": 2 | ||
| }, | ||
| "lat/.zarray": { | ||
| "chunks": [ | ||
| 5 | ||
| ], | ||
| "compressor": { | ||
| "blocksize": 0, | ||
| "clevel": 5, | ||
| "cname": "lz4", | ||
| "id": "blosc", | ||
| "shuffle": 1 | ||
| }, | ||
| "dtype": "<f8", | ||
| "fill_value": "NaN", | ||
| "filters": null, | ||
| "order": "C", | ||
| "shape": [ | ||
| 5 | ||
| ], | ||
| "zarr_format": 2 | ||
| }, | ||
| "lat/.zattrs": { | ||
| "_ARRAY_DIMENSIONS": [ | ||
| "lat" | ||
| ], | ||
| "bounds": "lat_bnds", | ||
| "standard_name": "latitude", | ||
| "units": "degrees_north" | ||
| }, | ||
| "lat_bnds/.zarray": { | ||
| "chunks": [ | ||
| 3, | ||
| 2 | ||
| ], | ||
| "compressor": { | ||
| "blocksize": 0, | ||
| "clevel": 5, | ||
| "cname": "lz4", | ||
| "id": "blosc", | ||
| "shuffle": 1 | ||
| }, | ||
| "dtype": "<f8", | ||
| "fill_value": "NaN", | ||
| "filters": null, | ||
| "order": "C", | ||
| "shape": [ | ||
| 5, | ||
| 2 | ||
| ], | ||
| "zarr_format": 2 | ||
| }, | ||
| "lat_bnds/.zattrs": { | ||
| "_ARRAY_DIMENSIONS": [ | ||
| "lat", | ||
| "bounds2" | ||
| ] | ||
| }, | ||
| "lon/.zarray": { | ||
| "chunks": [ | ||
| 8 | ||
| ], | ||
| "compressor": { | ||
| "blocksize": 0, | ||
| "clevel": 5, | ||
| "cname": "lz4", | ||
| "id": "blosc", | ||
| "shuffle": 1 | ||
| }, | ||
| "dtype": "<f8", | ||
| "fill_value": "NaN", | ||
| "filters": null, | ||
| "order": "C", | ||
| "shape": [ | ||
| 8 | ||
| ], | ||
| "zarr_format": 2 | ||
| }, | ||
| "lon/.zattrs": { | ||
| "_ARRAY_DIMENSIONS": [ | ||
| "lon" | ||
| ], | ||
| "bounds": "lon_bnds", | ||
| "standard_name": "longitude", | ||
| "units": "degrees_east" | ||
| }, | ||
| "lon_bnds/.zarray": { | ||
| "chunks": [ | ||
| 4, | ||
| 2 | ||
| ], | ||
| "compressor": { | ||
| "blocksize": 0, | ||
| "clevel": 5, | ||
| "cname": "lz4", | ||
| "id": "blosc", | ||
| "shuffle": 1 | ||
| }, | ||
| "dtype": "<f8", | ||
| "fill_value": "NaN", | ||
| "filters": null, | ||
| "order": "C", | ||
| "shape": [ | ||
| 8, | ||
| 2 | ||
| ], | ||
| "zarr_format": 2 | ||
| }, | ||
| "lon_bnds/.zattrs": { | ||
| "_ARRAY_DIMENSIONS": [ | ||
| "lon", | ||
| "bounds2" | ||
| ] | ||
| }, | ||
| "q/.zarray": { | ||
| "chunks": [ | ||
| 3, | ||
| 4 | ||
| ], | ||
| "compressor": { | ||
| "blocksize": 0, | ||
| "clevel": 5, | ||
| "cname": "lz4", | ||
| "id": "blosc", | ||
| "shuffle": 1 | ||
| }, | ||
| "dtype": "<f8", | ||
| "fill_value": "NaN", | ||
| "filters": null, | ||
| "order": "C", | ||
| "shape": [ | ||
| 5, | ||
| 8 | ||
| ], | ||
| "zarr_format": 2 | ||
| }, | ||
| "q/.zattrs": { | ||
| "_ARRAY_DIMENSIONS": [ | ||
| "lat", | ||
| "lon" | ||
| ], | ||
| "cell_methods": "area: mean", | ||
| "coordinates": "time", | ||
| "project": "research", | ||
| "standard_name": "specific_humidity", | ||
| "units": "1" | ||
| }, | ||
| "time/.zarray": { | ||
| "chunks": [], | ||
| "compressor": null, | ||
| "dtype": "<f8", | ||
| "fill_value": "NaN", | ||
| "filters": null, | ||
| "order": "C", | ||
| "shape": [], | ||
| "zarr_format": 2 | ||
| }, | ||
| "time/.zattrs": { | ||
| "_ARRAY_DIMENSIONS": [], | ||
| "standard_name": "time", | ||
| "units": "days since 2018-12-01" | ||
| } | ||
| }, | ||
| "zarr_consolidated_format": 1 | ||
| } |
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 @@ | ||
| { | ||
| "chunks": [ | ||
| 5 | ||
| ], | ||
| "compressor": { | ||
| "blocksize": 0, | ||
| "clevel": 5, | ||
| "cname": "lz4", | ||
| "id": "blosc", | ||
| "shuffle": 1 | ||
| }, | ||
| "dtype": "<f8", | ||
| "fill_value": "NaN", | ||
| "filters": null, | ||
| "order": "C", | ||
| "shape": [ | ||
| 5 | ||
| ], | ||
| "zarr_format": 2 | ||
| } |
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,8 @@ | ||
| { | ||
| "_ARRAY_DIMENSIONS": [ | ||
| "lat" | ||
| ], | ||
| "bounds": "lat_bnds", | ||
| "standard_name": "latitude", | ||
| "units": "degrees_north" | ||
| } |
Binary file not shown.
Oops, something went wrong.
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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
this will fix that failing test, and all the almost 1000 tests pass now 😃 The problem is that this method (and the previous approach) are not 100% exhaustive in figuring out which are coords that should have realized data, so a few of these warnings will be raised for those that the check misses them, but beats me how I can create a bulletproof check, given the variety of names and metadata, and differences between Zarr2 and Zarr3 (alas, most of them don't raise the warning)