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)
Return an iterator over 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:
An iterator returning 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.CubeListor 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 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:
- grib_messages:
An iterable of
iris_grib.message.GribMessage.
- 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-id) pairs.
Produces pairs of 2D cubes and GRIB messages, the result of the 2D cube being processed by the GRIB save rules.
Args:
- cube:
A
iris.cube.Cube,iris.cube.CubeListor list of cubes.
- Returns:
a iterator returning (cube, field) pairs, where each
cubeis a 2d slice of the input and each``field`` is an eccodes message “id”. N.B. the message “id”s are integer handles.
- 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.