# WeightedMean: Calculate spatial area-weighted average of multidimensional... In ClimProjDiags: Set of Tools to Compute Various Climate Indices

## Description

This function computes a spatial area-weighted average of n-dimensional arrays being possible to select a region and to add a mask to be applied when computing the average.

## Usage

 ```1 2``` ```WeightedMean(data, lon, lat, region = NULL, mask = NULL, londim = NULL, latdim = NULL) ```

## Arguments

 `data` An array with minimum two dimensions of latitude and longitude. `lon` Numeric vector of longitude locations of the cell centers of the grid of `data`. This vector must be the same length as the longitude dimension in the parameter `data`. `lat` Numeric vector of latitude locations of the cell centers of the grid of `data`. This vector must be the same length as the latitude dimension in the parameter `data`. `region` A vector of length four indicating the minimum longitude, the maximum longitude, the minimum latitude and the maximum latitude of the region to be averaged. `mask` A matrix with the same spatial dimensions of `data`. It can contain either a) TRUE where the value at that position is to be accounted for and FALSE where not, or b) numeric values, where those greater or equal to 0.5 are to be accounted for, and those smaller are not. Attention: if the longitude and latitude dimensions of the data and mask coincide in length, the user must ensure the dimensions of the mask are in the same order as the dimensions in the array provided in the parameter `data`. `londim` An integer number indicating the position of the longitude dimension in the `data` object. `latdim` An integer number indicating the position of the latitude dimension in the `data` object.

## Value

An array, matrix or vector containig the area-weighted average with the same dimensions as `data`, except for the spatial longitude and latitude dimensions, which disappear.

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30``` ```##Example synthetic data 1: data <- 1:(2 * 3 * 4 * 5) dim(data) <- c(lon = 2, lat = 3, time = 4, model = 5) lat <- c(1, 10, 20) lon <- c(1, 10) a <- WeightedMean(data = data, lon = lon, lat = lat, region = NULL, mask = NULL, londim = 1, latdim = 2) str(a) mask <- c(0, 1, 0, 1, 0, 1) dim(mask) <- c(lon = 2, lat = 3) a <- WeightedMean(data = data, lon = lon, lat = lat, region = NULL, mask = mask, londim = 1, latdim = 2) str(a) region <- c(1, 10, 1, 10) a <- WeightedMean(data = data, lon = lon, lat = lat, region = region, mask = mask, londim = 1, latdim = 2) str(a) ##Example synthetic data: data <- 1:(2 * 3 * 4) dim(data) <- c(lon = 2, lat = 3, time=4) lat <- c(1, 10, 20) lon <- c(1, 10) a <- WeightedMean(data = data, lon = lon, lat = lat, region = NULL, mask = NULL, londim = 1, latdim = 2) str(a) ```

### Example output

``` num [1:4, 1:5] 3.46 9.46 15.46 21.46 27.46 ...
num [1:4, 1:5] 3.96 9.96 15.96 21.96 27.96 ...
num [1:4, 1:5] 3 9 15 21 27 33 39 45 51 57 ...
num [1:4] 3.46 9.46 15.46 21.46
```

ClimProjDiags documentation built on Jan. 7, 2021, 5:10 p.m.