def test_vicinity_names(self): """Test plugin names the cube and threshold coordinate correctly for a vicinity diagnostic""" input = "lwe_thickness_of_precipitation_amount_in_vicinity" output = "thickness_of_rainfall_amount_in_vicinity" self.cube.rename(f"probability_of_{input}_above_threshold") cubelist = iris.cube.CubeList([self.cube.copy(), self.multiplier]) result = CubeMultiplier(broadcast_to_threshold=True)(cubelist, output) self.assertEqual(result.name(), f"probability_of_{output}_above_threshold") self.assertEqual( result.coord(var_name="threshold").name(), "thickness_of_rainfall_amount")
def test_vicinity_names(self): """Test plugin names the cube and threshold coordinate correctly for a vicinity diagnostic""" input = "lwe_thickness_of_precipitation_amount_in_vicinity" output = "thickness_of_rainfall_amount_in_vicinity" self.cube4.rename(f"probability_of_{input}_above_threshold") cube = self.cube4[:, 0, ...].copy() cube.data = np.ones_like(cube.data) cube.remove_coord("lwe_thickness_of_precipitation_amount") cubelist = iris.cube.CubeList([self.cube4.copy(), cube]) input_copy = deepcopy(cubelist) result = CubeMultiplier()(cubelist, output, broadcast_to_threshold=True) self.assertEqual(result.name(), f"probability_of_{output}_above_threshold") self.assertEqual( result.coord(var_name="threshold").name(), "thickness_of_rainfall_amount" )
def test_unmodified_cell_methods(self): """Test that plugin leaves cell methods that are diagnostic name agnostic unchanged.""" cell_methods = list(self.cube.cell_methods) additional_cell_method_1 = CellMethod("sum", coords="longitude") additional_cell_method_2 = CellMethod("sum", coords="latitude", comments="Kittens are great") cell_methods.extend( [additional_cell_method_1, additional_cell_method_2]) self.cube.cell_methods = cell_methods cubelist = iris.cube.CubeList([self.cube, self.multiplier]) new_cube_name = "new_cube_name" expected = [ CellMethod("sum", coords="time", comments=f"of {new_cube_name}"), additional_cell_method_1, additional_cell_method_2, ] result = CubeMultiplier()(cubelist, new_cube_name, broadcast_to_threshold=True) self.assertArrayEqual(result.cell_methods, expected)
def test_broadcast_coord(self): """Test that plugin broadcasts to threshold coord without changing inputs. Using the broadcast_to_coords argument including a value of "threshold" will result in the returned cube maintaining the probabilistic elements of the name of the first input cube.""" cubelist = iris.cube.CubeList([self.cube.copy(), self.multiplier]) input_copy = deepcopy(cubelist) result = CubeMultiplier(broadcast_to_threshold=True)(cubelist, "new_cube_name") self.assertIsInstance(result, Cube) self.assertEqual(result.name(), "probability_of_new_cube_name_above_threshold") self.assertEqual( result.coord(var_name="threshold").name(), "new_cube_name") self.assertArrayAlmostEqual(result.data, self.cube.data) self.assertCubeListEqual(input_copy, cubelist)
def test_broadcast_coord(self): """Test that plugin broadcasts to threshold coord without changing inputs. Using the broadcast_to_coords argument including a value of "threshold" will result in the returned cube maintaining the probabilistic elements of the name of the first input cube.""" cube = self.cube4[:, 0, ...].copy() cube.data = np.ones_like(cube.data) cube.remove_coord("lwe_thickness_of_precipitation_amount") cubelist = iris.cube.CubeList([self.cube4.copy(), cube]) input_copy = deepcopy(cubelist) result = CubeMultiplier()( cubelist, "new_cube_name", broadcast_to_threshold=True ) self.assertIsInstance(result, Cube) self.assertEqual(result.name(), "probability_of_new_cube_name_above_threshold") self.assertEqual(result.coord(var_name="threshold").name(), "new_cube_name") self.assertArrayAlmostEqual(result.data, self.cube4.data) self.assertCubeListEqual(input_copy, cubelist)
def test_error_broadcast_coord_is_auxcoord(self): """Test that plugin throws an error if asked to broadcast to a threshold coord that already exists on later cubes""" self.multiplier.add_aux_coord(self.threshold_aux) cubelist = iris.cube.CubeList([self.cube.copy(), self.multiplier]) msg = "Cannot broadcast to coord threshold as it already exists as an AuxCoord" with self.assertRaisesRegex(TypeError, msg): CubeMultiplier(broadcast_to_threshold=True)(cubelist, "new_cube_name")
def test_error_broadcast_coord_not_found(self): """Test that plugin throws an error if asked to broadcast to a threshold coord that is not present on the first cube""" cubelist = iris.cube.CubeList([self.multiplier, self.cube.copy()]) msg = ( r"Cannot find coord threshold in " r"<iris 'Cube' of " r"probability_of_lwe_thickness_of_precipitation_amount_above_threshold / \(1\) " r"\(realization: 3; latitude: 2; longitude: 2\)> to broadcast to") with self.assertRaisesRegex(CoordinateNotFoundError, msg): CubeMultiplier(broadcast_to_threshold=True)(cubelist, "new_cube_name")
def test_update_cell_methods(self): """Test that plugin updates cell methods where required when a new diagnostic name is provided.""" cubelist = iris.cube.CubeList([self.cube, self.multiplier]) new_cube_name = "new_cube_name" expected = CellMethod("sum", coords="time", comments=f"of {new_cube_name}") result = CubeMultiplier(broadcast_to_threshold=True)(cubelist, new_cube_name) self.assertEqual(result.cell_methods[0], expected)