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.
- _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.
- static _equalise_cell_methods(cubelist)[source]
Function to equalise cell methods that do not match. Modifies cubes in place.
- 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:
- 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:
- 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:
- 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.
- 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.
- compare_attributes(cubes, attribute_filter=None)[source]
Function to compare attributes of cubes
- Parameters:
- Return type:
- 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:
- Return type:
- 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:
- 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.
- 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_dim_coord_names(cube)[source]
Returns an ordered list of dimension coordinate names on the cube
- 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:
- Return type:
- 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:
- 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:
- Return type:
- 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.