def test_cubelist_slice_over_realization_only(self): """ Test that the utility returns an iris.cube.Cube with the expected realization coordinate, if a CubeList containing cubes with different realizations is passed in as the input. """ plugin = ConcatenateCubes("time", coords_to_slice_over=["realization"]) result = plugin.process(self.cubelist) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual( result.coord("realization").points, [0, 1, 2])
def test_cubelist_different_number_of_realizations_time(self): """ Test that the utility returns the expected error message, if a CubeList containing cubes with different numbers of realizations are passed in as the input, and the slicing done, in order to help the concatenation is only done over time. """ plugin = ConcatenateCubes("time", coords_to_slice_over=["time"]) cube1 = self.cube.copy() cube3 = iris.cube.CubeList([]) for cube in cube1.slices_over("realization"): if cube.coord("realization").points == 0: cube2 = cube elif cube.coord("realization").points in [1, 2]: cube3.append(cube) cube3 = cube3.merge_cube() cubelist = iris.cube.CubeList([cube2, cube3]) msg = "failed to concatenate into a single cube" with self.assertRaisesRegex(iris.exceptions.ConcatenateError, msg): plugin.process(cubelist)
def test_cubelist_slice_over_time_only(self): """ Test that the utility returns an iris.cube.Cube with the expected time and associated forecast period coordinates, if a CubeList containing cubes with different timesteps is passed in as the input. """ plugin = ConcatenateCubes("time", coords_to_slice_over=["time"]) expected_time_points = [ self.cube.coord("time").points[0], self.later_cube.coord("time").points[0]] expected_fp_points = [ self.cube.coord("forecast_period").points[0], self.later_cube.coord("forecast_period").points[0]] result = plugin.process(self.cubelist) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual( result.coord("time").points, expected_time_points) self.assertArrayAlmostEqual( result.coord("forecast_period").points, expected_fp_points)
class Test_process(IrisTest): """Test the process method (see also test_concatenate_cubes.py)""" def setUp(self): """Set up default plugin and test cubes.""" self.plugin = ConcatenateCubes("time") data = 275.0 * np.ones((3, 3, 3), dtype=np.float32) cube = set_up_variable_cube(data, time=dt(2017, 1, 10, 3), frt=dt(2017, 1, 10, 0)) # cubes can only be concatenated along an existing dimension; # therefore promote "time" self.cube = iris.util.new_axis(cube, scalar_coord="time") # create a cube for 3 hours later from the same forecast cycle self.later_cube = self.cube.copy() self.later_cube.coord("time").points = ( self.later_cube.coord("time").points + 3 * 3600) self.later_cube.coord("forecast_period").points = ( self.later_cube.coord("forecast_period").points + 3 * 3600) self.cubelist = iris.cube.CubeList([self.cube, self.later_cube]) def test_basic(self): """Test that the utility returns an iris.cube.Cube.""" result = self.plugin.process(self.cubelist) self.assertIsInstance(result, iris.cube.Cube) def test_single_cube(self): """Test a single cube is returned unchanged""" result = self.plugin.process(self.cube) self.assertArrayAlmostEqual(result.data, self.cube.data) self.assertEqual(result.metadata, self.cube.metadata) def test_single_item_cubelist(self): """Test a single item cubelist returns the cube unchanged""" result = self.plugin.process([self.cube]) self.assertArrayAlmostEqual(result.data, self.cube.data) self.assertEqual(result.metadata, self.cube.metadata) def test_missing_master_coord(self): """Test error is raised if the master coordinate is missing""" self.cube.remove_coord("time") msg = "Master coordinate time is not present" with self.assertRaisesRegex(ValueError, msg): self.plugin.process([self.cube, self.cube]) def test_identical_cubes(self): """ Test that the utility returns the expected error message, if an attempt is made to concatenate identical cubes. """ cubes = iris.cube.CubeList([self.cube, self.cube]) msg = "An unexpected problem prevented concatenation." with self.assertRaisesRegex(iris.exceptions.ConcatenateError, msg): self.plugin.process(cubes) def test_cubelist_type_and_data(self): """ Test that the utility returns an iris.cube.Cube with the expected resulting data, if a CubeList containing non-identical cubes (different values for the time coordinate) is passed in as the input. """ data = self.cube.data.copy() expected_result = np.vstack([data, data]) result = self.plugin.process(self.cubelist) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(expected_result, result.data) def test_cubelist_different_number_of_realizations(self): """ Test that the utility returns an iris.cube.Cube with the expected realizations, if a CubeList containing cubes with different numbers of realizations are passed in as the input. """ cube1 = self.cube.copy() cube3 = iris.cube.CubeList([]) for cube in cube1.slices_over("realization"): if cube.coord("realization").points == 0: cube2 = cube elif cube.coord("realization").points in [1, 2]: cube3.append(cube) cube3 = cube3.merge_cube() cubelist = iris.cube.CubeList([cube2, cube3]) result = ConcatenateCubes("time", coords_to_slice_over=["realization" ]).process(cubelist) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual( result.coord("realization").points, [0, 1, 2]) def test_cubelist_different_number_of_realizations_time(self): """ Test that the utility returns the expected error message, if a CubeList containing cubes with different numbers of realizations are passed in as the input, and the slicing done, in order to help the concatenation is only done over time. """ plugin = ConcatenateCubes("time", coords_to_slice_over=["time"]) cube1 = self.cube.copy() cube3 = iris.cube.CubeList([]) for cube in cube1.slices_over("realization"): if cube.coord("realization").points == 0: cube2 = cube elif cube.coord("realization").points in [1, 2]: cube3.append(cube) cube3 = cube3.merge_cube() cubelist = iris.cube.CubeList([cube2, cube3]) msg = "failed to concatenate into a single cube" with self.assertRaisesRegex(iris.exceptions.ConcatenateError, msg): plugin.process(cubelist) def test_cubelist_slice_over_time_only(self): """ Test that the utility returns an iris.cube.Cube with the expected time and associated forecast period coordinates, if a CubeList containing cubes with different timesteps is passed in as the input. """ plugin = ConcatenateCubes("time", coords_to_slice_over=["time"]) expected_time_points = [ self.cube.coord("time").points[0], self.later_cube.coord("time").points[0], ] expected_fp_points = [ self.cube.coord("forecast_period").points[0], self.later_cube.coord("forecast_period").points[0], ] result = plugin.process(self.cubelist) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual( result.coord("time").points, expected_time_points) self.assertArrayAlmostEqual( result.coord("forecast_period").points, expected_fp_points) def test_cubelist_slice_over_realization_only(self): """ Test that the utility returns an iris.cube.Cube with the expected realization coordinate, if a CubeList containing cubes with different realizations is passed in as the input. """ plugin = ConcatenateCubes("time", coords_to_slice_over=["realization"]) result = plugin.process(self.cubelist) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual( result.coord("realization").points, [0, 1, 2]) def test_cubelist_different_var_names(self): """ Test that the utility returns an iris.cube.Cube, if a CubeList containing non-identical cubes is passed in as the input. """ cube1 = self.cube.copy() cube2 = self.later_cube.copy() cube1.coord("time").var_name = "time_0" cube2.coord("time").var_name = "time_1" cubelist = iris.cube.CubeList([cube1, cube2]) result = self.plugin.process(cubelist) self.assertIsInstance(result, iris.cube.Cube)