import re


def decode_sigma(ds,sigma_v):
    """
    ds: Dataset
    sigma_v: sigma coordinate variable.
    return DataArray of z coordinate implied by sigma_v
    """
    formula_terms=sigma_v.attrs['formula_terms']
    terms={}
    for hit in re.findall(r'\s*(\w+)\s*:\s*(\w+)', formula_terms):
        terms[hit[0]]=ds[hit[1]]

    # this is where xarray really shines -- it will promote z to the
    # correct dimensions automatically, by name
    # This ordering of the multiplication puts laydim last, which is
    # assumed in some other [fragile] code.
    # a little shady, but its helpful to make the ordering here intentional
    z=(terms['eta'] - terms['bedlevel'])*terms['sigma'] + terms['bedlevel']

    return z