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

Add .cf.formula_terms #211

Closed
dcherian opened this issue Apr 20, 2021 · 0 comments · Fixed by #213
Closed

Add .cf.formula_terms #211

dcherian opened this issue Apr 20, 2021 · 0 comments · Fixed by #213

Comments

@dcherian
Copy link
Contributor

With ROMS, here's what the parametric coordinates look like

        double s_rho(s_rho) ;
                s_rho:long_name = "S-coordinate at RHO-points" ;
                s_rho:valid_min = -1. ;
                s_rho:valid_max = 0. ;
                s_rho:positive = "up" ;
                s_rho:standard_name = "ocean_s_coordinate_g2" ;
                s_rho:formula_terms = "s: s_rho C: Cs_r eta: zeta depth: h depth_c: hc" ;
                s_rho:field = "s_rho, scalar" ;
        double s_w(s_w) ;
                s_w:long_name = "S-coordinate at W-points" ;
                s_w:valid_min = -1. ;
                s_w:valid_max = 0. ;
                s_w:positive = "up" ;
                s_w:standard_name = "ocean_s_coordinate_g2" ;
                s_w:formula_terms = "s: s_w C: Cs_w eta: zeta depth: h depth_c: hc" ;
                s_w:field = "s_w, scalar" ;

I think the only way this makes sense is if we have

ds.cf.formula_terms = {
    "s_rho": {"s": ds["s_rho"], etc},
    "s_w": {"s": ds["s_w"], etc.}
}

Note we cannot use standard_name as keys because the standard name is the same for both s_w and s_rho.

For DataArrays we can also have

ds.s_rho.cf.formula_terms = {
    "s": ds["s_rho"], etc...
}

We just need refactor the terms dictionary from here:

if (
"formula_terms" not in ds[dim].attrs
or "standard_name" not in ds[dim].attrs
):
continue
formula_terms = ds[dim].attrs["formula_terms"]
stdname = ds[dim].attrs["standard_name"]
# map "standard" formula term names to actual variable names
terms = {}
for mapping in re.sub(": ", ":", formula_terms).split(" "):
key, value = mapping.split(":")
if value not in ds:
raise KeyError(
f"Variable {value!r} is required to decode coordinate for {dim} but it is absent in the Dataset."
)
terms[key] = ds[value]

Note the apparently more permissive implementation for a DataArray here:
https://github.com/NOAA-GFDL/MDTF-diagnostics/blob/70622b2fb9395de3a8ac3bc92c55c955e3d489b2/src/xr_parser.py#L264-L275

xref NOAA-GFDL/MDTF-diagnostics#186

dcherian added a commit to dcherian/cf-xarray that referenced this issue Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant