Skip to content

Commit

Permalink
Merge pull request #4 from rcaneill/rc_dev_orga
Browse files Browse the repository at this point in the history
Re-organizing code
  • Loading branch information
DocOtak authored Jan 27, 2022
2 parents dbdde80 + 34e3db2 commit 7c0433c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 143 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# macOS noise
.DS_Store

# emacs tmp
*~

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
71 changes: 1 addition & 70 deletions gsw_xarray/__init__.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,7 @@
__version__ = "0.1.0"
from functools import wraps

import gsw
import xarray as xr


_func_standard_name_units = (
("CT_from_enthalpy", "sea_water_conservative_temperature", "degC"),
("CT_from_enthalpy_exact", "sea_water_conservative_temperature", "degC"),
("CT_from_entropy", "sea_water_conservative_temperature", "degC"),
("CT_from_pt", "sea_water_conservative_temperature", "degC"),
("CT_from_rho", "sea_water_conservative_temperature", "degC"),
("CT_from_t", "sea_water_conservative_temperature", "degC"),
("C_from_SP", "sea_water_electrical_conductivity", "mS cm-1"),
("SA_from_SP", "sea_water_absolute_salinity", "g kg-1"),
("SA_from_SP_Baltic", "sea_water_absolute_salinity", "g kg-1"),
("SA_from_Sstar", "sea_water_absolute_salinity", "g kg-1"),
("SA_from_rho", "sea_water_absolute_salinity", "g kg-1"),
("SP_from_C", "sea_water_practical_salinity", "1"),
("SP_from_SA", "sea_water_practical_salinity", "1"),
("SP_from_SA_Baltic", "sea_water_practical_salinity", "1"),
("SP_from_SK", "sea_water_practical_salinity", "1"),
("SP_from_SR", "sea_water_practical_salinity", "1"),
("SP_from_Sstar", "sea_water_practical_salinity", "1"),
("SP_salinometer", "sea_water_practical_salinity", "1"),
("SR_from_SP", "sea_water_reference_salinity", "g kg-1"),
("Sstar_from_SA", "sea_water_preformed_salinity", "g kg-1"),
("Sstar_from_SP", "sea_water_preformed_salinity", "g kg-1"),
("f", "coriolis_parameter", "s-1"),
("p_from_z", "sea_water_pressure", "dbar"),
("pt0_from_t", "sea_water_potential_temperature", "degC"),
("pt_from_CT", "sea_water_potential_temperature", "degC"),
("pt_from_entropy", "sea_water_potential_temperature", "degC"),
# if p_ref is not 0 this next standard name is incorrect
("pt_from_t", "sea_water_potential_temperature", "degC"),
("rho", "sea_water_density", "kg m-3"),
("rho_t_exact", "sea_water_density", "kg m-3"),
("sigma0", "sea_water_sigma_t", "kg m-3"), # only applies to pressure=0
("sound_speed", "speed_of_sound_in_sea_water", "m s-1"),
("sound_speed_t_exact", "speed_of_sound_in_sea_water", "m s-1"),
("t_from_CT", "sea_water_temperature", "degC"),
# not sure how I feel about this next one
("z_from_p", "height_above_mean_sea_level", "m"),
)


def cf_attrs(standard_name, units, extra=None):
def cf_attrs_decorator(func):
@wraps(func)
def cf_attrs_wrapper(*args, **kwargs):
rv = func(*args, **kwargs)
if isinstance(rv, xr.DataArray):
rv.attrs["standard_name"] = standard_name
rv.attrs["units"] = units

return rv

return cf_attrs_wrapper

return cf_attrs_decorator


def _init_funcs():
_wrapped_funcs = {}
for func, name, units in _func_standard_name_units:
_wrapped_funcs[func] = cf_attrs(name, units)(getattr(gsw, func))
return _wrapped_funcs


_wrapped_funcs = _init_funcs()

from ._core import _wrapped_funcs

# See PEP 562
def __getattr__(name):
Expand All @@ -83,6 +15,5 @@ def __getattr__(name):
f"module {__name__} has no attribute {name}"
) from error


def __dir__():
return list(sorted(set([*_wrapped_funcs.keys(), *dir(gsw)])))
38 changes: 38 additions & 0 deletions gsw_xarray/_cf_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
_func_standard_name_units = (
("CT_from_enthalpy", "sea_water_conservative_temperature", "degC"),
("CT_from_enthalpy_exact", "sea_water_conservative_temperature", "degC"),
("CT_from_entropy", "sea_water_conservative_temperature", "degC"),
("CT_from_pt", "sea_water_conservative_temperature", "degC"),
("CT_from_rho", "sea_water_conservative_temperature", "degC"),
("CT_from_t", "sea_water_conservative_temperature", "degC"),
("C_from_SP", "sea_water_electrical_conductivity", "mS cm-1"),
("SA_from_SP", "sea_water_absolute_salinity", "g kg-1"),
("SA_from_SP_Baltic", "sea_water_absolute_salinity", "g kg-1"),
("SA_from_Sstar", "sea_water_absolute_salinity", "g kg-1"),
("SA_from_rho", "sea_water_absolute_salinity", "g kg-1"),
("SP_from_C", "sea_water_practical_salinity", "1"),
("SP_from_SA", "sea_water_practical_salinity", "1"),
("SP_from_SA_Baltic", "sea_water_practical_salinity", "1"),
("SP_from_SK", "sea_water_practical_salinity", "1"),
("SP_from_SR", "sea_water_practical_salinity", "1"),
("SP_from_Sstar", "sea_water_practical_salinity", "1"),
("SP_salinometer", "sea_water_practical_salinity", "1"),
("SR_from_SP", "sea_water_reference_salinity", "g kg-1"),
("Sstar_from_SA", "sea_water_preformed_salinity", "g kg-1"),
("Sstar_from_SP", "sea_water_preformed_salinity", "g kg-1"),
("f", "coriolis_parameter", "s-1"),
("p_from_z", "sea_water_pressure", "dbar"),
("pt0_from_t", "sea_water_potential_temperature", "degC"),
("pt_from_CT", "sea_water_potential_temperature", "degC"),
("pt_from_entropy", "sea_water_potential_temperature", "degC"),
# if p_ref is not 0 this next standard name is incorrect
("pt_from_t", "sea_water_potential_temperature", "degC"),
("rho", "sea_water_density", "kg m-3"),
("rho_t_exact", "sea_water_density", "kg m-3"),
("sigma0", "sea_water_sigma_t", "kg m-3"), # only applies to pressure=0
("sound_speed", "speed_of_sound_in_sea_water", "m s-1"),
("sound_speed_t_exact", "speed_of_sound_in_sea_water", "m s-1"),
("t_from_CT", "sea_water_temperature", "degC"),
# not sure how I feel about this next one
("z_from_p", "height_above_mean_sea_level", "m"),
)
30 changes: 30 additions & 0 deletions gsw_xarray/_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from functools import wraps

import gsw
import xarray as xr

from ._cf_names import _func_standard_name_units

def cf_attrs(standard_name, units, extra=None):
def cf_attrs_decorator(func):
@wraps(func)
def cf_attrs_wrapper(*args, **kwargs):
rv = func(*args, **kwargs)
if isinstance(rv, xr.DataArray):
rv.attrs["standard_name"] = standard_name
rv.attrs["units"] = units

return rv

return cf_attrs_wrapper

return cf_attrs_decorator

def _init_funcs():
_wrapped_funcs = {}
for func, name, units in _func_standard_name_units:
_wrapped_funcs[func] = cf_attrs(name, units)(getattr(gsw, func))
return _wrapped_funcs


_wrapped_funcs = _init_funcs()
24 changes: 24 additions & 0 deletions gsw_xarray/tests/test_gsw_xarray.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
from gsw_xarray import __version__
import gsw_xarray as gsw
import xarray as xr

def _create_ds():
import numpy as np
ds = xr.Dataset()
id = np.arange(3)
ds['id'] = xr.DataArray(id, coords={'id':id})
ds['CT'] = ds['id'] * 10
ds['CT'].attrs = {'standard_name':'sea_water_conservative_temperature'}
ds['SA'] = ds['id'] * 0.1 + 34
ds['SA'].attrs = {'standard_name':'sea_water_absolute_salinity'}
return ds

def test_version():
assert __version__ == "0.1.0"

def test_func_standard():
"""Basic test"""
ds = _create_ds()
sigma0 = gsw.sigma0(SA=ds.SA, CT=ds.CT)
assert sigma0.attrs['standard_name'] == 'sea_water_sigma_t'

def test_func_standard_module():
"""gsw can be used with modules, e.g. gsw.density"""
ds = _create_ds()
sigma0 = gsw.density.sigma0(SA=ds.SA, CT=ds.CT)
assert sigma0.attrs['standard_name'] == 'sea_water_sigma_t'
Loading

0 comments on commit 7c0433c

Please sign in to comment.