def process( orography: cli.inputcube, land_sea_mask: cli.inputcube = None, *, bands_config: cli.inputjson = None, ): """Runs topographic bands mask generation. Reads orography and land_sea_mask fields of a cube. Creates a series of masks, where each mask excludes data below or equal to the lower threshold and excludes data above the upper threshold. Args: orography (iris.cube.Cube): The orography on a standard grid. land_sea_mask (iris.cube.Cube): The land mask on standard grid, with land points set to one and sea points set to zero. If provided sea points will be set to zero in every band. If no land mask is provided, sea points will be included in the appropriate topographic band. bands_config (dict): Definition of orography bands required. The expected format of the dictionary is e.g {'bounds':[[0, 50], [50, 200]], 'units': 'm'} The default dictionary has the following form: {'bounds': [[-500., 50.], [50., 100.], [100., 150.],[150., 200.], [200., 250.], [250., 300.], [300., 400.], [400., 500.], [500., 650.],[650., 800.], [800., 950.], [950., 6000.]], 'units': 'm'} Returns: iris.cube.Cube: list of orographic band mask cube. """ from improver.generate_ancillaries.generate_ancillary import ( GenerateOrographyBandAncils, THRESHOLDS_DICT, ) if bands_config is None: bands_config = THRESHOLDS_DICT if land_sea_mask: land_sea_mask = next( land_sea_mask.slices( [land_sea_mask.coord(axis="y"), land_sea_mask.coord(axis="x")])) orography = next( orography.slices( [orography.coord(axis="y"), orography.coord(axis="x")])) result = GenerateOrographyBandAncils()(orography, bands_config, landmask=land_sea_mask) result = result.concatenate_cube() return result
def process(cube: cli.inputcube): """Sets night values to zero for UV index. Args: cube (iris.cube.Cube): Cube that will have night values set to zero. This should contain either diagnostic values or probabilities of UV index above threshold. Returns: iris.cube.Cube: Input cube with all night values set to zero. Raises: ValueError: If input cube is suspicious, within reason. Note that this is a general check: the CLI expects a cube of UV index or probability of UV index above thresold, and will raise an error if given a probability below threshold, but will not recognise a completely inappropriate cube (eg temperature in Kelvin). Therefore this CLI should be used with care. """ import numpy as np from improver.metadata.probabilistic import is_probability from improver.utilities.solar import DayNightMask if is_probability(cube): if "above_threshold" not in cube.name(): raise ValueError(f"{cube.name()} unsuitable for night masking") mask = DayNightMask()(cube).data # Broadcast mask to shape of input cube to account for additional dimensions. mask = np.broadcast_to(mask, cube.shape) # setting night values to zero. cube.data = np.where(mask == DayNightMask().night, 0, cube.data) return cube
def process(cube: cli.inputcube, raw_cube: cli.inputcube = None, *, realizations_count: int = None, random_seed: int = None, ignore_ecc_bounds=False): """Converts an incoming cube into one containing realizations. Args: cube (iris.cube.Cube): A cube to be processed. raw_cube (iris.cube.Cube): Cube of raw (not post processed) weather data. If this argument is given ensemble realizations will be created from percentiles by reshuffling them in correspondence to the rank order of the raw ensemble. Otherwise, the percentiles are rebadged as realizations. realizations_count (int): The number of ensemble realizations in the output. random_seed (int): Option to specify a value for the random seed for testing purposes, otherwise the default random seed behaviours is utilised. The random seed is used in the generation of the random numbers used for splitting tied values within the raw ensemble, so that the values from the input percentiles can be ordered to match the raw ensemble. ignore_ecc_bounds (bool): If True, where percentiles exceed the ECC bounds range, raises a warning rather than an exception. Returns: iris.cube.Cube: The processed cube. """ from improver.cli import (percentiles_to_realizations, probabilities_to_realizations) if cube.coords('percentile'): output_cube = percentiles_to_realizations.process( cube, raw_cube=raw_cube, realizations_count=realizations_count, random_seed=random_seed, ignore_ecc_bounds=ignore_ecc_bounds) elif cube.coords(var_name='threshold'): output_cube = probabilities_to_realizations.process( cube, raw_cube=raw_cube, realizations_count=realizations_count, random_seed=random_seed, ignore_ecc_bounds=ignore_ecc_bounds) elif cube.coords(var_name='realization'): output_cube = cube else: raise ValueError("Unable to convert to realizations:\n" + str(cube)) return output_cube
def process(orography: cli.inputcube, land_sea_mask: cli.inputcube = None, *, bands_config: cli.inputjson = None): """Runs topographic weights generation. Reads the orography and land_sea_mask fields of a cube. Creates a series of topographic zone weights to indicate where an orography point sits within the defined topographic bands. If the orography point is in the centre of a topographic band, then a single band will have a weight 1.0. If the orography point is at the edge of a topographic band, then the upper band will have a 0.5 weight whilst the lower band will also have a 0.5 weight. Otherwise the weight will vary linearly between the centre of a topographic band and the edge. Args: orography (iris.cube.Cube): The orography on a standard grid. land_sea_mask (iris.cube.Cube): Land mask on a standard grid. If provided, sea points will be masked and set to the default fill value. If no land mask is provided, weights will be generated for sea points as well as land in the appropriate topographic band. bands_config (dict): Definition of orography bands required. The expected format of the dictionary is e.g {'bounds':[[0, 50], [50, 200]], 'units': 'm'} The default dictionary has the following form: {'bounds': [[-500., 50.], [50., 100.], [100., 150.],[150., 200.], [200., 250.], [250., 300.], [300., 400.], [400., 500.], [500., 650.],[650., 800.], [800., 950.], [950., 6000.]], 'units': 'm'} Returns: iris.cube.Cube: Cube containing the weights depending upon where the orography point is within the topographical zones. """ from improver.generate_ancillaries.generate_topographic_zone_weights \ import GenerateTopographicZoneWeights from improver.generate_ancillaries.generate_ancillary import ( THRESHOLDS_DICT) if bands_config is None: bands_config = THRESHOLDS_DICT if land_sea_mask: land_sea_mask = next(land_sea_mask.slices( [land_sea_mask.coord(axis='y'), land_sea_mask.coord(axis='x')])) orography = next(orography.slices( [orography.coord(axis='y'), orography.coord(axis='x')])) result = GenerateTopographicZoneWeights().process( orography, bands_config, landmask=land_sea_mask) return result
def process(cube: cli.inputcube, *, realizations_count: int = None, ignore_ecc_bounds=False): """Converts an incoming cube into one containing realizations. Args: cube (iris.cube.Cube): A cube to be processed. realizations_count (int): The number of ensemble realizations in the output. ignore_ecc_bounds (bool): If True, where percentiles exceed the ECC bounds range, raises a warning rather than an exception. Returns: iris.cube.Cube: The processed cube. """ from improver.cli import (percentiles_to_realizations, probabilities_to_realizations) if cube.coords('percentile'): output_cube = percentiles_to_realizations.process( cube, realizations_count=realizations_count, ignore_ecc_bounds=ignore_ecc_bounds) elif cube.coords(var_name='threshold'): output_cube = probabilities_to_realizations.process( cube, realizations_count=realizations_count, ignore_ecc_bounds=ignore_ecc_bounds) elif cube.coords(var_name='realization'): output_cube = cube else: raise ValueError("Unable to convert to realizations:\n" + str(cube)) return output_cube
def process(cube: cli.inputcube): """Sets night values to zero for UV index. Args: cube (iris.cube.Cube): Cube that will have night values set to zero. Returns: iris.cube.Cube: Input cube with all night values set to zero. """ import numpy as np from improver.utilities.solar import DayNightMask mask = DayNightMask()(cube).data # Broadcast mask to shape of input cube to account for additional dimensions. mask = np.broadcast_to(mask, cube.shape) # setting night values to zero. cube.data = np.where(mask == DayNightMask().night, 0, cube.data) return cube
def process( neighbour_cube: cli.inputcube, cube: cli.inputcube, lapse_rate: cli.inputcube = None, *, apply_lapse_rate_correction=False, land_constraint=False, similar_altitude=False, extract_percentiles: cli.comma_separated_list = None, ignore_ecc_bounds=False, new_title: str = None, suppress_warnings=False, ): """Module to run spot data extraction. Extract diagnostic data from gridded fields for spot data sites. It is possible to apply a temperature lapse rate adjustment to temperature data that helps to account for differences between the spot site's real altitude and that of the grid point from which the temperature data is extracted. Args: neighbour_cube (iris.cube.Cube): Cube of spot-data neighbours and the spot site information. cube (iris.cube.Cube): Cube containing the diagnostic data to be extracted. lapse_rate (iris.cube.Cube): Optional cube containing temperature lapse rates. If this cube is provided and a screen temperature cube is being processed, the lapse rates will be used to adjust the temperature to better represent each spot's site-altitude. apply_lapse_rate_correction (bool): Use to apply a lapse-rate correction to screen temperature data so that the data are a better match the altitude of the spot site for which they have been extracted. land_constraint (bool): Use to select the nearest-with-land-constraint neighbour-selection method from the neighbour_cube. This means that the grid points should be land points except for sites where none were found within the search radius when the neighbour cube was created. May be used with similar_altitude. similar_altitude (bool): Use to select the nearest-with-height-constraint neighbour-selection method from the neighbour_cube. These are grid points that were found to be the closest in altitude to the spot site within the search radius defined when the neighbour cube was created. May be used with land_constraint. extract_percentiles (list or int): If set to a percentile value or a list of percentile values, data corresponding to those percentiles will be returned. For example "25, 50, 75" will result in the 25th, 50th and 75th percentiles being returned from a cube of probabilities, percentiles or realizations. Deterministic input data will raise a warning message. Note that for percentiles inputs, the desired percentile(s) must exist in the input cube. ignore_ecc_bounds (bool): Demotes exceptions where calculated percentiles are outside the ECC bounds range to warnings. new_title (str): New title for the spot-extracted data. If None, this attribute is removed from the output cube since it has no prescribed standard and may therefore contain grid information that is no longer correct after spot-extraction. suppress_warnings (bool): Suppress warning output. This option should only be used if it is known that warnings will be generated but they are not required. Returns: iris.cube.Cube: Cube of spot data. Raises: ValueError: If the percentile diagnostic cube does not contain the requested percentile value. ValueError: If the lapse rate cube was provided but the diagnostic being processed is not air temperature. ValueError: If the lapse rate cube provided does not have the name "air_temperature_lapse_rate" ValueError: If the lapse rate cube does not contain a single valued height coordinate. Warns: warning: If diagnostic cube is not a known probabilistic type. warning: If a lapse rate cube was provided, but the height of the temperature does not match that of the data used. warning: If a lapse rate cube was not provided, but the option to apply the lapse rate correction was enabled. """ import warnings import iris import numpy as np from iris.exceptions import CoordinateNotFoundError from improver.ensemble_copula_coupling.ensemble_copula_coupling import ( ConvertProbabilitiesToPercentiles, ) from improver.metadata.probabilistic import find_percentile_coordinate from improver.percentile import PercentileConverter from improver.spotdata.apply_lapse_rate import SpotLapseRateAdjust from improver.spotdata.neighbour_finding import NeighbourSelection from improver.spotdata.spot_extraction import SpotExtraction from improver.utilities.cube_extraction import extract_subcube neighbour_selection_method = NeighbourSelection( land_constraint=land_constraint, minimum_dz=similar_altitude).neighbour_finding_method_name() result = SpotExtraction( neighbour_selection_method=neighbour_selection_method)( neighbour_cube, cube, new_title=new_title) # If a probability or percentile diagnostic cube is provided, extract # the given percentile if available. This is done after the spot-extraction # to minimise processing time; usually there are far fewer spot sites than # grid points. if extract_percentiles: extract_percentiles = [np.float32(x) for x in extract_percentiles] try: perc_coordinate = find_percentile_coordinate(result) except CoordinateNotFoundError: if "probability_of_" in result.name(): result = ConvertProbabilitiesToPercentiles( ecc_bounds_warning=ignore_ecc_bounds)( result, percentiles=extract_percentiles) result = iris.util.squeeze(result) elif result.coords("realization", dim_coords=True): fast_percentile_method = not np.ma.isMaskedArray(result.data) result = PercentileConverter( "realization", percentiles=extract_percentiles, fast_percentile_method=fast_percentile_method, )(result) else: msg = ("Diagnostic cube is not a known probabilistic type. " "The {} percentile could not be extracted. Extracting " "data from the cube including any leading " "dimensions.".format(extract_percentiles)) if not suppress_warnings: warnings.warn(msg) else: constraint = [ "{}={}".format(perc_coordinate.name(), extract_percentiles) ] perc_result = extract_subcube(result, constraint) if perc_result is not None: result = perc_result else: msg = ("The percentile diagnostic cube does not contain the " "requested percentile value. Requested {}, available " "{}".format(extract_percentiles, perc_coordinate.points)) raise ValueError(msg) # Check whether a lapse rate cube has been provided and we are dealing with # temperature data and the lapse-rate option is enabled. if apply_lapse_rate_correction and lapse_rate: if not result.name() == "air_temperature": msg = ("A lapse rate cube was provided, but the diagnostic being " "processed is not air temperature and cannot be adjusted.") raise ValueError(msg) if not lapse_rate.name() == "air_temperature_lapse_rate": msg = ("A cube has been provided as a lapse rate cube but does " "not have the expected name air_temperature_lapse_rate: " "{}".format(lapse_rate.name())) raise ValueError(msg) try: lapse_rate_height_coord = lapse_rate.coord("height") except (ValueError, CoordinateNotFoundError): msg = ("Lapse rate cube does not contain a single valued height " "coordinate. This is required to ensure it is applied to " "equivalent temperature data.") raise ValueError(msg) # Check the height of the temperature data matches that used to # calculate the lapse rates. If so, adjust temperatures using the lapse # rate values. if cube.coord("height") == lapse_rate_height_coord: plugin = SpotLapseRateAdjust( neighbour_selection_method=neighbour_selection_method) result = plugin(result, neighbour_cube, lapse_rate) elif not suppress_warnings: warnings.warn( "A lapse rate cube was provided, but the height of the " "temperature data does not match that of the data used " "to calculate the lapse rates. As such the temperatures " "were not adjusted with the lapse rates.") elif apply_lapse_rate_correction and not lapse_rate: if not suppress_warnings: warnings.warn( "A lapse rate cube was not provided, but the option to " "apply the lapse rate correction was enabled. No lapse rate " "correction could be applied.") # Remove the internal model_grid_hash attribute if present. result.attributes.pop("model_grid_hash", None) return result
def process( wind_speed: cli.inputcube, sigma: cli.inputcube, target_orography: cli.inputcube, standard_orography: cli.inputcube, silhouette_roughness: cli.inputcube, vegetative_roughness: cli.inputcube = None, *, model_resolution: float, output_height_level: float = None, output_height_level_units="m", ): """Wind downscaling. Run wind downscaling to apply roughness correction and height correction to wind fields as described in Howard and Clark (2007). All inputs must be on the same standard grid. Args: wind_speed (iris.cube.Cube): Cube of wind speed on standard grid. Any units can be supplied. sigma (iris.cube.Cube): Cube of standard deviation of model orography height. Units of field: m. target_orography (iris.cube.Cube): Cube of orography to downscale fields to. Units of field: m. standard_orography (iris.cube.Cube): Cube of orography on standard grid. (interpolated model orography). Units of field: m. silhouette_roughness (iris.cube.Cube): Cube of model silhouette roughness. Units of field: dimensionless. vegetative_roughness (iris.cube.Cube): Cube of vegetative roughness length. Units of field: m. model_resolution (float): Original resolution of model orography (before interpolation to standard grid) Units of field: m. output_height_level (float): If only a single height level is desired as output from wind-downscaling, this option can be used to select the height level. If no units are provided with 'output_height_level_units', metres are assumed. output_height_level_units (str): If a single height level is selected as output using 'output_height_level', this additional argument may be used to specify the units of the value entered to select the level. e.g hPa. Returns: iris.cube.Cube: The processed Cube. Rises: ValueError: If the requested height value is not found. """ import warnings import iris from iris.exceptions import CoordinateNotFoundError from improver.utilities.cube_extraction import apply_extraction from improver.wind_calculations import wind_downscaling if output_height_level_units and output_height_level is None: warnings.warn( "output_height_level_units has been set but no " "associated height level has been provided. These units " "will have no effect." ) try: wind_speed_iterator = wind_speed.slices_over("realization") except CoordinateNotFoundError: wind_speed_iterator = [wind_speed] wind_speed_list = iris.cube.CubeList() for wind_speed_slice in wind_speed_iterator: result = wind_downscaling.RoughnessCorrection( silhouette_roughness, sigma, target_orography, standard_orography, model_resolution, z0_cube=vegetative_roughness, height_levels_cube=None, )(wind_speed_slice) wind_speed_list.append(result) wind_speed = wind_speed_list.merge_cube() non_dim_coords = [x.name() for x in wind_speed.coords(dim_coords=False)] if "realization" in non_dim_coords: wind_speed = iris.util.new_axis(wind_speed, "realization") if output_height_level is not None: constraints = {"height": output_height_level} units = {"height": output_height_level_units} single_level = apply_extraction( wind_speed, iris.Constraint(**constraints), units ) if not single_level: raise ValueError( "Requested height level not found, no cube " "returned. Available height levels are:\n" "{0:}\nin units of {1:}".format( wind_speed.coord("height").points, wind_speed.coord("height").units ) ) wind_speed = single_level return wind_speed
def process( cube: cli.inputcube, *, coordinates: cli.comma_separated_list = None, percentiles: cli.comma_separated_list = None, ignore_ecc_bounds=False, ): r"""Collapses cube coordinates and calculate percentiled data. Calculate percentiled data over a given coordinate by collapsing that coordinate. Typically used to convert realization data into percentiled data, but may calculate over any dimension coordinate. Alternatively calling this with a dataset containing probabilities will convert those to percentiles using the ensemble coupla coupling plugin. If no particular percentiles are given at which to calculate values and no 'number of percentiles' to calculate are specified, the following defaults will be used. '[0, 5, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90, 95, 100]' Args: cube (iris.cube.Cube): A Cube for processing. coordinates (str or list): Coordinate or coordinates over which to collapse data and calculate percentiles; e.g. 'realization' or 'latitude,longitude'. This argument must be provided when collapsing a coordinate or coordinates to create percentiles, but is redundant when converting probabilities to percentiles and may be omitted. This coordinate(s) will be removed and replaced by a percentile coordinate. percentiles (list): Optional definition of percentiles at which to calculate data. ignore_ecc_bounds (bool): If True, where calculated percentiles are outside the ECC bounds range, raises a warning rather than an exception. Returns: iris.cube.Cube: The processed Cube. Raises: ValueError: If the cube name does not contain 'probability_of\_' and coordinates isn't used. Warns: Warning: If 'probability_of\_' is in the cube name and coordinates is used. """ import warnings import numpy as np from improver.ensemble_copula_coupling.ensemble_copula_coupling import ( ConvertProbabilitiesToPercentiles, ) from improver.metadata.probabilistic import is_probability from improver.percentile import PercentileConverter if percentiles is not None: percentiles = [float(p) for p in percentiles] if is_probability(cube): result = ConvertProbabilitiesToPercentiles( ecc_bounds_warning=ignore_ecc_bounds)(cube, percentiles=percentiles) if coordinates: warnings.warn("Converting probabilities to percentiles. The " "provided COORDINATES_TO_COLLAPSE variable will " "not be used.") else: if not coordinates: raise ValueError("To collapse a coordinate to calculate " "percentiles, a coordinate or list of " "coordinates must be provided.") # Switch back to use the slow scipy method if the cube contains masked # data which the numpy method cannot handle. fast_percentile_method = True if np.ma.is_masked(cube.data): # Check for masked points: fast_percentile_method = False elif np.ma.isMaskedArray(cube.data): # Check if we have a masked array with an empty mask. If so, # replace it with a non-masked array: cube.data = cube.data.data result = PercentileConverter( coordinates, percentiles=percentiles, fast_percentile_method=fast_percentile_method, )(cube) return result
def process(cube: cli.inputcube, coefficients: cli.inputcube = None, land_sea_mask: cli.inputcube = None, *, distribution, realizations_count: int = None, randomise=False, random_seed: int = None, ignore_ecc_bounds=False, predictor='mean', shape_parameters: cli.comma_separated_list = None): """Applying coefficients for Ensemble Model Output Statistics. Load in arguments for applying coefficients for Ensemble Model Output Statistics (EMOS), otherwise known as Non-homogeneous Gaussian Regression (NGR). The coefficients are applied to the forecast that is supplied, so as to calibrate the forecast. The calibrated forecast is written to a cube. If no coefficients are provided the input forecast is returned unchanged. Args: cube (iris.cube.Cube): A Cube containing the forecast to be calibrated. The input format could be either realizations, probabilities or percentiles. coefficients (iris.cube.Cube): A cube containing the coefficients used for calibration or None. If none then then input is returned unchanged. land_sea_mask (iris.cube.Cube): A cube containing the land-sea mask on the same domain as the forecast that is to be calibrated. Land points are " "specified by ones and sea points are specified by zeros. " "If not None this argument will enable land-only calibration, in " "which sea points are returned without the application of " "calibration." distribution (str): The distribution for constructing realizations, percentiles or probabilities. This should typically match the distribution used for minimising the Continuous Ranked Probability Score when estimating the EMOS coefficients. The distributions available are those supported by :data:`scipy.stats`. realizations_count (int): Option to specify the number of ensemble realizations that will be created from probabilities or percentiles for input into EMOS. randomise (bool): Option to reorder the post-processed forecasts randomly. If not set, the ordering of the raw ensemble is used. This option is only valid when the input format is realizations. random_seed (int): Option to specify a value for the random seed for testing purposes, otherwise the default random seen behaviour is utilised. The random seed is used in the generation of the random numbers used for either the randomise option to order the input percentiles randomly, rather than use the ordering from the raw ensemble, or for splitting tied values within the raw ensemble, so that the values from the input percentiles can be ordered to match the raw ensemble. ignore_ecc_bounds (bool): If True, where the percentiles exceed the ECC bounds range, raises a warning rather than an exception. This occurs when the current forecasts is in the form of probabilities and is converted to percentiles, as part of converting the input probabilities into realizations. predictor (str): String to specify the form of the predictor used to calculate the location parameter when estimating the EMOS coefficients. Currently the ensemble mean ("mean") and the ensemble realizations ("realizations") are supported as the predictors. shape_parameters (float or str): The shape parameters required for defining the distribution specified by the distribution argument. The shape parameters should either be a number or 'inf' or '-inf' to represent infinity. Further details about appropriate shape parameters are available in scipy.stats. For the truncated normal distribution with a lower bound of zero, as available when estimating EMOS coefficients, the appropriate shape parameters are 0 and inf. Returns: iris.cube.Cube: The calibrated forecast cube. Raises: ValueError: If the current forecast is a coefficients cube. ValueError: If the coefficients cube does not have the right name of "emos_coefficients". ValueError: If the forecast type is 'percentiles' or 'probabilities' and the realizations_count argument is not provided. """ import warnings import numpy as np from iris.exceptions import CoordinateNotFoundError from improver.calibration.ensemble_calibration import ( ApplyCoefficientsFromEnsembleCalibration) from improver.ensemble_copula_coupling.ensemble_copula_coupling import ( EnsembleReordering, ConvertLocationAndScaleParametersToPercentiles, ConvertLocationAndScaleParametersToProbabilities, ConvertProbabilitiesToPercentiles, RebadgePercentilesAsRealizations, ResamplePercentiles) from improver.calibration.utilities import merge_land_and_sea from improver.metadata.probabilistic import find_percentile_coordinate current_forecast = cube if current_forecast.name() in ['emos_coefficients', 'land_binary_mask']: msg = "The current forecast cube has the name {}" raise ValueError(msg.format(current_forecast.name())) if coefficients is None: msg = ("There are no coefficients provided for calibration. The " "uncalibrated forecast will be returned.") warnings.warn(msg) return current_forecast if coefficients.name() != 'emos_coefficients': msg = ("The current coefficients cube does not have the " "name 'emos_coefficients'") raise ValueError(msg) if land_sea_mask and land_sea_mask.name() != 'land_binary_mask': msg = ("The land_sea_mask cube does not have the " "name 'land_binary_mask'") raise ValueError(msg) original_current_forecast = current_forecast.copy() try: find_percentile_coordinate(current_forecast) input_forecast_type = "percentiles" except CoordinateNotFoundError: input_forecast_type = "realizations" if current_forecast.name().startswith("probability_of"): input_forecast_type = "probabilities" conversion_plugin = ConvertProbabilitiesToPercentiles( ecc_bounds_warning=ignore_ecc_bounds) elif input_forecast_type == "percentiles": # Initialise plugin to resample percentiles so that the percentiles are # evenly spaced. conversion_plugin = ResamplePercentiles( ecc_bounds_warning=ignore_ecc_bounds) if input_forecast_type in ["percentiles", "probabilities"]: if not realizations_count: raise ValueError( "The current forecast has been provided as {0}. " "These {0} need to be converted to realizations " "for ensemble calibration. The realizations_count " "argument is used to define the number of realizations " "to construct from the input {0}, so if the " "current forecast is provided as {0} then " "realizations_count must be defined.".format( input_forecast_type)) current_forecast = conversion_plugin.process( current_forecast, no_of_percentiles=realizations_count) current_forecast = ( RebadgePercentilesAsRealizations().process(current_forecast)) # Apply coefficients as part of Ensemble Model Output Statistics (EMOS). ac = ApplyCoefficientsFromEnsembleCalibration(predictor=predictor) location_parameter, scale_parameter = ac.process( current_forecast, coefficients, landsea_mask=land_sea_mask) if shape_parameters: shape_parameters = [np.float32(x) for x in shape_parameters] # Convert the output forecast type (i.e. realizations, percentiles, # probabilities) to match the input forecast type. if input_forecast_type == "probabilities": result = ConvertLocationAndScaleParametersToProbabilities( distribution=distribution, shape_parameters=shape_parameters).process( location_parameter, scale_parameter, original_current_forecast) elif input_forecast_type == "percentiles": perc_coord = find_percentile_coordinate(original_current_forecast) result = ConvertLocationAndScaleParametersToPercentiles( distribution=distribution, shape_parameters=shape_parameters).process( location_parameter, scale_parameter, original_current_forecast, percentiles=perc_coord.points) elif input_forecast_type == "realizations": # Ensemble Copula Coupling to generate realizations # from the location and scale parameter. no_of_percentiles = len(current_forecast.coord('realization').points) percentiles = ConvertLocationAndScaleParametersToPercentiles( distribution=distribution, shape_parameters=shape_parameters).process( location_parameter, scale_parameter, original_current_forecast, no_of_percentiles=no_of_percentiles) result = EnsembleReordering().process(percentiles, current_forecast, random_ordering=randomise, random_seed=random_seed) if land_sea_mask: # Fill in masked sea points with uncalibrated data. merge_land_and_sea(result, original_current_forecast) return result
def process( cube: cli.inputcube, mask: cli.inputcube = None, *, neighbourhood_output, neighbourhood_shape="square", radii: cli.comma_separated_list, lead_times: cli.comma_separated_list = None, degrees_as_complex=False, weighted_mode=False, area_sum=False, percentiles: cli.comma_separated_list = DEFAULT_PERCENTILES, halo_radius: float = None, ): """Runs neighbourhood processing. Apply the requested neighbourhood method via the NeighbourhoodProcessing plugin to a Cube. Args: cube (iris.cube.Cube): The Cube to be processed. mask (iris.cube.Cube): A cube to mask the input cube. The data should contain 1 for usable points and 0 for discarded points. Can't be used with "percentiles" as neighbourhood_output (Optional) neighbourhood_output (str): The form of the results generated using neighbourhood processing. If "probabilities" is selected, the mean probability with a neighbourhood is calculated. If "percentiles" is selected, then the percentiles are calculated with a neighbourhood. Calculating percentiles from a neighbourhood is only supported for a circular neighbourhood. Options: "probabilities", "percentiles". neighbourhood_shape (str): Name of the neighbourhood method to use. Only a "circular" neighbourhood shape is applicable for calculating "percentiles" output. Options: "circular", "square". Default: "square". radii (list of float): The radius or a list of radii in metres of the neighbourhood to apply. If it is a list, it must be the same length as lead_times, which defines at which lead time to use which nbhood radius. The radius will be interpolated for intermediate lead times. lead_times (list of int): The lead times in hours that correspond to the radii to be used. If lead_times are set, radii must be a list the same length as lead_times. degrees_as_complex (bool): Include this option to process angles as complex numbers. Not compatible with circular kernel or percentiles. weighted_mode (bool): Include this option to set the weighting to decrease with radius. Otherwise a constant weighting is assumed. weighted_mode is only applicable for calculating "probability" neighbourhood output using the circular kernel. area_sum (bool): Return sum rather than fraction over the neighbourhood area. percentiles (float): Calculates value at the specified percentiles from the neighbourhood surrounding each grid point. This argument has no effect if the output is probabilities. halo_radius (float): Set this radius in metres to define the excess halo to clip. Used where a larger grid was defined than the standard grid and we want to clip the grid back to the standard grid. Otherwise no clipping is applied. Returns: iris.cube.Cube: A processed Cube. Raises: RuntimeError: If weighted_mode is used with the wrong neighbourhood_output. RuntimeError: If degree_as_complex is used with neighbourhood_output='percentiles'. RuntimeError: If degree_as_complex is used with neighbourhood_shape='circular'. """ from improver.nbhood import radius_by_lead_time from improver.nbhood.nbhood import ( GeneratePercentilesFromANeighbourhood, NeighbourhoodProcessing, ) from improver.utilities.pad_spatial import remove_cube_halo from improver.wind_calculations.wind_direction import WindDirection if neighbourhood_output == "percentiles": if weighted_mode: raise RuntimeError("weighted_mode cannot be used with" 'neighbourhood_output="percentiles"') if degrees_as_complex: raise RuntimeError("Cannot generate percentiles from complex " "numbers") if neighbourhood_shape == "circular": if degrees_as_complex: raise RuntimeError( "Cannot process complex numbers with circular neighbourhoods") if degrees_as_complex: # convert cube data into complex numbers cube.data = WindDirection.deg_to_complex(cube.data) radius_or_radii, lead_times = radius_by_lead_time(radii, lead_times) if neighbourhood_output == "probabilities": result = NeighbourhoodProcessing( neighbourhood_shape, radius_or_radii, lead_times=lead_times, weighted_mode=weighted_mode, sum_only=area_sum, re_mask=True, )(cube, mask_cube=mask) elif neighbourhood_output == "percentiles": result = GeneratePercentilesFromANeighbourhood( radius_or_radii, lead_times=lead_times, percentiles=percentiles, )(cube) if degrees_as_complex: # convert neighbourhooded cube back to degrees result.data = WindDirection.complex_to_deg(result.data) if halo_radius is not None: result = remove_cube_halo(result, halo_radius) return result
def process( cube: cli.inputcube, coefficients: inputcoeffs = None, land_sea_mask: cli.inputcube = None, *, realizations_count: int = None, randomise=False, random_seed: int = None, ignore_ecc_bounds=False, predictor="mean", ): """Applying coefficients for Ensemble Model Output Statistics. Load in arguments for applying coefficients for Ensemble Model Output Statistics (EMOS), otherwise known as Non-homogeneous Gaussian Regression (NGR). The coefficients are applied to the forecast that is supplied, so as to calibrate the forecast. The calibrated forecast is written to a cube. If no coefficients are provided the input forecast is returned unchanged. Args: cube (iris.cube.Cube): A Cube containing the forecast to be calibrated. The input format could be either realizations, probabilities or percentiles. coefficients (iris.cube.CubeList): A cubelist containing the coefficients used for calibration or None. If none then then input is returned unchanged. land_sea_mask (iris.cube.Cube): A cube containing the land-sea mask on the same domain as the forecast that is to be calibrated. Land points are " "specified by ones and sea points are specified by zeros. " "If not None this argument will enable land-only calibration, in " "which sea points are returned without the application of " "calibration." realizations_count (int): Option to specify the number of ensemble realizations that will be created from probabilities or percentiles when applying the EMOS coefficients. randomise (bool): Option to reorder the post-processed forecasts randomly. If not set, the ordering of the raw ensemble is used. This option is only valid when the input format is realizations. random_seed (int): Option to specify a value for the random seed for testing purposes, otherwise the default random seen behaviour is utilised. The random seed is used in the generation of the random numbers used for either the randomise option to order the input percentiles randomly, rather than use the ordering from the raw ensemble, or for splitting tied values within the raw ensemble, so that the values from the input percentiles can be ordered to match the raw ensemble. ignore_ecc_bounds (bool): If True, where the percentiles exceed the ECC bounds range, raises a warning rather than an exception. This occurs when the current forecasts is in the form of probabilities and is converted to percentiles, as part of converting the input probabilities into realizations. predictor (str): String to specify the form of the predictor used to calculate the location parameter when estimating the EMOS coefficients. Currently the ensemble mean ("mean") and the ensemble realizations ("realizations") are supported as the predictors. Returns: iris.cube.Cube: The calibrated forecast cube. Raises: ValueError: If the current forecast is a coefficients cube. ValueError: If the coefficients cube does not have the right name of "emos_coefficients". ValueError: If the forecast type is 'percentiles' or 'probabilities' and the realizations_count argument is not provided. """ import warnings from improver.calibration.ensemble_calibration import ApplyEMOS if coefficients is None: msg = ("There are no coefficients provided for calibration. The " "uncalibrated forecast will be returned.") warnings.warn(msg) return cube if land_sea_mask and land_sea_mask.name() != "land_binary_mask": msg = "The land_sea_mask cube does not have the name 'land_binary_mask'" raise ValueError(msg) calibration_plugin = ApplyEMOS() result = calibration_plugin( cube, coefficients, land_sea_mask=land_sea_mask, realizations_count=realizations_count, ignore_ecc_bounds=ignore_ecc_bounds, predictor=predictor, randomise=randomise, random_seed=random_seed, ) return result
def process(cube: cli.inputcube, raw_cube: cli.inputcube = None, *, realizations_count: int = None, random_seed: int = None, ignore_ecc_bounds=False): """Convert probabilities to ensemble realizations using Ensemble Copula Coupling. Probabilities are first converted to percentiles, which are then either rebadged as realizations or reordered if the raw_cube argument is given. Args: cube (iris.cube.Cube): Cube to be processed. raw_cube (iris.cube.Cube): Cube of raw (not post processed) weather data. If this argument is given ensemble realizations will be created from percentiles by reshuffling them in correspondence to the rank order of the raw ensemble. Otherwise, the percentiles are rebadged as realizations. realizations_count (int): Optional definition of the number of ensemble realizations to be generated. These are generated though an intermediate percentile representation. Theses percentiles will be distributed regularly with the aim of dividing into blocks of equal probability. If the raw_cube is given and the number of realization is not given the number of realizations is taken from the number of realizations in the raw_cube. random_seed (int): Option to specify a value for the random seed for testing purposes, otherwise the default random seed behaviours is utilised. The random seed is used in the generation of the random numbers used for splitting tied values within the raw ensemble, so that the values from the input percentiles can be ordered to match the raw ensemble. ignore_ecc_bounds (bool): If True, where percentiles (calculated as an intermediate output before realization) exceed to ECC bounds range, raises a warning rather than an exception. Returns: iris.cube.Cube: Processed result Cube. """ from improver.ensemble_copula_coupling.ensemble_copula_coupling import ( ConvertProbabilitiesToPercentiles, RebadgePercentilesAsRealizations, EnsembleReordering) if realizations_count is None and raw_cube: # If realizations_count is not given, take the number from the raw # ensemble cube. realizations_count = len(raw_cube.coord("realization").points) result = ConvertProbabilitiesToPercentiles( ecc_bounds_warning=ignore_ecc_bounds)( cube, no_of_percentiles=realizations_count) if raw_cube: result = EnsembleReordering()(result, raw_cube, random_ordering=False, random_seed=random_seed) else: result = RebadgePercentilesAsRealizations()(result) return result
def process( start_cube: cli.inputcube, end_cube: cli.inputcube, *, interval_in_mins: int = None, times: cli.comma_separated_list = None, interpolation_method="linear", ): """Interpolate data between validity times. Interpolate data to intermediate times between the validity times of two cubes. This can be used to fill in missing data (e.g. for radar fields) or to ensure data is available at the required intervals when model data is not available at these times. Args: start_cube (iris.cube.Cube): Cube containing the data at the beginning. end_cube (iris.cube.Cube): Cube containing the data at the end. interval_in_mins (int): Specifies the interval in minutes at which to interpolate between the two input cubes. A number of minutes which does not divide up the interval equally will raise an exception. If intervals_in_mins is set then times can not be used. times (str): Specifies the times in the format {YYYYMMDD}T{HHMM}Z at which to interpolate between the two input cubes. Where {YYYYMMDD} is year, month, day and {HHMM} is hour and minutes e.g 20180116T0100Z. More than one time can be provided separated by a comma. If times are set, interval_in_mins can not be used. interpolation_method (str): ["linear", "solar", "daynight"] Specifies the interpolation method; solar interpolates using the solar elevation, daynight uses linear interpolation but sets night time points to 0.0 linear is linear interpolation. Returns: iris.cube.CubeList: A list of cubes interpolated to the desired times. The interpolated cubes will always be in chronological order of earliest to latest regardless of the order of the input. """ from improver.utilities.cube_manipulation import MergeCubes from improver.utilities.temporal import cycletime_to_datetime, iris_time_to_datetime from improver.utilities.temporal_interpolation import TemporalInterpolation (time_start,) = iris_time_to_datetime(start_cube.coord("time")) (time_end,) = iris_time_to_datetime(end_cube.coord("time")) if time_end < time_start: # swap cubes start_cube, end_cube = end_cube, start_cube if times is not None: times = [cycletime_to_datetime(timestr) for timestr in times] result = TemporalInterpolation( interval_in_minutes=interval_in_mins, times=times, interpolation_method=interpolation_method, )(start_cube, end_cube) return MergeCubes()(result)
def process( cube: cli.inputcube, coefficients: cli.inputcube = None, land_sea_mask: cli.inputcube = None, *, distribution, realizations_count: int = None, randomise=False, random_seed: int = None, ignore_ecc_bounds=False, predictor="mean", shape_parameters: cli.comma_separated_list = None, ): """Applying coefficients for Ensemble Model Output Statistics. Load in arguments for applying coefficients for Ensemble Model Output Statistics (EMOS), otherwise known as Non-homogeneous Gaussian Regression (NGR). The coefficients are applied to the forecast that is supplied, so as to calibrate the forecast. The calibrated forecast is written to a cube. If no coefficients are provided the input forecast is returned unchanged. Args: cube (iris.cube.Cube): A Cube containing the forecast to be calibrated. The input format could be either realizations, probabilities or percentiles. coefficients (iris.cube.Cube): A cube containing the coefficients used for calibration or None. If none then then input is returned unchanged. land_sea_mask (iris.cube.Cube): A cube containing the land-sea mask on the same domain as the forecast that is to be calibrated. Land points are " "specified by ones and sea points are specified by zeros. " "If not None this argument will enable land-only calibration, in " "which sea points are returned without the application of " "calibration." distribution (str): The distribution for constructing realizations, percentiles or probabilities. This should typically match the distribution used for minimising the Continuous Ranked Probability Score when estimating the EMOS coefficients. The distributions available are those supported by :data:`scipy.stats`. realizations_count (int): Option to specify the number of ensemble realizations that will be created from probabilities or percentiles for input into EMOS. randomise (bool): Option to reorder the post-processed forecasts randomly. If not set, the ordering of the raw ensemble is used. This option is only valid when the input format is realizations. random_seed (int): Option to specify a value for the random seed for testing purposes, otherwise the default random seen behaviour is utilised. The random seed is used in the generation of the random numbers used for either the randomise option to order the input percentiles randomly, rather than use the ordering from the raw ensemble, or for splitting tied values within the raw ensemble, so that the values from the input percentiles can be ordered to match the raw ensemble. ignore_ecc_bounds (bool): If True, where the percentiles exceed the ECC bounds range, raises a warning rather than an exception. This occurs when the current forecasts is in the form of probabilities and is converted to percentiles, as part of converting the input probabilities into realizations. predictor (str): String to specify the form of the predictor used to calculate the location parameter when estimating the EMOS coefficients. Currently the ensemble mean ("mean") and the ensemble realizations ("realizations") are supported as the predictors. shape_parameters (float or str): The shape parameters required for defining the distribution specified by the distribution argument. The shape parameters should either be a number or 'inf' or '-inf' to represent infinity. Further details about appropriate shape parameters are available in scipy.stats. For the truncated normal distribution with a lower bound of zero, as available when estimating EMOS coefficients, the appropriate shape parameters are 0 and inf. Returns: iris.cube.Cube: The calibrated forecast cube. Raises: ValueError: If the current forecast is a coefficients cube. ValueError: If the coefficients cube does not have the right name of "emos_coefficients". ValueError: If the forecast type is 'percentiles' or 'probabilities' and the realizations_count argument is not provided. """ import warnings import numpy as np from improver.calibration.ensemble_calibration import ApplyEMOS if cube.name() in ["emos_coefficients", "land_binary_mask"]: msg = "Invalid forecast cube provided (name '{}')" raise ValueError(msg.format(cube.name())) if coefficients is None: msg = ("There are no coefficients provided for calibration. The " "uncalibrated forecast will be returned.") warnings.warn(msg) return cube if coefficients.name() != "emos_coefficients": msg = "Invalid coefficients cube provided (name '{}')" raise ValueError(msg.format(coefficients.name())) if land_sea_mask and land_sea_mask.name() != "land_binary_mask": msg = "The land_sea_mask cube does not have the " "name 'land_binary_mask'" raise ValueError(msg) if shape_parameters: shape_parameters = [np.float32(x) for x in shape_parameters] calibration_plugin = ApplyEMOS() result = calibration_plugin( cube, coefficients, land_sea_mask=land_sea_mask, realizations_count=realizations_count, ignore_ecc_bounds=ignore_ecc_bounds, predictor=predictor, distribution=distribution, shape_parameters=shape_parameters, randomise=randomise, random_seed=random_seed, ) return result
def process(cube: cli.inputcube, mask: cli.inputcube, weights: cli.inputcube = None, *, radii: cli.comma_separated_list, lead_times: cli.comma_separated_list = None, area_sum=False, return_intermediate=False): """ Module to process land and sea separately before combining them. Neighbourhood the input dataset over two distinct regions of land and sea. If performed as a single level neighbourhood, a land-sea mask should be provided. If instead topographic_zone neighbourhooding is being employed, the mask should be one of topographic zones. In the latter case a weights array is also needed to collapse the topographic_zone coordinate. These weights are created with the improver generate-topography-bands-weights CLI and should be made using a land-sea mask, which will then be employed within this code to draw the distinction between the two surface types. Args: cube (iris.cube.Cube): A cube to be processed. mask (iris.cube.Cube): A cube containing either a mask of topographic zones over land or a land-sea mask. weights (iris.cube.Cube): A cube containing the weights which are used for collapsing the dimension gained through masking. These weights must have been created using a land-sea mask. (Optional). radii (list of float): The radius or a list of radii in metres of the neighbourhood to apply. If it is a list, it must be the same length as lead_times, which defines at which lead time to use which nbhood radius. The radius will be interpolated for intermediate lead times. lead_times (list of int): The lead times in hours that correspond to the radii to be used. If lead_times are set, radii must be a list the same length as lead_times. Lead times must be given as integer values. area_sum (bool): Return sum rather than fraction over the neighbourhood area. return_intermediate (bool): Include this option to return a cube with results following topographic masked neighbourhood processing of land points and prior to collapsing the topographic_zone coordinate. If no topographic masked neighbourhooding occurs, there will be no intermediate cube and a warning. Returns: (tuple): tuple containing: **result** (iris.cube.Cube): A cube of the processed data. **intermediate_cube** (iris.cube.Cube): A cube of the intermediate data, before collapsing. Raises: ValueError: If the topographic zone mask has the attribute topographic_zones_include_seapoints. IOError: if a weights cube isn't given and a topographic_zone mask is given. ValueError: If the weights cube has the attribute topographic_zones_include_seapoints. RuntimeError: If lead times are not None and has a different length to radii. TypeError: A weights cube has been provided but no topographic zone. """ import warnings import numpy as np from improver.nbhood.nbhood import NeighbourhoodProcessing from improver.nbhood.use_nbhood import ( ApplyNeighbourhoodProcessingWithAMask, CollapseMaskedNeighbourhoodCoordinate) sum_or_fraction = 'sum' if area_sum else 'fraction' masking_coordinate = intermediate_cube = None if any([ 'topographic_zone' in coord.name() for coord in mask.coords(dim_coords=True) ]): if mask.attributes['topographic_zones_include_seapoints'] == 'True': raise ValueError('The topographic zones mask cube must have been ' 'masked to exclude sea points, but ' 'topographic_zones_include_seapoints = True') if not weights: raise TypeError('A weights cube must be provided if using a mask ' 'of topographic zones to collapse the resulting ' 'vertical dimension.') if weights.attributes['topographic_zones_include_seapoints'] == 'True': raise ValueError('The weights cube must be masked to exclude sea ' 'points, but topographic_zones_include_seapoints ' '= True') masking_coordinate = 'topographic_zone' land_sea_mask = weights[0].copy(data=weights[0].data.mask) land_sea_mask.rename('land_binary_mask') land_sea_mask.remove_coord(masking_coordinate) # Create land and sea masks in IMPROVER format (inverse of # numpy standard) 1 - include this region, 0 - exclude this region. land_only = land_sea_mask.copy( data=np.logical_not(land_sea_mask.data).astype(int)) sea_only = land_sea_mask.copy(data=land_sea_mask.data.astype(int)) else: if weights is not None: raise TypeError('A weights cube has been provided but will not be ' 'used') land_sea_mask = mask # In this case the land is set to 1 and the sea is set to 0 in the # input mask. sea_only = land_sea_mask.copy( data=np.logical_not(land_sea_mask.data).astype(int)) land_only = land_sea_mask.copy(data=land_sea_mask.data.astype(int)) if lead_times is None: radius_or_radii = float(radii[0]) else: if len(radii) != len(lead_times): raise RuntimeError("If leadtimes are supplied, it must be a list" " of equal length to a list of radii.") radius_or_radii = [float(x) for x in radii] lead_times = [int(x) for x in lead_times] if return_intermediate is not None and masking_coordinate is None: warnings.warn('No topographic_zone coordinate found, so no ' 'intermediate file will be saved.') # Section for neighbourhood processing land points. if land_only.data.max() > 0.0: if masking_coordinate is None: result_land = NeighbourhoodProcessing( 'square', radius_or_radii, lead_times=lead_times, sum_or_fraction=sum_or_fraction, re_mask=True).process(cube, land_only) else: result_land = ApplyNeighbourhoodProcessingWithAMask( masking_coordinate, radius_or_radii, lead_times=lead_times, sum_or_fraction=sum_or_fraction, re_mask=False).process(cube, mask) if return_intermediate: intermediate_cube = result_land.copy() # Collapse the masking coordinate. result_land = CollapseMaskedNeighbourhoodCoordinate( masking_coordinate, weights=weights).process(result_land) result = result_land # Section for neighbourhood processing sea points. if sea_only.data.max() > 0.0: result_sea = NeighbourhoodProcessing('square', radius_or_radii, lead_times=lead_times, sum_or_fraction=sum_or_fraction, re_mask=True).process( cube, sea_only) result = result_sea # Section for combining land and sea points following land and sea points # being neighbourhood processed individually. if sea_only.data.max() > 0.0 and land_only.data.max() > 0.0: # Recombine cubes to be a single output. combined_data = result_land.data.filled(0) + result_sea.data.filled(0) result = result_land.copy(data=combined_data) return result, intermediate_cube
def process(temperature: cli.inputcube, orography: cli.inputcube = None, land_sea_mask: cli.inputcube = None, *, max_height_diff: float = 35, nbhood_radius: int = 7, max_lapse_rate: float = -3 * DALR, min_lapse_rate: float = DALR, dry_adiabatic=False): """Calculate temperature lapse rates in units of K m-1 over orography grid. Args: temperature (iris.cube.Cube): Air temperature data. This is required even when returning DALR, as this defines the grid on which lapse rates are required. orography (iris.cube.Cube): Orography data. land_sea_mask (iris.cube.Cube): Binary land-sea mask data. True for land-points, False for sea. max_height_diff (float): Maximum allowable height difference between the central point and points in the neighbourhood over which the lapse rate will be calculated. nbhood_radius (int): Radius of neighbourhood in grid points around each point. The neighbourhood is a square array with side length 2*nbhood_radius + 1. The default value of 7 is from the reference paper (see plugin documentation). max_lapse_rate (float): Maximum lapse rate allowed, in K m-1. min_lapse_rate (float): Minimum lapse rate allowed, in K m-1. dry_adiabatic (bool): If True, returns a cube containing the dry adiabatic lapse rate rather than calculating the true lapse rate. Returns: iris.cube.Cube: Lapse rate (K m-1) Raises: ValueError: If minimum lapse rate is greater than maximum. ValueError: If Maximum height difference is less than zero. ValueError: If neighbourhood radius is less than zero. RuntimeError: If calculating the true lapse rate and orography or land mask arguments are not given. """ import numpy as np from improver.lapse_rate import LapseRate from improver.metadata.amend import amend_attributes attributes_dict = { "title": "remove", "source": "remove", "history": "remove", "um_version": "remove" } if dry_adiabatic: result = temperature.copy(data=np.full_like(temperature.data, DALR)) result.rename('air_temperature_lapse_rate') result.units = U_DALR amend_attributes(result, attributes_dict) return result if min_lapse_rate > max_lapse_rate: msg = 'Minimum lapse rate specified is greater than the maximum.' raise ValueError(msg) if max_height_diff < 0: msg = 'Maximum height difference specified is less than zero.' raise ValueError(msg) if nbhood_radius < 0: msg = 'Neighbourhood radius specified is less than zero.' raise ValueError(msg) if orography is None or land_sea_mask is None: msg = 'Missing orography and/or land mask arguments.' raise RuntimeError(msg) result = LapseRate(max_height_diff=max_height_diff, nbhood_radius=nbhood_radius, max_lapse_rate=max_lapse_rate, min_lapse_rate=min_lapse_rate).process( temperature, orography, land_sea_mask) amend_attributes(result, attributes_dict) return result
def process( cube: cli.inputcube, mask: cli.inputcube, weights: cli.inputcube = None, *, neighbourhood_shape="square", radii: cli.comma_separated_list, lead_times: cli.comma_separated_list = None, area_sum=False, ): """ Module to process land and sea separately before combining them. Neighbourhood the input dataset over two distinct regions of land and sea. If performed as a single level neighbourhood, a land-sea mask should be provided. If instead topographic_zone neighbourhooding is being employed, the mask should be one of topographic zones. In the latter case a weights array is also needed to collapse the topographic_zone coordinate. These weights are created with the improver generate-topography-bands-weights CLI and should be made using a land-sea mask, which will then be employed within this code to draw the distinction between the two surface types. Args: cube (iris.cube.Cube): A cube to be processed. mask (iris.cube.Cube): A cube containing either a mask of topographic zones over land or a land-sea mask. If this is a land-sea mask, land points should be set to one and sea points set to zero. weights (iris.cube.Cube): A cube containing the weights which are used for collapsing the dimension gained through masking. These weights must have been created using a land-sea mask. (Optional). neighbourhood_shape (str): Name of the neighbourhood method to use. Options: "circular", "square". Default: "square". radii (list of float): The radius or a list of radii in metres of the neighbourhood to apply. If it is a list, it must be the same length as lead_times, which defines at which lead time to use which nbhood radius. The radius will be interpolated for intermediate lead times. lead_times (list of int): The lead times in hours that correspond to the radii to be used. If lead_times are set, radii must be a list the same length as lead_times. Lead times must be given as integer values. area_sum (bool): Return sum rather than fraction over the neighbourhood area. Returns: (tuple): tuple containing: **result** (iris.cube.Cube): A cube of the processed data. Raises: ValueError: If the topographic zone mask has the attribute topographic_zones_include_seapoints. IOError: if a weights cube isn't given and a topographic_zone mask is given. ValueError: If the weights cube has the attribute topographic_zones_include_seapoints. RuntimeError: If lead times are not None and has a different length to radii. TypeError: A weights cube has been provided but no topographic zone. """ import numpy as np from improver.nbhood.nbhood import NeighbourhoodProcessing from improver.nbhood.use_nbhood import ApplyNeighbourhoodProcessingWithAMask masking_coordinate = None if any( "topographic_zone" in coord.name() for coord in mask.coords(dim_coords=True) ): if mask.attributes["topographic_zones_include_seapoints"] == "True": raise ValueError( "The topographic zones mask cube must have been " "masked to exclude sea points, but " "topographic_zones_include_seapoints = True" ) if not weights: raise TypeError( "A weights cube must be provided if using a mask " "of topographic zones to collapse the resulting " "vertical dimension." ) if weights.attributes["topographic_zones_include_seapoints"] == "True": raise ValueError( "The weights cube must be masked to exclude sea " "points, but topographic_zones_include_seapoints " "= True" ) masking_coordinate = "topographic_zone" land_sea_mask = weights[0].copy(data=weights[0].data.mask) land_sea_mask.rename("land_binary_mask") land_sea_mask.remove_coord(masking_coordinate) # Create land and sea masks in IMPROVER format (inverse of # numpy standard) 1 - include this region, 0 - exclude this region. land_only = land_sea_mask.copy( data=np.logical_not(land_sea_mask.data).astype(int) ) sea_only = land_sea_mask.copy(data=land_sea_mask.data.astype(int)) else: if weights is not None: raise TypeError("A weights cube has been provided but will not be " "used") land_sea_mask = mask # In this case the land is set to 1 and the sea is set to 0 in the # input mask. sea_only = land_sea_mask.copy( data=np.logical_not(land_sea_mask.data).astype(int) ) land_only = land_sea_mask.copy(data=land_sea_mask.data.astype(int)) if lead_times is None: radius_or_radii = float(radii[0]) else: if len(radii) != len(lead_times): raise RuntimeError( "If leadtimes are supplied, it must be a list" " of equal length to a list of radii." ) radius_or_radii = [float(x) for x in radii] lead_times = [int(x) for x in lead_times] # Section for neighbourhood processing land points. if land_only.data.max() > 0.0: if masking_coordinate is None: result_land = NeighbourhoodProcessing( neighbourhood_shape, radius_or_radii, lead_times=lead_times, sum_only=area_sum, re_mask=True, )(cube, land_only) else: result_land = ApplyNeighbourhoodProcessingWithAMask( masking_coordinate, neighbourhood_shape, radius_or_radii, lead_times=lead_times, collapse_weights=weights, sum_only=area_sum, )(cube, mask) result = result_land # Section for neighbourhood processing sea points. if sea_only.data.max() > 0.0: result_sea = NeighbourhoodProcessing( neighbourhood_shape, radius_or_radii, lead_times=lead_times, sum_only=area_sum, re_mask=True, )(cube, sea_only) result = result_sea # Section for combining land and sea points following land and sea points # being neighbourhood processed individually. if sea_only.data.max() > 0.0 and land_only.data.max() > 0.0: # Recombine cubes to be a single output. combined_data = result_land.data.filled(0) + result_sea.data.filled(0) result = result_land.copy(data=combined_data) return result
def process( cube: cli.inputcube, raw_cube: cli.inputcube = None, *, realizations_count: int = None, random_seed: int = None, ignore_ecc_bounds=False, ): """Converts an incoming cube into one containing realizations. Args: cube (iris.cube.Cube): A cube to be processed. raw_cube (iris.cube.Cube): Cube of raw (not post processed) weather data. If this argument is given ensemble realizations will be created from percentiles by reshuffling them in correspondence to the rank order of the raw ensemble. Otherwise, the percentiles are rebadged as realizations. realizations_count (int): The number of ensemble realizations in the output. random_seed (int): Option to specify a value for the random seed when reordering percentiles. This value is for testing purposes only, to ensure reproduceable outputs. It should not be used in real time operations as it may introduce a bias into the reordered forecasts. ignore_ecc_bounds (bool): If True where percentiles (calculated as an intermediate output before realization) exceed the ECC bounds range, raises a warning rather than an exception. Returns: iris.cube.Cube: The processed cube. """ from improver.ensemble_copula_coupling.ensemble_copula_coupling import ( ConvertProbabilitiesToPercentiles, EnsembleReordering, RebadgePercentilesAsRealizations, ResamplePercentiles, ) from improver.metadata.probabilistic import is_probability if cube.coords("realization"): return cube if not cube.coords("percentile") and not is_probability(cube): raise ValueError("Unable to convert to realizations:\n" + str(cube)) if realizations_count is None: try: realizations_count = len(raw_cube.coord("realization").points) except AttributeError: # raised if raw_cube is None, hence has no attribute "coord" msg = "Either realizations_count or raw_cube must be provided" raise ValueError(msg) if cube.coords("percentile"): percentiles = ResamplePercentiles( ecc_bounds_warning=ignore_ecc_bounds)( cube, no_of_percentiles=realizations_count) else: percentiles = ConvertProbabilitiesToPercentiles( ecc_bounds_warning=ignore_ecc_bounds)( cube, no_of_percentiles=realizations_count) if raw_cube: result = EnsembleReordering()(percentiles, raw_cube, random_seed=random_seed) else: result = RebadgePercentilesAsRealizations()(percentiles) return result