improver.utilities.mathematical_operations module

Module to contain mathematical operations.

class Integration(coord_name_to_integrate, start_point=None, end_point=None, positive_integration=False)[source]

Bases: BasePlugin

Perform integration along a chosen coordinate. This class currently supports the integration of positive values only, in order to support its usage as part of computing the wet-bulb temperature integral. Generalisation of this class to support standard numerical integration can be undertaken, if required.

__init__(coord_name_to_integrate, start_point=None, end_point=None, positive_integration=False)[source]

Initialise class.

Parameters:
  • coord_name_to_integrate (str) – Name of the coordinate to be integrated.

  • start_point (Optional[float]) – Point at which to start the integration. Default is None. If start_point is None, integration starts from the first available point.

  • end_point (Optional[float]) – Point at which to end the integration. Default is None. If end_point is None, integration will continue until the last available point.

  • positive_integration (bool) – Description of the direction in which to integrate. True corresponds to the values within the array increasing as the array index increases. False corresponds to the values within the array decreasing as the array index increases.

_abc_impl = <_abc_data object>
_create_output_cube(template, data, points, bounds)[source]

Populates a template cube with data from the integration

Parameters:
  • template (Cube) – Copy of upper or lower bounds cube, based on direction of integration

  • data (Union[List[float], ndarray]) – Integrated data

  • points (Union[List[float], ndarray]) – Points values for the integrated coordinate. These will not match the template cube if any slices were skipped in the integration, and therefore are used to slice the template cube to match the data array.

  • bounds (Union[List[float], ndarray]) – Bounds values for the integrated coordinate

Return type:

Cube

Returns:

Cube with data from integration

_generate_output_name_and_units()[source]

Gets suitable output name and units from input cube metadata

Return type:

Tuple[str, str]

ensure_monotonic_increase_in_chosen_direction(cube)[source]

Ensure that the chosen coordinate is monotonically increasing in the specified direction.

Parameters:

cube (Cube) – The cube containing the coordinate to check. Note that the input cube will be modified by this method.

Return type:

Cube

Returns:

The cube containing a coordinate that is monotonically increasing in the desired direction.

perform_integration(upper_bounds_cube, lower_bounds_cube)[source]

Perform the integration.

Integration is performed by firstly defining the stride as the difference between the upper and lower bound. The contribution from the uppermost half of the stride is calculated by multiplying the upper bound value by 0.5 * stride, and the contribution from the lowermost half of the stride is calculated by multiplying the lower bound value by 0.5 * stride. The contribution from the uppermost half of the stride and the bottom half of the stride is summed.

Integration is performed ONLY over positive values.

Parameters:
  • upper_bounds_cube (Cube) – Cube containing the upper bounds to be used during the integration.

  • lower_bounds_cube (Cube) – Cube containing the lower bounds to be used during the integration.

Return type:

Cube

Returns:

Cube containing the output from the integration.

prepare_for_integration()[source]

Prepare for integration by creating the cubes needed for the integration. These are separate cubes for representing the upper and lower limits of the integration.

Return type:

Tuple[Cube, Cube]

Returns:

  • Cube containing the upper bounds to be used during the integration.

  • Cube containing the lower bounds to be used during the integration.

process(cube)[source]

Integrate data along a specified coordinate. Only positive values are integrated; zero and negative values are not included in the sum or as levels on the integrated cube.

Parameters:

cube (Cube) – Cube containing the data to be integrated.

Return type:

Cube

Returns:

The cube containing the result of the integration. This will have the same name and units as the input cube (TODO same name and units are incorrect - fix this).

fast_linear_fit(x_data, y_data, axis=None, keepdims=False, gradient_only=False, with_nan=False)[source]

Uses a simple linear fit approach to calculate the gradient along specified axis (default is to fit all points). Uses vectorized operations, so it’s much faster than using scipy lstsq in a loop. This function does not handle NaNs, but will work with masked arrays.

Parameters:
  • x_data (ndarray) – x axis data.

  • y_data (ndarray) – y axis data.

  • axis (Union[int, Tuple[int, ...], None]) – Optional argument, specifies the axis to operate on. Default is to flatten arrays and fit all points.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • gradient_only (bool) – If true only returns the gradient.

  • with_nan (bool) – If true, there are NaNs in your data (that you know about).

Return type:

Tuple[ndarray, ndarray]

Returns:

tuple with first element being the gradient between x and y, and the second element being the calculated y-intercepts.