def test_collapse_dims_with_weights(self): """Test function matches when the blend coordinate is first or second.""" coord = "forecast_reference_time" # Create a new axis. new_cube = add_coordinate(self.cube, [0.5], "height", coord_units="m") new_cube = iris.util.new_axis(new_cube, "height") plugin = WeightedBlendAcrossWholeDimension(coord) result_blend_coord_second = plugin.weighted_mean(new_cube, self.weights1d) # Reorder the axis to move the blending coordinate to the front. order = np.array([1, 0, 2, 3]) new_cube.transpose(order) result_blend_coord_first = plugin.weighted_mean(new_cube, self.weights1d) expected = np.full((2, 2), 1.5) self.assertIsInstance(result_blend_coord_first, iris.cube.Cube) self.assertIsInstance(result_blend_coord_second, iris.cube.Cube) self.assertArrayAlmostEqual( result_blend_coord_first.data, result_blend_coord_second.data ) self.assertArrayAlmostEqual(result_blend_coord_first.data, expected)
def test_with_weights(self): """Test function when a data cube and a weights cube are provided.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) result = plugin.weighted_mean(self.cube, self.weights1d) expected = np.full((2, 2), 1.5) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, expected)
def test_without_weights(self): """Test function when a data cube is provided, but no weights cube which should result in equal weightings.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) result = plugin.weighted_mean(self.cube, None) expected = np.full((2, 2), 2.0) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, expected)
def test_with_spatially_varying_weights(self): """Test function when a data cube and a multi dimensional weights cube are provided. This tests spatially varying weights, where each x-y position is weighted differently in each slice along the blending coordinate.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) result = plugin.weighted_mean(self.cube, self.weights3d) expected = np.array([[2.7, 2.1], [2.4, 1.8]]) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, expected)