Skip to content

Commit af39db6

Browse files
authored
Drop range related attributes on load (#3072)
1 parent 4a056fa commit af39db6

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

esmvalcore/preprocessor/_io.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@
4242
}
4343

4444

45+
def _drop_range_attributes(cube: Cube) -> Cube:
46+
"""Drop range related attributes from cube and its components."""
47+
drop_attrs = (
48+
"actual_range",
49+
"valid_max",
50+
"valid_min",
51+
"valid_range",
52+
)
53+
cube = cube.copy()
54+
for attr in drop_attrs:
55+
cube.attributes.pop(attr, None)
56+
for coord in cube.dim_coords:
57+
coord.attributes.pop(attr, None)
58+
for aux_coord in cube.aux_coords:
59+
aux_coord.attributes.pop(attr, None)
60+
for cell_measure in cube.cell_measures():
61+
cell_measure.attributes.pop(attr, None)
62+
for ancillary_variable in cube.ancillary_variables():
63+
ancillary_variable.attributes.pop(attr, None)
64+
return cube
65+
66+
4567
def load(
4668
file: str
4769
| Path
@@ -133,7 +155,9 @@ def load(
133155
)
134156
warnings.warn(warn_msg, ESMValCoreLoadWarning, stacklevel=2)
135157

136-
return cubes
158+
# Drop range related attributes as these are likely to be
159+
# invalidated by preprocessing the data.
160+
return CubeList(_drop_range_attributes(cube) for cube in cubes)
137161

138162

139163
def _load_zarr(

tests/integration/preprocessor/_io/test_load.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
import pytest
1111
import xarray as xr
12-
from iris.coords import DimCoord
12+
from iris.coords import AncillaryVariable, CellMeasure, DimCoord
1313
from iris.cube import Cube, CubeList
1414

1515
from esmvalcore.exceptions import ESMValCoreLoadWarning
@@ -39,6 +39,39 @@ def test_load(tmp_path, sample_cube):
3939
assert_array_equal(sample_cube.coord("latitude").points, [1, 2])
4040

4141

42+
def test_load_with_range_attrs(tmp_path: Path, sample_cube: Cube) -> None:
43+
"""Test loading multiple files."""
44+
sample_cube.attributes["valid_max"] = 1
45+
ancillary_var = AncillaryVariable(
46+
[0, 1.1],
47+
standard_name="land_area_fraction",
48+
units="%",
49+
)
50+
ancillary_var.attributes["valid_max"] = 1
51+
sample_cube.add_ancillary_variable(ancillary_var, data_dims=[0])
52+
cell_measure = CellMeasure([1, 1], standard_name="cell_area", units="m2")
53+
cell_measure.attributes["valid_min"] = 1
54+
sample_cube.add_cell_measure(cell_measure, data_dims=[0])
55+
temp_file = tmp_path / "cube.nc"
56+
iris.save(sample_cube, temp_file)
57+
58+
cubes = load(temp_file)
59+
60+
assert len(cubes) == 1
61+
sample_cube = cubes[0]
62+
assert "valid_max" not in sample_cube.attributes
63+
assert_array_equal(
64+
sample_cube.data,
65+
np.ma.array([1, 2], mask=[False, True]),
66+
)
67+
assert "valid_max" not in sample_cube.ancillary_variables()[0].attributes
68+
assert_array_equal(
69+
sample_cube.ancillary_variables()[0].data,
70+
np.ma.array([0, 1.1], mask=[False, True]),
71+
)
72+
assert "valid_min" not in sample_cube.cell_measures()[0].attributes
73+
74+
4275
def test_load_grib():
4376
"""Test loading a grib file."""
4477
grib_path = (

0 commit comments

Comments
 (0)