Skip to content

Commit 2bad68c

Browse files
committed
p_from_z: raise ValueError if any z > 5
The criterion for raising the exception is taken from the Matlab version. Closes #223.
1 parent 81caf0f commit 2bad68c

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

gsw/_fixed_wrapped_ufuncs.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,50 @@
88
from ._wrapped_ufuncs import *
99

1010
_p_from_z = p_from_z
11+
12+
1113
def p_from_z(z, lat, geo_strf_dyn_height=0, sea_surface_geopotential=0):
14+
if numpy.ma.any(numpy.ma.asarray(z) > 5):
15+
raise ValueError(f"z should be generally negative; found max(z) = {numpy.ma.max(z)}")
1216
return _p_from_z(z, lat, geo_strf_dyn_height, sea_surface_geopotential)
17+
18+
1319
p_from_z.__doc__ = _p_from_z.__doc__
1420

1521
_z_from_p = z_from_p
22+
23+
1624
def z_from_p(p, lat, geo_strf_dyn_height=0, sea_surface_geopotential=0):
1725
return _z_from_p(p, lat, geo_strf_dyn_height, sea_surface_geopotential)
26+
27+
1828
z_from_p.__doc__ = _z_from_p.__doc__
1929

2030
_gibbs = gibbs
31+
32+
2133
def gibbs(ns, nt, np, SA, t, p):
2234
params = {"ns": ns, "nt": nt, "np": np}
2335
for k, v in params.items():
2436
u = numpy.unique(v)
2537
if u.min() < 0 or u.max() > 2 or u.dtype.kind != "i":
26-
raise ValueError("ns, nt, np must contain integers 0, 1, or 2;"
27-
f" found {k}={v}")
38+
raise ValueError(f"ns, nt, np must contain integers 0, 1, or 2; found {k}={v}")
2839
return _gibbs(ns, nt, np, SA, t, p)
40+
41+
2942
gibbs.__doc__ = _gibbs.__doc__
3043

3144

3245
_gibbs_ice = gibbs_ice
46+
47+
3348
def gibbs_ice(nt, np, t, p):
3449
params = {"nt": nt, "np": np}
3550
for k, v in params.items():
3651
u = numpy.unique(v)
3752
if u.min() < 0 or u.max() > 2 or u.dtype.kind != "i":
38-
raise ValueError("nt, np must contain integers 0, 1, or 2;"
39-
f" found {k}={v}")
53+
raise ValueError(f"nt, np must contain integers 0, 1, or 2; found {k}={v}")
4054
return _gibbs_ice(nt, np, t, p)
55+
56+
4157
gibbs_ice.__doc__ = _gibbs_ice.__doc__

gsw/_wrapped_ufuncs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4302,7 +4302,7 @@ def p_from_z(z, lat, geo_strf_dyn_height, sea_surface_geopotential):
43024302
Parameters
43034303
----------
43044304
z : array-like
4305-
Depth, positive up, m
4305+
Height, positive up (so z = -depth), m
43064306
lat : array-like
43074307
Latitude, -90 to 90 degrees
43084308
geo_strf_dyn_height : array-like

gsw/tests/test_fixups.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Tests of function modifications by _fixed_wrapped_ufuncs.py
3+
"""
4+
5+
import numpy as np
6+
import pytest
7+
8+
import gsw
9+
10+
zvals_ok = [
11+
-5500,
12+
np.linspace(-100, 0, 11),
13+
np.nan,
14+
[np.nan, -100],
15+
np.ma.masked_invalid([np.nan, -100]),
16+
]
17+
18+
zvals_bad = [
19+
5500,
20+
np.linspace(0, 100, 11),
21+
[np.nan, 100],
22+
np.ma.masked_invalid([np.nan, 100]),
23+
]
24+
25+
26+
@pytest.mark.parametrize("z", zvals_ok)
27+
def test_p_from_z_ok(z):
28+
# smoke test: doesn't raise an exception
29+
gsw.p_from_z(z, 30)
30+
31+
32+
@pytest.mark.parametrize("z", zvals_bad)
33+
def test_p_from_z_bad(z):
34+
with pytest.raises(ValueError):
35+
gsw.p_from_z(z, 30)

tools/docstring_parts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
pt = "Potential temperature referenced to a sea pressure, degrees C",
3737
rho = "Seawater density (not anomaly) in-situ, e.g., 1026 kg/m^3.",
3838
t_Ih = "In-situ temperature of ice (ITS-90), degrees C",
39-
z = "Depth, positive up, m",
39+
z = "Height, positive up (so z = -depth), m",
4040
SA_bulk = "bulk Absolute Salinity of the seawater and ice mixture, g/kg",
4141
w_Ih =
4242
"""mass fraction of ice: the mass of ice divided by the

0 commit comments

Comments
 (0)