Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent warning repeats #5506

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
replace all warnings
  • Loading branch information
stephenworsley committed Feb 11, 2024
commit 8e45ceb42f00dbc8ebd04fb76f850f2871949e37
4 changes: 2 additions & 2 deletions lib/iris/_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Automatic concatenation of multiple cubes over one or more existing dimensions."""

from collections import defaultdict, namedtuple
import warnings
from iris.exceptions import warn_once_at_level

import dask.array as da
import numpy as np
Expand Down Expand Up @@ -911,7 +911,7 @@ def register(
raise iris.exceptions.ConcatenateError([msg])
elif not match:
msg = f"Found cubes with overlap on concatenate axis {candidate_axis}, skipping concatenation for these cubes"
warnings.warn(msg, category=iris.exceptions.IrisUserWarning)
warn_once_at_level(msg, category=iris.exceptions.IrisUserWarning)

# Check for compatible AuxCoords.
if match:
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE in the root of the repository for full licensing details.
"""Utilities for producing runtime deprecation messages."""

import warnings
from iris.exceptions import warn_once_at_level


class IrisDeprecation(UserWarning):
Expand Down Expand Up @@ -44,7 +44,7 @@ def warn_deprecated(msg, stacklevel=2):
>>>

"""
warnings.warn(msg, category=IrisDeprecation, stacklevel=stacklevel)
warn_once_at_level(msg, category=IrisDeprecation, stacklevel=stacklevel)


# A Mixin for a wrapper class that copies the docstring of the wrapped class
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/analysis/_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import copy
import functools
import warnings
from iris.exceptions import warn_once_at_level

import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -1106,6 +1106,6 @@ def regrid_reference_surface(
"Cannot update aux_factory {!r} because of dropped"
" coordinates.".format(factory.name())
)
warnings.warn(msg, category=IrisImpossibleUpdateWarning)
warn_once_at_level(msg, category=IrisImpossibleUpdateWarning)

return result
4 changes: 2 additions & 2 deletions lib/iris/analysis/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

import re
import warnings
from iris.exceptions import warn_once_at_level

import cf_units
import numpy as np
Expand Down Expand Up @@ -85,7 +85,7 @@ def _construct_midpoint_coord(coord, circular=None):
"Construction coordinate midpoints for the '{}' coordinate, "
"though it has the attribute 'circular'={}."
)
warnings.warn(
warn_once_at_level(
msg.format(circular, coord.circular, coord.name()),
category=IrisUserWarning,
)
Expand Down
14 changes: 7 additions & 7 deletions lib/iris/analysis/cartography.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from collections import namedtuple
import copy
import warnings
from iris.exceptions import warn_once_at_level

import cartopy.crs as ccrs
import cartopy.img_transform
Expand Down Expand Up @@ -410,7 +410,7 @@ def area_weights(cube, normalize=False):
cs = cube.coord_system("CoordSystem")
if isinstance(cs, iris.coord_systems.GeogCS):
if cs.inverse_flattening != 0.0:
warnings.warn(
warn_once_at_level(
"Assuming spherical earth from ellipsoid.",
category=iris.exceptions.IrisDefaultingWarning,
)
Expand All @@ -419,13 +419,13 @@ def area_weights(cube, normalize=False):
cs.ellipsoid is not None
):
if cs.ellipsoid.inverse_flattening != 0.0:
warnings.warn(
warn_once_at_level(
"Assuming spherical earth from ellipsoid.",
category=iris.exceptions.IrisDefaultingWarning,
)
radius_of_earth = cs.ellipsoid.semi_major_axis
else:
warnings.warn(
warn_once_at_level(
"Using DEFAULT_SPHERICAL_EARTH_RADIUS.",
category=iris.exceptions.IrisDefaultingWarning,
)
Expand Down Expand Up @@ -567,7 +567,7 @@ def cosine_latitude_weights(cube):
if np.any(lat.points < -np.pi / 2.0 - threshold) or np.any(
lat.points > np.pi / 2.0 + threshold
):
warnings.warn(
warn_once_at_level(
"Out of range latitude values will be clipped to the valid range.",
category=iris.exceptions.IrisDefaultingWarning,
)
Expand Down Expand Up @@ -683,7 +683,7 @@ def project(cube, target_proj, nx=None, ny=None):
# Determine source coordinate system
if lat_coord.coord_system is None:
# Assume WGS84 latlon if unspecified
warnings.warn(
warn_once_at_level(
"Coordinate system of latitude and longitude "
"coordinates is not specified. Assuming WGS84 Geodetic.",
category=iris.exceptions.IrisDefaultingWarning,
Expand Down Expand Up @@ -871,7 +871,7 @@ def project(cube, target_proj, nx=None, ny=None):
new_cube.add_aux_coord(coord.copy(), cube.coord_dims(coord))
discarded_coords = coords_to_ignore.difference([lat_coord, lon_coord])
if discarded_coords:
warnings.warn(
warn_once_at_level(
"Discarding coordinates that share dimensions with {} and {}: {}".format(
lat_coord.name(),
lon_coord.name(),
Expand Down
10 changes: 5 additions & 5 deletions lib/iris/analysis/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

"""

import warnings
from iris.exceptions import warn_once_at_level

import numpy as np
from shapely.geometry import Polygon
Expand Down Expand Up @@ -69,7 +69,7 @@ def _extract_relevant_cube_slice(cube, geometry):
x_min_ix = np.where(x_bounds_lower <= x_min_geom)[0]
x_min_ix = x_min_ix[np.argmax(x_bounds_lower[x_min_ix])]
except ValueError:
warnings.warn(
warn_once_at_level(
"The geometry exceeds the cube's x dimension at the lower end.",
category=iris.exceptions.IrisGeometryExceedWarning,
)
Expand All @@ -79,7 +79,7 @@ def _extract_relevant_cube_slice(cube, geometry):
x_max_ix = np.where(x_bounds_upper >= x_max_geom)[0]
x_max_ix = x_max_ix[np.argmin(x_bounds_upper[x_max_ix])]
except ValueError:
warnings.warn(
warn_once_at_level(
"The geometry exceeds the cube's x dimension at the upper end.",
category=iris.exceptions.IrisGeometryExceedWarning,
)
Expand All @@ -89,7 +89,7 @@ def _extract_relevant_cube_slice(cube, geometry):
y_min_ix = np.where(y_bounds_lower <= y_min_geom)[0]
y_min_ix = y_min_ix[np.argmax(y_bounds_lower[y_min_ix])]
except ValueError:
warnings.warn(
warn_once_at_level(
"The geometry exceeds the cube's y dimension at the lower end.",
category=iris.exceptions.IrisGeometryExceedWarning,
)
Expand All @@ -99,7 +99,7 @@ def _extract_relevant_cube_slice(cube, geometry):
y_max_ix = np.where(y_bounds_upper >= y_max_geom)[0]
y_max_ix = y_max_ix[np.argmin(y_bounds_upper[y_max_ix])]
except ValueError:
warnings.warn(
warn_once_at_level(
"The geometry exceeds the cube's y dimension at the upper end.",
category=iris.exceptions.IrisGeometryExceedWarning,
)
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/analysis/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import inspect
import math
import operator
import warnings
from iris.exceptions import warn_once_at_level

import cf_units
import dask.array as da
Expand Down Expand Up @@ -938,7 +938,7 @@ def _broadcast_cube_coord_data(cube, other, operation_name, dim=None):
raise iris.exceptions.CoordinateMultiDimError(other)

if other.has_bounds():
warnings.warn(
warn_once_at_level(
"Using {!r} with a bounded coordinate is not well "
"defined; ignoring bounds.".format(operation_name),
category=iris.exceptions.IrisIgnoringBoundsWarning,
Expand Down
38 changes: 19 additions & 19 deletions lib/iris/aux_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Definitions of derived coordinates."""

from abc import ABCMeta, abstractmethod
import warnings
from iris.exceptions import warn_once_at_level

import cf_units
import dask.array as da
Expand Down Expand Up @@ -429,7 +429,7 @@ def _check_dependencies(pressure_at_top, sigma, surface_air_pressure):
f"Coordinate '{coord.name()}' has bounds. These will "
"be disregarded"
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

# Check units
if sigma.units.is_unknown():
Expand Down Expand Up @@ -502,7 +502,7 @@ def make_coord(self, coord_dims_func):
if sigma.shape[-1:] not in ok_bound_shapes:
raise ValueError("Invalid sigma coordinate bounds")
if pressure_at_top.shape[-1:] not in [(), (1,)]:
warnings.warn(
warn_once_at_level(
"Pressure at top coordinate has bounds. These are being "
"disregarded",
category=IrisIgnoringBoundsWarning,
Expand All @@ -511,7 +511,7 @@ def make_coord(self, coord_dims_func):
bds_shape = list(pressure_at_top_pts.shape) + [1]
pressure_at_top = pressure_at_top_pts.reshape(bds_shape)
if surface_air_pressure.shape[-1:] not in [(), (1,)]:
warnings.warn(
warn_once_at_level(
"Surface pressure coordinate has bounds. These are being "
"disregarded",
category=IrisIgnoringBoundsWarning,
Expand Down Expand Up @@ -572,7 +572,7 @@ def __init__(self, delta=None, sigma=None, orography=None):
"Orography coordinate {!r} has bounds."
" These will be disregarded.".format(orography.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

self.delta = delta
self.sigma = sigma
Expand Down Expand Up @@ -648,7 +648,7 @@ def make_coord(self, coord_dims_func):
if sigma.shape[-1:] not in ok_bound_shapes:
raise ValueError("Invalid sigma coordinate bounds.")
if orography.shape[-1:] not in [(), (1,)]:
warnings.warn(
warn_once_at_level(
"Orography coordinate has bounds. These are being disregarded.",
category=IrisIgnoringBoundsWarning,
stacklevel=2,
Expand Down Expand Up @@ -704,7 +704,7 @@ def update(self, old_coord, new_coord=None):
"Orography coordinate {!r} has bounds."
" These will be disregarded.".format(new_coord.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
self.orography = new_coord


Expand Down Expand Up @@ -768,7 +768,7 @@ def _check_dependencies(delta, sigma, surface_air_pressure):
"Surface pressure coordinate {!r} has bounds. These will"
" be disregarded.".format(surface_air_pressure.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

# Check units.
if sigma is not None and sigma.units.is_unknown():
Expand Down Expand Up @@ -854,7 +854,7 @@ def make_coord(self, coord_dims_func):
if sigma.shape[-1:] not in ok_bound_shapes:
raise ValueError("Invalid sigma coordinate bounds.")
if surface_air_pressure.shape[-1:] not in [(), (1,)]:
warnings.warn(
warn_once_at_level(
"Surface pressure coordinate has bounds. "
"These are being disregarded.",
category=IrisIgnoringBoundsWarning,
Expand Down Expand Up @@ -968,7 +968,7 @@ def _check_dependencies(sigma, eta, depth, depth_c, nsigma, zlev):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(term, coord.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

for coord, term in ((depth_c, "depth_c"), (nsigma, "nsigma")):
if coord is not None and coord.shape != (1,):
Expand Down Expand Up @@ -1125,7 +1125,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
# Swap bounds with points.
bds_shape = list(nd_points_by_key[key].shape) + [1]
bounds = nd_points_by_key[key].reshape(bds_shape)
Expand Down Expand Up @@ -1205,7 +1205,7 @@ def _check_dependencies(sigma, eta, depth):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(term, coord.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

# Check units.
if sigma is not None and sigma.units.is_unknown():
Expand Down Expand Up @@ -1281,7 +1281,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
# Swap bounds with points.
bds_shape = list(nd_points_by_key[key].shape) + [1]
bounds = nd_points_by_key[key].reshape(bds_shape)
Expand Down Expand Up @@ -1372,7 +1372,7 @@ def _check_dependencies(s, c, eta, depth, depth_c):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(term, coord.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

if depth_c is not None and depth_c.shape != (1,):
msg = "Expected scalar {} coordinate {!r}: got shape {!r}.".format(
Expand Down Expand Up @@ -1466,7 +1466,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
# Swap bounds with points.
bds_shape = list(nd_points_by_key[key].shape) + [1]
bounds = nd_points_by_key[key].reshape(bds_shape)
Expand Down Expand Up @@ -1561,7 +1561,7 @@ def _check_dependencies(s, eta, depth, a, b, depth_c):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(term, coord.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

coords = ((a, "a"), (b, "b"), (depth_c, "depth_c"))
for coord, term in coords:
Expand Down Expand Up @@ -1658,7 +1658,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
# Swap bounds with points.
bds_shape = list(nd_points_by_key[key].shape) + [1]
bounds = nd_points_by_key[key].reshape(bds_shape)
Expand Down Expand Up @@ -1753,7 +1753,7 @@ def _check_dependencies(s, c, eta, depth, depth_c):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(term, coord.name())
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)

if depth_c is not None and depth_c.shape != (1,):
msg = "Expected scalar depth_c coordinate {!r}: got shape {!r}.".format(
Expand Down Expand Up @@ -1847,7 +1847,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
warn_once_at_level(msg, category=IrisIgnoringBoundsWarning, stacklevel=2)
# Swap bounds with points.
bds_shape = list(nd_points_by_key[key].shape) + [1]
bounds = nd_points_by_key[key].reshape(bds_shape)
Expand Down
6 changes: 3 additions & 3 deletions lib/iris/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import contextlib
import logging
import os.path
import warnings
from iris.exceptions import warn_once_at_level

import iris.exceptions

Expand Down Expand Up @@ -137,7 +137,7 @@ def get_dir_option(section, option, default=None):
"Ignoring config item {!r}:{!r} (section:option) as {!r}"
" is not a valid directory path."
)
warnings.warn(
warn_once_at_level(
msg.format(section, option, c_path),
category=iris.exceptions.IrisIgnoringWarning,
)
Expand Down Expand Up @@ -244,7 +244,7 @@ def __setattr__(self, name, value):
"Attempting to set invalid value {!r} for "
"attribute {!r}. Defaulting to {!r}."
)
warnings.warn(
warn_once_at_level(
wmsg.format(value, name, good_value),
category=iris.exceptions.IrisDefaultingWarning,
)
Expand Down
Loading