Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
eed755f
add a user warning when data is not lazy
valeriupredoi Aug 8, 2025
f13b642
add a test module for zarrs
valeriupredoi Aug 8, 2025
3f7cf4a
add tiny Zarr sample data
valeriupredoi Aug 8, 2025
91ce068
add test
valeriupredoi Aug 8, 2025
edd6c36
make warning more robust
valeriupredoi Aug 8, 2025
105031f
add Zarr3 test data
valeriupredoi Aug 8, 2025
16fb588
rm erroneous file
valeriupredoi Aug 8, 2025
f6c2766
add zarr3 test
valeriupredoi Aug 8, 2025
7846f77
make the warning better
valeriupredoi Aug 8, 2025
2d2c661
full test suite
valeriupredoi Aug 8, 2025
1796100
more general search meth
valeriupredoi Aug 8, 2025
ba10ec7
add extra deps and trigger GHA locally
valeriupredoi Aug 11, 2025
74a7c0e
run just a simple pytest session
valeriupredoi Aug 11, 2025
d7a2698
pop a conda env file
valeriupredoi Aug 11, 2025
16e28ca
use conda env file in GHA
valeriupredoi Aug 11, 2025
6fdb0b9
run just simple pytest
valeriupredoi Aug 11, 2025
1d6cddd
Merge branch 'main' into add_warning_and_tests
pp-mo Aug 11, 2025
01cce51
Update lib/ncdata/xarray.py
valeriupredoi Aug 13, 2025
b7ad9f4
unreference uneeded conda env file
valeriupredoi Aug 13, 2025
cc79ff1
restore GHA workflow to original
valeriupredoi Aug 13, 2025
e2f607f
add comment
valeriupredoi Aug 13, 2025
773de45
Update lib/ncdata/xarray.py
valeriupredoi Aug 13, 2025
edf06ac
remove user warning
valeriupredoi Aug 13, 2025
aba9fa6
remove test for warning
valeriupredoi Aug 13, 2025
5f96f32
Merge branch 'add_warning_and_tests' of https://github.com/valeriupre…
valeriupredoi Aug 13, 2025
1fb1db7
readd empty line
valeriupredoi Aug 13, 2025
75e24cf
unrun GHA on push
valeriupredoi Aug 13, 2025
b48088a
Update tests/integration/test_zarr_to_iris.py
valeriupredoi Aug 13, 2025
7365729
Update tests/integration/test_zarr_to_iris.py
valeriupredoi Aug 13, 2025
33a9c80
Update tests/integration/test_zarr_to_iris.py
valeriupredoi Aug 13, 2025
0518afe
shorten imports
valeriupredoi Aug 13, 2025
5039d07
Merge branch 'add_warning_and_tests' of https://github.com/valeriupre…
valeriupredoi Aug 13, 2025
8f4e4da
correct test for s3 connection
valeriupredoi Aug 13, 2025
1c21daa
add dependency
valeriupredoi Aug 13, 2025
ec91745
remove zarr sample data
valeriupredoi Aug 13, 2025
0d74e97
move zzarr sample data
valeriupredoi Aug 13, 2025
b6dd8cb
fix test for new sample data path
valeriupredoi Aug 13, 2025
f8dd7be
run pre-commit
valeriupredoi Aug 13, 2025
e7df68d
remove unify chunks
valeriupredoi Aug 14, 2025
f1a4c46
Merge branch 'main' into add_warning_and_tests
valeriupredoi Aug 14, 2025
c6bc3ca
Merge 'main' into pp__warning_and_tests
pp-mo Aug 14, 2025
8c6460a
Added changenote.
pp-mo Aug 14, 2025
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
3 changes: 1 addition & 2 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:
push:
branches:
- "main"
- "v*x"
tags:
- "v*"
pull_request:
Expand Down Expand Up @@ -49,7 +48,7 @@ jobs:

- name: "Install dependencies"
run: |
conda install --yes numpy pytest pytest-mock iris xarray filelock requests
conda install --yes numpy pytest pytest-mock iris xarray filelock requests zarr aiohttp fsspec

- name: "Install *latest* Iris"
run: |
Expand Down
1 change: 1 addition & 0 deletions docs/changelog_fragments/145.dev.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@valeriupredoi added test for Zarr conversion to Iris cubes.
111 changes: 111 additions & 0 deletions tests/integration/test_zarr_to_iris.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""Test conversion of remote and local Zarr store to iris Cube."""

from importlib.resources import files as importlib_files
from pathlib import Path

import fsspec
import iris
import pytest
import xarray as xr
from ncdata.iris_xarray import cubes_from_xarray as conversion_func


def _return_kwargs():
time_coder = xr.coders.CFDatetimeCoder(use_cftime=True)
return {
"consolidated": True,
"decode_times": time_coder,
"engine": "zarr",
"chunks": {},
"backend_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"))
/ "testdata"
/ "zarr-sample-data"
/ "example_field_0.zarr2"
)

xr_kwargs = _return_kwargs()
zarr_xr = xr.open_dataset(zarr_path, **xr_kwargs)

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"))
/ "testdata"
/ "zarr-sample-data"
/ "example_field_0.zarr3"
)

xr_kwargs = _return_kwargs()
zarr_xr = xr.open_dataset(zarr_path, **xr_kwargs)

cubes = conversion_func(zarr_xr)

assert len(cubes) == 1
cube = cubes[0]
_run_checks(cube)


def _is_url_ok(url):
fs = fsspec.filesystem("http")
valid_zarr = True
try:
fs.open(str(url) + "/zarr.json", "rb") # Zarr3
except Exception: # noqa: BLE001
try:
fs.open(str(url) + "/.zmetadata", "rb") # Zarr2
except Exception: # noqa: BLE001
valid_zarr = False

return valid_zarr


S3_TEST_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"
)
_S3_accessible = _is_url_ok(S3_TEST_PATH)


@pytest.mark.skipif(not _S3_accessible, reason="S3 url not accessible")
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 = S3_TEST_PATH

xr_kwargs = _return_kwargs()
zarr_xr = xr.open_dataset(zarr_path, **xr_kwargs)

cubes = conversion_func(zarr_xr)

assert isinstance(cubes, iris.cube.CubeList)
assert len(cubes) == 1
assert cubes[0].has_lazy_data()
3 changes: 3 additions & 0 deletions tests/testdata/zarr-sample-data/example_field_0.zarr2/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Conventions": "CF-1.12"
}
3 changes: 3 additions & 0 deletions tests/testdata/zarr-sample-data/example_field_0.zarr2/.zgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_format": 2
}
171 changes: 171 additions & 0 deletions tests/testdata/zarr-sample-data/example_field_0.zarr2/.zmetadata
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
}
20 changes: 20 additions & 0 deletions tests/testdata/zarr-sample-data/example_field_0.zarr2/lat/.zarray
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
}
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_ARRAY_DIMENSIONS": [
"lat",
"bounds2"
]
}
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/testdata/zarr-sample-data/example_field_0.zarr2/lon/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"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
}
Loading
Loading