AreaWeighted: Calculate the spatial area-weighted average of...

View source: R/AreaWeighted.R

AreaWeightedR Documentation

Calculate the spatial area-weighted average of multidimensional arrays.

Description

This function computes a spatial area-weighted average of n-dimensional arrays given a spatial mask containing the area (e.g. in m^2) for each grid box.

Usage

AreaWeighted(data, area, lon_dim = "lon", lat_dim = "lat", ncores = NULL)

Arguments

data

A multidimensional array with named dimensions (at least lon_dim and lat_dim) containing climate data.

area

A multidimensional array with named dimensions (at least lon_dim and lat_dim) containing area of each grid box. The resolution, length and order of the lon_dim and lat_dim dimensions should be identical as for data. Missing values are allowed and treated as 0s.

lon_dim

A character string indicating the name of the longitudinal dimension. The default value is 'lon'.

lat_dim

A character string indicating the name of the latitudinal dimension. The default value is 'lat'.

ncores

An integer indicating the number of cores to use for parallel computation. The default value is NULL.

Value

An array with the same dimensions as data, except with lon_dim and lat_dim removed. Any extra dimensions in area not present in data are preserved in the output (e.g., region dimension).

Examples

data <- array(data = 1:10, dim = c(year = 10, lon = 5, lat = 3))
area <- array(data = 1:10, dim = c(lon = 5, lat = 3))
AreaWeighted(data, area)
# With extra region dimension
area <- array(data = 1:10, dim = c(lon = 5, lat = 3, region = 4))
AreaWeighted(data, area)


CSTools documentation built on Nov. 14, 2025, 5:07 p.m.