"""Generic calculus functions."""
import numpy as np
__all__ = ("integrate",)
[docs]def integrate(cube, coord):
"""
Integrate the cube along a 1D coordinate using the trapezoidal rule.
Note: `coord` must be one of the dimensional coordinates of the cube.
Parameters
----------
cube: iris.cube.Cube
Input cube containing the given coordinate.
coord: str or iris.coords.Coord
Coordinate for integration.
Returns
-------
iris.cube.Cube
integrated cube.
"""
# TODO: allow non-dim coordinates
c = cube.coord(coord)
others = [dc.name() for dc in cube.dim_coords if cube.coord_dims(dc) != cube.coord_dims(c)]
dim = cube.coord_dims(c)[0]
data = np.trapz(cube.data, c.points, axis=dim)
res = next(cube.slices(others)).copy(data=data)
res.units = cube.units * c.units
res.remove_coord(c)
res.rename(f"integral_of_{cube.name()}_wrt_{c.name()}")
# ensure_bounds(cube, [c])
# delta = iris.coords.AuxCoord(c.bounds[:, 1] - c.bounds[:, 0], units=c.units)
# res = iris.analysis.maths.multiply(cube, delta, dim=dim).collapsed(c, iris.analysis.SUM)
return res