def test_mismatching_coords_wrong_shape(self): """Test raises an error if shape do not match. """ cube2 = create_cube_with_threshold( threshold_values=np.array([1.0, 2.0], dtype=np.float32)) msg = "Can not combine cubes, mismatching shapes" with self.assertRaisesRegex(ValueError, msg): resolve_metadata_diff(self.cube1, cube2)
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)
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]))
def test_mismatching_coords_same_shape(self): """Test works with mismatching coords but coords same shape.""" cube2 = create_cube_with_threshold( threshold_values=np.array([2.0], dtype=np.float32)) result = resolve_metadata_diff(self.cube1, cube2) self.assertIsInstance(result, tuple) self.assertArrayEqual(result[0].coord(self.coord_name).points, np.array([1.0])) self.assertArrayEqual(result[1].coord(self.coord_name).points, np.array([2.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]))
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. 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: 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