Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for parametric vertical coordinate #528

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
94a0ce0
Adds initial parametric functions
jasonb5 May 24, 2024
4bb4c02
Updates parametric module and adds tests
jasonb5 Jun 7, 2024
49838e6
Adds function to help transpose outputs
jasonb5 Jun 18, 2024
2bacb6f
Removes hardcoded dims
jasonb5 Jul 17, 2024
e37173a
Fixes optional argument bug and handling case insensitive terms
jasonb5 Jul 18, 2024
622d8a4
Merge branch 'main' into adds-parametric-vertical-coordinate
jasonb5 Jul 18, 2024
24aa5df
Moves from function to class based implementation of transforms
jasonb5 Aug 17, 2024
c76828b
Fixes failing test
jasonb5 Aug 20, 2024
2e02ef9
Removes extras spaces in text
jasonb5 Aug 20, 2024
08170d1
Fixes formatting
jasonb5 Aug 20, 2024
80d1214
Resolves mypy errors
jasonb5 Aug 20, 2024
381ea30
Removes redundant code
jasonb5 Aug 21, 2024
54f6492
Fixes global variable case
jasonb5 Aug 21, 2024
32eae7e
Updates _derive_ocean_stdname
jasonb5 Aug 21, 2024
9d22976
Moves to dataclass
jasonb5 Aug 21, 2024
9396ef8
Fixes passing variables to class constructor
jasonb5 Aug 21, 2024
9c16996
Removes redundant code
jasonb5 Aug 21, 2024
4f3886a
Merge branch 'main' into adds-parametric-vertical-coordinate
dcherian Aug 21, 2024
cfd919e
Fixes handling unknown standard names
jasonb5 Aug 21, 2024
1c5d49f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 21, 2024
f3a6928
Merge remote-tracking branch 'upstream/main' into adds-parametric-ver…
jasonb5 Oct 10, 2024
a966919
Removes squeeze and fixes constants
jasonb5 Oct 11, 2024
ca33c67
Adds entry to CITATIONS.cff
jasonb5 Oct 11, 2024
978948f
cleanup
dcherian Oct 22, 2024
c9849f6
tweak docs
dcherian Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 13 additions & 40 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from xarray.core.rolling import Coarsen, Rolling
from xarray.core.weighted import Weighted

from . import sgrid
from . import parametric, sgrid
from .criteria import (
_DSG_ROLES,
_GEOMETRY_TYPES,
Expand Down Expand Up @@ -2748,13 +2748,8 @@
"""
ds = self._obj

requirements = {
"ocean_s_coordinate_g1": {"depth_c", "depth", "s", "C", "eta"},
"ocean_s_coordinate_g2": {"depth_c", "depth", "s", "C", "eta"},
"ocean_sigma_coordinate": {"sigma", "eta", "depth"},
}

allterms = self.formula_terms

for dim in allterms:
if prefix is None:
assert (
Expand All @@ -2776,6 +2771,7 @@
suffix = dim.split("_")
zname = f"{prefix}_" + "_".join(suffix[1:])

# never touched, if standard name is missing it's not included in allterms
if "standard_name" not in ds[dim].attrs:
continue
stdname = ds[dim].attrs["standard_name"]
Expand All @@ -2784,46 +2780,23 @@
terms = {}
for key, value in allterms[dim].items():
if value not in ds:
# is this ever hit, if variable is missing it's missing in decoded allterms
raise KeyError(
f"Variable {value!r} is required to decode coordinate for {dim!r}"
" but it is absent in the Dataset."
)
terms[key] = ds[value]

absent_terms = requirements[stdname] - set(terms)
if absent_terms:
raise KeyError(f"Required terms {absent_terms} absent in dataset.")

if stdname == "ocean_s_coordinate_g1":
# S(k,j,i) = depth_c * s(k) + (depth(j,i) - depth_c) * C(k)
S = (
terms["depth_c"] * terms["s"]
+ (terms["depth"] - terms["depth_c"]) * terms["C"]
)

# z(n,k,j,i) = S(k,j,i) + eta(n,j,i) * (1 + S(k,j,i) / depth(j,i))
ztemp = S + terms["eta"] * (1 + S / terms["depth"])
# keys should be case insensitive
terms[key.lower()] = ds[value]

elif stdname == "ocean_s_coordinate_g2":
# make sure all necessary terms are present in terms
# (depth_c * s(k) + depth(j,i) * C(k)) / (depth_c + depth(j,i))
S = (terms["depth_c"] * terms["s"] + terms["depth"] * terms["C"]) / (
terms["depth_c"] + terms["depth"]
)

# z(n,k,j,i) = eta(n,j,i) + (eta(n,j,i) + depth(j,i)) * S(k,j,i)
ztemp = terms["eta"] + (terms["eta"] + terms["depth"]) * S

elif stdname == "ocean_sigma_coordinate":
# z(n,k,j,i) = eta(n,j,i) + sigma(k)*(depth(j,i)+eta(n,j,i))
ztemp = terms["eta"] + terms["sigma"] * (terms["depth"] + terms["eta"])

else:
try:
transform = parametric.TRANSFORM_FROM_STDNAME[stdname]
except KeyError:

Check warning on line 2793 in cf_xarray/accessor.py

View check run for this annotation

Codecov / codecov/patch

cf_xarray/accessor.py#L2793

Added line #L2793 was not covered by tests
# Should occur since stdname is check before
raise NotImplementedError(
f"Coordinate function for {stdname!r} not implemented yet. Contributions welcome!"
)
f"Coordinate function for {stdname!r} not implmented yet. Contributions welcome!"
) from None

ds.coords[zname] = ztemp
ds.coords[zname] = transform.from_terms(terms)


@xr.register_dataarray_accessor("cf")
Expand Down
Loading
Loading