improver.utilities.cube_manipulation module

Provides support utilities for cube manipulation.

class MergeCubes[source]

Bases: BasePlugin

Class adding functionality to iris.merge_cubes()

Accounts for differences in attributes, cell methods and bounds ranges to avoid merge failures and anonymous dimensions.

__init__()[source]

Initialise constants

_abc_impl = <_abc_data object>
static _check_time_bounds_ranges(cube)[source]

Check the bounds on any dimensional time coordinates after merging. For example, to check time and forecast period ranges for accumulations to avoid blending 1 hr with 3 hr accumulations. If points on the coordinate are not compatible, raise an error.

Parameters:

cube (Cube) – Merged cube

Return type:

None

static _equalise_cell_methods(cubelist)[source]

Function to equalise cell methods that do not match. Modifies cubes in place.

Parameters:

cubelist (CubeList) – List of cubes to check the cell methods and revise.

Return type:

None

process(cubes_in, check_time_bounds_ranges=False, slice_over_realization=False, copy=True)[source]

Function to merge cubes, accounting for differences in attributes, coordinates and cell methods. Note that cubes with different sets of coordinates (as opposed to cubes with the same coordinates with different values) cannot be merged.

If the input is a single Cube, this is returned unmodified. A CubeList of length 1 is checked for mismatched time bounds before returning the single Cube (since a CubeList of this form may be the result of premature iris merging on load).

Parameters:
  • cubes_in (Union[List[Cube], CubeList]) – Cubes to be merged.

  • check_time_bounds_ranges (bool) – Flag to check whether scalar time bounds ranges match. This is for when we are expecting to create a new “time” axis through merging for eg precipitation accumulations, where we want to make sure that the bounds match so that we are not eg combining 1 hour with 3 hour accumulations.

  • slice_over_realization (bool) – Options to combine cubes with different realization dimensions. These cannot always be concatenated directly as this can create a non-monotonic realization coordinate.

  • copy (bool) – If True, this will copy the cubes, thus not having any impact on the original objects.

Return type:

Cube

Returns:

Merged cube.

add_coordinate_to_cube(cube, new_coord, new_dim_location=0, copy_metadata=True)[source]

Create a copy of input cube with an additional dimension coordinate added to the cube at the specified axis. The data from input cube is broadcast over this new dimension.

Parameters:
  • cube (Cube) – cube to add realization dimension to.

  • new_coord (DimCoord) – new coordinate to add to input cube.

  • new_dim_location (int) – position in cube.data to position the new dimension coord. Default is to add the new coordinate as the leading dimension.

  • copy_metadata (bool) – flag as to whether to carry metadata over to output cube.

Return type:

Cube

Returns:

A copy of cube broadcast over the new dimension coordinate.

clip_cube_data(cube, minimum_value, maximum_value)[source]

Apply np.clip to data in a cube to ensure that the limits do not go beyond the provided minimum and maximum values.

Parameters:
  • cube (Cube) – The cube that has been processed and contains data that is to be clipped.

  • minimum_value (float) – The minimum value, with data in the cube that falls below this threshold set to it.

  • maximum_value (float) – The maximum value, with data in the cube that falls above this threshold set to it.

Return type:

Cube

Returns:

The processed cube with the data clipped to the limits of the original preprocessed cube.

collapse_realizations(cube, method='mean')[source]

Collapses the realization coord of a cube and strips the coord from the cube.

Parameters:
  • cube (Cube) – Cube to be aggregated.

  • method – One of “sum”, “mean”, “median”, “std_dev”, “min”, “max”; default is “mean”.

Return type:

Cube

Returns:

Cube with realization coord collapsed and removed.

collapsed(cube, *args, **kwargs)[source]

Collapses the cube with given arguments.

The cell methods of the output cube will match the cell methods from the input cube. Any cell methods generated by the iris collapsed method will not be retained.

Parameters:

cube (Cube) – A Cube to be collapsed.

Return type:

Cube

Returns:

A collapsed cube where the cell methods match the input cube.

compare_attributes(cubes, attribute_filter=None)[source]

Function to compare attributes of cubes

Parameters:
  • cubes (CubeList) – List of cubes to compare (must be more than 1)

  • attribute_filter (Optional[str]) – A string to filter which attributes are actually compared. If None all attributes are compared.

Return type:

List[Dict]

Returns:

List of dictionaries of unmatching attributes

Warns:

Warning – If only a single cube is supplied

compare_coords(cubes, ignored_coords=None)[source]

Function to compare the coordinates of the cubes

Parameters:
  • cubes (CubeList) – List of cubes to compare (must be more than 1)

  • ignored_coords (Optional[List[str]]) – List of coordinate names that identify coordinates to exclude from the comparison.

Return type:

List[Dict]

Returns:

List of dictionaries of unmatching coordinates Number of dictionaries equals number of cubes unless cubes is a single cube in which case unmatching_coords returns an empty list.

Warns:

Warning – If only a single cube is supplied

enforce_coordinate_ordering(cube, coord_names, anchor_start=True)[source]

Function to reorder dimensions within a cube. Note that the input cube is modified in place.

Parameters:
  • cube (Cube) – Cube where the ordering will be enforced to match the order within the coord_names. This input cube will be modified as part of this function.

  • coord_names (Union[List[str], str]) – List of the names of the coordinates to order. If a string is passed in, only the single specified coordinate is reordered.

  • anchor_start (bool) – Define whether the specified coordinates should be moved to the start (True) or end (False) of the list of dimensions. If True, the coordinates are inserted as the first dimensions in the order in which they are provided. If False, the coordinates are moved to the end. For example, if the specified coordinate names are [“time”, “realization”] then “realization” will be the last coordinate within the cube, whilst “time” will be the last but one.

Return type:

None

expand_bounds(result_cube, cubelist, coord_names)[source]

Alter a coordinate on result_cube such that bounds are expanded to cover the entire range of the input cubes (cubelist). The input result_cube is modified in place and returned.

For example, in the case of time cubes if the input cubes have bounds of [0000Z, 0100Z] & [0100Z, 0200Z] then the output cube will have bounds of [0000Z,0200Z]. The returned coordinate point will be equal to the upper bound.

Parameters:
  • result_cube (Cube) – Cube with coords requiring expansion

  • cubelist (Union[List[Cube], CubeList]) – List of input cubes with source coords

  • coord_names (List[str]) – Coordinates which should be expanded

Return type:

Cube

Returns:

Cube with coords expanded.

filter_realizations(cubes)[source]

For a given list of cubes, identifies the set of times, filters out any realizations that are not present at all times and returns a merged cube of the result.

Parameters:

cubes (CubeList) – List of cubes to be filtered

Returns:

Filtered and merged cube

Return type:

Cube

get_coord_names(cube)[source]

Returns a list of all coordinate names on the cube

Parameters:

cube (Cube) –

Return type:

List[str]

Returns:

List of all coordinate names

get_dim_coord_names(cube)[source]

Returns an ordered list of dimension coordinate names on the cube

Parameters:

cube (Cube) –

Return type:

List[str]

Returns:

List of dimension coordinate names

get_filtered_attributes(cube, attribute_filter=None)[source]

Build dictionary of attributes that match the attribute_filter. If the attribute_filter is None, return all attributes.

Parameters:
  • cube (Cube) – A cube from which attributes partially matching the attribute_filter will be returned.

  • attribute_filter (Optional[str]) – A string to match, or partially match, against attributes to build a filtered attribute dictionary. If None, all attributes are returned.

Return type:

Dict

Returns:

A dictionary of attributes partially matching the attribute_filter that were found on the input cube.

maximum_in_height(cube, lower_height_bound=None, upper_height_bound=None, new_name=None)[source]

Calculate the maximum value over the height coordinate. If bounds are specified then the maximum value between the lower_height_bound and upper_height_bound is calculated.

If either the upper or lower bound is None then no bound is applied. For example if no lower bound is provided but an upper bound of 300m is provided then the maximum is calculated for all height levels less than 300m.

Parameters:
  • cube (Cube) – A cube with a height coordinate.

  • lower_height_bound (Optional[float]) – The lower bound for the height coordinate. This is either a float or None if no lower bound is desired. Any specified bounds should have the same units as the height coordinate of cube.

  • upper_height_bound (Optional[float]) – The upper bound for the height coordinate. This is either a float or None if no upper bound is desired. Any specified bounds should have the same units as the height coordinate of cube.

  • new_name (Optional[str]) – The new name to be assigned to the output cube. If unspecified the name of the original cube is used.

Return type:

Cube

Returns:

A cube of the maximum value over the height coordinate or maximum value between the desired height values. This cube inherits Iris’ meta-data updates to the height coordinate and to the cell methods.

Raises:

ValueError – If the cube has no height levels between the lower_height_bound and upper_height_bound

sort_coord_in_cube(cube, coord, descending=False)[source]

Sort a cube based on the ordering within the chosen coordinate. Sorting can either be in ascending or descending order. This code is based upon https://gist.github.com/pelson/9763057.

Parameters:
  • cube (Cube) – The input cube to be sorted.

  • coord (str) – Name of the coordinate to be sorted.

  • descending (bool) – If True it will be sorted in descending order.

Return type:

Cube

Returns:

Cube where the chosen coordinate has been sorted into either ascending or descending order.

Warns:

Warning if the coordinate being processed is a circular coordinate.

strip_var_names(cubes)[source]

Strips var_name from the cube and from all coordinates except where required to support probabilistic metadata. Inputs are modified in place.

Parameters:

cubes (Union[Cube, CubeList]) –

Return type:

CubeList

Returns:

cubes with stripped var_name