Calculate a weighted vertical average and/or sum (integral).
Available in version 6.4.0 and later.
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" function wgt_vertical_n ( x : numeric, dp : numeric, dim  : integer, iopt : integer ) return_val [dimsizes(x)] : float or double
Array to be integrated or averaged. No missing data allowed.dp
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.
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.
Calculate the weighted vertical average and/or sum along a specific dimension.
vertical_average__X = SUM[x*dp]/SUM[dp] ; iopt=0 vertical_integral_X = SUM[x*dp] ; iopt=1NOTE: 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.
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) ;print("vavg="+vavg) ;print("========") ;or vopt = 1 ; vertically weighted sum (integral) vsum = 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 vSum = vAvgSumWgt vWgt = vAvgSumWgt
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+")"