# wgt_vertical_n

Calculate a weighted vertical average and/or sum (integral).

*Available in version 6.4.0 and later.*

## Prototype

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" ; This library isautomatically loaded; from NCL V6.2.0 onward. ; No need for user to explicitly load. function wgt_vertical_n ( x : numeric, dp : numeric, dim [1] : integer, iopt : integer ) return_val [dimsizes(x)] : float or double

## Arguments

*x*

Array to be integrated or averaged. No missing data allowed.

*dp*

Pressure thicknesses computed by **dpres_hybrid_ccm** or
**dpres_plevel**. These are the 'weights' associated with each level.

*dim*

The dimension(s) of x on which to calculate the vertical sum or average.

*iopt*

- iopt=0 weighted vertical average
- iopt=1 weighted vertical sum
- iopt=2 return a variable of type 'list' containing the weighted vertical sum, vertical average and the weights used.

## Return value

If *opt*=0 or 1, an array of of one less rank than the input *x*.
If *opt*=2, a of type 'list' containing the weighted vertical sum and vertical average.
The output will be double if *x* or *dp* is of type double.
Appropriate meat data are also returned.

## Description

Calculate the weighted vertical average and/or sum along a specific dimension.

vertical_average__X = SUM[x*dp]/SUM[dp] ; iopt=0vertical_integral_X = SUM[x*dp] ; iopt=1

**NOTE:**This function is documented as weighted vertical sum/average but it can be used for any weighted quantity along any coordinate. It was developed to isolate vertical sum/average so it is documented as such.

## See Also

## Examples

**Example 1:** A sounding with specific humidity at standard pressure levels.

ptop = 0 ; integrate ptop==>psfc at each grid point ; user could specify (say) 525 hPa. psfc = 1008.0 plev = (/1000.,950.,900.,850.,800.,750.,700.,650.,600. \ , 550.,500.,450.,400.,350.,300.,250.,200. \ , 175.,150.,125.,100., 80., 70., 60., 50. \ , 40., 30., 25., 20. /) plev@units = "hPa" q =(/ 19.03,16.14,13.71,11.56,9.80,8.33,6.75,6.06,5.07 \ , 3.88, 3.29, 2.39, 1.70,1.00,0.60,0.20,0.00,0.00 \ , 0.00, 0.00, 0.00, 0.00,0.00,0.00,0.00,0.00,0.00 \ , 0.00, 0.00 /) q@units = "g/kg" ; layer thickness dp =dpres_plevel(plev, psfc, ptop, 0) ;Integrate: vopt = 0 ; vertically weighted average vavg =wgt_vertical_n(q, dp, vopt, 0) ;wgt_vertical_n(q, dp, vopt, 0) ;print("vsum="+vsum) ;print("========") ;or vopt = 2 ; vertically weighted sum (integral) and average vAvgSumWgt =wgt_vertical_n(q, dp, vopt, 0) ; extract from list variable [convenience only] vAvg = vAvgSumWgt[0] vSum = vAvgSumWgt[1] vWgt = vAvgSumWgt[2]

**Example 2:** Similar to Example 1 but q(time,lev,lat,lon) and, psfc(time,lat,lon).
Note the *psfc* could also be constant. Here, set *ptop* to the highest level (lowest pressure).

f =addfile("...", "r") psfc = f->PSFC ; psfc(time,lat,lon) q = f->Q ; q(time,plev,lat,lon) ; dimension (0,1,2,3) plev = f->plev ; q&plev ptop =min(plev) ; 300==>psfc at each grid point ; layer thicknesses dp =dpres_plevel_Wrap(plev, psfc, ptop, 0) vopt = 0 ; vertically weighted average over the level (1-th dimension) vavg =wgt_vertical_n(q, dp, vopt,1) ; (time,lat,lon) vavg@long_name = "Q: vertically weighted average" vavg@long_name = q@units vopt = 1 ; vertically weighted sum vavg =wgt_vertical_n(q, dp, vopt,1) ; (time,lat,lon) vavg@long_name = "Q*DP: vertically weighted sum" vavg@long_name = "("+q@units" - "+psfc@units+")"