# wgt_runave

Calculate a weighted running average. Missing values are allowed

Note: This function is only available in version 4.1.1 of NCL. If your site is licensed for version 4.1, then you can get version 4.1.1 for free. To get version 4.1.1 of NCAR Graphics software, please contact your site representative. If you don't know who your site representative is, then send email to ncarginf@ucar.edu or call (303) 497-1201.

## Synopsis

```    function wgt_runave(
x    : float,
wgt  : float,
kopt : integer
)
```

## Arguments

x
An array with one or more dimensions. The rightmost dimension will be the dimension on which the weighted running average is performed. Missing values should be indicated by x@_FillValue. If x@_FillValue is not set, then the NCL default (-999) will be assumed.
wgt
Vector containing all the weights
kopt
End-point option (kopt = 0 is the most frequently used)

In the following, N=last point in the series:

```kopt < 0 : utilize cyclic conditions
e.g.,  nwgt=3
x(0) = w(0)*x(N)  +w(1)*x(0)+w(2)*x(1)
x(N) = w(0)*x(N-1)+w(1)*x(N)+w(2)*x(0)
e.g.,  nwgt=4 at n=1 and n=N
x(0) = w(0)*x(N-1)+w(1)*x(N)  +w(2)*x(0)+w(3)*x(1)
x(N) = w(0)*x(N-2)+w(1)*x(N-1)+w(2)*x(N)+w(3)*x(0)

kopt = 0 : set unsmoothed beginning and end pts to x@_FillValue [most common]
e.g.,  nwgt=3 , x(0) = x@_FillValue and x(N) = x@_FillValue
, x(1) = w(0)*x(0)+w(1)*x(1)+w(2)*x(2)
e.g.,  nwgt=4 , x(0),x(1),x(N-1) and x(N) = x@_FillValue
, x(2) = w(0)*x(0)+w(1)*x(1)+w(2)*x(2)+w(3)*x(3)

kopt > 0 : utilize reflective (symmetric) conditions
e.g.,  nwgt=3
x(0) = w(0)*x(1)  +w(1)*x(0)+w(2)*x(1)
x(N) = w(0)*x(N-1)+w(1)*x(N)+w(2)*x(N-1)
e.g.,  nwgt=4
x(0) = w(0)*x(2)  +w(1)*x(1)  +w(2)*x(0)+w(3)*x(1)
x(N) = w(0)*x(N-2)+w(1)*x(N-1)+w(2)*x(N)+w(3)*x(N-1)
```

## Description

wgt_runave returns a float array of the same dimensionality as x with the last dimension smoothed. The running average is a special case of a filter where all weights are the same. The filter is applied to the i-th time of the requested series as follows:
F(i) = SUM{w(j)*UF(i-(nwgt/2)+j-1)} from j=0,nwgt-1
where F is the filtered field, UF is the unfiltered field, w(j) is the j-th filter weight, and nwgt is the number of weights.

With the proper choice of weights, this "filter" can also be used to compute time differences and derivatives.

If the number of weights is even, the filter's center falls between series elements. In this case, the center is shifted one-half of a time increment towards the latter element.

## Example 1

Let x be dimensioned nlat x mlon x ktimes where nlat=64, mlon=128, ktimes=1000. Perform a 3 point (1-2-1) average and use kopt=0. Return the nlat x mlon smoothed series to the original x array:

```    x = wgt_runave (x, (/ 0.25, 0.50, 0.25 /), 0)
```

## Example 2

Let x be dimensioned ntimes x nlat x mlon with named dimensions "time" , "lat" , "lon". (Assume the "time" dimension contains monthly data.) Use the 11-point Trenberth filter on the monthly data [Trenberth, K.E.: Mon. Wea. Review, February 1984]. The Trenberth filter effectively removes fluctuations with periods of less than 8 months but includes all others. At 24 months 80% of the variance is retained.

```    kopt = 0
wgt  = (/ 0.0270, 0.05856, 0.09030, 0.11742, 0.13567, \
0.1421, 0.13567, 0.11742, 0.09030, 0.05856, 0.027 /)
y    = wgt_runave (x(lat|:,lon|:,time|:), wgt, kopt)
```
y will be a 3-dimensional array of length nlat x mlon x time.

## Example 3

Let x be dimensioned ntimes x klev x nlat x mlon with named dimensions "time" , "lev" , "lat" , "lon". (Assume the "time" dimension contains twice daily data.) Use the low, medium and high pass 31-point Blackmon filters on the twice daily data. [Blackmon, M.B.: J . Atmos. Sci., August, 1976, p1609]. The low-pass filter allows most of the variance from fluctuations of 10 days or more; the medium pass filter allows variance of 2.5 to 6 days; and the high pass filter allows variance 1 to 2 days.

```    kopt  = 0
wlow  = (/-0.0059591606, -0.0074864031, -0.0081766107, -0.0074769889 \
,-0.0048789007,  0.0         ,  0.0073407153,  0.0170670479 \
, 0.0288191067,  0.0419626250,  0.0556356004,  0.0688283154 \
, 0.0804876892,  0.0896329731,  0.0954676702 \
, 0.0974726419 \                              ; central wgt
, 0.0954676702,  0.0896329731,  0.0804876892 \
, 0.0688283154,  0.0556356004,  0.0419626250,  0.0288191067 \
, 0.0170670479,  0.0073407153,  0.0         , -0.0048789007 \
,-0.0074769889,  -0.0081766107,-0.0074864031, -0.0059591606 /)
ylow  = wgt_runave (x(lev|:,lat|:,lon|:,time|:), wlow, kopt)

wmed  = (/-0.0030384857, -0.0001341773, -0.0096723016, -0.0191709641 \
,-0.0020017146,  0.0304306715,  0.0328072034,  0.0041075557 \
, 0.0033466748,  0.0419335015,  0.0283041151, -0.0923257264 \
,-0.1947701551, -0.1020097578,  0.1433496840 \
, 0.2776877534 \                              ; central weight
, 0.1433496840, -0.1020097578, -0.1947701551 \
,-0.0923257264,  0.0283041151,  0.0419335015,  0.0033466748 \
, 0.0041075557,  0.0328072034,  0.0304306715, -0.0020017146 \
,-0.0191709641, -0.0096723016, -0.0001341773, -0.0030384857 /)
ymed  = wgt_runave (x(lev|:,lat|:,lon|:,time|:), wmed, kopt)

whigh = (/ 0.0036193743,  0.0062672457, -0.0071490567, -0.0089978990 \
, 0.0125704103,  0.0117924147, -0.0207251125, -0.0144542141 \
, 0.0333056699,  0.0167834343, -0.0546750050, -0.0185972473 \
, 0.1009810886,  0.0197489990, -0.3186000638 \
, 0.4762599236 \  ; central weight
,-0.3186000638,  0.0197489990,  0.1009810886 \
,-0.0185972473, -0.0546750050,  0.0167834343,  0.0333056699 \
,-0.0144542141, -0.0207251125,  0.0117924147,  0.0125704103 \
,-0.0089978990, -0.0071490567,  0.0062672457,  0.0036193743 /)
yhigh = wgt_runave (x(lev|:,lat|:,lon|:,time|:), whigh, kopt)
```
y will be a 4-dimensional array of length klev x nlat x mlon x time.

In the above example it might be better to create a new variable xnew rather than reorder the dimensions each time. For example, if you are going to apply this filter for low, medium, and high filters, it will be more efficient to create a new variable tnew:

```  tnew = t(lev|:,lat|:,lon|:,time|:)
```
and then use tnew in the calls (be sure to delete tnew after finishing).

## Example 4

Let x be dimensioned ntimes x klev x nlat x mlon. Perform a 5 point running average use the cyclic option in the longitude direction:

```     kopt = -1
wgt  = (/ 1., 3., 5., 3., 1. /)
wgt  = wgt/sum(wgt)             ; normalize
x    = wgt_runave (x,wgt, kopt)
```

### Reference Manual Control Panel

\$Revision: 1.4 \$ \$Date: 1999/03/22 20:49:12 \$