In [1]:
import iris
import iris.analysis.cartography

fname = iris.sample_data_path('uk_hires.pp')
cube = iris.load_cube(fname, 'air_potential_temperature')


cube.coord('grid_latitude').guess_bounds()
cube.coord('grid_longitude').guess_bounds()
grid_areas = iris.analysis.cartography.area_weights(cube)

area_avg = cube.collapsed(['grid_longitude', 'grid_latitude'],
                          iris.analysis.MEAN,
                          weights=grid_areas)
print(area_avg)


air_potential_temperature / (K)     (time: 3; model_level_number: 7)
     Dimension coordinates:
          time                           x                      -
          model_level_number             -                      x
     Auxiliary coordinates:
          forecast_period                x                      -
          level_height                   -                      x
          sigma                          -                      x
     Derived coordinates:
          altitude                       -                      x
     Scalar coordinates:
          forecast_reference_time: 2009-11-19 04:00:00
          grid_latitude: 1.51455 degrees, bound=(0.13755, 2.89155) degrees
          grid_longitude: 358.749 degrees, bound=(357.487, 360.012) degrees
          surface_altitude: 399.625 m, bound=(-14.0, 813.25) m
     Attributes:
          STASH: m01s00i004
          source: Data from Met Office Unified Model
          um_version: 7.3
     Cell methods:
          mean: grid_longitude, grid_latitude
/Users/dawson/miniconda3/envs/python_workshop/lib/python3.5/site-packages/iris/coords.py:988: UserWarning: Collapsing a multi-dimensional coordinate. Metadata may not be fully descriptive for 'surface_altitude'.
  warnings.warn(msg.format(self.name()))
/Users/dawson/miniconda3/envs/python_workshop/lib/python3.5/site-packages/iris/cube.py:1103: UserWarning: Orography coordinate 'surface_altitude' has bounds. These will be disregarded.
  factory.update(old_coord, new_coord)

Exercise 5: What other aggregators are available? Calculate the potential temperature variance over model level for the area averaged cube (hint: We want to reduce the vertical dimension, and end up with a cube of length 3). Print the data values of the resulting cube.


In [2]:
var = area_avg.collapsed('model_level_number', iris.analysis.VARIANCE)
print(var.data)


[ 6.59274238  6.36927192  5.99507707]
/Users/dawson/miniconda3/envs/python_workshop/lib/python3.5/site-packages/iris/aux_factory.py:263: UserWarning: Orography coordinate 'surface_altitude' has bounds. These will be disregarded.
  return type(self)(**new_dependencies)
/Users/dawson/miniconda3/envs/python_workshop/lib/python3.5/site-packages/iris/coords.py:992: UserWarning: Collapsing a non-contiguous coordinate. Metadata may not be fully descriptive for 'model_level_number'.
  warnings.warn(msg.format(self.name()))
/Users/dawson/miniconda3/envs/python_workshop/lib/python3.5/site-packages/iris/coords.py:992: UserWarning: Collapsing a non-contiguous coordinate. Metadata may not be fully descriptive for 'level_height'.
  warnings.warn(msg.format(self.name()))
/Users/dawson/miniconda3/envs/python_workshop/lib/python3.5/site-packages/iris/coords.py:992: UserWarning: Collapsing a non-contiguous coordinate. Metadata may not be fully descriptive for 'sigma'.
  warnings.warn(msg.format(self.name()))