Skip to content

Commit

Permalink
Unified ignoring of iris.warnings.IrisVagueMetadataWarning in prepr…
Browse files Browse the repository at this point in the history
…ocessors (#2646)

Co-authored-by: Valeriu Predoi <valeriu.predoi@gmail.com>
  • Loading branch information
schlunma and valeriupredoi authored Jan 30, 2025
1 parent 8f85fad commit 2405d95
Show file tree
Hide file tree
Showing 20 changed files with 215 additions and 121 deletions.
28 changes: 20 additions & 8 deletions esmvalcore/cmor/_fixes/emac/emac.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import logging
import warnings
from shutil import copyfile

import dask.array as da
Expand All @@ -23,6 +24,8 @@
from netCDF4 import Dataset
from scipy import constants

from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings

from ..shared import add_aux_coords_from_cubes
from ._base_fixes import EmacFix, NegateData

Expand Down Expand Up @@ -230,14 +233,22 @@ def fix_metadata(self, cubes):
)
dt_cube = self.get_cube(cubes, var_name="dt")

cube = (
noxcg_cube.collapsed(
["longitude", "latitude"], iris.analysis.SUM, weights=None
)
+ noxic_cube.collapsed(
["longitude", "latitude"], iris.analysis.SUM, weights=None
with warnings.catch_warnings(), ignore_iris_vague_metadata_warnings():
warnings.filterwarnings(
"ignore",
message="Collapsing spatial coordinate 'latitude' without "
"weighting",
category=UserWarning,
module="iris",
)
) / dt_cube
cube = (
noxcg_cube.collapsed(
["longitude", "latitude"], iris.analysis.SUM, weights=None
)
+ noxic_cube.collapsed(
["longitude", "latitude"], iris.analysis.SUM, weights=None
)
) / dt_cube
cube.units = "kg s-1"
cube.var_name = self.vardef.short_name

Expand All @@ -261,7 +272,8 @@ def fix_metadata(self, cubes):
cubes = super().fix_metadata(cubes)
cube = self.get_cube(cubes)
z_coord = cube.coord(axis="Z")
cube = cube.collapsed(z_coord, iris.analysis.SUM)
with ignore_iris_vague_metadata_warnings():
cube = cube.collapsed(z_coord, iris.analysis.SUM)
return CubeList([cube])


Expand Down
21 changes: 21 additions & 0 deletions esmvalcore/iris_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from __future__ import annotations

import warnings
from collections.abc import Generator
from contextlib import contextmanager
from typing import Dict, Iterable, List, Literal, Sequence

import dask.array as da
Expand All @@ -13,10 +16,28 @@
from iris.coords import Coord
from iris.cube import Cube
from iris.exceptions import CoordinateMultiDimError, CoordinateNotFoundError
from iris.warnings import IrisVagueMetadataWarning

from esmvalcore.typing import NetCDFAttr


@contextmanager
def ignore_iris_vague_metadata_warnings() -> Generator[None]:
"""Ignore specific warnings.
This can be used as a context manager. See also
https://scitools-iris.readthedocs.io/en/stable/generated/api/iris.warnings.html#iris.warnings.IrisVagueMetadataWarning.
"""
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=IrisVagueMetadataWarning,
module="iris",
)
yield


def add_leading_dim_to_cube(cube, dim_coord):
"""Add new leading dimension to cube.
Expand Down
18 changes: 6 additions & 12 deletions esmvalcore/preprocessor/_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from __future__ import annotations

import logging
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Iterable, Literal, Optional

Expand All @@ -21,6 +20,7 @@
from iris.cube import Cube, CubeList
from iris.exceptions import CoordinateNotFoundError

from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings
from esmvalcore.preprocessor._shared import (
apply_mask,
get_dims_along_axes,
Expand Down Expand Up @@ -240,7 +240,8 @@ def zonal_statistics(
"Zonal statistics on irregular grids not yet implemented"
)
(agg, agg_kwargs) = get_iris_aggregator(operator, **operator_kwargs)
result = cube.collapsed("longitude", agg, **agg_kwargs)
with ignore_iris_vague_metadata_warnings():
result = cube.collapsed("longitude", agg, **agg_kwargs)
if normalize is not None:
result = get_normalized_cube(cube, result, normalize)
return result
Expand Down Expand Up @@ -289,7 +290,8 @@ def meridional_statistics(
"Meridional statistics on irregular grids not yet implemented"
)
(agg, agg_kwargs) = get_iris_aggregator(operator, **operator_kwargs)
result = cube.collapsed("latitude", agg, **agg_kwargs)
with ignore_iris_vague_metadata_warnings():
result = cube.collapsed("latitude", agg, **agg_kwargs)
if normalize is not None:
result = get_normalized_cube(cube, result, normalize)
return result
Expand Down Expand Up @@ -355,15 +357,7 @@ def area_statistics(
agg, agg_kwargs, "cell_area", cube, try_adding_calculated_cell_area
)

with warnings.catch_warnings():
# Silence various warnings about collapsing multi-dimensional and/or
# non contiguous coordinates as this should be fine when the cell areas
# are provided and will fail when they needed but not provided.
warnings.filterwarnings(
"ignore",
category=iris.warnings.IrisVagueMetadataWarning,
module="iris",
)
with ignore_iris_vague_metadata_warnings():
result = cube.collapsed(["latitude", "longitude"], agg, **agg_kwargs)
if normalize is not None:
result = get_normalized_cube(cube, result, normalize)
Expand Down
8 changes: 6 additions & 2 deletions esmvalcore/preprocessor/_compare_with_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
from iris.cube import Cube, CubeList
from scipy.stats import wasserstein_distance

from esmvalcore.iris_helpers import rechunk_cube
from esmvalcore.iris_helpers import (
ignore_iris_vague_metadata_warnings,
rechunk_cube,
)
from esmvalcore.preprocessor._io import concatenate
from esmvalcore.preprocessor._other import histogram
from esmvalcore.preprocessor._shared import (
Expand Down Expand Up @@ -431,7 +434,8 @@ def _calculate_metric(

# Get result cube with correct dimensional metadata by using dummy
# operation (max)
res_cube = cube.collapsed(coords, iris.analysis.MAX)
with ignore_iris_vague_metadata_warnings():
res_cube = cube.collapsed(coords, iris.analysis.MAX)
res_cube.data = res_data
res_cube.metadata = res_metadata
res_cube.cell_methods = [*cube.cell_methods, CellMethod(metric, coords)]
Expand Down
7 changes: 5 additions & 2 deletions esmvalcore/preprocessor/_cycles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import iris
import iris.coord_categorisation

from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -69,8 +71,9 @@ def amplitude(cube, coords):
)

# Calculate amplitude
max_cube = cube.aggregated_by(coords, iris.analysis.MAX)
min_cube = cube.aggregated_by(coords, iris.analysis.MIN)
with ignore_iris_vague_metadata_warnings():
max_cube = cube.aggregated_by(coords, iris.analysis.MAX)
min_cube = cube.aggregated_by(coords, iris.analysis.MIN)
amplitude_cube = max_cube - min_cube
amplitude_cube.metadata = cube.metadata

Expand Down
26 changes: 15 additions & 11 deletions esmvalcore/preprocessor/_derive/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from iris import NameConstraint
from scipy import constants

from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings

logger = logging.getLogger(__name__)


Expand All @@ -21,12 +23,13 @@ def cloud_area_fraction(cubes, tau_constraint, plev_constraint):
for coord in new_cube.coords()
if len(coord.points) > 1
]
if "atmosphere_optical_thickness_due_to_cloud" in coord_names:
new_cube = new_cube.collapsed(
"atmosphere_optical_thickness_due_to_cloud", iris.analysis.SUM
)
if "air_pressure" in coord_names:
new_cube = new_cube.collapsed("air_pressure", iris.analysis.SUM)
with ignore_iris_vague_metadata_warnings():
if "atmosphere_optical_thickness_due_to_cloud" in coord_names:
new_cube = new_cube.collapsed(
"atmosphere_optical_thickness_due_to_cloud", iris.analysis.SUM
)
if "air_pressure" in coord_names:
new_cube = new_cube.collapsed("air_pressure", iris.analysis.SUM)

return new_cube

Expand Down Expand Up @@ -108,11 +111,12 @@ def column_average(cube, hus_cube, zg_cube, ps_cube):
cube.data = cube.core_data() * n_dry.core_data()

# Column-average
cube = cube.collapsed("air_pressure", iris.analysis.SUM)
cube.data = (
cube.core_data()
/ n_dry.collapsed("air_pressure", iris.analysis.SUM).core_data()
)
with ignore_iris_vague_metadata_warnings():
cube = cube.collapsed("air_pressure", iris.analysis.SUM)
cube.data = (
cube.core_data()
/ n_dry.collapsed("air_pressure", iris.analysis.SUM).core_data()
)
return cube


Expand Down
8 changes: 4 additions & 4 deletions esmvalcore/preprocessor/_derive/amoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import iris
import numpy as np

from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings

from ._baseclass import DerivedVariableBase


Expand Down Expand Up @@ -86,9 +88,7 @@ def calculate(cubes):
cube = cube.extract(constraint=rapid_constraint)

# 4: find the maximum in the water column along the time axis.
cube = cube.collapsed(
["depth", "region"],
iris.analysis.MAX,
)
with ignore_iris_vague_metadata_warnings():
cube = cube.collapsed(["depth", "region"], iris.analysis.MAX)

return cube
13 changes: 4 additions & 9 deletions esmvalcore/preprocessor/_derive/toz.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Derivation of variable ``toz``."""

import warnings

import cf_units
import iris
from scipy import constants

from esmvalcore.cmor.table import CMOR_TABLES
from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings

from .._regrid import extract_levels, regrid
from ._baseclass import DerivedVariableBase
Expand Down Expand Up @@ -104,7 +103,8 @@ def calculate(cubes):
# have correct shapes
if not o3_cube.coords("longitude"):
o3_cube = add_longitude_coord(o3_cube)
ps_cube = ps_cube.collapsed("longitude", iris.analysis.MEAN)
with ignore_iris_vague_metadata_warnings():
ps_cube = ps_cube.collapsed("longitude", iris.analysis.MEAN)
ps_cube.remove_coord("longitude")
ps_cube = add_longitude_coord(ps_cube)

Expand All @@ -117,12 +117,7 @@ def calculate(cubes):
# widths
p_layer_widths = pressure_level_widths(o3_cube, ps_cube, top_limit=0.0)
toz_cube = o3_cube * p_layer_widths / STANDARD_GRAVITY * MW_O3 / MW_AIR
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=UserWarning,
message="Collapsing a non-contiguous coordinate",
)
with ignore_iris_vague_metadata_warnings():
toz_cube = toz_cube.collapsed("air_pressure", iris.analysis.SUM)
toz_cube.units = (
o3_cube.units
Expand Down
5 changes: 4 additions & 1 deletion esmvalcore/preprocessor/_derive/troz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import dask.array as da
import iris

from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings

from ._baseclass import DerivedVariableBase
from .soz import STRATOSPHERIC_O3_THRESHOLD
from .toz import DerivedVariable as Toz
Expand Down Expand Up @@ -45,7 +47,8 @@ def calculate(cubes):
# have correct shapes
if not o3_cube.coords("longitude"):
o3_cube = add_longitude_coord(o3_cube)
ps_cube = ps_cube.collapsed("longitude", iris.analysis.MEAN)
with ignore_iris_vague_metadata_warnings():
ps_cube = ps_cube.collapsed("longitude", iris.analysis.MEAN)
ps_cube.remove_coord("longitude")
ps_cube = add_longitude_coord(ps_cube)

Expand Down
5 changes: 4 additions & 1 deletion esmvalcore/preprocessor/_derive/uajet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import iris
import numpy as np

from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings

from ._baseclass import DerivedVariableBase

# Constants (Southern hemisphere at 850 hPa)
Expand Down Expand Up @@ -31,7 +33,8 @@ def calculate(cubes):
ua_cube = ua_cube.extract(
iris.Constraint(latitude=lambda cell: LAT[0] <= cell <= LAT[1])
)
ua_cube = ua_cube.collapsed("longitude", iris.analysis.MEAN)
with ignore_iris_vague_metadata_warnings():
ua_cube = ua_cube.collapsed("longitude", iris.analysis.MEAN)

# Calculate maximum jet position
uajet_vals = []
Expand Down
31 changes: 18 additions & 13 deletions esmvalcore/preprocessor/_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from iris.cube import Cube
from iris.util import rolling_window

from esmvalcore.preprocessor._shared import apply_mask
from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings
from esmvalcore.preprocessor._shared import (
apply_mask,
)

from ._supplementary_vars import register_supplementaries

Expand Down Expand Up @@ -396,12 +399,13 @@ def count_spells(
# if you want overlapping windows set the step to be m*spell_length
# where m is a float
###############################################################
hit_windows = rolling_window(
data_hits,
window=spell_length,
step=spell_length,
axis=axis,
)
with ignore_iris_vague_metadata_warnings():
hit_windows = rolling_window(
data_hits,
window=spell_length,
step=spell_length,
axis=axis,
)
# Find the windows "full of True-s" (along the added 'window axis').
full_windows = array_module.all(hit_windows, axis=axis + 1)
# Count points fulfilling the condition (along the time axis).
Expand Down Expand Up @@ -726,12 +730,13 @@ def _get_fillvalues_mask(
)

# Calculate the statistic.
counts_windowed_cube = cube.collapsed(
"time",
spell_count,
threshold=min_value,
spell_length=time_window,
)
with ignore_iris_vague_metadata_warnings():
counts_windowed_cube = cube.collapsed(
"time",
spell_count,
threshold=min_value,
spell_length=time_window,
)

# Create mask
mask = counts_windowed_cube.core_data() < counts_threshold
Expand Down
Loading

0 comments on commit 2405d95

Please sign in to comment.