Skip to content

CF Checker in python

Ag Stephens edited this page Jun 8, 2017 · 1 revision

Calling the CF-Checker inside Python (and interpreting the results)

Get your environment set up

Firstly, you might need to set up a python environment (here we use virtualenv) and activate it:

$ virtualenv venv_cfchecker
$  . venv_cfchecker/bin/activate

Pip install cfchecker into the virtualenv:

$ pip install cfchecker

Which version is it?

$ python -c 'import cfchecker as c; print c.__version__'
3.0.4

Enter python shell and run the CF-Checker interactively

Let's see how to extract the output from the CF-Checker inside a python session:

$ python
>>> from cfchecker import cfchecks
>>> checker = cfchecks.CFChecker()
>>> res = checker.checker("/badc/cmip5/data/cmip5/output1/MOHC/HadGEM2-ES/rcp85/day/atmos/day/r1i1p1/v20111128/tas/tas_day_HadGEM2-ES_rcp85_r1i1p1_22991201-22991230.nc")

>>> print res
{'variables': OrderedDict([('time', {'WARN': [], 'INFO': [], 'VERSION': [], 'FATAL': [], 'ERROR': []}), ('time_bnds', {'WARN': [], 'INFO': [], 'VERSION': [], 'FATAL': [], 'ERROR': []}), ('lat', {'WARN': [], 'INFO': [], 'VERSION': [], 'FATAL': [], 'ERROR': []}), ('lat_bnds', {'WARN': [], 'INFO': [], 'VERSION': [], 'FATAL': [], 'ERROR': []}), ('lon', {'WARN': [], 'INFO': [], 'VERSION': [], 'FATAL': [], 'ERROR': []}), ('lon_bnds', {'WARN': [], 'INFO': [], 'VERSION': [], 'FATAL': [], 'ERROR': []}), ('height', {'WARN': [], 'INFO': [], 'VERSION': [], 'FATAL': [], 'ERROR': []}), ('tas', {'WARN': [u"(7.2): cell_measures refers to variable areacella that doesn't exist in this netCDF file. This is strictly an error if the cell_measures variable is not included in the dataset."], 'INFO': ['attribute history is being used in a non-standard way'], 'VERSION': [], 'FATAL': [], 'ERROR': []})]), 'global': {'WARN': [u"(2.6.1): Inconsistency - This netCDF file appears to contain CF-1.4 data, but you've requested a validity check against CF-1.6"], 'INFO': [], 'VERSION': ['CHECKING NetCDF FILE: /badc/cmip5/data/cmip5/output1/MOHC/HadGEM2-ES/rcp85/day/atmos/day/r1i1p1/v20111128/tas/tas_day_HadGEM2-ES_rcp85_r1i1p1_22991201-22991230.nc', 'Using CF Checker Version 3.0.4', 'Checking against CF Version CF-1.6', u'Using Standard Name Table Version 44 (2017-05-23T11:17:23Z)', u'Using Area Type Table Version 6 (22 February 2017)'], 'FATAL': [], 'ERROR': []}}

>>> print res.keys()
['variables', 'global']

>>> print res['variables'].keys()
['time', 'time_bnds', 'lat', 'lat_bnds', 'lon', 'lon_bnds', 'height', 'tas']

>>> print res['variables']['tas']
{'WARN': [u"(7.2): cell_measures refers to variable areacella that doesn't exist in this netCDF file. This is strictly an error if the cell_measures variable is not included in the dataset."], 'INFO': ['attribute history is being used in a non-standard way'], 'VERSION': [], 'FATAL': [], 'ERROR': []}

>>> for err_type in ('WARN', 'ERROR', 'FATAL', 'VERSION'):
...     print '{}: {}'.format(err_type, res['variables']['tas'].get(err_type, 'NOT SPECIFIED'))
...
WARN: [u"(7.2): cell_measures refers to variable areacella that doesn't exist in this netCDF file. This is strictly an error if the cell_measures variable is not included in the dataset."]
ERROR: []
FATAL: []
VERSION: []

And you can provide more information in the arguments to the class creator (CFChecker(...)), such as:

CFChecker(uploader=None, useFileName="yes", badc=None, coards=None, cfStandardNamesXML=STANDARDNAME, cfAreaTypesXML=AREATYPES, version=newest_version,
debug=False, silent=False)

E.g.: don't print output to terminal...

>>> checker = cfchecks.CFChecker(silent=True)
>>> res = checker.checker(fpath)

Set local tables and CF version:

>>> checker = cfchecks.CFChecker(cfStandardNamesXML=cfchecks.STANDARDNAME, cfAreaTypesXML=cfchecks.AREATYPES, version=cfchecks.vn1_4)
>>> res = checker.checker(fpath)
CHECKING NetCDF FILE: /badc/cmip5/data/cmip5/output1/MOHC/HadGEM2-ES/rcp85/day/atmos/day/r1i1p1/v20111128/tas/tas_day_HadGEM2-ES_rcp85_r1i1p1_22991201-22991230.nc
=====================
Using CF Checker Version 3.0.4
Checking against CF Version CF-1.4
Using Standard Name Table Version 44 (2017-05-23T11:17:23Z)
Using Area Type Table Version 6 (22 February 2017)


------------------
Checking variable: time
------------------

------------------
Checking variable: time_bnds
------------------

------------------
Checking variable: lat
------------------

------------------
Checking variable: lat_bnds
------------------

------------------
Checking variable: lon
------------------

------------------
Checking variable: lon_bnds
------------------

------------------
Checking variable: height
------------------
ERROR: (4): Axis attribute is not allowed for auxillary coordinate variables.

------------------
Checking variable: tas
------------------
INFO: attribute history is being used in a non-standard way
WARN: (7.2): cell_measures refers to variable areacella that doesn't exist in this netCDF file. This is strictly an error if the cell_measures variable is not included in the dataset.

ERRORS detected: 1
WARNINGS given: 1
INFORMATION messages: 1