def setUp(self): """Create cubes for testing the split_forecasts_and_truth method. Forecast data is all set to 1, and truth data to 0, allowing for a simple check that the cubes have been separated as expected.""" thresholds = [283, 288] probability_data = np.ones((2, 4, 4), dtype=np.float32) realization_data = np.ones((4, 4), dtype=np.float32) self.truth_attribute = "mosg__model_configuration=uk_det" truth_attributes = {"mosg__model_configuration": "uk_det"} probability_forecast_1 = set_up_probability_cube( probability_data, thresholds) probability_forecast_2 = set_up_probability_cube( probability_data, thresholds, time=datetime(2017, 11, 11, 4, 0), frt=datetime(2017, 11, 11, 0, 0), ) self.probability_forecasts = [ probability_forecast_1, probability_forecast_2 ] probability_truth_1 = probability_forecast_1.copy( data=np.zeros((2, 4, 4), dtype=np.float32)) probability_truth_2 = probability_forecast_2.copy( data=np.zeros((2, 4, 4), dtype=np.float32)) probability_truth_1.attributes.update(truth_attributes) probability_truth_2.attributes.update(truth_attributes) self.probability_truths = [probability_truth_1, probability_truth_2] realization_forecast_1 = set_up_variable_cube(realization_data) realization_forecast_2 = set_up_variable_cube( realization_data, time=datetime(2017, 11, 11, 4, 0), frt=datetime(2017, 11, 11, 0, 0), ) self.realization_forecasts = [ realization_forecast_1, realization_forecast_2 ] realization_truth_1 = realization_forecast_1.copy( data=np.zeros((4, 4), dtype=np.float32)) realization_truth_2 = realization_forecast_2.copy( data=np.zeros((4, 4), dtype=np.float32)) realization_truth_1.attributes.update(truth_attributes) realization_truth_2.attributes.update(truth_attributes) self.realization_truths = [realization_truth_1, realization_truth_2] self.landsea_mask = realization_truth_1.copy() self.landsea_mask.rename("land_binary_mask")
def setUp(self): """Set up cube """ data = np.array( [0, 1, 5, 11, 20, 5, 9, 10, 4, 2, 0, 1, 29, 30, 1, 5, 6, 6], dtype=np.int32).reshape((2, 3, 3)) cube = set_up_variable_cube( data, "weather_code", "1", ) date_times = [ datetime.datetime(2017, 11, 19, 0, 30), datetime.datetime(2017, 11, 19, 1, 30), ] self.cube = add_coordinate( cube, date_times, "time", is_datetime=True, order=[1, 0, 2, 3], ) self.wxcode = np.array(list(WX_DICT.keys())) self.wxmeaning = " ".join(WX_DICT.values()) self.data_directory = mkdtemp() self.nc_file = self.data_directory + "/wxcode.nc" pathlib.Path(self.nc_file).touch(exist_ok=True)
def setUp(self): """Set up distance.""" self.distance = 2000 grid_values = np.arange(0.0, 10000.0, 2000.0, dtype=np.float32) self.cube = set_up_variable_cube(np.zeros((5, 5), dtype=np.float32), spatial_grid="equalarea") self.cube.coord("projection_y_coordinate").points = grid_values self.cube.coord("projection_x_coordinate").points = grid_values
def setUp(self): """Set up orography cube (as zeros) and falling_phase_level cube with multiple realizations designed to return snow, sleet and rain. The middle realization gives both not-snow and not-rain because both the 20th percentile is <= zero and the 80th percentile is >= zero.""" # cubes for testing have a grid-length of 333333m. self.plugin = PrecipPhaseProbability(radius=350000.0) self.mandatory_attributes = { "title": "mandatory title", "source": "mandatory_source", "institution": "mandatory_institution", } data = np.zeros((3, 3), dtype=np.float32) orog_cube = set_up_variable_cube( data, name="surface_altitude", units="m", spatial_grid="equalarea", attributes=self.mandatory_attributes, ) falling_level_data = np.array( [ [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]], [[0, -1, 0], [0, 1, 0], [0, -1, 0]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]], ], dtype=np.float32, ) falling_level_cube = set_up_variable_cube( falling_level_data, units="m", spatial_grid="equalarea", name="altitude_of_snow_falling_level", realizations=[0, 1, 2], attributes=self.mandatory_attributes, ) self.cubes = iris.cube.CubeList([falling_level_cube, orog_cube])
def setUp(self): """Set up cube.""" data = np.array([[1, 2, 3], [2, 4, 6], [5, 10, 15]]) self.cube = set_up_variable_cube( data, "wind_speed", "m s-1", "equalarea", ) self.plugin = DifferenceBetweenAdjacentGridSquares()
def test_spatial_mismatch(self): """Test that process raises an exception when the input cubes have different spatial coordinates.""" self.cubes[1] = set_up_variable_cube( self.cubes[1].data, name='surface_altitude', units='m', spatial_grid='latlon', attributes=self.mandatory_attributes) msg = 'Spatial coords mismatch between' with self.assertRaisesRegex(ValueError, msg): self.plugin.process(self.cubes)
def setUp(self): """Set up distance.""" self.distance = 2000 coords = np.array([0.0, 2000.0, 4000.0, 6000.0], dtype=np.float32) self.timesteps = [ datetime.datetime(2017, 11, 9, 12), datetime.datetime(2017, 11, 9, 15), ] self.cube = set_up_variable_cube( np.zeros((2, 4, 4), dtype=np.float32), "lwe_precipitation_rate", "m s-1", "equalarea", ) self.cube.coord("projection_y_coordinate").points = coords self.cube.coord("projection_x_coordinate").points = coords
def set_up_precipitation_rate_cube(): """Create a cube with metadata and values suitable for precipitation rate.""" data = np.zeros((1, 4, 4)) data[0, 0, :] = 2.0 data[0, 1, :] = 4.0 data[0, 2, :] = 8.0 data[0, 3, :] = 16.0 data[0, 0, 2] = 0.0 data[0, 2, 1] = 0.0 data[0, 3, 0] = 0.0 precip_cube = set_up_variable_cube( data.astype(np.float32), "lwe_precipitation_rate", "mm h-1", "equalarea", ) precip_cube.convert_units("m s-1") coord_points = np.array([0.0, 2000.0, 4000.0, 6000.0]) precip_cube.coord("projection_y_coordinate").points = coord_points precip_cube.coord("projection_x_coordinate").points = coord_points return precip_cube
def test_3d_cube(self): """Test the differences are calculated along both the x and y dimensions and returned as separate cubes when a 3d cube is input.""" data = np.array([[[1, 2, 3], [2, 4, 6], [5, 10, 15]], [[1, 2, 3], [2, 2, 6], [5, 10, 20]]]) expected_x = np.array([[[1, 1], [2, 2], [5, 5]], [[1, 1], [0, 4], [5, 10]]]) expected_y = np.array([[[1, 2, 3], [3, 6, 9]], [[1, 0, 3], [3, 8, 14]]]) cube = set_up_variable_cube( data, "wind_speed", "m s-1", "equalarea", realizations=np.array([1, 2]), ) result = self.plugin.process(cube) self.assertIsInstance(result[0], iris.cube.Cube) self.assertArrayEqual(result[0].data, expected_x) self.assertIsInstance(result[1], iris.cube.Cube) self.assertArrayEqual(result[1].data, expected_y)
def setUp(self): """Set up the cube.""" data = np.ones((4, 4), dtype=np.float32) self.cube = set_up_variable_cube(data, spatial_grid='equalarea')
def test_multiple_lead_times_neighbourhooding(self): """Test where neighbourhood is applied for multiple lead times, where different radii are applied at each lead time.""" expected = np.array( [ [ [ [0.25, 0.166667, 0.166667, 0.0], [0.166667, 0.11111111, 0.11111111, 0.0], [0.166667, 0.11111111, 0.11111111, 0.0], [0.0, 0.0, 0.0, 0.0], ], [ [0.1111111, 0.0833333, 0.0833333, 0.1111111], [0.0833333, 0.0625, 0.0625, 0.0833333], [0.0833333, 0.0625, 0.0625, 0.0833333], [0.1111111, 0.0833333, 0.0833333, 0.1111111], ], ] ] ) # Set up a cube with 3 and 6 hour forecast periods precip = set_up_variable_cube( np.ones((1, 4, 4), dtype=np.float32), "lwe_precipitation_rate", "mm h-1", "equalarea", time=datetime.datetime(2015, 11, 19, 3), frt=datetime.datetime(2015, 11, 19, 0), ) precip = add_coordinate( precip, [datetime.datetime(2015, 11, 19, 3), datetime.datetime(2015, 11, 19, 6)], "time", order=[1, 0, 2, 3], is_datetime=True, ) coord_points = np.array([0.0, 2000.0, 4000.0, 6000.0]) precip.coord("projection_y_coordinate").points = coord_points precip.coord("projection_x_coordinate").points = coord_points data = np.full((1, 2, 4, 4), 1.0) data[0, 0, 1, 1] = 20.0 data[0, 1, 1, 1] = 20.0 precip.data = data.astype(np.float32) precip.convert_units("m s-1") cubelist = lower_higher_threshold_cubelist( precip, self.lower_threshold, self.higher_threshold ) lead_times = [3, 6] radii = [2000.0, 4000.0] result = DiagnoseConvectivePrecipitation( self.lower_threshold, self.higher_threshold, self.neighbourhood_method, radii=radii, lead_times=lead_times, )._calculate_convective_ratio(cubelist, self.threshold_list) self.assertArrayAlmostEqual(result, expected)