def test_basic_with_Combine(self): """Test that the basic test also works through the Combine plugin.""" plugin = Combine("+", new_name="new_cube_name") cubelist = iris.cube.CubeList([self.cube1, self.cube2]) input_copy = deepcopy(cubelist) result = plugin.process(cubelist) self.assertIsInstance(result, Cube) self.assertEqual(result.name(), "new_cube_name") expected_data = np.full((2, 2), 1.1, dtype=np.float32) self.assertArrayAlmostEqual(result.data, expected_data) self.assertCubeListEqual(input_copy, cubelist)
def test_with_Combine(self): """Test plugin works through the Combine plugin""" cubelist = iris.cube.CubeList([self.cube.copy(), self.multiplier]) result = Combine("*", broadcast_to_threshold=True, new_name="new_cube_name")(cubelist) 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)
def test_filtering_realizations(realization_cubes, short_realizations): """Run Combine with minimum_realizations and a realization time series where 0 or more are short of the final time step""" if short_realizations == 0: cubes = realization_cubes expected_realization_points = [0, 1, 2, 3] else: cubes = CubeList(realization_cubes[:-short_realizations]) cubes.append(realization_cubes[-short_realizations][:-1]) expected_realization_points = [0, 1, 2, 3][:-short_realizations] result = Combine("+", broadcast_to_threshold=False, minimum_realizations=1, new_name="name")(cubes) assert isinstance(result, Cube) assert np.allclose( result.coord("realization").points, expected_realization_points) assert np.allclose(result.data, 3)
def test_init(operation, expected_instance, minimum_realizations, broadcast_to_threshold): """Ensure the class initialises as expected""" result = Combine( operation, broadcast_to_threshold=broadcast_to_threshold, minimum_realizations=minimum_realizations, new_name="name", ) assert isinstance(result.plugin, expected_instance) assert result.new_name == "name" assert result.minimum_realizations == minimum_realizations assert result.broadcast_to_threshold == broadcast_to_threshold if broadcast_to_threshold and isinstance(result.plugin, CubeMultiplier): assert result.plugin.broadcast_to_threshold == broadcast_to_threshold
def process( *cubes: cli.inputcube, operation="+", new_name=None, broadcast_to_threshold=False, minimum_realizations=None, ): r"""Combine input cubes. Combine the input cubes into a single cube using the requested operation. The first cube in the input list provides the template for output metadata. If coordinates are expanded as a result of this combine operation (e.g. expanding time for accumulations / max in period) the upper bound of the new coordinate will also be used as the point for the new coordinate. Args: cubes (iris.cube.CubeList or list of iris.cube.Cube): An iris CubeList to be combined. operation (str): An operation to use in combining input cubes. One of: +, -, \*, add, subtract, multiply, min, max, mean new_name (str): New name for the resulting dataset. broadcast_to_threshold (bool): If True, broadcast input cubes to the threshold coord prior to combining - a threshold coord must already exist on the first input cube. minimum_realizations (int): If specified, the input cubes will be filtered to ensure that only realizations that include all available lead times are combined. If the number of realizations that meet this criteria are fewer than this integer, an error will be raised. Returns: result (iris.cube.Cube): Returns a cube with the combined data. """ from iris.cube import CubeList from improver.cube_combiner import Combine return Combine( operation, broadcast_to_threshold=broadcast_to_threshold, minimum_realizations=minimum_realizations, new_name=new_name, )(CubeList(cubes))
def test_empty_cubelist(): """Ensure supplying an empty CubeList raises an error""" with pytest.raises(TypeError, match="A cube is needed to be combined."): Combine("+")(CubeList())
def test_minimum_realizations_exceptions(minimum_realizations, error_class, msg, realization_cubes): """Ensure specifying too few realizations will raise an error""" with pytest.raises(error_class, match=msg): Combine("+", minimum_realizations=minimum_realizations)(realization_cubes)