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))
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))
Simple and efficient! (At least I like to think so ;-)
In [5]:
HTML(html)
Out[5]: