Gradient#

Spatial and temporal gradient calculation utilities for atmospheric and climate data.

Spatial Gradients#

calculate_gradient(field, coordinates, axis=-1, radius=None)[source]#

Calculate gradient of an arbitrary dimensional array along specified coordinates

Parameters:
  • field (ndarray) – Data field for gradient calculation, can be any dimensional array, e.g., (time, level, lat, lon)

  • coordinates (ndarray) – Coordinate array along which to calculate the gradient, such as latitude or longitude values

  • axis (int) – Specifies the dimensional axis for gradient calculation, defaults to the last dimension (-1)

  • radius (float | None) – Earth radius, default is None. If provided, coordinates are treated as latitude in degrees and converted to distance in meters. If None, coordinates are used directly.

Returns:

Gradient field with the same shape as the input field

Return type:

ndarray

calculate_meridional_gradient(field, latitudes, lat_axis=-1, radius=6371000.0)[source]#

Calculate meridional gradient (gradient along latitude direction)

Parameters:
  • field (ndarray) – Data field for gradient calculation, can be any dimensional array

  • latitudes (ndarray) – Latitude array (degrees)

  • lat_axis (int) – Specifies the axis for latitude, defaults to the last dimension (-1)

  • radius (float) – Earth radius, default is 6371000.0 meters

Returns:

Meridional gradient field

Return type:

ndarray

calculate_zonal_gradient(field, longitudes, latitudes, lon_axis=-1, lat_axis=-2, radius=6371000.0)[source]#

Calculate zonal gradient (gradient along longitude direction)

Parameters:
  • field (ndarray) – Data field for gradient calculation, can be any dimensional array

  • longitudes (ndarray) – Longitude array (degrees)

  • latitudes (ndarray) – Latitude array (degrees), used to calculate actual distance between longitudes at different latitudes

  • lon_axis (int) – Specifies the axis for longitude, defaults to the last dimension (-1)

  • lat_axis (int) – Specifies the axis for latitude, defaults to the second-to-last dimension (-2)

  • radius (float) – Earth radius, default is 6371000.0 meters

Returns:

Zonal gradient field

Return type:

ndarray

calculate_vertical_gradient(field, pressure, pressure_axis=-3)[source]#

Calculate vertical gradient (gradient along pressure direction)

Parameters:
  • field (ndarray) – Data field for gradient calculation

  • pressure (ndarray) – Pressure array (Pa), must be monotonically decreasing

  • pressure_axis (int) – Specifies the axis for pressure, defaults to the third-to-last dimension (-3)

Returns:

Vertical gradient field

Return type:

ndarray

Example Usage#

import skyborn as skb
import xarray as xr

# Load atmospheric data
data = xr.open_dataset('temperature_data.nc')
temp = data['temperature']

# Calculate meridional temperature gradient
meridional_grad = skb.calculate_meridional_gradient(temp)

# Calculate zonal temperature gradient
zonal_grad = skb.calculate_zonal_gradient(temp)

# Calculate vertical gradient (if pressure levels available)
vertical_grad = skb.calculate_vertical_gradient(temp, data['pressure'])
    data['temperature'],
    data['latitude']
)

# Calculate temporal gradient
temp_grad = skb.temporal_gradient(
    data['temperature'],
    data['time']
)