Skip to content

Commit 62ff402

Browse files
jasonb5dcherianpre-commit-ci[bot]
authored
Adds support for parametric vertical coordinate (#528)
* Adds initial parametric functions * Updates parametric module and adds tests * Adds function to help transpose outputs * Removes hardcoded dims * Fixes optional argument bug and handling case insensitive terms * Moves from function to class based implementation of transforms * Fixes failing test * Removes extras spaces in text * Fixes formatting * Resolves mypy errors * Removes redundant code * Fixes global variable case * Updates _derive_ocean_stdname * Moves to dataclass * Fixes passing variables to class constructor * Removes redundant code * Fixes handling unknown standard names Co-authored-by: Deepak Cherian <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removes squeeze and fixes constants * Adds entry to CITATIONS.cff * cleanup * tweak docs --------- Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deepak Cherian <[email protected]>
1 parent f474e5c commit 62ff402

File tree

6 files changed

+1467
-84
lines changed

6 files changed

+1467
-84
lines changed

CITATION.cff

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ authors:
8383
- family-names: Haëck
8484
given-names: Clément
8585
affiliation: Laboratoire d'Océanographie et du Climat (LOCEAN), Paris
86+
- family-names: Boutte
87+
given-names: Jason
88+
orcid: 'https://orcid.org/0009-0009-3996-3772'
89+
affiliation: Lawrence Livermore National Laboratory
8690
identifiers:
8791
- type: doi
8892
value: 10.5281/zenodo.4749735

cf_xarray/accessor.py

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from xarray.core.rolling import Coarsen, Rolling
3131
from xarray.core.weighted import Weighted
3232

33-
from . import sgrid
33+
from . import parametric, sgrid
3434
from .criteria import (
3535
_DSG_ROLES,
3636
_GEOMETRY_TYPES,
@@ -2754,13 +2754,8 @@ def decode_vertical_coords(self, *, outnames=None, prefix=None):
27542754
"""
27552755
ds = self._obj
27562756

2757-
requirements = {
2758-
"ocean_s_coordinate_g1": {"depth_c", "depth", "s", "C", "eta"},
2759-
"ocean_s_coordinate_g2": {"depth_c", "depth", "s", "C", "eta"},
2760-
"ocean_sigma_coordinate": {"sigma", "eta", "depth"},
2761-
}
2762-
27632757
allterms = self.formula_terms
2758+
27642759
for dim in allterms:
27652760
if prefix is None:
27662761
assert (
@@ -2782,6 +2777,7 @@ def decode_vertical_coords(self, *, outnames=None, prefix=None):
27822777
suffix = dim.split("_")
27832778
zname = f"{prefix}_" + "_".join(suffix[1:])
27842779

2780+
# never touched, if standard name is missing it's not included in allterms
27852781
if "standard_name" not in ds[dim].attrs:
27862782
continue
27872783
stdname = ds[dim].attrs["standard_name"]
@@ -2790,46 +2786,23 @@ def decode_vertical_coords(self, *, outnames=None, prefix=None):
27902786
terms = {}
27912787
for key, value in allterms[dim].items():
27922788
if value not in ds:
2789+
# is this ever hit, if variable is missing it's missing in decoded allterms
27932790
raise KeyError(
27942791
f"Variable {value!r} is required to decode coordinate for {dim!r}"
27952792
" but it is absent in the Dataset."
27962793
)
2797-
terms[key] = ds[value]
2798-
2799-
absent_terms = requirements[stdname] - set(terms)
2800-
if absent_terms:
2801-
raise KeyError(f"Required terms {absent_terms} absent in dataset.")
2802-
2803-
if stdname == "ocean_s_coordinate_g1":
2804-
# S(k,j,i) = depth_c * s(k) + (depth(j,i) - depth_c) * C(k)
2805-
S = (
2806-
terms["depth_c"] * terms["s"]
2807-
+ (terms["depth"] - terms["depth_c"]) * terms["C"]
2808-
)
2809-
2810-
# z(n,k,j,i) = S(k,j,i) + eta(n,j,i) * (1 + S(k,j,i) / depth(j,i))
2811-
ztemp = S + terms["eta"] * (1 + S / terms["depth"])
2794+
# keys should be case insensitive
2795+
terms[key.lower()] = ds[value]
28122796

2813-
elif stdname == "ocean_s_coordinate_g2":
2814-
# make sure all necessary terms are present in terms
2815-
# (depth_c * s(k) + depth(j,i) * C(k)) / (depth_c + depth(j,i))
2816-
S = (terms["depth_c"] * terms["s"] + terms["depth"] * terms["C"]) / (
2817-
terms["depth_c"] + terms["depth"]
2818-
)
2819-
2820-
# z(n,k,j,i) = eta(n,j,i) + (eta(n,j,i) + depth(j,i)) * S(k,j,i)
2821-
ztemp = terms["eta"] + (terms["eta"] + terms["depth"]) * S
2822-
2823-
elif stdname == "ocean_sigma_coordinate":
2824-
# z(n,k,j,i) = eta(n,j,i) + sigma(k)*(depth(j,i)+eta(n,j,i))
2825-
ztemp = terms["eta"] + terms["sigma"] * (terms["depth"] + terms["eta"])
2826-
2827-
else:
2797+
try:
2798+
transform = parametric.TRANSFORM_FROM_STDNAME[stdname]
2799+
except KeyError:
2800+
# Should occur since stdname is check before
28282801
raise NotImplementedError(
2829-
f"Coordinate function for {stdname!r} not implemented yet. Contributions welcome!"
2830-
)
2802+
f"Coordinate function for {stdname!r} not implmented yet. Contributions welcome!"
2803+
) from None
28312804

2832-
ds.coords[zname] = ztemp
2805+
ds.coords[zname] = transform.from_terms(terms)
28332806

28342807

28352808
@xr.register_dataarray_accessor("cf")

0 commit comments

Comments
 (0)