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

Numpy 2 compatibility #3491

Merged
merged 5 commits into from
May 10, 2024
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
12 changes: 9 additions & 3 deletions src/metpy/calc/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"""Contains calculation of various derived indices."""
import numpy as np

# Can drop fallback once we rely on numpy>=2
try:
from numpy import trapezoid
except ImportError:
from numpy import trapz as trapezoid

from .basic import wind_speed
from .thermo import mixing_ratio, saturation_vapor_pressure
from .tools import _remove_nans, get_layer
Expand Down Expand Up @@ -90,7 +96,7 @@ def precipitable_water(pressure, dewpoint, *, bottom=None, top=None):
w = mixing_ratio(saturation_vapor_pressure(dewpoint_layer), pres_layer)

# Since pressure is in decreasing order, pw will be the opposite sign of that expected.
pw = -np.trapz(w, pres_layer) / (mpconsts.g * mpconsts.rho_l)
pw = -trapezoid(w, pres_layer) / (mpconsts.g * mpconsts.rho_l)
return pw.to('millimeters')


Expand Down Expand Up @@ -168,7 +174,7 @@ def mean_pressure_weighted(pressure, *args, height=None, bottom=None, depth=None
pres_int = 0.5 * (pres_prof[-1] ** 2 - pres_prof[0] ** 2)

# Perform integration on the profile for each variable
return [np.trapz(var_prof * pres_prof, x=pres_prof) / pres_int for var_prof in others]
return [trapezoid(var_prof * pres_prof, x=pres_prof) / pres_int for var_prof in others]


@exporter.export
Expand Down Expand Up @@ -228,7 +234,7 @@ def weighted_continuous_average(pressure, *args, height=None, bottom=None, depth
pressure, *args, height=height, bottom=bottom, depth=depth
)

return [np.trapz(var_prof, x=pres_prof) / (pres_prof[-1] - pres_prof[0])
return [trapezoid(var_prof, x=pres_prof) / (pres_prof[-1] - pres_prof[0])
for var_prof in others]


Expand Down
17 changes: 12 additions & 5 deletions src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
from inspect import Parameter, Signature, signature

import numpy as np

# Can drop fallback once we rely on numpy>=2
try:
from numpy import trapezoid
except ImportError:
from numpy import trapz as trapezoid

import scipy.integrate as si
import scipy.optimize as so
import xarray as xr
Expand Down Expand Up @@ -2455,15 +2462,15 @@ def cape_cin(pressure, temperature, dewpoint, parcel_profile, which_lfc='bottom'
x_clipped = x[p_mask].magnitude
y_clipped = y[p_mask].magnitude
cape = (mpconsts.Rd
* units.Quantity(np.trapz(y_clipped, np.log(x_clipped)), 'K')).to(units('J/kg'))
* units.Quantity(trapezoid(y_clipped, np.log(x_clipped)), 'K')).to(units('J/kg'))

# CIN
# Only use data between the surface and LFC for calculation
p_mask = _greater_or_close(x.m, lfc_pressure)
x_clipped = x[p_mask].magnitude
y_clipped = y[p_mask].magnitude
cin = (mpconsts.Rd
* units.Quantity(np.trapz(y_clipped, np.log(x_clipped)), 'K')).to(units('J/kg'))
* units.Quantity(trapezoid(y_clipped, np.log(x_clipped)), 'K')).to(units('J/kg'))

# Set CIN to 0 if it's returned as a positive value (#1190)
if cin > units.Quantity(0, 'J/kg'):
Expand Down Expand Up @@ -3240,7 +3247,7 @@ def downdraft_cape(pressure, temperature, dewpoint):

# Find DCAPE
dcape = -(mpconsts.Rd
* units.Quantity(np.trapz(diff, lnp), 'K')
* units.Quantity(trapezoid(diff, lnp), 'K')
).to(units('J/kg'))

return dcape, down_pressure, down_parcel_trace
Expand Down Expand Up @@ -3439,7 +3446,7 @@ def mixed_layer(pressure, *args, height=None, bottom=None, depth=None, interpola
ret = []
for datavar_layer in datavars_layer:
actual_depth = abs(p_layer[0] - p_layer[-1])
ret.append(units.Quantity(np.trapz(datavar_layer.m, p_layer.m) / -actual_depth.m,
ret.append(units.Quantity(trapezoid(datavar_layer.m, p_layer.m) / -actual_depth.m,
datavar_layer.units))
return ret

Expand Down Expand Up @@ -3691,7 +3698,7 @@ def thickness_hydrostatic(pressure, temperature, mixing_ratio=None,
# Take the integral
return (
-mpconsts.nounit.Rd / mpconsts.nounit.g
* np.trapz(layer_virttemp, np.log(layer_p))
* trapezoid(layer_virttemp, np.log(layer_p))
)


Expand Down
8 changes: 7 additions & 1 deletion src/metpy/calc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
from operator import itemgetter

import numpy as np
from numpy.core.numeric import normalize_axis_index

# Can drop fallback once we rely on numpy>=2
try:
from numpy.lib.array_utils import normalize_axis_index
except ImportError:
from numpy.core.numeric import normalize_axis_index

import numpy.ma as ma
from pyproj import CRS, Geod, Proj
from scipy.spatial import cKDTree
Expand Down
2 changes: 1 addition & 1 deletion src/metpy/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _parse_version_spec(version_spec):
tuple of str : Parsed specification groups of package name, comparison, and version

"""
pattern = re.compile(r'(\w+)\s*([<>!=]+)\s*([\d.]+)')
pattern = re.compile(r'(\w+)\s*([<>!=]+)\s*([\d\w.]+)')
match = pattern.match(version_spec)

if not match:
Expand Down
2 changes: 1 addition & 1 deletion tests/calc/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ def test_isentropic_pressure_interp(press_3d):
"""Test calculation of isentropic pressure function."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
if press_3d:
lev = np.lib.stride_tricks.broadcast_to(lev[:, None, None], (4, 5, 5))
lev = np.broadcast_to(lev[:, None, None], (4, 5, 5))
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
Expand Down
2 changes: 1 addition & 1 deletion tests/plots/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@ def test_declarative_plot_geometry_lines(ccrs):
geo.stroke = 'green'
geo.labels = ['Irma', '+/- 0.25 deg latitude']
geo.label_facecolor = None
geo.mpl_args = {'linewidth': 1}
geo.mpl_args = {'linewidths': 1}

# Place plot in a panel and container
panel = MapPanel()
Expand Down