python4oceanographers

Turning ripples into waves

Finding CF-standard Names

The CF conventions uses standard names for any variable stored using the CF-rules. I often find myself looking for possible names for a specific variable (e.g.: currents or temperature). Sadly, the answer is not that easy. Take a look at the CF Standard Names table and check confusion you get when searching for velocity.

This post is a quick hack in the xml standard names table to quickly find the proper names. Finding the names is useful when trying to query data that uses CF-rules.

In [2]:
from xml.etree import ElementTree


def remove_name(exclude, standard_name):
    excluded = False
    for e in exclude:
        if e in standard_name:
            excluded = True
    return excluded


def get_standard_names(hints, units, strict=False, exclude=[]):
    """If `hints` and `units` are True return `standard_name` in the list.
    Searches for `hints` in the description+standard_name, searches and units
    in the canonical_units field."""

    standard_names = []
    tree = ElementTree.parse('./data/cf-standard-name-table.xml')
    root = tree.getroot()
    entries = root.findall('entry')
    for entry in entries:
        standard_name = entry.get('id')
        canonical_units = entry.find('canonical_units').text
        description = entry.find('description').text
        if description:
            description = '{} {}'.format(description.lower(), standard_name)
        else:
            description = '{}'.format(standard_name)
        description = set(description.lower().strip().split())

        if strict:
            remove = remove_name(exclude, standard_name)
            if (set(hints) < description and units == canonical_units and not remove):
                standard_names.append(standard_name)
        else:
            for hint in hints:
                remove = remove_name(exclude, standard_name)
                if (hint in description and units == canonical_units and not remove):
                    standard_names.append(standard_name)
    return standard_names
In [3]:
hints = ['temperature', 'sea']
exclude = ['air']
units = 'K'
CF_names = get_standard_names(hints=hints, units=units, strict=True, exclude=exclude)

print('\n'.join(CF_names))
change_over_time_in_sea_water_conservative_temperature
change_over_time_in_sea_water_potential_temperature
change_over_time_in_sea_water_temperature
equivalent_potential_temperature
pseudo_equivalent_potential_temperature
sea_ice_surface_temperature
sea_surface_foundation_temperature
sea_surface_skin_temperature
sea_surface_subskin_temperature
sea_surface_temperature
sea_water_conservative_temperature
sea_water_potential_temperature
sea_water_temperature
temperature_of_sensor_for_oxygen_in_sea_water

In [4]:
hints = ['velocity']
exclude = ['ice', 'land', 'wind', 'air']
units = 'm s-1'
CF_names = get_standard_names(hints=hints, units=units, exclude=exclude)

print('\n'.join(CF_names))
baroclinic_eastward_sea_water_velocity
baroclinic_northward_sea_water_velocity
barotropic_eastward_sea_water_velocity
barotropic_northward_sea_water_velocity
barotropic_sea_water_x_velocity
barotropic_sea_water_y_velocity
bolus_eastward_sea_water_velocity
bolus_northward_sea_water_velocity
bolus_sea_water_x_velocity
bolus_sea_water_y_velocity
bolus_upward_sea_water_velocity
eastward_sea_water_velocity
eastward_sea_water_velocity_assuming_no_tide
northward_sea_water_velocity
northward_sea_water_velocity_assuming_no_tide
product_of_eastward_sea_water_velocity_and_salinity
product_of_northward_sea_water_velocity_and_salinity
radial_sea_water_velocity_away_from_instrument
radial_velocity_of_scatterers_away_from_instrument
sea_water_x_velocity
sea_water_y_velocity
surface_eastward_sea_water_velocity
surface_geostrophic_eastward_sea_water_velocity
surface_geostrophic_eastward_sea_water_velocity_assuming_sea_level_for_geoid
surface_geostrophic_northward_sea_water_velocity
surface_geostrophic_northward_sea_water_velocity_assuming_sea_level_for_geoid
surface_geostrophic_sea_water_x_velocity
surface_geostrophic_sea_water_x_velocity_assuming_sea_level_for_geoid
surface_geostrophic_sea_water_y_velocity
surface_geostrophic_sea_water_y_velocity_assuming_sea_level_for_geoid
surface_northward_sea_water_velocity
upward_sea_water_velocity

Simple and efficient! (At least I like to think so ;-)

In [5]:
HTML(html)
Out[5]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.

Comments