# 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:

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:

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.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)
>>> 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


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:

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.