improver.ensemble_calibration.ensemble_calibration_utilities module

This module defines all the utilities used by the “plugins” specific for ensemble calibration.

class improver.ensemble_calibration.ensemble_calibration_utilities.SplitHistoricForecastAndTruth(historic_forecast_dict, truth_dict)[source]

Bases: improver.BasePlugin

Split the historic forecasts and truth datasets based on the metadata identifiers provided.

__init__(historic_forecast_dict, truth_dict)[source]

Initialise the plugin.

Parameters
  • historic_forecast_dict (dict) –

    Dictionary specifying the metadata that defines the historic forecast. For example:

    {
        "attributes": {
            "mosg__model_configuration": "uk_ens"
        }
    }
    

  • truth_dict (dict) –

    Dictionary specifying the metadata that defines the truth. For example:

    {
        "attributes": {
            "mosg__model_configuration": "uk_det"
        }
    }
    

Raises

NotImplementedError – ‘attributes’ is the only supported key for the input dictionaries.

_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 213
_abc_registry = <_weakrefset.WeakSet object>
static _find_required_cubes_using_metadata(cubes, input_dict)[source]

Extract the cube that matches the information within the input dictionary.

Parameters
  • cubes (iris.cube.CubeList) – The cubes that will be checked for matches against the metadata specified in the input_dict.

  • input_dict (dict) – A dictionary containing the metadata that will be used to identify the desired cubes.

Returns

CubeList containing cubes that match the metadata supplied within the input dictionary.

Return type

iris.cube.CubeList

Raises

ValueError – The metadata supplied resulted in no matching cubes.

process(cubes)[source]

Separate the input cubes into the historic_forecasts and truth based on the metadata information supplied within the input dictionaries.

Parameters

cubes (iris.cube.CubeList) – CubeList of input cubes that are expected to contain a mixture of historic forecasts and truth.

Returns

tuple containing:
iris.cube.Cube:

A cube containing the historic forecasts.

iris.cube.Cube:

A cube containing the truth datasets.

Return type

(tuple)

improver.ensemble_calibration.ensemble_calibration_utilities.check_predictor_of_mean_flag(predictor_of_mean_flag)[source]

Check the predictor_of_mean_flag at the start of the process methods in relevant ensemble calibration plugins, to avoid having to check and raise an error later.

Parameters

predictor_of_mean_flag (str) – String to specify the input to calculate the calibrated mean. Currently the ensemble mean (“mean”) and the ensemble realizations (“realizations”) are supported as the predictors.

Raises

ValueError – If the predictor_of_mean_flag is not valid.

improver.ensemble_calibration.ensemble_calibration_utilities.convert_cube_data_to_2d(forecast, coord='realization', transpose=True)[source]

Function to convert data from a N-dimensional cube into a 2d numpy array. The result can be transposed, if required.

Parameters
  • forecast (iris.cube.Cube) – N-dimensional cube to be reshaped.

  • coord (str) – The data will be flattened along this coordinate.

  • transpose (bool) – If True, the resulting flattened data is transposed. This will transpose a 2d array of the format [:, coord] to [coord, :]. If False, the resulting flattened data is not transposed. This will result in a 2d array of format [:, coord].

Returns

Reshaped 2d array.

Return type

numpy.ndarray

improver.ensemble_calibration.ensemble_calibration_utilities.flatten_ignoring_masked_data(data_array, preserve_leading_dimension=False)[source]

Flatten an array, selecting only valid data if the array is masked. There is also the option to reshape the resulting array so it has the same leading dimension as the input array, but the other dimensions of the array are flattened. It is assumed that each of the slices along the leading dimension are masked in the same way. This functionality is used in EstimateCoefficientsForEnsembleCalibration when realizations are used as predictors.

Parameters
  • data_array (numpy.ndarray or numpy.ma.MaskedArray) – An array or masked array to be flattened. If it is masked and the leading dimension is preserved the mask must be the same for every slice along the leading dimension.

  • preserve_leading_dimension (bool) – Default False. If True the flattened array is reshaped so it has the same leading dimension as the input array. If False the returned array is 1D.

Returns

A flattened array containing only valid data. Either 1D or, if preserving the leading dimension 2D. In the latter case the leading dimension is the same as the input data_array.

Return type

numpy.ndarray

Raises

ValueError – If preserving the leading dimension and the mask on the input array is not the same for every slice along the leading dimension.