improver.nbhood.recursive_filter module

Module to apply a recursive filter to neighbourhooded data.

class RecursiveFilter(iterations=None, edge_width=15)[source]

Bases: PostProcessingPlugin

Apply a recursive filter to the input cube.

__init__(iterations=None, edge_width=15)[source]

Initialise the class.

Parameters:
  • iterations (Optional[int]) – The number of iterations of the recursive filter.

  • edge_width (int) – Half the width of the padding halo applied before recursive filtering.

Raises:

ValueError – If number of iterations is not None and is set such that iterations is less than 1.

_abc_impl = <_abc_data object>
_pad_coefficients(coeff_x, coeff_y)[source]

Pad smoothing coefficients

static _recurse_backward(grid, smoothing_coefficients, axis)[source]

Method to run the recursive filter in the backwards direction.

In the backwards direction:

Recursive filtering is calculated as:

\[B_i = ((1 - \rm{smoothing\_coefficient}) \times A_i) + (\rm{smoothing\_coefficient} \times B_{i+1})\]
Progressing from gridpoint i+1 to i:

\(B_i\) = new value at gridpoint i

\(A_i\) = Old value at gridpoint i

\(B_{i+1}\) = New value at gridpoint i+1

Parameters:
  • grid (ndarray) – 2D array containing the input data to which the recursive filter will be applied.

  • smoothing_coefficients (ndarray) – Matching 2D array of smoothing_coefficient values that will be used when applying the recursive filter along the specified axis.

  • axis (int) – Index of the spatial axis (0 or 1) over which to recurse.

Return type:

ndarray

Returns:

2D array containing the smoothed field after the recursive filter method has been applied to the input array in the backwards direction along the specified axis.

static _recurse_forward(grid, smoothing_coefficients, axis)[source]

Method to run the recursive filter in the forward direction.

In the forward direction:

Recursive filtering is calculated as:

\[B_i = ((1 - \rm{smoothing\_coefficient_{i-1}}) \times A_i) + (\rm{smoothing\_coefficient_{i-1}} \times B_{i-1})\]
Progressing from gridpoint i-1 to i:

\(B_i\) = new value at gridpoint i

\(A_i\) = Old value at gridpoint i

\(B_{i-1}\) = New value at gridpoint i - 1

Parameters:
  • grid (ndarray) – 2D array containing the input data to which the recursive filter will be applied.

  • smoothing_coefficients (ndarray) – Matching 2D array of smoothing_coefficient values that will be used when applying the recursive filter along the specified axis.

  • axis (int) – Index of the spatial axis (0 or 1) over which to recurse.

Return type:

ndarray

Returns:

2D array containing the smoothed field after the recursive filter method has been applied to the input array in the forward direction along the specified axis.

static _run_recursion(cube, smoothing_coefficients_x, smoothing_coefficients_y, iterations)[source]

Method to run the recursive filter.

Parameters:
  • cube (Cube) – 2D cube containing the input data to which the recursive filter will be applied.

  • smoothing_coefficients_x (Cube) – 2D cube containing array of smoothing_coefficient values that will be used when applying the recursive filter along the x-axis.

  • smoothing_coefficients_y (Cube) – 2D cube containing array of smoothing_coefficient values that will be used when applying the recursive filter along the y-axis.

  • iterations (int) – The number of iterations of the recursive filter

Return type:

Cube

Returns:

Cube containing the smoothed field after the recursive filter method has been applied to the input cube.

static _update_coefficients_from_mask(coeffs_x, coeffs_y, mask)[source]

Zero all smoothing coefficients for data points that are masked

Parameters:
Return type:

Tuple[Cube, Cube]

Returns:

Updated smoothing coefficients

_validate_coefficients(cube, smoothing_coefficients)[source]

Validate the smoothing coefficients cubes.

Parameters:
  • cube (Cube) – 2D cube containing the input data to which the recursive filter will be applied.

  • smoothing_coefficients (CubeList) – A cubelist containing two cubes of smoothing_coefficient values, one corresponding to smoothing in the x-direction, and the other to smoothing in the y-direction.

Returns:

[x-coeffs, y-coeffs].

Return type:

A list of smoothing coefficients cubes ordered

Raises:
  • ValueError – Smoothing coefficient cubes are not named correctly.

  • ValueError – If any smoothing_coefficient cube value is over 0.5

  • ValueError – The coordinate to be smoothed within the smoothing coefficient cube is not of the expected length.

  • ValueError – The coordinate to be smoothed within the smoothing coefficient cube does not have the expected points.

process(cube, smoothing_coefficients)[source]

Set up the smoothing_coefficient parameters and run the recursive filter. Smoothing coefficients can be generated using OrographicSmoothingCoefficients and generate_orographic_smoothing_coefficients(). The steps undertaken are:

  1. Split the input cube into slices determined by the co-ordinates in the x and y directions.

  2. Construct an array of filter parameters (smoothing_coefficients_x and smoothing_coefficients_y) for each cube slice that are used to weight the recursive filter in the x- and y-directions.

  3. Pad each cube slice with a square-neighbourhood halo and apply the recursive filter for the required number of iterations.

  4. Remove the halo from the cube slice and append the recursed cube slice to a ‘recursed cube’.

  5. Merge all the cube slices in the ‘recursed cube’ into a ‘new cube’.

  6. Modify the ‘new cube’ so that its scalar dimension co-ordinates are consistent with those in the original input cube.

  7. Return the ‘new cube’ which now contains the recursively filtered values for the original input cube.

The smoothing_coefficient determines how much “value” of a cell undergoing filtering is comprised of the current value at that cell and how much comes from the adjacent cell preceding it in the direction in which filtering is being applied. A larger smoothing_coefficient results in a more significant proportion of a cell’s new value coming from its neighbouring cell.

Parameters:
  • cube (Cube) – Cube containing the input data to which the recursive filter will be applied.

  • smoothing_coefficients (CubeList) – A cubelist containing two cubes of smoothing_coefficient values, one corresponding to smoothing in the x-direction, and the other to smoothing in the y-direction.

Return type:

Cube

Returns:

Cube containing the smoothed field after the recursive filter method has been applied.

Raises:

ValueError – If the cube contains masked data from multiple cycles or times