Loading in constraint using cell_methods?

547 views
Skip to first unread message

Seonaid Dey

unread,
Oct 2, 2013, 1:02:00 PM10/2/13
to scitoo...@googlegroups.com
Dear all,

I have a pp file containing three cubes which have the same dimension coordinates and attributes but differ their cell method and forecast reference time coordinates (see below). I only want to load in the field without a specified cell method . Is this possible? If so how? The cubes are of 1.5m temperature (the one I want, no cell method specified), min hourly temperature and max hourly temperature.

air_temperature / K                 (realization: 2; time: 37; grid_latitude: 548; grid_longitude: 422)
     Dimension coordinates:
          realization                           x        -                  -                    -
          time                                  -        x                  -                    -
          grid_latitude                         -        -                  x                    -
          grid_longitude                        -        -                  -                    x
     Auxiliary coordinates:
          forecast_period                       -        x                  -                    -
     Scalar coordinates:
          forecast_reference_time: 2013-07-17 03:00:00
          height: 1.5 m
     Attributes:
          STASH: m01s03i236
          source: Data from Met Office Unified Model 8.02

air_temperature / K                 (realization: 2; time: 36; grid_latitude: 548; grid_longitude: 422)
     Dimension coordinates:
          realization                           x        -                  -                    -
          time                                  -        x                  -                    -
          grid_latitude                         -        -                  x                    -
          grid_longitude                        -        -                  -                    x
     Auxiliary coordinates:
          forecast_period                       -        x                  -                    -
          forecast_reference_time               -        x                  -                    -
     Scalar coordinates:
          height: 1.5 m
     Attributes:
          STASH: m01s03i236
          source: Data from Met Office Unified Model 8.02
     Cell methods:
          maximum: time (1 hour)

Any help much appreciated!

Thanks,
Seonaid


esc24

unread,
Oct 3, 2013, 1:14:08 PM10/3/13
to scitoo...@googlegroups.com
You can do this using the cube_func keyword arg of iris.Constraint. I believe the constraint you want is:

constraint = iris.Constraint(cube_func=lambda cube: not cube.cell_methods)

Seonaid Dey

unread,
Oct 7, 2013, 6:50:17 AM10/7/13
to scitoo...@googlegroups.com
Thanks, this seems to work. Just one more question. I already have a constraint defining the stash codes I want to read in (that works)

 for stash in stasharr:
        stash_constr.append(iris.AttributeConstraint(STASH=stash))


How do I combine the cell_method constraint with this so that I only load cubes which meet the stash and cell_method criteria?

I have tried
 
cell_constr=iris.Constraint(cube_func=lambda cube: not cube.cell_methods)
fields= iris.load(filearr,  stash_constr & cell_constr, callback=lagged_ensemble_callback)

But this ignores the stash constraint!

Thanks very much for your help,

Seonaid

Seonaid Dey

unread,
Oct 7, 2013, 9:56:34 AM10/7/13
to scitoo...@googlegroups.com
Dear all,

Don't worry problem solved!

    fields= iris.load(filearr, stash_constr, callback=lagged_ensemble_callback).extract(cell_constr)      

Thanks,
Seonaid

Steven Chan

unread,
Jun 21, 2017, 5:44:13 AM6/21/17
to Iris
Please pardon me for replying to an old question. I think the natural extension of the question is how to distinguish between different cell methods. For instance, you have a file with maximum, minimum and mean temperature, and they are distinguished by different information in Cell methods, for instance:

>>> print cubes 
0: air_temperature / (K)               (time: 28; grid_latitude: 1536; grid_longitude: 1536)
1: air_temperature / (K)               (time: 28; grid_latitude: 1536; grid_longitude: 1536)
2: air_temperature / (K)               (time: 28; grid_latitude: 1536; grid_longitude: 1536)
>>> print cubes[0]
air_temperature / (K)               (time: 28; grid_latitude: 1536; grid_longitude: 1536)
     Dimension coordinates:
          time                           x                  -                     -
          grid_latitude                  -                  x                     -
          grid_longitude                 -                  -                     x
     Auxiliary coordinates:
          forecast_period                x                  -                     -
     Scalar coordinates:
          forecast_reference_time: 1998-03-01 00:00:00

          height: 1.5 m
     Attributes:
          STASH: m01s03i236
          source: Data from Met Office Unified Model
          um_version: 10.1
     Cell methods:
          mean: time (1 hour)
>>> print cubes[1]
air_temperature / (K)               (time: 28; grid_latitude: 1536; grid_longitude: 1536)
     Dimension coordinates:
          time                           x                  -                     -
          grid_latitude                  -                  x                     -
          grid_longitude                 -                  -                     x
     Auxiliary coordinates:
          forecast_period                x                  -                     -
     Scalar coordinates:
          forecast_reference_time: 1998-03-01 00:00:00

          height: 1.5 m
     Attributes:
          STASH: m01s03i236
          source: Data from Met Office Unified Model
          um_version: 10.1
     Cell methods:
          minimum: time (1 hour)
>>> print cubes[2]
air_temperature / (K)               (time: 28; grid_latitude: 1536; grid_longitude: 1536)
     Dimension coordinates:
          time                           x                  -                     -
          grid_latitude                  -                  x                     -
          grid_longitude                 -                  -                     x
     Auxiliary coordinates:
          forecast_period                x                  -                     -
     Scalar coordinates:
          forecast_reference_time: 1998-03-01 00:00:00

          height: 1.5 m
     Attributes:
          STASH: m01s03i236
          source: Data from Met Office Unified Model
          um_version: 10.1

     Cell methods:
          maximum: time (1 hour)
>>>

Is there a straightforward constraint that would work here? I do find out what you can do:

>>> stuff = cubes[0].cell_methods
>>> stuff
(CellMethod(method='mean', coord_names=('time',), intervals=('1 hour',), comments=()),)
>>> constraint = iris.Constraint(cube_func=lambda cube: cube.cell_methods is stuff)
>>> cubes.extract(constraint)
[<iris 'Cube' of air_temperature / (K) (time: 28; grid_latitude: 1536; grid_longitude: 1536)>]

That is rather clumsy, and will only work if you save a sample cube with the correct method somewhere. (i.e. you save a small sample; when you need to do the extraction, you import the sample, and use the method from the sample to do the constraint).

Thank you
Steven

RuthC

unread,
Jun 26, 2017, 11:39:04 AM6/26/17
to Iris
Hi Steven,

you can create a cell method from scratch:

my_cell_method = iris.coords.CellMethod(method='mean', coords='time', intervals='1 hour')

Ruth

Jonny Williams

unread,
Nov 12, 2019, 5:51:43 PM11/12/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hey Ruth

How would you use this as part of the loading process?

For example, how I would I use this type of cell_method constraint to only load the hourly mean data here?...

In [130]: files
Out[130]:
['/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950apr.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950aug.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950dec.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950feb.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950jan.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950jul.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950jun.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950mar.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950may.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950nov.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950oct.pp',
 '/home/williamsjh/cylc-run/u-bl658/share/data/History_Data/bl658a.pm1950sep.pp']

In [131]: constraint
Out[131]: AttributeConstraint({'STASH': 'm01s05i216'})

In [132]: cubes = iris.load(files,constraint)

In [133]: cubes
Out[133]:
[<iris 'Cube' of precipitation_flux / (kg m-2 s-1) (time: 12; latitude: 144; longitude: 192)>,
<iris 'Cube' of precipitation_flux / (kg m-2 s-1) (-- : 96; latitude: 144; longitude: 192)>]

In [134]: cubes[0].cell_methods
Out[134]: (CellMethod(method='mean', coord_names=('time',), intervals=('1 hour',), comments=()),)

In [135]: cubes[1].cell_methods
Out[135]: (CellMethod(method='mean', coord_names=('time',), intervals=('24 hour',), comments=()),)



Thanks!

Jonny

RuthC

unread,
Nov 13, 2019, 7:21:41 AM11/13/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hi Jonny,

you should be able to create the constraints you want, and combine them using "&":

stash_constraint = iris.AttributeConstraint(STASH='m01s05i216')

desired_cell_method
=
iris.coords.CellMethod(method='mean', coords='time', intervals='1 hour')
cell_method_constraint
= iris.Constraint(cube_func=lambda cube: desired_cell_method in cube.cell_methods)

cube = iris.load_cube(files, stash_constraint & cell_method_constraint)

Cheers,

Ruth

Jonny Williams

unread,
Mar 22, 2020, 7:23:55 PM3/22/20
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hey Ruth.

Thanks a lot for this, this is super helpful! Now being used in earnest!

Cheers

Jonny

Nick Dunstone

unread,
Jul 8, 2021, 6:29:26 AM7/8/21
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Yes, very helpful, thanks Ruth! :-)
Reply all
Reply to author
Forward
0 new messages