Parametric Vertical Coordinates#

cf_xarray supports decoding parametric vertical coordinates encoded in the formula_terms attribute using Dataset.cf.decode_vertical_coords(). Right now, only the two ocean s-coordinates and ocean_sigma_coordinate are supported, but support for the rest should be easy to add (Pull Requests are very welcome!).

Decoding parametric coordinates#

from cf_xarray.datasets import romsds

romsds
<xarray.Dataset>
Dimensions:      (s_rho: 30, ocean_time: 2)
Coordinates:
  * s_rho        (s_rho) float64 -0.9833 -0.95 -0.9167 ... -0.05 -0.01667
    hc           float64 20.0
    h            float64 603.9
    Vtransform   float64 2.0
    Cs_r         (s_rho) float64 -0.933 -0.8092 ... -0.0005206 -5.758e-05
    z_rho_dummy  (ocean_time, s_rho) float64 -0.2111 -1.653 ... 0.6641 0.6351
Dimensions without coordinates: ocean_time
Data variables:
    zeta         (ocean_time) float64 -0.1554 -0.1274
    temp         (ocean_time, s_rho) float64 20.0 20.34 20.69 ... 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
<xarray.DataArray 'z_rho' (ocean_time: 2, s_rho: 30)>
-564.4 -491.5 -426.3 -368.5 -317.6 -273.1 ... -5.883 -4.08 -2.593 -1.399 -0.4837
Coordinates:
  * s_rho        (s_rho) float64 -0.9833 -0.95 -0.9167 ... -0.05 -0.01667
    hc           float64 20.0
    h            float64 603.9
    Vtransform   float64 2.0
    Cs_r         (s_rho) float64 -0.933 -0.8092 ... -0.0005206 -5.758e-05
    z_rho_dummy  (ocean_time, s_rho) float64 -0.2111 -1.653 ... 0.6641 0.6351
    z_rho        (ocean_time, s_rho) float64 -564.4 -491.5 ... -1.399 -0.4837
Dimensions without coordinates: ocean_time

Formula terms#

To see whether decoding is possible, use the Dataset.cf.formula_terms attribute

romsds.cf.formula_terms
{'s_rho': {'s': 's_rho',
  'C': 'Cs_r',
  'eta': 'zeta',
  'depth': 'h',
  'depth_c': 'hc'}}