diff --git a/improver/nbhood/nbhood.py b/improver/nbhood/nbhood.py index e37ef38933..4abd8459b3 100644 --- a/improver/nbhood/nbhood.py +++ b/improver/nbhood/nbhood.py @@ -382,7 +382,7 @@ def _do_nbhood_sum( if size != data.size: # Determine default array for the extremes around the edges, or everywhere if isinstance(when_all_extremes, np.ndarray): - untrimmed = when_all_extremes + untrimmed = when_all_extremes.copy() else: untrimmed = np.full(data_shape, when_all_extremes) if size: diff --git a/improver_tests/nbhood/nbhood/test_NeighbourhoodProcessing.py b/improver_tests/nbhood/nbhood/test_NeighbourhoodProcessing.py index 2f41f36720..9d2c75b6a2 100644 --- a/improver_tests/nbhood/nbhood/test_NeighbourhoodProcessing.py +++ b/improver_tests/nbhood/nbhood/test_NeighbourhoodProcessing.py @@ -152,6 +152,27 @@ def test_basic_circular(self): result = plugin._calculate_neighbourhood(self.data) self.assertArrayAlmostEqual(result.data, expected_array) + def test_edge_circular(self): + """Test the _calculate_neighbourhood method with a circular neighbourhood that crosses the + edge. The zero is now in the left column and the "nearest" method means that this zero + is repeated in the sum, so the final calculation is 3 / 5 instead of 4 / 5.""" + data = np.ones_like(self.data) + data[:, :3] = self.data[:, 2:] + expected_array = np.array( + [ + [1.0, 1.0, 1.0, 1.0, 1.0], + [0.8, 1.0, 1.0, 1.0, 1.0], + [0.6, 0.8, 1.0, 1.0, 1.0], + [0.8, 1.0, 1.0, 1.0, 1.0], + [1.0, 1.0, 1.0, 1.0, 1.0], + ] + ) + plugin = NeighbourhoodProcessing("circular", self.RADIUS) + plugin.kernel = self.circular_kernel + plugin.nb_size = max(plugin.kernel.shape) + result = plugin._calculate_neighbourhood(data) + self.assertArrayAlmostEqual(result.data, expected_array) + def test_basic_weighted_circular(self): """Test the _calculate_neighbourhood method with a weighted circular neighbourhood."""