; Comments added by Erik Johnson 7/16/07
; five_sec_avg_fast_avg.pro, compiles and runs with idl
; called by five_sec_avg.pro which is used in the 1/2 sec to 5 sec avg step
; inputs b1 & b2
; returns b_avg
function five_sec_avg_fast_avg,b1,b2
; # of sec in a day is 864000
b_tmp = fltarr(86400*2)
t1 = lindgen(86400)*2
t2 = lindgen(86400)*2+1
b_tmp(t1) = b1
b_tmp(t2) = b2
; now all b1 and b2 for the same day
; are adjacent in the b_tmp array
; Always skip the first five values in front
; and the fifteen values in the back
; (see in five_sec_avg.pro where the values
; are printed), so basically two 5 sec intervals
; are missing in the final result
b_tmp2 = reform(b_tmp(6:172785),10,17278)
; now b_tmp2 includes 10x17278 array, i.e. all
; the values from the a 5 sec interval are
; grouped together
; Nb. Mean of each row is then the 5 sec avg
; Nb. The bad value is 32767000L in the raw data.
; As the derived data is scaled with 1/1000 that
; gives this derived bad value
bad = b_tmp2 eq 32767
badsums = float(total(bad,1))/10.
bad = where(b_tmp2 eq 32767,count)
if count ne 0 then b_tmp2(bad)=!values.f_nan
; Nb. Strictly speaking the next line is not valid
; as it is illdefined, when badsums = 10,
; i.e. all the data points in the interval bad.
; This is recovered from in the next to last step
; by setting the average of any interval with more
; than 4 bad points to '32767' the designated bad value.
b_avg = total(b_tmp2,1,/nan)/(10.-badsums*10.)
bad = where(badsums ge 0.4,count)
if count ne 0 then b_avg(bad) = 32767.
return,b_avg
end

