improver.wxcode.weather_symbols module¶
Module containing weather symbol implementation.
-
class
improver.wxcode.weather_symbols.WeatherSymbols(wxtree='high_resolution')[source]¶ Bases:
objectDefinition 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):
- 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.
-
check_input_cubes(cubes)[source]¶ Check that the input cubes contain all the diagnostics and thresholds required by the decision tree.
Parameters: cubes (iris.cube.CubeList) – A CubeList containing the input diagnostic cubes. 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 (string or list of strings) – 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 (string) – 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
-
static
construct_extract_constraint(diagnostics, thresholds)[source]¶ Construct an iris constraint.
Parameters: - diagnostics (string or list of strings) – The names of the diagnostics to be extracted from the CubeList.
- thresholds (iris.AuxCoord or list of iris.AuxCoord) – A thresholds within the given diagnostic cubes that are needed. Including units.
Returns: String, or list of strings, encoding iris cube constraints.
Return type: string or list of strings
-
static
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: condition_chain (list)
-
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: symbols (iris.cube.Cube)
-
static
find_all_routes(graph, start, end, 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 (string) – The node name of the tree root (currently always heavy_precipitation).
- end (int) – The weather symbol code to which we are tracing all routes.
- 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: routes (list)
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: - conditions (list) – A list of conditions to be combined into a single comparison statement.
- condition_combination (string) – The method by which multiple conditions should be combined, either AND or OR.
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.
Parameters: test_conditions (dict) – A single query from the decision tree. Returns: - tuple containing:
- inverted_threshold (string):
- A string representing the inverted comparison.
- inverted_combination (string):
- A string representing the inverted combination
Return type: (tuple)
-
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: symbols (iris.cube.Cube)
-