NCL Home > Documentation > Functions > General applied math

# dim_sum_wgt_n

Computes the weighted sum of a variable's given dimension at all other dimensions.

## Prototype

```	function dim_sum_wgt_n (
x       : numeric,
w   [*] : numeric,
opt  : integer,
dim     : integer
)

return_val  :  float or double
```

## Arguments

x

A variable of numeric type and any dimensionality. Missing values are indicated by the _FillValue attribute.

w

A one dimensional array of weights. The length must be the same as the dim-th dimension of x.

opt

A scalar: (a) opt=0 means compute the weighted sum only if all values are not missing; (b) opt=1 means compute the weighted sum of all non-missing values; (c) opt>1 means to return the sum only if the number of non-missing values is greater-than or equal to opt.

dim

A scalar, the dimension of "x" of which to do the weighted sum across.

## Return value

The output will be double if x is double, and float otherwise.

The output dimensionality will be the same as all but the dim-th dimension of the input variable. The dimension rank of the input variable will be reduced by one.

## Description

The dim_sum_wgt_n function computes the weighted sum of all elements of the dim-th dimension for each index of the remaining dimensions.

Basically, the following is done:

```
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) = 82.3
```

Use the dim_sum_wgt_n_Wrap function if metadata retention is desired. The interface is identical.

## Examples

Example 1

Let z(12,nlat,mlon) contain monthly means and with named dimensions "time", "lat" and "lon", respectively. Compute the weighted annual sum (total) at each lat/lon point.

```    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 /)
pTot = dim_sum_wgt_n(precip, wgt, 0, 0)    ; ==> pTot(nlat,mlon)

; Use dim_sum_wgt_n_Wrap if metadata retention is desired
; pTot = dim_sum_wgt_n_Wrap(precip, wgt, 0, 0)    ; ==> pTot(nlat,mlon)
```

Example 2

Let T(time,lev,lat,lon). Compute the weighted vertical sum total at each time/lat/lon point.

```    wgt = (/50,50,100,100,100,50,25,10/) ; same size as dimension "lev"

Tsum = dim_sum_wgt_n( T, wgt, 0, 1 )   ; ==> Tsum(time,lat,lon)

; Use dim_sum_wgt_n_Wrap if metadata retention is desired
; Tsum = dim_sum_wgt_n_Wrap( T, wgt, 0, 1 )   ; ==> Tsum(time,lat,lon)
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.63724405,"math_prob":0.9989873,"size":2728,"snap":"2022-05-2022-21","text_gpt3_token_len":853,"char_repetition_ratio":0.16042584,"word_repetition_ratio":0.068292685,"special_character_ratio":0.33064517,"punctuation_ratio":0.22797927,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9997034,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-23T12:27:39Z\",\"WARC-Record-ID\":\"<urn:uuid:de0913ea-b488-404a-abed-b39b2108d2e4>\",\"Content-Length\":\"19118\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9948c949-6e48-4339-875a-1967873b14b7>\",\"WARC-Concurrent-To\":\"<urn:uuid:e53eb040-c593-4ea5-a2f4-e7c62c4d4c21>\",\"WARC-IP-Address\":\"128.117.225.48\",\"WARC-Target-URI\":\"http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_sum_wgt_n.shtml\",\"WARC-Payload-Digest\":\"sha1:PY3CFTCUGBIUDL6G6RZP3N3DA3OH42XV\",\"WARC-Block-Digest\":\"sha1:AWL5RPDGC7HVSLHYC5EP3HBE4G6DFMHA\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662558015.52_warc_CC-MAIN-20220523101705-20220523131705-00732.warc.gz\"}"}