class Test_process(IrisTest): """Test the process method.""" def setUp(self): """Set up cube.""" data = np.array([[1, 2, 3], [2, 4, 6], [5, 10, 15]]) self.cube = set_up_cube(data, "wind_speed", "m s-1") self.plugin = DifferenceBetweenAdjacentGridSquares() def test_basic(self): """Test that differences are calculated along both the x and y dimensions and returned as separate cubes.""" expected_x = np.array([[1, 1], [2, 2], [5, 5]]) expected_y = np.array([[1, 2, 3], [3, 6, 9]]) result = self.plugin.process(self.cube) self.assertIsInstance(result[0], Cube) self.assertArrayAlmostEqual(result[0].data, expected_x) self.assertIsInstance(result[1], Cube) self.assertArrayAlmostEqual(result[1].data, expected_y) 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_cube(data, "wind_speed", "m s-1", realizations=np.array([1, 2])) result = self.plugin.process(cube) self.assertIsInstance(result[0], iris.cube.Cube) self.assertArrayAlmostEqual(result[0].data, expected_x) self.assertIsInstance(result[1], iris.cube.Cube) self.assertArrayAlmostEqual(result[1].data, expected_y)
class Test_process(IrisTest): """Test the process method.""" 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", realizations=np.array([1, 2]), ) self.plugin = DifferenceBetweenAdjacentGridSquares() def test_basic(self): """Test that differences are calculated along both the x and y dimensions and returned as separate cubes.""" expected_x = np.array([[1, 1], [2, 2], [5, 5]]) expected_y = np.array([[1, 2, 3], [3, 6, 9]]) result = self.plugin.process(self.cube) self.assertIsInstance(result[0], Cube) self.assertArrayEqual(result[0].data, expected_x) self.assertIsInstance(result[1], Cube) self.assertArrayEqual(result[1].data, expected_y) def test_metadata(self): """Test the resulting metadata is correct.""" cell_method_x = CellMethod("difference", coords=["projection_x_coordinate"], intervals="1 grid length") cell_method_y = CellMethod("difference", coords=["projection_y_coordinate"], intervals="1 grid length") result = self.plugin.process(self.cube) for cube, cm in zip(result, [cell_method_x, cell_method_y]): self.assertEqual(cube.cell_methods[0], cm) self.assertEqual(cube.attributes["form_of_difference"], "forward_difference") self.assertEqual(cube.name(), "difference_of_wind_speed") 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)