spatial_average#

spatial_average(data, dxdy, mask_ocean=None, lon_t=None, lat_t=None, lon_min=None, lon_max=None, lat_min=None, lat_max=None)[source]#

Compute an area-weighted spatial mean on a possibly non-regular grid, supporting both 2D and 3D data (time, lat, lon), geographic subsetting, and masking (e.g., land/ocean separation).

Parameters:
  • data (np.ndarray) – 2-D array [Y, X] or 3-D array [T, Y, X] containing the field to average. NaNs are ignored.

  • dxdy (np.ndarray) – Grid-cell weights or areas. Must match or broadcast to spatial shape [Y, X].

  • mask_ocean (np.ndarray, optional) – Ocean mask where valid (ocean) cells are 1 and land cells are 0. Must match spatial shape [Y, X]. Only points with mask == 1 are used.

  • lon_t (np.ndarray, optional) – Longitudes of grid. Required if lon/lat bounds are used.

  • lat_t (np.ndarray, optional) – Latitudes of grid. Required if lon/lat bounds are used.

  • lon_min (float, optional) – Geographic subset boundaries.

  • lon_max (float, optional) – Geographic subset boundaries.

  • lat_min (float, optional) – Geographic subset boundaries.

  • lat_max (float, optional) – Geographic subset boundaries.

Returns:

Weighted spatial mean. If data is 2D → scalar; if 3D → 1D array [T].

Return type:

np.ndarray or float