Skip to content

Commit 28ccff8

Browse files
authored
Merge pull request #27 from DocOtak/rcaneill-issue21
Change backend to using dict
2 parents c7a85a4 + ebb8d3b commit 28ccff8

File tree

8 files changed

+333
-61
lines changed

8 files changed

+333
-61
lines changed

docs/conf.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,35 @@
1717

1818
# -- Project information -----------------------------------------------------
1919

20-
project = 'gsw-xarray'
21-
copyright = '2022, Andrew Barna; Romain Caneill'
22-
author = 'Andrew Barna; Romain Caneill'
20+
project = "gsw-xarray"
21+
copyright = "2022, Andrew Barna; Romain Caneill"
22+
author = "Andrew Barna; Romain Caneill"
2323

2424

2525
# -- General configuration ---------------------------------------------------
2626

2727
# Add any Sphinx extension module names here, as strings. They can be
2828
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
2929
# ones.
30-
extensions = [
31-
]
30+
extensions = []
3231

3332
# Add any paths that contain templates here, relative to this directory.
34-
templates_path = ['_templates']
33+
templates_path = ["_templates"]
3534

3635
# List of patterns, relative to source directory, that match files and
3736
# directories to ignore when looking for source files.
3837
# This pattern also affects html_static_path and html_extra_path.
39-
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
38+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
4039

4140

4241
# -- Options for HTML output -------------------------------------------------
4342

4443
# The theme to use for HTML and HTML Help pages. See the documentation for
4544
# a list of builtin themes.
4645
#
47-
html_theme = 'furo'
46+
html_theme = "furo"
4847

4948
# Add any paths that contain custom static files (such as style sheets) here,
5049
# relative to this directory. They are copied after the builtin static files,
5150
# so a file named "default.css" will overwrite the builtin "default.css".
52-
html_static_path = ['_static']
51+
html_static_path = ["_static"]

gsw_xarray/_attributes.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
_func_attrs = {
2+
"CT_first_derivatives": (
3+
{
4+
"units": "K/(g/kg)",
5+
},
6+
{
7+
"units": "1",
8+
},
9+
),
10+
"CT_from_enthalpy": {
11+
"standard_name": "sea_water_conservative_temperature",
12+
"units": "degC",
13+
"reference_scale": "ITS-90",
14+
},
15+
"CT_from_enthalpy_exact": {
16+
"standard_name": "sea_water_conservative_temperature",
17+
"units": "degC",
18+
"reference_scale": "ITS-90",
19+
},
20+
"CT_from_entropy": {
21+
"standard_name": "sea_water_conservative_temperature",
22+
"units": "degC",
23+
"reference_scale": "ITS-90",
24+
},
25+
"CT_from_pt": {
26+
"standard_name": "sea_water_conservative_temperature",
27+
"units": "degC",
28+
"reference_scale": "ITS-90",
29+
},
30+
"CT_from_rho": {
31+
"standard_name": "sea_water_conservative_temperature",
32+
"units": "degC",
33+
"reference_scale": "ITS-90",
34+
},
35+
"CT_from_t": {
36+
"standard_name": "sea_water_conservative_temperature",
37+
"units": "degC",
38+
"reference_scale": "ITS-90",
39+
},
40+
"C_from_SP": {
41+
"standard_name": "sea_water_electrical_conductivity",
42+
"units": "mS/cm",
43+
},
44+
"SA_from_SP": {
45+
"standard_name": "sea_water_absolute_salinity",
46+
"units": "g/kg",
47+
},
48+
"SA_from_SP_Baltic": {
49+
"standard_name": "sea_water_absolute_salinity",
50+
"units": "g/kg",
51+
},
52+
"SA_from_Sstar": {
53+
"standard_name": "sea_water_absolute_salinity",
54+
"units": "g/kg",
55+
},
56+
"SA_from_rho": {
57+
"standard_name": "sea_water_absolute_salinity",
58+
"units": "g/kg",
59+
},
60+
"SP_from_C": {
61+
"standard_name": "sea_water_practical_salinity",
62+
"units": "1",
63+
},
64+
"SP_from_SA": {
65+
"standard_name": "sea_water_practical_salinity",
66+
"units": "1",
67+
},
68+
"SP_from_SA_Baltic": {
69+
"standard_name": "sea_water_practical_salinity",
70+
"units": "1",
71+
},
72+
"SP_from_SK": {
73+
"standard_name": "sea_water_practical_salinity",
74+
"units": "1",
75+
},
76+
"SP_from_SR": {
77+
"standard_name": "sea_water_practical_salinity",
78+
"units": "1",
79+
},
80+
"SP_from_Sstar": {
81+
"standard_name": "sea_water_practical_salinity",
82+
"units": "1",
83+
},
84+
"SP_salinometer": {
85+
"standard_name": "sea_water_practical_salinity",
86+
"units": "1",
87+
},
88+
"SR_from_SP": {
89+
"standard_name": "sea_water_reference_salinity",
90+
"units": "g/kg",
91+
},
92+
"Sstar_from_SA": {
93+
"standard_name": "sea_water_preformed_salinity",
94+
"units": "g/kg",
95+
},
96+
"Sstar_from_SP": {
97+
"standard_name": "sea_water_preformed_salinity",
98+
"units": "g/kg",
99+
},
100+
"f": {
101+
"standard_name": "coriolis_parameter",
102+
"units": "radians/s",
103+
},
104+
"p_from_z": {
105+
"standard_name": "sea_water_pressure",
106+
"units": "dbar",
107+
},
108+
"pt0_from_t": {
109+
"standard_name": "sea_water_potential_temperature",
110+
"units": "degC",
111+
"reference_scale": "ITS-90",
112+
},
113+
"pt_from_CT": {
114+
"standard_name": "sea_water_potential_temperature",
115+
"units": "degC",
116+
"reference_scale": "ITS-90",
117+
},
118+
"pt_from_entropy": {
119+
"standard_name": "sea_water_potential_temperature",
120+
"units": "degC",
121+
"reference_scale": "ITS-90",
122+
},
123+
"pt_from_t": {
124+
"standard_name": "sea_water_potential_temperature",
125+
"units": "degC",
126+
"reference_scale": "ITS-90",
127+
},
128+
"rho": {
129+
"standard_name": "sea_water_density",
130+
"units": "kg m^-3",
131+
},
132+
"rho_t_exact": {
133+
"standard_name": "sea_water_density",
134+
"units": "kg/m^3",
135+
},
136+
"sigma0": {
137+
"standard_name": "sea_water_sigma_t",
138+
"units": "kg/m^3",
139+
},
140+
"sound_speed": {
141+
"standard_name": "speed_of_sound_in_sea_water",
142+
"units": "m/s",
143+
},
144+
"sound_speed_t_exact": {
145+
"standard_name": "speed_of_sound_in_sea_water",
146+
"units": "m/s",
147+
},
148+
"t_from_CT": {
149+
"standard_name": "sea_water_temperature",
150+
"units": "degC",
151+
"reference_scale": "ITS-90",
152+
},
153+
"z_from_p": {
154+
"standard_name": "height_above_mean_sea_level",
155+
"units": "m",
156+
},
157+
}

gsw_xarray/_cf_names.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

gsw_xarray/_check_funcs.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def check_z_from_p(attrs, args, kwargs):
2+
"""
3+
If the 2 optional arguments are not 0, removes the standard name.
4+
5+
Reminder of the function call:
6+
gsw.z_from_p(p, lat, geo_strf_dyn_height=0, sea_surface_geopotential=0)
7+
8+
Parameters
9+
----------
10+
args and kwargs : list and dict
11+
the arguments passed to gsw.z_from_p
12+
attrs : dict
13+
the attributes associated with the function z_from_p
14+
(e.g. standard_name, units, etc)
15+
"""
16+
# Getting the values of optional parameters
17+
# If the values are in kwargs, all good
18+
# Otherwise, we get them from args
19+
try:
20+
geo_strf_dyn_height = kwargs["geo_strf_dyn_height"]
21+
except KeyError:
22+
try:
23+
geo_strf_dyn_height = args[2]
24+
except IndexError:
25+
geo_strf_dyn_height = 0 # Could use inspect.signature to get default value
26+
27+
try:
28+
sea_surface_geopotential = kwargs["sea_surface_geopotential"]
29+
except KeyError:
30+
try:
31+
sea_surface_geopotential = args[3]
32+
except IndexError:
33+
sea_surface_geopotential = (
34+
0 # Could use inspect.signature to get default value
35+
)
36+
37+
if geo_strf_dyn_height != 0 or sea_surface_geopotential != 0:
38+
attrs = attrs.copy()
39+
# necessary to use a copy, or it will pop the item from the original dict
40+
attrs.pop("standard_name")
41+
return attrs
42+
43+
44+
_check_funcs = {"z_from_p": check_z_from_p}

gsw_xarray/_core.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33
import gsw
44
import xarray as xr
55

6-
from ._cf_names import _func_standard_name_units
6+
from ._attributes import _func_attrs
7+
from ._names import _names
8+
from ._check_funcs import _check_funcs
79

810

9-
def cf_attrs(standard_name, units, extra=None):
11+
def add_attrs(rv, attrs, name):
12+
rv.name = name
13+
rv.attrs = attrs
14+
15+
16+
def cf_attrs(attrs, name, check_func):
1017
def cf_attrs_decorator(func):
1118
@wraps(func)
1219
def cf_attrs_wrapper(*args, **kwargs):
1320
rv = func(*args, **kwargs)
14-
if isinstance(rv, xr.DataArray):
15-
rv.attrs["standard_name"] = standard_name
16-
rv.attrs["units"] = units
17-
21+
attrs_checked = check_func(attrs, args, kwargs)
22+
if isinstance(rv, tuple):
23+
for (i, da) in enumerate(rv):
24+
add_attrs(da, attrs_checked[i], name[i])
25+
elif isinstance(rv, xr.DataArray):
26+
add_attrs(rv, attrs_checked, name)
1827
return rv
1928

2029
return cf_attrs_wrapper
@@ -24,8 +33,12 @@ def cf_attrs_wrapper(*args, **kwargs):
2433

2534
def _init_funcs():
2635
_wrapped_funcs = {}
27-
for func, name, units in _func_standard_name_units:
28-
_wrapped_funcs[func] = cf_attrs(name, units)(getattr(gsw, func))
36+
for func in _func_attrs.keys():
37+
_wrapped_funcs[func] = cf_attrs(
38+
_func_attrs[func],
39+
_names[func],
40+
_check_funcs.get(func, lambda attrs, *args, **kwargs: attrs),
41+
)(getattr(gsw, func))
2942
return _wrapped_funcs
3043

3144

gsw_xarray/_names.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
_names = {
2+
"CT_first_derivatives": ("CT_SA", "CT_pt"),
3+
"CT_from_enthalpy": "CT",
4+
"CT_from_enthalpy_exact": "CT",
5+
"CT_from_entropy": "CT",
6+
"CT_from_pt": "CT",
7+
"CT_from_rho": "CT",
8+
"CT_from_t": "CT",
9+
"C_from_SP": "C",
10+
"SA_from_SP": "SA",
11+
"SA_from_SP_Baltic": "SA",
12+
"SA_from_Sstar": "SA",
13+
"SA_from_rho": "SA",
14+
"SP_from_C": "SP",
15+
"SP_from_SA": "SP",
16+
"SP_from_SA_Baltic": "SP",
17+
"SP_from_SK": "SP",
18+
"SP_from_SR": "SP",
19+
"SP_from_Sstar": "SP",
20+
"SP_salinometer": "SP",
21+
"SR_from_SP": "SR",
22+
"Sstar_from_SA": "Sstar",
23+
"Sstar_from_SP": "Sstar",
24+
"f": "f",
25+
"p_from_z": "p",
26+
"pt0_from_t": "pt0",
27+
"pt_from_CT": "pt",
28+
"pt_from_entropy": "pt",
29+
"pt_from_t": "pt",
30+
"rho": "rho",
31+
"rho_t_exact": "rho",
32+
"sigma0": "sigma0",
33+
"sound_speed": "sound_speed",
34+
"sound_speed_t_exact": "sound_speed",
35+
"t_from_CT": "temperature",
36+
"z_from_p": "z",
37+
}

gsw_xarray/tests/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import xarray as xr
44
import numpy as np
55

6+
67
@pytest.fixture
78
def ds():
89
ds = xr.Dataset()
910
id = np.arange(3)
10-
ds['id'] = xr.DataArray(id, coords={'id':id})
11-
ds['CT'] = ds['id'] * 10
12-
ds['CT'].attrs = {'standard_name':'sea_water_conservative_temperature'}
13-
ds['SA'] = ds['id'] * 0.1 + 34
14-
ds['SA'].attrs = {'standard_name':'sea_water_absolute_salinity'}
11+
ds["id"] = xr.DataArray(id, coords={"id": id})
12+
ds["CT"] = ds["id"] * 10
13+
ds["CT"].attrs = {"standard_name": "sea_water_conservative_temperature"}
14+
ds["SA"] = ds["id"] * 0.1 + 34
15+
ds["SA"].attrs = {"standard_name": "sea_water_absolute_salinity"}
1516
return ds

0 commit comments

Comments
 (0)