; Comments added by Erik Johnson 7/17/07
; sec_to_min.pro, compiles and runs with idl
; it is used in the 5 second to 1 minute average step
; needs read_maccs.pro & write_maccs.pro to run
; input must be 5 second average files (.5s files)
; output is 1 minute average files (.1m files)
; example file names: input: CD04024.5s output: CD04024.1m
; to compile: .compile sec_to_min
; to run: sec_to_min, files="/path_to_input_file(s)/name_of_input_file(s)", out_dir="/path_to_output_directory/" (you can use * and ?)
; Example: five_sec_avg, files="/Volumes/physics_data/Ftp/MACCS_DATA/Unrotated/5sec/CD/2005/*.5s", out_dir="/Volumes/physics_data/Ftp/MACCS_DATA/Unrotated/5sec/CD/2005/"
pro sec_to_min, files=files, out_dir=out_dir
if (not keyword_set(files)) and (not keyword_set(out_dir)) then begin
print,'Enter files=search string and out_dir=output directory'
print,'Also, you must complie first with .r sec_to_min'
stop
endif
; This program makes 1 min averages from 5 sec MACCS data 10/30/01
print,'running sec_to_min ver 1.0'
data_files = findfile(files,count=count)
if count eq 0 then begin
print,'No files found in '+in_dir+' with '+files
stop
endif
for i = 0, count-1 do begin
read_maccs,data_files(i),time,bx,by,bz,/nt
bx_avg = fast_avg(bx)
by_avg = fast_avg(by)
bz_avg = fast_avg(bz)
; plot,bx_avg,max_val=30000,/ystyle
dot_pos=strpos(data_files(i),'.')
new_file = out_dir+$
strmid(data_files(i),dot_pos-7,8)+'1m'
write_maccs,new_file,[32767,bx_avg,32767],$
[32767,by_avg,32767],$
[32767,bz_avg,32767],type='1mg'
endfor
end
function fast_avg,b
b_tmp = reform(b(12:17267),12,1438)
bad = b_tmp eq 32767
badsums = float(total(bad,1))/12.
bad = where(b_tmp eq 32767,count)
if count ne 0 then b_tmp(bad)=!values.f_nan
b_avg = total(b_tmp,1,/nan)/(12.-badsums*12.)
bad = where(badsums ge 0.4,count)
if count ne 0 then b_avg(bad) = 32767.
return,b_avg
end

