improver.blending.spatial_weights module

Module to adjust weights spatially based on missing data in input cubes.

class improver.blending.spatial_weights.SpatiallyVaryingWeightsFromMask(fuzzy_length=10)[source]

Bases: improver.BasePlugin

Plugin for adjusting weights spatially based on missing data in the input cube. It takes in an initial one dimensional cube of weights which would be used to collapse a dimension on the input cube and outputs weights which have been adjusted based on the presence of missing data in x-y slices of input data. The resulting weights cube has a x and y dimensions in addition to the one dimension in the initial cube of weights.

The plugin does the following steps:

  1. Creates an initial spatial weights based on the mask in the input cube giving zero weight to where there is masked data and a weight of 1 where there is valid data.

  2. Make these weights fuzzy by smoothing the boundary between where there is valid data and no valid data. This keeps areas of zero weight, but reduces nearby grid points with a weight of 1 depending on how far they are from a grid point with a weight of zero. The range of the effect is controlled by the supplied fuzzy_length

  3. Multiplies the fuzzy spatial weights by the one dimensional weights supplied to the plugin.

  4. Normalises the weights along the coordinate that will be collapsed when blending is carried out using these weights.

__init__(fuzzy_length=10)[source]

Initialise class.

Parameters

fuzzy_length (int or float) – The length in terms of number of grid squares, over which the weights from the input data mask are smoothed. This is used when calculating a fuzzy weighting based on how far away each grid point is from a masked point. The distance is taken as the euclidean distance from the masked point, so the fuzzy length can be a non-integer value. Any points that are at least this distance away from a masked point keep a weight of one, and any points closer than this distance to a masked point have a weight of less than one based on how close to the masked point they are.

_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 213
_abc_registry = <_weakrefset.WeakSet object>
static create_initial_weights_from_mask(cube)[source]

Generate a cube with weights generated from the mask of the input cube. Where the data is masked we set the weight to zero, otherwise the weight is one.

Parameters

cube (iris.cube.Cube) – A cube containing the data we want to collapse by doing a weighted blend along a given coordinate.

Returns

A cube containing an initial set of weights based on the mask on the input cube.

Return type

iris.cube.Cube

Rasies:

ValueError : If the input cube does not have a mask.

static create_template_slice(cube_to_collapse, blend_coord)[source]

Create a template cube from a slice of the cube we are collapsing. The slice will be over blend_coord, y and x and will remove any other dimensions. This means that the resulting spatial weights won’t vary in any other dimension other than the blend_coord. If the mask does vary in another dimension an error is raised.

Parameters
  • cube_to_collapse (iris.cube.Cube) – The cube that will be collapsed along the blend_coord using the spatial weights generated using this plugin. Must be masked where there is invalid data.

  • blend_coord (str) – A string containing the name of the coordinate that the cube_to_collapse will be collapsed along. Also matches the coordinate in one_dimensional_weights_cube.

Returns

A cube containing dimension coordinates blend_coord, y, x, with all other dimensions stripped out.

Return type

iris.cube.Cube

Raises
  • ValueError – if the blend coordinate is associated with more than one dimension on the cube to collapse, or no dimension

  • ValueError – if the mask on cube_to_collapse varies along a dimension other than the dimension associated with blend_coord.

static multiply_weights(weights_from_mask, one_dimensional_weights_cube, blend_coord)[source]

Multiply two cubes together by taking slices along the coordinate matching the blend_coord string.

Parameters
  • weights_from_mask (iris.cube.Cube) – A cube with spatial weights and any other leading dimensions. This cube must have a coordinate matching the name given by blend_coord and this coordinate must be the same length as the corresponding coordinate in the one_dimensional_weights_cube.

  • one_dimensional_weights_cube (iris.cube.Cube) – A cube with one_dimensional weights. The only dimension coordinate in this cube matches the string given by blend_coord and the length of this coord must match the length of the same coordinate in weights_from_mask.

  • blend_coord (str) – The string that will match to a coordinate in both input cube. This is the coordinate that the input cubes will be sliced along and then multiplied. The corresponds to the coordinate used to collapse a cube using the weights generated by this plugin.

Returns

A cube with the same dimensions as the input cube, but with the weights multiplied by the weights from the one_dimensional_weights_cube. The blend_coord will be the leading dimension on the output cube.

Return type

iris.cube.Cube

static normalised_masked_weights(weights_cube, blend_coord)[source]

Normalise spatial weights along dimension associated with the blend_coord. If for a given point the sum of the weights along the blend_coord is zero then the returned normalised weight for that point will also be zero. This correspsonds to the case where there is missing data for that point for all slices along the blend_coord.

Parameters
  • weights_cube (iris.cube.Cube) – A cube with spatial weights and any other leading dimension. This cube must have a coordinate matching the name given by blend_coord which corresponds to the dimension along which the normalisation is needed.

  • blend_coord (str) – The string that will match to a coordinate in both input cube. This coordinate corresponds to the dimension along which the normalisation is needed.

Returns

A cube with the same dimensions as the input cube, but with the weights normalised along the blend_coord dimension. The blend_coord will be the leading dimension on the output cube.

Return type

iris.cube.Cube

process(cube_to_collapse, one_dimensional_weights_cube, blend_coord)[source]

Create fuzzy spatial weights based on missing data in the cube we are going to collapse and combine these with 1D weights along the blend_coord.

Parameters
  • cube_to_collapse (iris.cube.Cube) – The cube that will be collapsed along the blend_coord using the spatial weights generated using this plugin. Must be masked where there is invalid data. The mask may only vary along the blend_coord, and not along any other dimensions on the cube.

  • one_dimensional_weights_cube (iris.cube.Cube) – A cube containing a single dimension coordinate with the same name given blend_coord. This cube contains 1D weights that will be applied along the blend_coord but need adjusting spatially based on missing data.

  • blend_coord (str) – A string containing the name of the coordinate that the cube_to_collapse will be collapsed along. Also matches the coordinate in one_dimensional_weights_cube.

Returns

A cube containing normalised spatial weights based on the cube_to_collapsemask and the one_dimensional weights supplied. Contains the dimensions, blend_coord, y, x.

Return type

iris.cube.Cube

smooth_initial_weights(weights_from_mask)[source]

Create fuzzy weights around points in the weights_from_mask with zero weight.

This works by doing an euclidean distance transform based on how far each grid point is from a masked point. This returns an array containing the distance each point is from the nearest masked point. The result is then rescaled so that any point that are at least as far as the fuzzy_length away from a masked point are set back to a weight of one and any points that are closer than the fuzzy_length to a masked point are scaled to be between 0 and 1.

Parameters

weights_from_mask (iris.cube.Cube) – A cube containing an initial set of weights based on the mask on the input cube.

Returns

A cube containing the fuzzy weights calculated based on the weights_from_mask. The dimension order may have changed from the input cube as it has been sliced over x and y coordinates.

Return type

iris.cube.Cube