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=None, x_coord=None, spot_index=None, realizations=None, vertical_levels=None, pressure=False, height=False)[source]#

Create array of all dimension coordinates. The expected dimension order for gridded cubes is realization, height/pressure, y, x or realization, height/pressure, spot_index for site cubes. The returned coordinates will reflect this ordering.

A realization coordinate will be created if the cube is (n_spatial_dims + 1) or (n_spatial_dims + 2), even if no values for the realizations argument are provided. To create a vertical coordinate, the vertical_levels must be provided.

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

_percentile_cube(function, data, percentiles, **kwargs)[source]#

Set up a cube containing percentiles of a variable with: - leading “percentile” dimension - “time”, “forecast_reference_time” and “forecast_period” scalar coords - option to specify additional scalar coordinates - configurable attributes

Parameters:
  • data (ndarray) – 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

_probability_cube(function, 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: - 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) – 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_domain_corner(ypoints, xpoints, x_grid_spacing, y_grid_spacing)[source]#

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

Parameters:
Return type:

Tuple[float, float]

Returns:

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

_variable_cube(data, dim_coords, name='air_temperature', units='K', time=datetime.datetime(2017, 11, 10, 4, 0), time_bounds=None, frt=None, blend_time=None, include_scalar_coords=None, attributes=None, standard_grid_metadata=None)[source]#

Set up a cube containing a single variable field with: - the provided dimension coordinates - “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.

  • dim_coords (Tuple[Union[AuxCoord, DimCoord]]) – Dimension coordinates for the constructed cube, e.g. the x and y dimensions.

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

  • units (str) – Variable units

  • 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

  • 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’.

Return type:

Cube

Returns:

Cube containing a single variable field

_yx_for_grid(data, spatial_grid, x_grid_spacing, y_grid_spacing, domain_corner)[source]#

Construct y and x coordinates for gridded data based upon the shape of the data array.

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(*args, **kwargs)[source]#

Set up a cube containing gridded percentiles of a variable.

Parameters:

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

Returns:

Cube containing gridded percentiles.

set_up_probability_cube(*args, **kwargs)[source]#

Set up a cube containing gridded probabilities at thresholds.

Parameters:

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

Returns:

Cube containing gridded probabilities.

set_up_spot_percentile_cube(*args, **kwargs)[source]#

Set up a cube containing spot percentiles of a variable.

Parameters:

data – 2D (percentile-sites ordered) array of data to put into the cube

Returns:

Cube containing spot percentiles.

set_up_spot_probability_cube(*args, **kwargs)[source]#

Set up a cube containing spot probabilities at thresholds.

Parameters:

data – 2D (threshold-sites) array of data to put into the cube

Returns:

Cube containing spot probabilities.

set_up_spot_variable_cube(data, latitudes=None, longitudes=None, altitudes=None, wmo_ids=None, unique_site_id=None, unique_site_id_key=None, realizations=None, vertical_levels=None, pressure=False, height=False, *args, **kwargs)[source]#

Set up a spot cube containing a single variable field with:

  • latitude, longitude, altitude, WMO ID and unique site ID coordinates associated with the sites.

  • optional leading realization coordinate.

  • optional height or pressure levels.

  • additional options available by keywords passed to the _variable_cube function.

Parameters:
  • data (ndarray) – 1D (length of no. of sites) or 2D (realization-sites ordered) array of data to put into the cube.

  • latitudes (Union[List[float], ndarray, None]) – Optional list of latitude values of the same length as the number of sites.

  • longitudes (Union[List[float], ndarray, None]) – Optional list of longitude values of the same length as the number of sites.

  • altitudes (Union[List[float], ndarray, None]) – Optional list of altitude values of the same length as the number of sites.

  • wmo_ids (Union[List[float], ndarray, None]) – Optional list of WMO IDs that identify the sites, these are stored as padded 5-digit strings. Same length as the number of sites.

  • unique_site_id (Union[List[str], ndarray, None]) – Optional list of IDs that identify the sites, these are stored as padded 8-digit strings. Same length as the number of sites. If provided, unique_site_id_key must also be provided.

  • unique_site_id_key (Optional[str]) – Optional string that names the unique_site_id coordinate.

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

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

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

  • height (bool) – Flag to indicate whether the vertical levels are specified as height, in metres.

Returns:

Cube containing a single spot variable field

set_up_variable_cube(data, spatial_grid='latlon', x_grid_spacing=None, y_grid_spacing=None, domain_corner=None, realizations=None, vertical_levels=None, pressure=False, height=False, *args, **kwargs)[source]#

Set up a gridded cube containing a single variable field with:

  • a lat/lon or equal areas projection.

  • optional leading realization coordinate.

  • optional height or pressure levels.

  • additional options available by keywords passed to the _variable_cube function.

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

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

  • 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).

  • 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).

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

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

  • height (bool) – Flag to indicate whether the vertical levels are specified as height, in metres.

Returns:

Cube containing a single gridded variable field