gradient#

metpy.calc.gradient(f, axes=None, coordinates=None, deltas=None)[source]#

Calculate the gradient of a scalar quantity, assuming Cartesian coordinates.

Works for both regularly-spaced data, and grids with varying spacing.

Either coordinates or deltas must be specified, or f must be given as an xarray.DataArray with attached coordinate and projection information. If f is an xarray.DataArray, and coordinates or deltas are given, f will be converted to a pint.Quantity and the gradient returned as a tuple of pint.Quantity, otherwise, if neither coordinates nor deltas are given, the attached coordinate information belonging to axis will be used and the gradient will be returned as a tuple of xarray.DataArray.

Parameters:
  • f (array-like) – Array of values of which to calculate the derivative

  • axes (Sequence[str] or Sequence[int], optional) – Sequence of strings (if f is a xarray.DataArray and implicit conversion to pint.Quantity is not used) or integers that specify the array axes along which to take the derivatives. Defaults to all axes of f. If given, and used with coordinates or deltas, its length must be less than or equal to that of the coordinates or deltas given. In general, each axis can be an axis number (integer), dimension coordinate name (string) or a standard axis type (string). The current standard axis types are ‘time’, ‘vertical’, ‘y’, and ‘x’.

  • coordinates (array-like, optional) – Sequence of arrays containing the coordinate values corresponding to the grid points in f in axis order.

  • deltas (array-like, optional) – Sequence of arrays or scalars that specify the spacing between the grid points in f in axis order. There should be one item less than the size of f along the applicable axis.

Returns:

tuple of array-like – The first derivative calculated along each specified axis of the original array

Notes

If this function is used without the axes parameter, the length of coordinates or deltas (as applicable) should match the number of dimensions of f.

This will not give projection-correct results for horizontal geospatial fields. Instead, for vector quantities, use vector_derivative, and for scalar quantities, use geospatial_gradient.

Changed in version 1.0: Changed signature from (f, **kwargs)

Examples using metpy.calc.gradient#

Gradient

Gradient