|
| 1 | +""" |
| 2 | +Integration tests for :func:`esmvalcore.preprocessor._io._load_zarr`. |
| 3 | +
|
| 4 | +This is a dedicated test module for Zarr files IO; we have identified |
| 5 | +a number of issues with Zarr IO so it deserves its own test module. |
| 6 | +
|
| 7 | +We have a permanent bucket: esmvaltool-zarr at CEDA's object store |
| 8 | +"url": "https://uor-aces-o.s3-ext.jc.rl.ac.uk/esmvaltool-zarr", |
| 9 | +where will host a number of test files. Bucket is anon/anon |
| 10 | +(read/GET-only, but PUT can be allowed). Bucket operations are done |
| 11 | +via usual MinIO client (mc command) e.g. ``mc list``, ``mc du`` etc. |
| 12 | +
|
| 13 | +Further performance investigations are being run with a number of tests |
| 14 | +that look at ncdata at https://github.com/valeriupredoi/esmvaltool_zarr_tests |
| 15 | +also see https://github.com/pp-mo/ncdata/issues/139 |
| 16 | +""" |
| 17 | + |
| 18 | +from importlib.resources import files as importlib_files |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import cf_units |
| 22 | +import pytest |
| 23 | + |
| 24 | +from esmvalcore.preprocessor._io import load |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("input_type", [str, Path]) |
| 28 | +def test_load_zarr2_local(input_type): |
| 29 | + """Test loading a Zarr2 store from local FS.""" |
| 30 | + zarr_path = ( |
| 31 | + Path(importlib_files("tests")) |
| 32 | + / "sample_data" |
| 33 | + / "zarr-sample-data" |
| 34 | + / "example_field_0.zarr2" |
| 35 | + ) |
| 36 | + |
| 37 | + cubes = load(input_type(zarr_path)) |
| 38 | + |
| 39 | + assert len(cubes) == 1 |
| 40 | + cube = cubes[0] |
| 41 | + assert cube.var_name == "q" |
| 42 | + assert cube.standard_name == "specific_humidity" |
| 43 | + assert cube.long_name is None |
| 44 | + assert cube.units == cf_units.Unit("1") |
| 45 | + coords = cube.coords() |
| 46 | + coord_names = [coord.standard_name for coord in coords] |
| 47 | + assert "longitude" in coord_names |
| 48 | + assert "latitude" in coord_names |
| 49 | + |
| 50 | + |
| 51 | +def test_load_zarr2_remote(): |
| 52 | + """Test loading a Zarr2 store from a https Object Store.""" |
| 53 | + zarr_path = ( |
| 54 | + "https://uor-aces-o.s3-ext.jc.rl.ac.uk/" |
| 55 | + "esmvaltool-zarr/example_field_0.zarr2" |
| 56 | + ) |
| 57 | + |
| 58 | + # with "dummy" storage options |
| 59 | + cubes = load( |
| 60 | + zarr_path, |
| 61 | + ignore_warnings=None, |
| 62 | + backend_kwargs={"storage_options": {}}, |
| 63 | + ) |
| 64 | + |
| 65 | + assert len(cubes) == 1 |
| 66 | + cube = cubes[0] |
| 67 | + assert cube.var_name == "q" |
| 68 | + assert cube.standard_name == "specific_humidity" |
| 69 | + assert cube.long_name is None |
| 70 | + assert cube.units == cf_units.Unit("1") |
| 71 | + coords = cube.coords() |
| 72 | + coord_names = [coord.standard_name for coord in coords] |
| 73 | + assert "longitude" in coord_names |
| 74 | + assert "latitude" in coord_names |
| 75 | + |
| 76 | + # without storage_options |
| 77 | + cubes = load(zarr_path) |
| 78 | + |
| 79 | + assert len(cubes) == 1 |
| 80 | + cube = cubes[0] |
| 81 | + assert cube.var_name == "q" |
| 82 | + assert cube.standard_name == "specific_humidity" |
| 83 | + assert cube.long_name is None |
| 84 | + assert cube.units == cf_units.Unit("1") |
| 85 | + coords = cube.coords() |
| 86 | + coord_names = [coord.standard_name for coord in coords] |
| 87 | + assert "longitude" in coord_names |
| 88 | + assert "latitude" in coord_names |
| 89 | + |
| 90 | + |
| 91 | +def test_load_zarr3_remote(): |
| 92 | + """Test loading a Zarr3 store from a https Object Store.""" |
| 93 | + zarr_path = ( |
| 94 | + "https://uor-aces-o.s3-ext.jc.rl.ac.uk/" |
| 95 | + "esmvaltool-zarr/example_field_0.zarr3" |
| 96 | + ) |
| 97 | + |
| 98 | + # with "dummy" storage options |
| 99 | + cubes = load( |
| 100 | + zarr_path, |
| 101 | + ignore_warnings=None, |
| 102 | + backend_kwargs={"storage_options": {}}, |
| 103 | + ) |
| 104 | + |
| 105 | + assert len(cubes) == 1 |
| 106 | + cube = cubes[0] |
| 107 | + assert cube.var_name == "q" |
| 108 | + assert cube.standard_name == "specific_humidity" |
| 109 | + assert cube.long_name is None |
| 110 | + assert cube.units == cf_units.Unit("1") |
| 111 | + coords = cube.coords() |
| 112 | + coord_names = [coord.standard_name for coord in coords] |
| 113 | + assert "longitude" in coord_names |
| 114 | + assert "latitude" in coord_names |
| 115 | + |
| 116 | + |
| 117 | +def test_load_zarr3_cmip6_metadata(): |
| 118 | + """ |
| 119 | + Test loading a Zarr3 store from a https Object Store. |
| 120 | +
|
| 121 | + This test loads just the metadata, no computations. |
| 122 | +
|
| 123 | + This is an actual CMIP6 dataset (Zarr built from netCDF4 via Xarray) |
| 124 | + - Zarr store on disk: 243 MiB |
| 125 | + - compression: Blosc |
| 126 | + - Dimensions: (lat: 128, lon: 256, time: 2352, axis_nbounds: 2) |
| 127 | + - chunking: time-slices; netCDF4.Dataset.chunking() = [1, 128, 256] |
| 128 | +
|
| 129 | + Test takes 8-9s (median: 8.5s) and needs max Res mem: 1GB |
| 130 | + """ |
| 131 | + zarr_path = ( |
| 132 | + "https://uor-aces-o.s3-ext.jc.rl.ac.uk/" |
| 133 | + "esmvaltool-zarr/pr_Amon_CNRM-ESM2-1_02Kpd-11_r1i1p2f2_gr_200601-220112.zarr3" |
| 134 | + ) |
| 135 | + |
| 136 | + # with "dummy" storage options |
| 137 | + cubes = load( |
| 138 | + zarr_path, |
| 139 | + ignore_warnings=None, |
| 140 | + backend_kwargs={"storage_options": {}}, |
| 141 | + ) |
| 142 | + |
| 143 | + assert len(cubes) == 1 |
| 144 | + cube = cubes[0] |
| 145 | + assert cube.var_name == "pr" |
| 146 | + assert cube.standard_name == "precipitation_flux" |
| 147 | + assert cube.long_name == "Precipitation" |
| 148 | + assert cube.units == cf_units.Unit("kg m-2 s-1") |
| 149 | + assert cube.has_lazy_data() |
| 150 | + |
| 151 | + |
| 152 | +def test_load_zarr_remote_not_zarr_file(): |
| 153 | + """ |
| 154 | + Test loading a Zarr store from a https Object Store. |
| 155 | +
|
| 156 | + This fails due to the file being loaded is not a Zarr file. |
| 157 | + """ |
| 158 | + zarr_path = ( |
| 159 | + "https://uor-aces-o.s3-ext.jc.rl.ac.uk/" |
| 160 | + "esmvaltool-zarr/example_field_0.zarr17" |
| 161 | + ) |
| 162 | + |
| 163 | + msg = ( |
| 164 | + "File 'https://uor-aces-o.s3-ext.jc.rl.ac.uk/" |
| 165 | + "esmvaltool-zarr/example_field_0.zarr17' can not " |
| 166 | + "be opened as Zarr file at the moment." |
| 167 | + ) |
| 168 | + with pytest.raises(ValueError, match=msg): |
| 169 | + load(zarr_path) |
| 170 | + |
| 171 | + |
| 172 | +def test_load_zarr_remote_not_file(): |
| 173 | + """ |
| 174 | + Test loading a Zarr store from a https Object Store. |
| 175 | +
|
| 176 | + This fails due to non-existing file. |
| 177 | + """ |
| 178 | + zarr_path = ( |
| 179 | + "https://uor-aces-o.s3-ext.jc.rl.ac.uk/" |
| 180 | + "esmvaltool-zarr/example_field_0.zarr22" |
| 181 | + ) |
| 182 | + |
| 183 | + msg = ( |
| 184 | + "File 'https://uor-aces-o.s3-ext.jc.rl.ac.uk/" |
| 185 | + "esmvaltool-zarr/example_field_0.zarr22' can not " |
| 186 | + "be opened as Zarr file at the moment." |
| 187 | + ) |
| 188 | + with pytest.raises(ValueError, match=msg): |
| 189 | + load(zarr_path) |
| 190 | + |
| 191 | + |
| 192 | +def test_load_zarr_local_not_file(): |
| 193 | + """ |
| 194 | + Test loading something that has a zarr extension. |
| 195 | +
|
| 196 | + But file doesn't exist (on local FS). |
| 197 | + """ |
| 198 | + zarr_path = "esmvaltool-zarr/example_field_0.zarr22" |
| 199 | + |
| 200 | + # "Unable to find group" or "No group found" |
| 201 | + # Zarr keeps changing the exception string so matching |
| 202 | + # is bound to fail the test |
| 203 | + with pytest.raises(FileNotFoundError): |
| 204 | + load(zarr_path) |
| 205 | + |
| 206 | + |
| 207 | +def test_load_zarr_local_not_zarr_file(): |
| 208 | + """ |
| 209 | + Test loading something that has a zarr extension. |
| 210 | +
|
| 211 | + But file is plaintext (on local FS). |
| 212 | + """ |
| 213 | + zarr_path = ( |
| 214 | + Path(importlib_files("tests")) |
| 215 | + / "sample_data" |
| 216 | + / "zarr-sample-data" |
| 217 | + / "example_field_0.zarr17" |
| 218 | + ) |
| 219 | + |
| 220 | + # "Unable to find group" or "No group found" |
| 221 | + # Zarr keeps changing the exception string so matching |
| 222 | + # is bound to fail the test |
| 223 | + with pytest.raises(FileNotFoundError): |
| 224 | + load(zarr_path) |
0 commit comments