コード例 #1
0
class Test_create_difference_cube(IrisTest):

    """Test the create_difference_cube 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_y_dimension(self):
        """Test differences calculated along the y dimension."""
        diff_array = np.array([[1, 2, 3],
                               [3, 6, 9]])
        result = self.plugin.create_difference_cube(
            self.cube, "projection_y_coordinate", diff_array)
        self.assertIsInstance(result, Cube)
        self.assertArrayAlmostEqual(result.data, diff_array)

    def test_x_dimension(self):
        """Test differences calculated along the x dimension."""
        diff_array = np.array([[1, 1],
                               [2, 2],
                               [5, 5]])
        result = self.plugin.create_difference_cube(
            self.cube, "projection_x_coordinate", diff_array)
        self.assertIsInstance(result, Cube)
        self.assertArrayAlmostEqual(result.data, diff_array)

    def test_metadata(self):
        """Test that the result has the expected metadata."""
        diff_array = np.array([[1, 2, 3],
                               [3, 6, 9]])
        cell_method = CellMethod(
            "difference", coords=["projection_y_coordinate"],
            intervals='1 grid length')
        result = self.plugin.create_difference_cube(
            self.cube, "projection_y_coordinate", diff_array)
        self.assertEqual(
            result.cell_methods[0], cell_method)
        self.assertEqual(
            result.attributes["form_of_difference"],
            "forward_difference")
        self.assertEqual(result.name(), 'difference_of_wind_speed')

    def test_othercoords(self):
        """Test that other coords are transferred properly"""
        diff_array = np.array([[1, 2, 3],
                               [3, 6, 9]])
        time_coord = self.cube.coord('time')
        proj_x_coord = self.cube.coord(axis='x')
        result = self.plugin.create_difference_cube(
            self.cube, "projection_y_coordinate", diff_array)
        self.assertEqual(result.coord(axis='x'), proj_x_coord)
        self.assertEqual(result.coord('time'), time_coord)
class Test_create_difference_cube(IrisTest):
    """Test the create_difference_cube method."""
    def setUp(self):
        """Set up cube."""
        data = np.array([[1, 2, 3], [2, 4, 6], [5, 10, 15]])
        self.diff_in_y_array = np.array([[1, 2, 3], [3, 6, 9]])
        self.cube = set_up_variable_cube(
            data,
            "wind_speed",
            "m s-1",
            "equalarea",
        )
        self.plugin = DifferenceBetweenAdjacentGridSquares()

    def test_y_dimension(self):
        """Test differences calculated along the y dimension."""
        points = self.cube.coord(axis="y").points
        expected_y = (points[1:] + points[:-1]) / 2
        result = self.plugin.create_difference_cube(self.cube,
                                                    "projection_y_coordinate",
                                                    self.diff_in_y_array)
        self.assertIsInstance(result, Cube)
        self.assertArrayAlmostEqual(result.coord(axis="y").points, expected_y)
        self.assertArrayEqual(result.data, self.diff_in_y_array)

    def test_x_dimension(self):
        """Test differences calculated along the x dimension."""
        diff_array = np.array([[1, 1], [2, 2], [5, 5]])
        points = self.cube.coord(axis="x").points
        expected_x = (points[1:] + points[:-1]) / 2
        result = self.plugin.create_difference_cube(self.cube,
                                                    "projection_x_coordinate",
                                                    diff_array)
        self.assertIsInstance(result, Cube)
        self.assertArrayAlmostEqual(result.coord(axis="x").points, expected_x)
        self.assertArrayEqual(result.data, diff_array)

    def test_othercoords(self):
        """Test that other coords are transferred properly"""
        time_coord = self.cube.coord("time")
        proj_x_coord = self.cube.coord(axis="x")
        result = self.plugin.create_difference_cube(self.cube,
                                                    "projection_y_coordinate",
                                                    self.diff_in_y_array)
        self.assertEqual(result.coord(axis="x"), proj_x_coord)
        self.assertEqual(result.coord("time"), time_coord)
 def test_gradient(self):
     """Test that the correct metadata is set if the desired output is a
     gradient not a difference (except name)"""
     plugin = DifferenceBetweenAdjacentGridSquares(gradient=True)
     diff_array = np.array([[1, 2, 3],
                            [3, 6, 9]])
     result = plugin.create_difference_cube(
         self.cube, "projection_y_coordinate", diff_array)
     self.assertNotIn("form_of_difference", result.attributes)
     self.assertFalse(result.cell_methods)