Exercise 3:

  1. Write a function, which when given a cube, returns True or False depending on whether a cell method exists.
  2. Use this function as a value for the iris.Constraint cube_func keyword, and load the file in iris.sample_data_path('A1B_north_america.nc') such that only cubes with cell methods are loaded (note: in this case, that is all that exists in the file).

In [1]:
def has_cell_methods(cube):
    if cube.cell_methods is not None:
        return True
    return False

In [2]:
import iris

fname = iris.sample_data_path('A1B_north_america.nc')
cubes = iris.load(fname, iris.Constraint(cube_func=has_cell_methods))

0: air_temperature / (K)               (time: 240; latitude: 37; longitude: 49)
/Users/dawson/miniconda3/envs/pfcs/lib/python3.5/site-packages/iris/fileformats/cf.py:1140: IrisDeprecation: NetCDF default loading behaviour currently does not expose variables which define reference surfaces for dimensionless vertical coordinates as independent Cubes. This behaviour is deprecated in favour of automatic promotion to Cubes. To switch to the new behaviour, set iris.FUTURE.netcdf_promote to True.