diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 2247469f56..65fab445fc 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -1929,7 +1929,7 @@ def cell(self, index): return Cell(point, bound) - def collapsed(self, dims_to_collapse=None): + def collapsed(self, dims_to_collapse=None, points_func=None): """ Returns a copy of this coordinate, which has been collapsed along the specified dimensions. @@ -2003,7 +2003,10 @@ def serialize(x): ], axis=-1, ) - points = al.array(bounds.sum(axis=-1) * 0.5, dtype=self.dtype) + if points_func is None: + points = al.array(bounds.sum(axis=-1) * 0.5, dtype=self.dtype) + else: + points = points_func(self.core_points(), axis=dims_to_collapse) # Create the new collapsed coordinate. coord = self.copy(points=points, bounds=bounds) @@ -2497,8 +2500,10 @@ def __getitem__(self, key): coord.circular = self.circular and coord.shape == self.shape return coord - def collapsed(self, dims_to_collapse=None): - coord = Coord.collapsed(self, dims_to_collapse=dims_to_collapse) + def collapsed(self, dims_to_collapse=None, points_func=None): + coord = Coord.collapsed( + self, dims_to_collapse=dims_to_collapse, points_func=points_func + ) if self.circular and self.units.modulus is not None: bnds = coord.bounds.copy() bnds[0, 1] = coord.bounds[0, 0] + self.units.modulus diff --git a/lib/iris/cube.py b/lib/iris/cube.py index f6b4ec4d38..8c6253da39 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -3414,7 +3414,7 @@ def __neg__(self): # END OPERATOR OVERLOADS - def collapsed(self, coords, aggregator, **kwargs): + def collapsed(self, coords, aggregator, points_funcs=dict(), **kwargs): """ Collapse one or more dimensions over the cube given the coordinate/s and an aggregation. @@ -3568,7 +3568,11 @@ def collapsed(self, coords, aggregator, **kwargs): for dim in dims_to_collapse if dim in coord_dims ] - collapsed_cube.replace_coord(coord.collapsed(local_dims)) + collapsed_cube.replace_coord( + coord.collapsed( + local_dims, points_func=points_funcs.get(coord.name()) + ) + ) untouched_dims = sorted(untouched_dims)