improver.synthetic_data.set_up_test_cubes module

Functions to set up variable, multi-realization, percentile and probability cubes for unit tests. Standardises time units and spatial coordinates, including coordinate order expected by IMPROVER plugins.

_construct_dimension_coords(data, y_coord, x_coord, realizations, height_levels, pressure)[source]

Create array of all dimension coordinates. These dimensions will be ordered: realization, height/pressure, y, x.

Return type:

DimCoord

_create_dimension_coord(coord_array, data_length, coord_name, **kwargs)[source]

Creates dimension coordinate from coord_array if not None, otherwise creating an array of integers with an interval of 1

Return type:

DimCoord

_create_time_point(time)[source]

Returns a coordinate point with appropriate units and datatype from a datetime.datetime instance.

Parameters:

time (datetime) –

Return type:

int

Returns:

Returns coordinate point as datatype specified in TIME_COORDS[“time”]

_create_yx_arrays(ypoints, xpoints, domain_corner, x_grid_spacing, y_grid_spacing)[source]

Creates arrays for constructing y and x DimCoords.

Parameters:
Return type:

Tuple[ndarray, ndarray]

Returns:

Tuple containing arrays of y and x coordinate values

_set_domain_corner(ypoints, xpoints, x_grid_spacing, y_grid_spacing)[source]

Set domain corner to create a grid around 0,0.

Parameters:
  • ypoints (int) –

  • xpoints (int) –

  • x_grid_spacing (float) –

  • y_grid_spacing (float) –

Return type:

Tuple[float, float]

Returns:

(y,x) values of the bottom left corner of the domain

add_coordinate(incube, coord_points, coord_name, coord_units=None, dtype=<class 'numpy.float32'>, order=None, is_datetime=False, attributes=None)[source]

Function to duplicate a sample cube with an additional coordinate to create a cubelist. The cubelist is merged to create a single cube, which can be reordered to place the new coordinate in the required position.

Parameters:
  • incube (Cube) – Cube to be duplicated.

  • coord_points (List) – Values for the coordinate.

  • coord_name (str) – Long name of the coordinate to be added.

  • coord_units (Optional[str]) – Coordinate unit required.

  • dtype (Type) – Datatype for coordinate points.

  • order (Optional[List[int]]) – Optional list of integers to reorder the dimensions on the new merged cube. For example, if the new coordinate is required to be in position 1 on a 4D cube, use order=[1, 0, 2, 3] to swap the new coordinate position with that of the original leading coordinate.

  • is_datetime (bool) – If “true”, the leading coordinate points have been given as a list of datetime objects and need converting. In this case the “coord_units” argument is overridden and the time points provided in seconds. The “dtype” argument is overridden and set to int64.

  • attributes (Optional[Dict[str, Any]]) – Optional coordinate attributes.

Return type:

Cube

Returns:

Cube containing an additional dimension coordinate.

construct_scalar_time_coords(time, time_bounds=None, frt=None, blend_time=None)[source]

Construct scalar time coordinates as aux_coord list

Parameters:
  • time (datetime) – Single time point

  • time_bounds (Optional[List[datetime]]) – Lower and upper bound on time point, if required

  • frt (Optional[datetime]) – Single forecast reference time point. Either this or blend_time is required.

  • blend_time (Optional[datetime]) – Single blend time point. Either this or frt is required. Both may not be supplied.

Return type:

List[Tuple[DimCoord, None]]

Returns:

List of iris.coords.DimCoord instances with the associated “None” dimension (format required by iris.cube.Cube initialisation).

Raises:

ValueError – if differing frt and blend_time are supplied

construct_yx_coords(ypoints, xpoints, spatial_grid, x_grid_spacing=None, y_grid_spacing=None, domain_corner=None)[source]

Construct y/x spatial dimension coordinates

Parameters:
  • ypoints (int) – Number of grid points required along the y-axis

  • xpoints (int) – Number of grid points required along the x-axis

  • spatial_grid (str) – Specifier to produce either a “latlon” or “equalarea” grid

  • x_grid_spacing (Optional[float]) –

    Grid resolution along the x axis. Degrees for latlon or metres for equalarea.

    If not provided, defaults to 10 degrees for “latlon” grid or 2000 metres for “equalarea” grid

  • y_grid_spacing (Optional[float]) – Grid resolution along the y axis. Degrees for latlon or metres for equalarea. If not provided, defaults to 10 degrees for “latlon” grid or 2000 metres for “equalarea” grid

  • domain_corner (Optional[Tuple[float, float]]) – Bottom left corner of grid domain (y,x) (degrees for latlon or metres for equalarea). If not provided, a grid is created centred around (0,0).

Return type:

Tuple[DimCoord, DimCoord]

Returns:

Tuple containing y and x iris.coords.DimCoords

set_up_percentile_cube(data, percentiles, **kwargs)[source]

Set up a cube containing percentiles of a variable with: - x/y spatial dimensions (equal area or lat / lon) - leading “percentile” dimension - “time”, “forecast_reference_time” and “forecast_period” scalar coords - option to specify additional scalar coordinates - configurable attributes

Parameters:
  • data (ndarray) – 3D (percentile-y-x ordered) array of data to put into the cube

  • percentiles (Union[List[float], ndarray]) – List of int / float percentile values whose length must match the first dimension on the input data cube

  • **kwargs (Any) – Additional keyword arguments passed to ‘set_up_variable_cube’ function

Return type:

Cube

Returns:

Cube containing percentiles

set_up_probability_cube(data, thresholds, variable_name='air_temperature', threshold_units='K', spp__relative_to_threshold='greater_than', **kwargs)[source]

Set up a cube containing probabilities at thresholds with: - x/y spatial dimensions (equal area or lat / lon) - leading “threshold” dimension - “time”, “forecast_reference_time” and “forecast_period” scalar coords - option to specify additional scalar coordinates - “spp__relative_to_threshold” attribute (default “greater_than”) - default or configurable attributes - configurable cube data, name conforms to “probability_of_X_above(or below)_threshold” convention

Parameters:
  • data (ndarray) – 3D (threshold-y-x ordered) array of data to put into the cube

  • thresholds (Union[List[float], ndarray]) – List of int / float threshold values whose length must match the first dimension on the input data cube

  • variable_name (str) – Name of the underlying variable to which the probability field applies, eg “air_temperature”. NOT name of probability field.

  • threshold_units (str) – Units of the underlying variable / threshold.

  • spp__relative_to_threshold (str) – Value of the attribute “spp__relative_to_threshold” which is required for IMPROVER probability cubes.

  • **kwargs (Any) – Additional keyword arguments passed to ‘set_up_variable_cube’ function

Return type:

Cube

Returns:

Cube containing probabilities at thresholds

set_up_variable_cube(data, name='air_temperature', units='K', spatial_grid='latlon', time=datetime.datetime(2017, 11, 10, 4, 0), time_bounds=None, frt=None, blend_time=None, realizations=None, include_scalar_coords=None, attributes=None, standard_grid_metadata=None, x_grid_spacing=None, y_grid_spacing=None, domain_corner=None, height_levels=None, pressure=False)[source]

Set up a cube containing a single variable field with: - x/y spatial dimensions (equal area or lat / lon) - optional leading “realization” dimension - optional “height” dimension - “time”, “forecast_reference_time” and “forecast_period” scalar coords - option to specify additional scalar coordinates - configurable attributes

Parameters:
  • data (ndarray) – 2D (y-x ordered) or 3D (realization-y-x ordered) array of data to put into the cube.

  • name (str) – Variable name (standard / long)

  • units (str) – Variable units

  • spatial_grid (str) – What type of x/y coordinate values to use. Permitted values are “latlon” or “equalarea”.

  • time (datetime) – Single cube validity time

  • time_bounds (Optional[Tuple[datetime, datetime]]) – Lower and upper bound on time point, if required

  • frt (Optional[datetime]) – Single cube forecast reference time. Default value is datetime(2017, 11, 10, 0, 0).

  • blend_time (Optional[datetime]) – Single cube blend time

  • realizations (Union[List[float], ndarray, None]) – List of forecast realizations. If not present, taken from the leading dimension of the input data array (if 3D).

  • include_scalar_coords (Optional[List[Coord]]) – List of iris.coords.DimCoord or AuxCoord instances of length 1.

  • attributes (Optional[Dict[str, str]]) – Optional cube attributes.

  • standard_grid_metadata (Optional[str]) – Recognised mosg__model_configuration for which to set up Met Office standard grid attributes. Should be ‘uk_det’, ‘uk_ens’, ‘gl_det’ or ‘gl_ens’.

  • x_grid_spacing (Optional[float]) – Grid resolution along the x axis (degrees for latlon or metres for equalarea).

  • y_grid_spacing (Optional[float]) – Grid resolution along the y axis (degrees for latlon or metres for equalarea).

  • domain_corner (Optional[Tuple[float, float]]) – Bottom left corner of grid domain (y,x) (degrees for latlon or metres for equalarea).

  • height_levels (Union[List[float], ndarray, None]) – List of height levels in metres or pressure levels in Pa.

  • pressure (bool) – Flag to indicate whether the height levels are specified as pressure, in Pa. If False, use height in metres.

Return type:

Cube

Returns:

Cube containing a single variable field