Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 25 additions & 1 deletion esmvalcore/preprocessor/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@
}


def _drop_range_attributes(cube: Cube) -> Cube:
"""Drop range related attributes from cube and its components."""
drop_attrs = (
"actual_range",
"valid_max",
"valid_min",
"valid_range",
)
cube = cube.copy()
for attr in drop_attrs:
cube.attributes.pop(attr, None)
for coord in cube.dim_coords:
coord.attributes.pop(attr, None)
for aux_coord in cube.aux_coords:
aux_coord.attributes.pop(attr, None)
for cell_measure in cube.cell_measures():
cell_measure.attributes.pop(attr, None)
for ancillary_variable in cube.ancillary_variables():
ancillary_variable.attributes.pop(attr, None)
Comment thread
schlunma marked this conversation as resolved.
return cube


def load(
file: str
| Path
Expand Down Expand Up @@ -133,7 +155,9 @@ def load(
)
warnings.warn(warn_msg, ESMValCoreLoadWarning, stacklevel=2)

return cubes
# Drop range related attributes as these are likely to be
# invalidated by preprocessing the data.
return CubeList(_drop_range_attributes(cube) for cube in cubes)


def _load_zarr(
Expand Down
35 changes: 34 additions & 1 deletion tests/integration/preprocessor/_io/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
import pytest
import xarray as xr
from iris.coords import DimCoord
from iris.coords import AncillaryVariable, CellMeasure, DimCoord
from iris.cube import Cube, CubeList

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


def test_load_with_range_attrs(tmp_path: Path, sample_cube: Cube) -> None:
"""Test loading multiple files."""
sample_cube.attributes["valid_max"] = 1
ancillary_var = AncillaryVariable(
[0, 1.1],
standard_name="land_area_fraction",
units="%",
)
ancillary_var.attributes["valid_max"] = 1
sample_cube.add_ancillary_variable(ancillary_var, data_dims=[0])
cell_measure = CellMeasure([1, 1], standard_name="cell_area", units="m2")
cell_measure.attributes["valid_min"] = 1
sample_cube.add_cell_measure(cell_measure, data_dims=[0])
temp_file = tmp_path / "cube.nc"
iris.save(sample_cube, temp_file)

cubes = load(temp_file)

assert len(cubes) == 1
sample_cube = cubes[0]
assert "valid_max" not in sample_cube.attributes
assert_array_equal(
sample_cube.data,
np.ma.array([1, 2], mask=[False, True]),
)
assert "valid_max" not in sample_cube.ancillary_variables()[0].attributes
assert_array_equal(
sample_cube.ancillary_variables()[0].data,
np.ma.array([0, 1.1], mask=[False, True]),
)
assert "valid_min" not in sample_cube.cell_measures()[0].attributes


def test_load_grib():
"""Test loading a grib file."""
grib_path = (
Expand Down