improver.wxcode.weather_symbols module¶
Module containing weather symbol implementation.
-
class
improver.wxcode.weather_symbols.WeatherSymbols(wxtree='high_resolution')[source]¶ Bases:
improver.BasePluginDefinition and implementation of a weather symbol decision tree. This plugin uses a variety of diagnostic inputs and the decision tree logic to determine the most representative weather symbol for each site defined in the input cubes.
-
__init__(wxtree='high_resolution')[source]¶ Define a decision tree for determining weather symbols based upon the input diagnostics. Use this decision tree to allocate a weather symbol to each point.
- Key Args:
- wxtree (str):
Used to choose weather symbol decision tree. Default is ‘high_resolution’ ‘global’ will load the global weather symbol decision tree.
float_tolerance defines the tolerance when matching thresholds to allow for the difficulty of float comparisons. float_abs_tolerance defines the tolerance for when the threshold is zero. It has to be sufficiently small that a valid rainfall rate or snowfall rate could not trigger it.
-
_abc_cache= <_weakrefset.WeakSet object>¶
-
_abc_negative_cache= <_weakrefset.WeakSet object>¶
-
_abc_negative_cache_version= 213¶
-
_abc_registry= <_weakrefset.WeakSet object>¶
-
check_input_cubes(cubes)[source]¶ Check that the input cubes contain all the diagnostics and thresholds required by the decision tree. Sets self.coord_named_threshold to “True” if threshold-type coordinates have the name “threshold” (as opposed to the standard name of the diagnostic), for backward compatibility.
- Parameters
cubes (iris.cube.CubeList) – A CubeList containing the input diagnostic cubes.
- Returns
A dictionary of (keyword) nodes names where the diagnostic data is missing and (values) node associated with diagnostic_missing_action.
- Return type
- Raises
IOError – Raises an IOError if any of the required input data is missing. The error includes details of which fields are missing.
-
static
construct_condition(extract_constraint, condition, probability_threshold, gamma)[source]¶ Create a string representing a comparison condition.
- Parameters
extract_constraint (str or list of str) – A string, or list of strings, encoding iris constraints that will be used to extract the correct diagnostic cube (by name) from the input cube list and the correct threshold from that cube.
condition (str) – The condition statement (e.g. greater than, >).
probability_threshold (float) – The probability value to use in the comparison.
gamma (float or None) – The gamma factor to multiply one field by when performing a subtraction. This value will be None in the case that extract_constraint is not a list; it will not be used.
- Returns
The formatted condition statement, e.g.:
cubes.extract(Constraint( name='probability_of_rainfall_rate_above_threshold', coord_values={'threshold': 0.03}) )[0].data < 0.5)
- Return type
string
-
construct_extract_constraint(diagnostics, thresholds, coord_named_threshold)[source]¶ Construct an iris constraint.
- Parameters
diagnostics (str or list of str) – The names of the diagnostics to be extracted from the CubeList.
thresholds (iris.AuxCoord or list of iris.AuxCoord) – All thresholds within the given diagnostic cubes that are needed, including units. Note these are NOT coords from the original cubes, just constructs to associate units with values.
coord_named_threshold (bool) – If true, use old naming convention for threshold coordinates (coord.long_name=threshold). Otherwise extract threshold coordinate name from diagnostic name
- Returns
String, or list of strings, encoding iris cube constraints.
- Return type
str or list of str
-
create_condition_chain(test_conditions)[source]¶ A wrapper to call the construct_condition function for all the conditions specified in a single query.
- Parameters
test_conditions (dict) – A query from the decision tree.
- Returns
A list of strings that describe the conditions comprising the query. e.g.:
[ "(cubes.extract(Constraint( name='probability_of_rainfall_rate_above_threshold', coord_values={'threshold': 0.03}) )[0].data < 0.5) | (cubes.extract(Constraint( name= 'probability_of_lwe_snowfall_rate_above_threshold', coord_values={'threshold': 0.03}) )[0].data < 0.5)" ]
- Return type
list of str
-
static
create_symbol_cube(cube)[source]¶ Create an empty weather_symbol cube initialised with -1 across the grid.
- Parameters
cube (iris.cube.Cube) – An x-y slice of one of the input cubes, used to define the size of the weather symbol grid.
- Returns
A cube full of -1 values, with suitable metadata to describe the weather symbols that will fill it.
- Return type
-
static
find_all_routes(graph, start, end, omit_nodes=None, route=None)[source]¶ Function to trace all routes through the decision tree.
- Parameters
graph (dict) – A dictionary that describes each node in the tree, e.g. {<node_name>: [<succeed_name>, <fail_name>]}
start (str) – The node name of the tree root (currently always heavy_precipitation).
end (int) – The weather symbol code to which we are tracing all routes.
omit_nodes (dict) – A dictionary of (keyword) nodes names where the diagnostic data is missing and (values) node associated with diagnostic_missing_action.
route (list) – A list of node names found so far.
- Returns
A list of node names that defines the route from the tree root to the weather symbol leaf (end of chain).
- Return type
list of str
References
Method based upon Python Patterns - Implementing Graphs essay https://www.python.org/doc/essays/graphs/
-
static
format_condition_chain(conditions, condition_combination='AND')[source]¶ Chain individual condition statements together in a format that numpy.where can use to make a series of comparisons.
- Parameters
- Returns
A string formatted as a chain of conditions suitable for use in a numpy.where statement. e.g. (condition 1) & (condition 2)
- Return type
string
-
static
invert_condition(test_conditions)[source]¶ Invert a comparison condition to select the negative case.
-
process(cubes)[source]¶ Apply the decision tree to the input cubes to produce weather symbol output.
- Parameters
cubes (iris.cube.CubeList) – A cubelist containing the diagnostics required for the weather symbols decision tree, these at co-incident times.
- Returns
A cube of weather symbols.
- Return type
-