
dim_avg_wgt
Computes the weighted average of a variable's rightmost dimension at all other dimensions.
Prototype
function dim_avg_wgt ( x : numeric, w [*] : numeric, opt [1] : integer ) return_val : float or double
Arguments
xA variable of numeric type and any dimensionality. Missing values are indicated by the _FillValue attribute.
wA one dimensional array of weights. The length must the same as the right dimension of x
optA scalar: (a) opt=0 means compute the weighted average only if all values are not missing; (b) opt=1 means compute the weighted mean of all non-missing values; (c) opt>1 means to return the mean only if the number of non-missing values is greater-then or equal to opt.
Return value
The output will be double if x is double, and float otherwise.
The output dimensionality is the same as the first n-2 dimensions of the input variable. That is, the dimension rank of the input variable will be reduced by one.
Description
The dim_avg_wgt function computes the weighted average of all elements of the n-1th (rightmost) dimension for each index of the dimensions 0...n-2.
Basically, the following is done for the rightmost dimension.
xAvg = SUM [x*w]/SUM[w]The weighting is strictly positional. EG: If x@_FillValue=1e20, and the rightmost dimension is of size 5 and the values are (/3.7, 1e20, 14.3, 1e20, 7.1 /) with weights (/1,3,5,3,1/) then the result will be
(3.7*1 + 14.3*5 + 7.1*1)/(1+5+1) = 11.757
Use dim_avg_wgt_n if you want to specify which dimension to do the weighted average across.
Use the dim_avg_wgt_Wrap function if metadata retention is desired. The interface is identical.
See Also
dim_avg_wgt_Wrap, dim_avg_wgt_n, dim_avg, avg, dim_median, dim_stddev, dim_num, dim_product, dim_rmsd, dim_rmvmean, dim_rmvmed, dim_standardize, dim_stat4, dim_stddev, dim_sum, dim_variance
Examples
Example 1
Let z(12,nlat,mlon) contain monthly means and with named dimensions "time", "lat" and "lon", respectively. Compute the weighted annual mean at each lat/lon point. Use dimension reordering to make time the rightmost dimension.
Note: Use dim_avg_wgt_n to avoid having to reorder your data.
wgt = (/0.08493151, 0.076712325, 0.08493151, 0.08219178 \ ; same size as dimension "time" ,0.08493151, 0.08219178 , 0.08493151, 0.08493151 \ ,0.08219178, 0.08493151 , 0.08219178, 0.08493151 /) zAnn = dim_avg_wgt( z(lat|:,lon|:,time|:), wgt, 0 ) ; ==> zAnn(nlat,mlon) ; Use dim_avg_wgt_Wrap if metadata retention is desired ; zAnn = dim_avg_wgt_Wrap( z(lat|:,lon|:,time|:), wgt, 0 ) ; ==> zAnn(nlat,mlon) zAnn = dim_avg_wgt_n( z, wgt, 0, 0) ; no reordering needed ; Use dim_avg_wgt_n_Wrap if metadata retention is desired ; zAnn = dim_avg_wgt_n_Wrap( z, wgt, 0, 0) ; no reordering needed
Example 2
Let T(time,lev,lat,lon). Compute the weighted vertical average total at each time/lat/lon point. Use dimension reordering to make level the rightmost dimension.
Note: Use dim_avg_wgt_n to avoid having to reorder your data.
wgt = (/50,50,100,100,100,50,25,10/) ; same size as dimension "lev" Tavg = dim_avg_wgt( T(time|:,lat|:,lon|:,lev|:), wgt, 0 ) ; ==> Tavg(time,lat,lon) ; Use dim_avg_wgt_Wrap if metadata retention is desired ; Tavg = dim_avg_wgt_Wrap( T(time|:,lat|:,lon|:,lev|:), wgt, 0 ) ; ==> Tavg(time,lat,lon) Tavg = dim_avg_wgt_n( T, wgt, 0, 1 ) ; no reordering needed ; Use dim_avg_wgt_n_Wrap if metadata retention is desired ; Tavg = dim_avg_wgt_n_Wrap( T, wgt, 0, 1 ) ; no reordering needed