Exemplo n.º 1
0
 def test_mismatching_coords_wrong_shape(self):
     """Test raises an error if shape do not match. """
     cube1 = create_cube_with_threshold()
     cube2 = create_cube_with_threshold(threshold_values=[1.0, 2.0])
     msg = "Can not combine cubes, mismatching shapes"
     with self.assertRaisesRegex(ValueError, msg):
         resolve_metadata_diff(cube1, cube2)
Exemplo n.º 2
0
 def test_basic(self):
     """Test that the function returns a tuple of Cubes. """
     cube2 = self.cube1.copy()
     result = resolve_metadata_diff(self.cube1, cube2)
     self.assertIsInstance(result, tuple)
     self.assertEqual(len(result), 2)
     self.assertIsInstance(result[0], Cube)
     self.assertIsInstance(result[1], Cube)
Exemplo n.º 3
0
 def test_mismatching_coords_1d_coord_pos0_on_cube2(self):
     """Test missing coord on cube1. Coord is leading coord in cube2."""
     cube2 = self.cube1.copy()
     self.cube1.remove_coord(self.coord_name)
     self.cube1 = iris.util.squeeze(self.cube1)
     result = resolve_metadata_diff(self.cube1, cube2)
     self.assertIsInstance(result, tuple)
     self.assertArrayEqual(result[0].shape, np.array([2, 2, 2]))
     self.assertArrayEqual(result[1].shape, np.array([2, 2, 2]))
Exemplo n.º 4
0
 def test_mismatching_coords_same_shape(self):
     """Test works with mismatching coords but coords same shape."""
     cube1 = create_cube_with_threshold()
     cube2 = create_cube_with_threshold(threshold_values=[2.0])
     result = resolve_metadata_diff(cube1, cube2)
     self.assertIsInstance(result, tuple)
     self.assertArrayEqual(result[0].coord('threshold').points,
                           np.array([1.0]))
     self.assertArrayEqual(result[1].coord('threshold').points,
                           np.array([2.0]))
Exemplo n.º 5
0
 def test_mismatching_coords_1d_coord_pos0_on_cube1(self):
     """Test missing coord on cube2. Coord is leading coord in cube1."""
     cube1 = create_cube_with_threshold()
     cube2 = cube1.copy()
     cube2.remove_coord('threshold')
     cube2 = iris.util.squeeze(cube2)
     result = resolve_metadata_diff(cube1, cube2)
     self.assertIsInstance(result, tuple)
     self.assertArrayEqual(result[0].shape, np.array([1, 2, 2, 2]))
     self.assertArrayEqual(result[1].shape, np.array([1, 2, 2, 2]))
Exemplo n.º 6
0
 def test_mismatching_coords_1d_coord_pos1_cube1(self):
     """Test missing 1d coord on cube2.
        Coord is not leading coord in cube1."""
     cube2 = self.cube2.copy()
     cube2.remove_coord('threshold')
     cube2 = iris.util.squeeze(cube2)
     result = resolve_metadata_diff(self.cube2, cube2)
     self.assertIsInstance(result, tuple)
     self.assertArrayEqual(result[0].shape, np.array([2, 1, 2, 2, 2]))
     self.assertArrayEqual(result[1].shape, np.array([2, 1, 2, 2, 2]))
Exemplo n.º 7
0
    def process(self,
                cube_list,
                new_diagnostic_name,
                revised_coords=None,
                revised_attributes=None):
        """
        Create a combined cube.

        Args:
            cube_list (iris.cube.CubeList):
                Cube List contain the cubes to combine.
            new_diagnostic_name (str):
                New name for the combined diagnostic.
        Keyword Args:
            revised_coords (dict or None):
                Revised coordinates for combined cube.
            revised_attributes (dict or None):
                Revised attributes for combined cube.

        Returns:
            result (iris.cube.Cube):
                Cube containing the combined data.

        """
        if not isinstance(cube_list, iris.cube.CubeList):
            msg = ('Expecting data to be an instance of '
                   'iris.cube.CubeList but is'
                   ' {0:s}.'.format(type(cube_list)))
            raise TypeError(msg)
        if len(cube_list) < 2:
            msg = 'Expecting 2 or more cubes in cube_list'
            raise ValueError(msg)

        # resulting cube will be based on the first cube.
        data_type = cube_list[0].dtype
        result = cube_list[0].copy()

        for ind in range(1, len(cube_list)):
            cube1, cube2 = (resolve_metadata_diff(
                result.copy(),
                cube_list[ind].copy(),
                warnings_on=self.warnings_on))
            result = self.combine(cube1, cube2, self.operation)

        if self.operation == 'mean':
            result = result / len(cube_list)

        result = amend_metadata(result,
                                new_diagnostic_name,
                                data_type,
                                revised_coords,
                                revised_attributes,
                                warnings_on=self.warnings_on)

        return result
Exemplo n.º 8
0
 def test_warnings_on_work(self, warning_list=None):
     """Test warning messages are given if warnings_on is set."""
     cube2 = self.cube1.copy()
     cube2.remove_coord(self.coord_name)
     cube2 = iris.util.squeeze(cube2)
     warning_msg = 'Adding new coordinate'
     result = resolve_metadata_diff(self.cube1, cube2, 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))
     self.assertIsInstance(result, tuple)
     self.assertArrayEqual(result[0].shape, np.array([1, 2, 2, 2]))
     self.assertArrayEqual(result[1].shape, np.array([1, 2, 2, 2]))
Exemplo n.º 9
0
 def test_warnings_on_work(self):
     """Test warning messages are given if warnings_on is set."""
     cube1 = create_cube_with_threshold()
     cube2 = cube1.copy()
     cube2.remove_coord('threshold')
     cube2 = iris.util.squeeze(cube2)
     warning_msg = 'Adding new coordinate'
     with warnings.catch_warnings(record=True) as warning_list:
         warnings.simplefilter("always")
         result = resolve_metadata_diff(cube1, cube2, 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))
         self.assertIsInstance(result, tuple)
         self.assertArrayEqual(result[0].shape, np.array([1, 2, 2, 2]))
         self.assertArrayEqual(result[1].shape, np.array([1, 2, 2, 2]))
Exemplo n.º 10
0
    def process(self,
                cube_list,
                new_diagnostic_name,
                revised_coords=None,
                revised_attributes=None,
                expanded_coord=None):
        """
        Create a combined cube.

        Args:
            cube_list (iris.cube.CubeList):
                Cube List contain the cubes to combine.
            new_diagnostic_name (str):
                New name for the combined diagnostic.
        Keyword Args:
            revised_coords (dict or None):
                Revised coordinates for combined cube.
            revised_attributes (dict or None):
                Revised attributes for combined cube.
            expanded_coord (dict or None):
                Coordinates to be expanded as a key, with the value
                indicating whether the upper or mid point of the coordinate
                should be used as the point value, e.g.
                {'time': 'upper'}.
        Returns:
            result (iris.cube.Cube):
                Cube containing the combined data.
        Raises:
            TypeError: If cube_list is not an iris.cube.CubeList.
            ValueError: If the cubelist contains only one cube.
        """
        if not isinstance(cube_list, iris.cube.CubeList):
            msg = ('Expecting data to be an instance of iris.cube.CubeList '
                   'but is {}.'.format(type(cube_list)))
            raise TypeError(msg)
        if len(cube_list) < 2:
            msg = 'Expecting 2 or more cubes in cube_list'
            raise ValueError(msg)

        # resulting cube will be based on the first cube.
        data_type = cube_list[0].dtype
        result = cube_list[0].copy()

        for ind in range(1, len(cube_list)):
            cube1, cube2 = (resolve_metadata_diff(
                result.copy(),
                cube_list[ind].copy(),
                warnings_on=self.warnings_on))
            result = self.combine(cube1, cube2)

        if self.operation == 'mean':
            result.data = result.data / len(cube_list)

        # If cube has coord bounds that we want to expand
        if expanded_coord:
            result = expand_bounds(result, cube_list, expanded_coord)

        result = amend_metadata(result,
                                new_diagnostic_name,
                                data_type,
                                revised_coords,
                                revised_attributes,
                                warnings_on=self.warnings_on)

        return result