Skip to content

Commit f935d4a

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 f935d4a

4 files changed

Lines changed: 80 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Tests of function modifications by _fixed_wrapped_ufuncs.py.
3+
"""
4+
5+
import numpy as np
6+
import pytest
7+
from numpy.testing import assert_allclose
8+
9+
import gsw
10+
11+
# In the following pairs of z, p, the expected values for p are simply the
12+
# values returned by z_from_p on an M4 Mac with gsw version
13+
# 3.6.22.dev3+g2bad68c16 (prior to the commit in which this test is added).
14+
15+
zpvals_ok = [
16+
(-5500, 5609.875428946266),
17+
(
18+
np.linspace(-100, 0, 11),
19+
np.array(
20+
[
21+
100.70968879,
22+
90.6365065,
23+
80.56381587,
24+
70.49161697,
25+
60.41990988,
26+
50.34869469,
27+
40.27797148,
28+
30.20774032,
29+
20.1380013,
30+
10.0687545,
31+
-0.0,
32+
]
33+
),
34+
),
35+
(np.nan, np.nan),
36+
([np.nan, -100], np.array([np.nan, 100.70968879])),
37+
(np.ma.masked_invalid([np.nan, -100]), np.ma.masked_invalid([np.nan, 100.70968879])),
38+
]
39+
40+
zvals_bad = [
41+
5500,
42+
np.linspace(0, 100, 11),
43+
[np.nan, 100],
44+
np.ma.masked_invalid([np.nan, 100]),
45+
]
46+
47+
48+
@pytest.mark.parametrize("zp", zpvals_ok)
49+
def test_p_from_z_ok(zp):
50+
z, expected = zp
51+
p = gsw.p_from_z(z, 30)
52+
assert_allclose(p, expected)
53+
54+
55+
@pytest.mark.parametrize("z", zvals_bad)
56+
def test_p_from_z_bad(z):
57+
with pytest.raises(ValueError):
58+
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)