improver.metadata.utilities module#

General IMPROVER metadata utilities

check_grid_match(cubes)[source]#

Checks that cubes are on, or originate from, compatible coordinate grids. Each cube is first checked for an existing ‘model_grid_hash’ which can be used to encode coordinate information on cubes that do not themselves contain a coordinate grid (e.g. spotdata cubes). If this is not found a new hash is generated to enable comparison. If the cubes are not compatible, an exception is raised to prevent the use of unmatched cubes.

Parameters:

cubes (Union[List[Cube], CubeList]) – A list of cubes to check for grid compatibility.

Raises:

ValueError – Raised if the cubes are not on matching grids as identified by the model_grid_hash.

Return type:

None

create_coordinate_hash(cube)[source]#

Generate a hash based on the input cube’s x and y coordinates. This acts as a unique identifier for the grid which can be used to allow two grids to be compared.

Parameters:

cube (Cube) – The cube from which x and y coordinates will be used to generate a hash.

Return type:

str

Returns:

A hash created using the x and y coordinates of the input cube.

create_new_diagnostic_cube(name, units, template_cube, mandatory_attributes, optional_attributes=None, data=None, dtype=<class 'numpy.float32'>)[source]#

Creates a new diagnostic cube with suitable metadata.

Parameters:
  • name (str) – Standard or long name for output cube

  • units (Union[Unit, str]) – Units for output cube

  • template_cube (Cube) – Cube from which to copy dimensional and auxiliary coordinates

  • mandatory_attributes (Union[Dict[str, str], Dict]) – Dictionary containing values for the mandatory attributes “title”, “source” and “institution”. These are overridden by values in the optional_attributes dictionary, if specified.

  • optional_attributes (Union[Dict[str, str], Dict, None]) – Dictionary of optional attribute names and values. If values for mandatory attributes are included in this dictionary they override the values of mandatory_attributes.

  • data (Union[MaskedArray, ndarray, None]) – Data array. If not set, cube is filled with zeros using a lazy data object, as this will be overwritten later by the caller routine.

  • dtype (Type) – Datatype for dummy cube data if “data” argument is None.

Return type:

Cube

Returns:

Cube with correct metadata to accommodate new diagnostic field

enforce_time_point_standard(cube)[source]#

Enforce the IMPROVER standard of a coordinate point that aligns with the upper bound of the period for time, forecast_period, and forecast reference time coordinates.

Two exceptions are captured. A CoordinateNotFoundError allows all the time coordinates to be modified if they exist and ignored if they don’t. The TypeError allows bounds that are set to None to be ignored.

The cube is modified in place.

Parameters:

cube (Cube) – Cube to enforce the IMPROVER standard on.

generate_hash(data_in)[source]#

Generate a hash from the data_in that can be used to uniquely identify equivalent data_in.

Parameters:

data_in (Any) – The data from which a hash is to be generated. This can be of any type that can be pretty printed.

Return type:

str

Returns:

A hexadecimal string which is a hash hexdigest of the data as a string.

generate_mandatory_attributes(diagnostic_cubes, model_id_attr=None)[source]#

Function to generate mandatory attributes for new diagnostics that are generated using several different model diagnostics as input to the calculation. If all input diagnostics have the same attribute use this, otherwise set a default value.

Parameters:
  • diagnostic_cubes (List[Cube]) – List of diagnostic cubes used in calculating the new diagnostic

  • model_id_attr (Optional[str]) – Name of attribute used to identify source model for blending, if required

Returns:

“value” pairs.

Return type:

Dictionary of mandatory attribute “key”

get_model_id_attr(cubes, model_id_attr)[source]#

Gets the specified model ID attribute from a list of input cubes, checking that the value is the same on all those cubes in the process.

Parameters:
  • cubes (List[Cube]) – List of cubes to get the attribute from

  • model_id_attr (str) – Attribute name

Return type:

str

Returns:

The unique attribute value

minimum_increment(cube, default=None)[source]#

Determine the minimum increment for the cube data based on the ‘least_significant_digit’ attribute. If the attribute is not present, the default value is used and a warning issued.

Parameters:
  • cube (Cube) – The cube for which to determine the minimum increment.

  • default (float) – The default minimum increment to use if the ‘least_significant_digit’ attribute is not present.

Return type:

Union[float, int]

Returns:

The minimum increment data value as a float.

Raises:

ValueError – If the ‘least_significant_digit’ attribute is not present and no default is provided.