Skip to content

Commit

Permalink
replace all warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenworsley committed Feb 10, 2024
1 parent 83859f7 commit 03731a4
Show file tree
Hide file tree
Showing 30 changed files with 155 additions and 154 deletions.
4 changes: 2 additions & 2 deletions lib/iris/_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

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 @@ -998,7 +998,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)
warn_once_at_level(msg)

# 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 @@ -8,7 +8,7 @@
"""

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, IrisDeprecation, stacklevel=stacklevel)
warn_once_at_level(msg, 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 @@ -6,7 +6,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 @@ -1136,6 +1136,6 @@ def regrid_reference_surface(
"Cannot update aux_factory {!r} because of dropped"
" coordinates.".format(factory.name())
)
warnings.warn(msg)
warn_once_at_level(msg)

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 @@ -11,7 +11,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(msg.format(circular, coord.circular, coord.name()))
warn_once_at_level(msg.format(circular, coord.circular, coord.name()))

if coord.ndim != 1:
raise iris.exceptions.CoordinateMultiDimError(coord)
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 @@ -10,7 +10,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 @@ -401,16 +401,16 @@ 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("Assuming spherical earth from ellipsoid.")
warn_once_at_level("Assuming spherical earth from ellipsoid.")
radius_of_earth = cs.semi_major_axis
elif isinstance(cs, iris.coord_systems.RotatedGeogCS) and (
cs.ellipsoid is not None
):
if cs.ellipsoid.inverse_flattening != 0.0:
warnings.warn("Assuming spherical earth from ellipsoid.")
warn_once_at_level("Assuming spherical earth from ellipsoid.")
radius_of_earth = cs.ellipsoid.semi_major_axis
else:
warnings.warn("Using DEFAULT_SPHERICAL_EARTH_RADIUS.")
warn_once_at_level("Using DEFAULT_SPHERICAL_EARTH_RADIUS.")
radius_of_earth = DEFAULT_SPHERICAL_EARTH_RADIUS

# Get the lon and lat coords and axes
Expand Down Expand Up @@ -548,7 +548,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.",
UserWarning,
Expand Down Expand Up @@ -663,7 +663,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."
)
Expand Down Expand Up @@ -851,7 +851,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(),
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 @@ -11,7 +11,7 @@
"""

import warnings
from iris.exceptions import warn_once_at_level

import numpy as np
from shapely.geometry import Polygon
Expand Down Expand Up @@ -72,7 +72,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.",
UserWarning,
)
Expand All @@ -82,7 +82,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.",
UserWarning,
)
Expand All @@ -92,7 +92,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.",
UserWarning,
)
Expand All @@ -102,7 +102,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.",
UserWarning,
)
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 @@ -12,7 +12,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 @@ -986,7 +986,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)
)
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 @@ -9,7 +9,7 @@
"""

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 @@ -441,7 +441,7 @@ def _check_dependencies(pressure_at_top, sigma, surface_air_pressure):
f"Coordinate '{coord.name()}' has bounds. These will "
"be disregarded"
)
warnings.warn(msg, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)

# Check units
if sigma.units.is_unknown():
Expand Down Expand Up @@ -520,15 +520,15 @@ 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"
)
pressure_at_top_pts = nd_points_by_key["pressure_at_top"]
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"
)
Expand Down Expand Up @@ -595,7 +595,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, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)

self.delta = delta
self.sigma = sigma
Expand Down Expand Up @@ -681,7 +681,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.",
UserWarning,
Expand Down Expand Up @@ -739,7 +739,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, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)
self.orography = new_coord


Expand Down Expand Up @@ -806,7 +806,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, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)

# Check units.
if sigma is not None and sigma.units.is_unknown():
Expand Down Expand Up @@ -896,7 +896,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."
)
Expand Down Expand Up @@ -1012,7 +1012,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, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, 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 @@ -1187,7 +1187,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, 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 @@ -1268,7 +1268,7 @@ def _check_dependencies(sigma, eta, depth):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(term, coord.name())
)
warnings.warn(msg, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)

# Check units.
if sigma is not None and sigma.units.is_unknown():
Expand Down Expand Up @@ -1349,7 +1349,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, 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 @@ -1444,7 +1444,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, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)

if depth_c is not None and depth_c.shape != (1,):
msg = (
Expand Down Expand Up @@ -1543,7 +1543,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, 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 @@ -1637,7 +1637,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, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)

coords = ((a, "a"), (b, "b"), (depth_c, "depth_c"))
for coord, term in coords:
Expand Down Expand Up @@ -1740,7 +1740,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, 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 @@ -1839,7 +1839,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, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, stacklevel=2)

if depth_c is not None and depth_c.shape != (1,):
msg = (
Expand Down Expand Up @@ -1938,7 +1938,7 @@ def make_coord(self, coord_dims_func):
"The {} coordinate {!r} has bounds. "
"These are being disregarded.".format(key, name)
)
warnings.warn(msg, UserWarning, stacklevel=2)
warn_once_at_level(msg, UserWarning, 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 @@ -34,7 +34,7 @@
import contextlib
import logging
import os.path
import warnings
from iris.exceptions import warn_once_at_level


def get_logger(
Expand Down Expand Up @@ -145,7 +145,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(msg.format(section, option, c_path))
warn_once_at_level(msg.format(section, option, c_path))
return path


Expand Down Expand Up @@ -251,7 +251,7 @@ def __setattr__(self, name, value):
"Attempting to set invalid value {!r} for "
"attribute {!r}. Defaulting to {!r}."
)
warnings.warn(wmsg.format(value, name, good_value))
warn_once_at_level(wmsg.format(value, name, good_value))
value = good_value
self.__dict__[name] = value

Expand Down
Loading

0 comments on commit 03731a4

Please sign in to comment.