Skip to content

Add .cf.formula_terms #211

Closed
Closed
@dcherian

Description

@dcherian

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions