Parametric Vertical Coordinates¶
See also
cf_xarray
supports decoding parametric vertical coordinates encoded in the formula_terms
attribute using Dataset.cf.decode_vertical_coords()
.
Decoding parametric coordinates¶
from cf_xarray.datasets import romsds
romsds
<xarray.Dataset> Size: 1kB Dimensions: (s_rho: 30, ocean_time: 2) Coordinates: * s_rho (s_rho) float64 240B -0.9833 -0.95 -0.9167 ... -0.05 -0.01667 hc float64 8B 20.0 h float64 8B 603.9 Vtransform float64 8B 2.0 Cs_r (s_rho) float64 240B -0.933 -0.8092 ... -0.0005206 -5.758e-05 z_rho_dummy (ocean_time, s_rho) float64 480B -0.1464 2.083 ... 0.5933 Dimensions without coordinates: ocean_time Data variables: zeta (ocean_time) float64 16B -0.1554 -0.1274 temp (ocean_time, s_rho) float64 480B 20.0 20.34 ... 29.66 30.0
Now we decode the vertical coordinates in-place. Note the new z_rho
variable. cf_xarray
sees that s_rho
has a formula_terms
attribute, looks up the right formula using s_rho.attrs["standard_name"]
and computes a new vertical coordinate variable.
romsds.cf.decode_vertical_coords(outnames={'s_rho': 'z_rho'}) # adds new z_rho variable
romsds.z_rho
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[3], line 1
----> 1 romsds.cf.decode_vertical_coords(outnames={'s_rho': 'z_rho'}) # adds new z_rho variable
2 romsds.z_rho
File ~/checkouts/readthedocs.org/user_builds/cf-xarray/checkouts/latest/cf_xarray/accessor.py:2834, in CFDatasetAccessor.decode_vertical_coords(self, outnames, prefix)
2828 except KeyError:
2829 # Should occur since stdname is check before
2830 raise NotImplementedError(
2831 f"Coordinate function for {stdname!r} not implmented yet. Contributions welcome!"
2832 ) from None
-> 2834 ds.coords[zname] = transform.from_terms(terms).decode()
File ~/checkouts/readthedocs.org/user_builds/cf-xarray/checkouts/latest/cf_xarray/parametric.py:636, in OceanSG2.decode(self)
632 S = (self.depth_c * self.s + self.depth * self.c) / (self.depth_c + self.depth)
634 z = self.eta + (self.eta + self.depth) * S
--> 636 return z.assign_attrs(standard_name=self.computed_standard_name)
File ~/checkouts/readthedocs.org/user_builds/cf-xarray/checkouts/latest/cf_xarray/parametric.py:641, in OceanSG2.computed_standard_name(self)
638 @property
639 def computed_standard_name(self) -> str:
640 """Computes coordinate standard name."""
--> 641 return _derive_ocean_stdname(eta=self.eta.attrs, depth=self.depth.attrs)
File ~/checkouts/readthedocs.org/user_builds/cf-xarray/checkouts/latest/cf_xarray/parametric.py:81, in _derive_ocean_stdname(zlev, eta, depth)
77 raise ValueError(
78 f"The values for {', '.join(sorted(search_vars.keys()))} cannot be `None`."
79 ) from None
80 except KeyError:
---> 81 raise ValueError(
82 f"The standard name for the {x!r} variable is not available."
83 ) from None
85 for x, y in OCEAN_STDNAME_MAP.items():
86 check_term = "".join(
87 [
88 y[i]
(...) 91 ]
92 )
ValueError: The standard name for the 'depth' variable is not available.
Formula terms¶
To see whether decoding is possible, use the Dataset.cf.formula_terms
attribute
romsds.cf.formula_terms