def test_warning_messages(self, warning_list=None): """Test that warning messages is raised correctly. """ warning_msg = "Adding new coordinate" add_coord(self.cube, self.coord_name, self.changes, warnings_on=True) self.assertTrue( any(item.category == UserWarning for item in warning_list)) self.assertTrue(any(warning_msg in str(item) for item in warning_list))
def test_works_two_thresh(self): """Test that the plugin works with a cube that contains multiple thresholds.""" width = 2.0 thresh_cube = self.cube.copy() thresh_cube.remove_coord("forecast_reference_time") changes = {'points': [0.25], 'units': '1', 'var_name': 'threshold'} cube_with_thresh1 = add_coord(thresh_cube.copy(), 'precipitation_amount', changes) changes = {'points': [0.5], 'units': '1', 'var_name': 'threshold'} cube_with_thresh2 = add_coord(thresh_cube.copy(), 'precipitation_amount', changes) changes = {'points': [0.75], 'units': '1', 'var_name': 'threshold'} cube_with_thresh3 = add_coord(thresh_cube.copy(), 'precipitation_amount', changes) cubelist = iris.cube.CubeList( [cube_with_thresh1, cube_with_thresh2, cube_with_thresh3]) thresh_cubes = concatenate_cubes( cubelist, coords_to_slice_over='precipitation_amount') plugin = TriangularWeightedBlendAcrossAdjacentPoints( 'forecast_period', self.forecast_period, 'hours', width) result = plugin.process(thresh_cubes) # Test that the result cube retains threshold co-ordinates # from original cube. self.assertEqual(thresh_cubes.coord('precipitation_amount'), result.coord('precipitation_amount'))
def test_non_name_value_error(self): """Test value errors thrown by iris.Coord (eg invalid units) are still raised""" self.changes['units'] = 'narwhal' msg = 'Failed to parse unit "narwhal"' with self.assertRaisesRegex(ValueError, msg): add_coord(self.cube, self.coord_name, self.changes)
def test_works_one_thresh(self): """Test that the plugin retains the single threshold from the input cube.""" # Creates a cube containing the expected outputs. fill_value = 1 + 1 / 3.0 data = np.full((2, 2), fill_value) # Take a slice of the time coordinate. expected_cube = self.cube[0].copy(data.astype(np.float32)) # Add threshold axis to expected output cube. changes = {'points': [0.5], 'units': '1', 'var_name': 'threshold'} expected_cube = add_coord(expected_cube, 'precipitation_amount', changes) # Add threshold axis to standard input cube. cube_with_thresh = add_coord(self.cube.copy(), 'precipitation_amount', changes) width = 2.0 plugin = TriangularWeightedBlendAcrossAdjacentPoints( 'forecast_period', self.forecast_period, 'hours', width) result = plugin.process(cube_with_thresh) # Test that the result cube retains threshold co-ordinates # from original cube. self.assertEqual(expected_cube.coord('precipitation_amount'), result.coord('precipitation_amount')) self.assertArrayEqual(expected_cube.data, result.data) self.assertEqual(expected_cube, result)
def test_basic(self): """Test that add_coord returns a Cube and adds coord correctly and does not modify the input cube""" original_cube = self.cube.copy() result = add_coord(self.cube, self.coord_name, self.changes) result_coord = result.coord(self.coord_name) self.assertIsInstance(result, Cube) self.assertArrayEqual(result_coord.points, np.array([2.0])) self.assertArrayEqual(result_coord.bounds, np.array([[0.1, 2.0]])) self.assertEqual(str(result_coord.units), 'mm') self.assertEqual(result_coord.var_name, "threshold") self.assertEqual(self.cube, original_cube)
def test_input_cube_no_change(self): """Test that the plugin does not change the original input cube.""" # Add threshold axis to standard input cube. changes = {'points': [0], 'units': '1', 'var_name': 'threshold'} cube_with_thresh = add_coord(self.cube.copy(), 'precipitation_amount', changes) original_cube = cube_with_thresh.copy() width = 2.0 plugin = TriangularWeightedBlendAcrossAdjacentPoints( 'forecast_period', self.forecast_period, 'hours', width) _ = plugin.process(cube_with_thresh) # Test that the input cube is unchanged by the function. self.assertEqual(cube_with_thresh, original_cube)
def test_fails_points_greater_than_1(self): """Test that add_coord fails if points greater than 1 """ changes = {'points': [0.1, 2.0]} msg = 'Can not add a coordinate of length > 1' with self.assertRaisesRegex(ValueError, msg): add_coord(self.cube, self.coord_name, changes)
def test_fails_no_points(self): """Test that add_coord fails if points not included in metadata """ changes = {'bounds': [0.1, 2.0], 'units': 'mm'} msg = 'Trying to add new coord but no points defined' with self.assertRaisesRegex(ValueError, msg): add_coord(self.cube, self.coord_name, changes)
def test_long_name(self): """Test a coordinate can be added with a name that is not standard""" result = add_coord(self.cube, "threshold", self.changes) self.assertEqual(result.coord("threshold").long_name, "threshold")
def test_standard_name(self): """Test default is for coordinate to be added as standard name""" result = add_coord(self.cube, self.coord_name, self.changes) self.assertEqual( result.coord(self.coord_name).standard_name, self.coord_name)