improver.wxcode.modal_code module
Module containing a plugin to calculate the modal weather code in a period.
- class ModalWeatherCode(model_id_attr=None, record_run_attr=None)[source]
Bases:
BasePluginPlugin that returns the modal code over the period spanned by the input data. In cases of a tie in the mode values, scipy returns the smaller value. The opposite is desirable in this case as the significance / importance of the weather codes generally increases with the value. To achieve this the codes are subtracted from an arbitrarily larger number prior to calculating the mode, and this operation reversed in the final output.
If there are many different codes for a single point over the time spanned by the input cubes it may be that the returned mode is not robust. Given the preference to return more significant codes explained above, a 12 hour period with 12 different codes, one of which is thunder, will return a thunder code to describe the whole period. This is likely not a good representation. In these cases grouping is used to try and select a suitable weather code (e.g. a rain shower if the codes include a mix of rain showers and dynamic rain) by providing a more robust mode. The lowest number (least significant) member of the group is returned as the code. Use of the least significant member reflects the lower certainty in the forecasts.
Where there are different weather codes available for night and day, the modal code returned is always a day code, regardless of the times covered by the input files.
- __init__(model_id_attr=None, record_run_attr=None)[source]
Set up plugin and create an aggregator instance for reuse
- Parameters
model_id_attr (
Optional[str]) – Name of attribute recording source models that should be inherited by the output cube. The source models are expected as a space-separated string.record_run_attr (
Optional[str]) – Name of attribute used to record models and cycles used in constructing the weather symbols.
- _abc_impl = <_abc_data object>
- static _group_codes(modal, cube)[source]
In instances where the mode returned is not significant, i.e. the weather code chosen occurs infrequently in the period, the codes can be grouped to yield a more definitive period code. Given the uncertainty, the least significant weather type (lowest number in a group that is found in the data) is used to replace the other data values that belong to that group prior to recalculating the modal code.
The modal cube is modified in place.
- static _set_blended_times(cube)[source]
Updates time coordinates so that time point is at the end of the time bounds, blend_time and forecast_reference_time (if present) are set to the end of the bound period and bounds are removed, and forecast_period is updated to match.
- Return type
- static _unify_day_and_night(cube)[source]
Remove distinction between day and night codes so they can each contribute when calculating the modal code. The cube of weather codes is modified in place with all night codes made into their daytime equivalents.
- Parameters
codes. (A cube of weather) –
- static mode_aggregator(data, axis)[source]
An aggregator for use with iris to calculate the mode along the specified axis. If the modal value selected comprises less than 10% of data along the dimension being collapsed, the value is set to the UNSET_CODE_INDICATOR to indicate that the uncertainty was too high to return a mode.
- process(cubes)[source]
Calculate the modal weather code, with handling for edge cases.
- Parameters
cubes (
CubeList) – A list of weather code cubes at different times. A modal code will be calculated over the time coordinate to return the most comon code, which is taken to be the best representation of the whole period.- Return type
- Returns
A single weather code cube with time bounds that span those of the input weather code cubes.