; $Id$ ;----------------------------------------------------------------------- ;+ ; NAME: ; BOXCAR ; ; PURPOSE: ; Calculate a boxcar average (i.e. running mean) of an ; array. ; ; CATEGORY: ; ; CALLING SEQUENCE: ; RESULT = BOXCAR( Array, Width [,keywords] ) ; ; INPUTS: ; Array - 1D array of values to average ; Width - number of elements to include in the average. Can be ; odd or even. ; ; KEYWORD PARAMETERS: ; CENTER - If used, then the averaging window for each element ; of the output array will be centered on the ; respective element of the input array. ; BACKWARD-Averaging window will include trailing elements. ; FORWARD -Averaging window will include leading elements. ; ; OUTPUTS: ; RESULT - 1D array with averages. Length will be same as input ; ARRAY. Elements on either end may be NaN ; ; SUBROUTINES: ; ; REQUIREMENTS: ; ; NOTES: ; A centered boxcar average with even widths is traditionally ; not allowed. This BOXCAR program allows even widths by ; giving half weights to elements on either end of the ; averaging window and full weights to all others. A centered ; average with even width therefore uses uses WIDTH+1 ; elements in its averaging kernel. ; ; EXAMPLE: ; ; MODIFICATION HISTORY: ; cdh, 31 Aug 2011: VERSION 1.00 ; ;- ; Copyright (C) 2011, Christopher Holmes, UC Irvine ; This software is provided as is without any warranty whatsoever. ; It may be freely used, copied or distributed for non-commercial ; purposes. This copyright notice must be kept with any copy of ; this software. If this software shall be used commercially or ; sold as part of a larger package, please contact the author. ; Bugs and comments should be directed to cdholmes@post.harvard.edu ; with subject "IDL routine boxcar" ;----------------------------------------------------------------------- function boxcar, Array, Width, $ Center=Center, $ Backward=Backward, $ Forward=Forward BACKWARD = keyword_set( BACKWARD ) FORWARD = keyword_set( FORWARD ) CENTER = keyword_set( CENTER ) ; Make sure that only one orientation keyword is used if ( total( [ CENTER, BACKWARD, FORWARD ] ) gt 1 ) then $ message,'Only one of the following keywords may be used: '+$ 'Center, Forward, Backward' ; Default to use Centered window if ( ~BACKWARD and ~FORWARD ) then CENTER = 1L N = n_elements( Array ) ; Smoothed array, initialize ArrayS = fltarr( N ) ArrayS[*] = !values.f_nan ; Setup for backward boxcar if keyword_set( Backward ) then begin ; Half widths before and after element I HW1 = Width-1L HW2 = Width - HW1 - 1L ; Uniform averaging kernel kernel = fltarr( Width ) + 1d0 endif ; Setup for forward boxcar if keyword_set( Forward ) then begin ; Half widths before and after element I HW1 = 0L HW2 = Width - HW1 - 1L ; Uniform averaging kernel kernel = fltarr( Width ) + 1d0 endif ; Setup for centered boxcar if keyword_set( Center ) then begin ; Separate treatments for odd and even boxcar widths if ( (Width mod 2) eq 1) then begin ; ODD WIDTHS ; Half widths before and after element I HW1 = floor( Width / 2. ) HW2 = Width - HW1 - 1L ; Uniform averaging kernel kernel = fltarr( Width ) + 1d0 endif else begin ; EVEN WIDTHS ; Half widths before and after after element I ; These are symmetric HW1 = Width / 2. HW2 = Width - HW1 ; Uniform kernel in middle kernel = fltarr( Width+1L ) + 1d0 ; Half weight kernel for ends kernel[0] = 0.5 kernel[Width] = 0.5 ; Normalize kernel kernel = kernel / total(kernel) endelse endif ; Do boxcar for I = HW1, N-HW2-1L do begin ; Sub array that we are operating on sub = Array[I-HW1:I+HW2] ; Kernel average, only over finite elements ; (NaNs removed from numerator and denominator) ArrayS[I] = total( sub * kernel, /nan ) / $ total( kernel[ where(finite(sub)) ] ) endfor return, ArrayS end