Skip to content

Commit 3c73617

Browse files
authored
Merge pull request #134 from efiring/gibbs
Add gibbs and gibbs_ice via ufunc wrapping.
2 parents 2037d14 + 35a7e72 commit 3c73617

14 files changed

Lines changed: 912 additions & 202 deletions

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ repos:
1313
exclude: >
1414
(?x)^(
1515
.*\.c|
16-
tools/fix_wrapped_ufunc_typos\.py
16+
tools/fix_wrapped_ufunc_typos\.py|
17+
gsw/tests/test_gibbs\.py
1718
)$
1819
args:
1920
- --ignore-words-list=nin,preformed,wih,

gsw/_fixed_wrapped_ufuncs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Users should import only from non-private modules, of course.
44
"""
55

6+
import numpy
7+
68
from ._wrapped_ufuncs import *
79

810
_p_from_z = p_from_z
@@ -15,5 +17,25 @@ def z_from_p(p, lat, geo_strf_dyn_height=0, sea_surface_geopotential=0):
1517
return _z_from_p(p, lat, geo_strf_dyn_height, sea_surface_geopotential)
1618
z_from_p.__doc__ = _z_from_p.__doc__
1719

20+
_gibbs = gibbs
21+
def gibbs(ns, nt, np, SA, t, p):
22+
params = {"ns": ns, "nt": nt, "np": np}
23+
for k, v in params.items():
24+
u = numpy.unique(v)
25+
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}")
28+
return _gibbs(ns, nt, np, SA, t, p)
29+
gibbs.__doc__ = _gibbs.__doc__
1830

1931

32+
_gibbs_ice = gibbs_ice
33+
def gibbs_ice(nt, np, t, p):
34+
params = {"nt": nt, "np": np}
35+
for k, v in params.items():
36+
u = numpy.unique(v)
37+
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}")
40+
return _gibbs_ice(nt, np, t, p)
41+
gibbs_ice.__doc__ = _gibbs_ice.__doc__

gsw/_utilities.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ def wrapper(*args, **kw):
3636
hasmasked = np.any(ismasked)
3737
hasduck = np.any(isduck)
3838

39+
# Handle the leading integer arguments in gibbs and gibbs_ice.
40+
# Wrapped ufuncs are constructed with the "types" attribute from the
41+
# underlying ufunc.
42+
if hasattr(f, "types"):
43+
argtypes = f.types[0].split("->")[0]
44+
first_double = argtypes.index("d")
45+
else:
46+
first_double = 0
47+
48+
3949
def fixup(ret):
4050
if hasduck:
4151
return ret
@@ -50,7 +60,9 @@ def fixup(ret):
5060

5161
newargs = []
5262
for i, arg in enumerate(args):
53-
if ismasked[i]:
63+
if i < first_double:
64+
newargs.append(arg) # for gibbs and gibbs_ice
65+
elif ismasked[i]:
5466
newargs.append(masked_to_nan(arg))
5567
elif isduck[i]:
5668
newargs.append(arg)

0 commit comments

Comments
 (0)