iris_grib package

Conversion of cubes to/from GRIB.

See: ECMWF ecCodes grib interface.

iris_grib.load_cubes(filenames, callback=None)

Return an iterator over cubes from the given list of filenames.

Args:

  • filenames:

    One or more GRIB filenames to load from.

Kwargs:

Returns:

An iterator returning Iris cubes loaded from the GRIB files.

iris_grib.load_pairs_from_fields(grib_messages)

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

Parameters

grib_messagesiterable on (cube, message)

An iterable of GribMessage.

Returns

iterable of (cube, message)

An iterable of (Cube, GribMessage), pairing each message with a corresponding generated cube.

Notes

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

iris_grib.save_pairs_from_cube(cube)

Convert one or more cubes to (2D cube, GRIB-message-id) pairs.

Produces pairs of 2D cubes and GRIB messages, the result of the 2D cube being processed by the GRIB save rules.

Args:

Returns:

a iterator returning (cube, field) pairs, where each cube is a 2d slice of the input and each``field`` is an eccodes message “id”. N.B. the message “id”s are integer handles.

Subpackages

Submodules

iris_grib.message module

Defines a lightweight wrapper class to wrap a single GRIB message.

class iris_grib.message.GribMessage(raw_message, recreate_raw, file_ref=None)

Bases: object

An in-memory representation of a GribMessage.

Provides access to the data() payload and the metadata elements by section via the sections() property.

static messages_from_filename(filename)

Return the messages in a file.

Return a generator of GribMessage instances; one for each message in the supplied GRIB file.

Args:

  • filename (string):

    Name of the file to generate fields from.

core_data()
property bmdi
property data

The data array from the GRIB message as a dask Array.

The shape of the array will match the logical shape of the message’s grid. For example, a simple global grid would be available as a 2-dimensional array with shape (Nj, Ni).

property sections

Return the key-value pairs of the message keys.

The key-value pairs are grouped by containing section.

Sections in a message are indexed by GRIB section-number, and values in a section are indexed by key strings.

class iris_grib.message.Section(message_id, number, keys)

Bases: object

A Section of a GRIB message.

This supports dictionary-like access to key values, using gribapi key strings.

Values for keys may be changed using assignment but this does not write to the file.

get_computed_key(key)

Get the computed value for a given key.

Returns the value associated with the given key in the GRIB message.

Args:

  • key:

    The GRIB key to retrieve the value of.

Returns the value associated with the requested key in the GRIB message.

keys()

Return coded keys available in this Section.