iris_grib

In this module:

  • load_cubes

  • save_grib2

  • load_pairs_from_fields

  • save_pairs_from_cube

  • save_messages

Conversion of cubes to/from GRIB.

See: ECMWF GRIB API.

iris_grib.load_cubes(filenames, callback=None)

Returns a generator of cubes from the given list of filenames.

Args:

  • filenames:

    One or more GRIB filenames to load from.

Kwargs:

  • callback:

    Function which can be passed on to iris.io.run_callback().

Returns:

A generator containing Iris cubes loaded from the GRIB files.

iris_grib.save_grib2(cube, target, append=False)

Save a cube or iterable of cubes to a GRIB2 file.

Args:

  • cube:

    The iris.cube.Cube, iris.cube.CubeList or list of cubes to save to a GRIB2 file.

  • target:

    A filename or open file handle specifying the GRIB2 file to save to.

Kwargs:

  • append:

    Whether to start a new file afresh or add the cube(s) to the end of the file. Only applicable when target is a filename, not a file handle. Default is False.

iris_grib.load_pairs_from_fields(grib_messages)

Convert an iterable of GRIB messages into an iterable of (Cube, Grib message) tuples.

This capability can be used to filter out fields before they are passed to the load pipeline, and amend the cubes once they are created, using GRIB metadata conditions. Where the filtering removes a significant number of fields, the speed up to load can be significant:

>>> import iris
>>> from iris_grib import load_pairs_from_fields
>>> from iris_grib.message import GribMessage
>>> filename = iris.sample_data_path('polar_stereo.grib2')
>>> filtered_messages = []
>>> for message in GribMessage.messages_from_filename(filename):
...     if message.sections[1]['productionStatusOfProcessedData'] == 0:
...         filtered_messages.append(message)
>>> cubes_messages = load_pairs_from_fields(filtered_messages)
>>> for cube, msg in cubes_messages:
...     prod_stat = msg.sections[1]['productionStatusOfProcessedData']
...     cube.attributes['productionStatusOfProcessedData'] = prod_stat
>>> print(cube.attributes['productionStatusOfProcessedData'])
0

This capability can also be used to alter fields before they are passed to the load pipeline. Fields with out of specification header elements can be cleaned up this way and cubes created:

>>> from iris_grib import load_pairs_from_fields
>>> cleaned_messages = GribMessage.messages_from_filename(filename)
>>> for message in cleaned_messages:
...     if message.sections[1]['productionStatusOfProcessedData'] == 0:
...         message.sections[1]['productionStatusOfProcessedData'] = 4
>>> cubes = load_pairs_from_fields(cleaned_messages)

Args:

Returns:

An iterable of tuples of (iris.cube.Cube, iris_grib.message.GribMessage).

iris_grib.save_pairs_from_cube(cube)

Convert one or more cubes to (2D cube, GRIB message) pairs. Returns an iterable of tuples each consisting of one 2D cube and one GRIB message ID, the result of the 2D cube being processed by the GRIB save rules.

Args:

  • cube:

    A iris.cube.Cube, iris.cube.CubeList or list of cubes.

iris_grib.save_messages(messages, target, append=False)

Save messages to a GRIB2 file. The messages will be released as part of the save.

Args:

  • messages:

    An iterable of grib_api message IDs.

  • target:

    A filename or open file handle.

Kwargs:

  • append:

    Whether to start a new file afresh or add the cube(s) to the end of the file. Only applicable when target is a filename, not a file handle. Default is False.