improver.blending.calculate_weights_and_blend module

Plugin to calculate blend weights and blend data across a dimension

class WeightAndBlend(blend_coord, wts_calc_method, weighting_coord=None, wts_dict=None, y0val=None, ynval=None, cval=None, inverse_ordering=False)[source]

Bases: PostProcessingPlugin

Wrapper class to calculate weights and blend data across cycles or models

__init__(blend_coord, wts_calc_method, weighting_coord=None, wts_dict=None, y0val=None, ynval=None, cval=None, inverse_ordering=False)[source]

Initialise central parameters

Parameters:
  • blend_coord (str) – Coordinate over which blending will be performed (eg “model” for grid blending)

  • wts_calc_method (str) – Method to use to calculate weights used in blending. “linear” (default): calculate linearly varying blending weights. “nonlinear”: calculate blending weights that decrease exponentially with increasing blending coordinates. “dict”: calculate weights using a dictionary passed in.

  • weighting_coord (Optional[str]) – Name of coordinate over which linear weights should be scaled. This coordinate must be available in the weights dictionary.

  • wts_dict (Optional[Dict[str, Dict[str, Any]]]) – Dictionary from which to calculate blending weights. Dictionary format is as specified in improver.blending.weights.ChooseWeightsLinear

  • y0val (Optional[float]) – The relative value of the weighting start point (lowest value of blend coord) for choosing default linear weights. If used this must be a positive float or 0.

  • ynval (Optional[float]) – The relative value of the weighting end point (highest value of blend coord) for choosing default linear weights. This must be a positive float or 0. Note that if blending over forecast reference time, ynval >= y0val would normally be expected (to give greater weight to the more recent forecast).

  • cval (Optional[float]) – Factor used to determine how skewed the non-linear weights will be. A value of 1 implies equal weighting.

  • inverse_ordering (bool) – Option to invert weighting order for non-linear weights plugin so that higher blend coordinate values get higher weights (eg if cycle blending over forecast reference time).

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

Wrapper for plugins to calculate blending weights by the appropriate method.

Parameters:

cube (Cube) – Cube of input data to be blended

Return type:

Cube

Returns:

Cube containing 1D array of weights for blending

_remove_zero_weighted_slices(cube, weights)[source]

Removes any cube and weights slices where the 1D weighting factor is zero

Parameters:
  • cube (Cube) – The data cube to be blended

  • weights (Cube) – 1D cube of weights varying along self.blend_coord

Return type:

Tuple[Cube, Cube]

Returns:

  • Data cube without zero-weighted slices

  • Weights without zeroes

_update_spatial_weights(cube, weights, fuzzy_length)[source]

Update weights using spatial information

Parameters:
  • cube (Cube) – Cube of input data to be blended

  • weights (Cube) – Initial 1D cube of weights scaled by self.weighting_coord

  • fuzzy_length (float) – Distance (in metres) over which to smooth weights at domain boundaries

Return type:

Cube

Returns:

Updated 3D cube of spatially-varying weights

process(cubelist, cycletime=None, model_id_attr=None, record_run_attr=None, spatial_weights=False, fuzzy_length=20000, attributes_dict=None)[source]

Merge a cubelist, calculate appropriate blend weights and compute the weighted mean. Returns a single cube collapsed over the dimension given by self.blend_coord.

Parameters:
  • cubelist (Union[List[Cube], CubeList]) – List of cubes to be merged and blended

  • cycletime (Optional[str]) – The forecast reference time to be used after blending has been applied, in the format YYYYMMDDTHHMMZ. If not provided, the blended file takes the latest available forecast reference time from the input datasets supplied.

  • model_id_attr (Optional[str]) – The name of the dataset attribute to be used to identify the source model when blending data from different models.

  • record_run_attr (Optional[str]) – The name of the dataset attribute to be used to store model and cycle sources in metadata, e.g. when blending data from different models. Requires model_id_attr.

  • spatial_weights (bool) – If True, this option will result in the generation of spatially varying weights based on the masks of the data we are blending. The one dimensional weights are first calculated using the chosen weights calculation method, but the weights will then be adjusted spatially based on where there is masked data in the data we are blending. The spatial weights are calculated using the SpatiallyVaryingWeightsFromMask plugin.

  • fuzzy_length (float) – When calculating spatially varying weights we can smooth the weights so that areas close to areas that are masked have lower weights than those further away. This fuzzy length controls the scale over which the weights are smoothed. The fuzzy length is in terms of m, the default is 20km. This distance is then converted into a number of grid squares, which does not have to be an integer. Assumes the grid spacing is the same in the x and y directions and raises an error if this is not true. See SpatiallyVaryingWeightsFromMask for more details.

  • attributes_dict (Optional[Dict[str, str]]) – Dictionary describing required changes to attributes after blending

Return type:

Cube

Returns:

Cube of blended data.

Raises:

ValueError – If attempting to use record_run_attr without providing model_id_attr.

Warns:

UserWarning – If blending masked data without spatial weights. This has not been fully tested.