; Comments added by Erik Johnson 7/5/07
; igloolik_despike.pro, compiles and runs with idl
; needs read_maccs.pro & write_maccs.pro to run
; input must be cleaned MACCS binary .s2 data file(s) (it accepts original files & despiked files)
; output is a despiked binary version of the cleaned data
; if the output directory is the same as the input directory the original cleaned file(s) is/are replaced by the despiked version(s)
; example file names: if input file is CD05004.s2, output file is CD05004.s2
; to compile & run: igloolik_despike, "name_of_input_file(s)", "/path_to_input_file(s)/", "/path_to_output_directory/" (you can use * and ?)
; to display windows with graphs (after the first 2 graphs appear, close the windows (bottom graph first) to display the next graph): view="something" (WARNING: If the input file has not been despiked and the view parameter is set, it will display hundreds of graphs that you have to click through one at a time before it outputs a despiked file. Also, if you click through the graphs too quickly it will have a bus error and exit IDL.)
; the spikeview parameter is not used in the program
; Example: igloolik_despike, "CD05*.s2", "/Volumes/physics_data/Ftp/MACCS_DATA/Clean/CD/2005/", "/Volumes/physics_data/Ftp/MACCS_DATA/Despiked/CD/2005/", view="view"
PRO igloolik_despike,File_Specification,in_dir,out_dir,view=view,spikeview=spikeview
!p.multi=[0,0,3]
window,0
window,1
; OLD files=findfile(in_dir+file)
files=FINDFILE(FILEPATH(File_Specification, ROOT_DIR = in_dir))
FOR j = 0, n_elements(files)-1 DO BEGIN
read_maccs,files(j),t,bx,by,bz,bx2=bx2,by2=by2,bz2=bz2,tf1=tf1,/nt
x_diff = abs(bx(1:86399) - bx(0:86398))
y_diff = abs(by(1:86399) - by(0:86398))
z_diff = abs(bz(1:86399) - bz(0:86398))
spikes = where((x_diff gt 10 and x_diff lt 10000) $
or (y_diff gt 10 and y_diff lt 10000) $
or (z_diff gt 10 and z_diff lt 10000))
index=where(spikes gt 11 and spikes lt 86388,count)
if count lt 2 then goto, next
spikes=spikes(index)
spike_diff = spikes(1:n_elements(spikes)-1) - spikes(0:n_elements(spikes)-2)
repeats=where(spike_diff gt 2)
spikes=spikes(repeats+1)
if keyword_set(view) then begin
wset,0
plot,bx,max_val=30000,/ystyle
for i = 0, n_elements(spikes)-1 do $
oplot,[spikes(i),spikes(i)],[-20000,bx(spikes(i))-20],linestyle=2
for i = 0, n_elements(spikes)-1 do $
oplot,[spikes(i),spikes(i)],[bx(spikes(i))+20,20000],linestyle=2
plot,by,max_val=30000,/ystyle
for i = 0, n_elements(spikes)-1 do $
oplot,[spikes(i),spikes(i)],[-20000,by(spikes(i))-20],linestyle=2
for i = 0, n_elements(spikes)-1 do $
oplot,[spikes(i),spikes(i)],[by(spikes(i))+20,20000],linestyle=2
plot,bz,min_val=40000,/ystyle
for i = 0, n_elements(spikes)-1 do $
oplot,[spikes(i),spikes(i)],[-20000,bz(spikes(i))-20],linestyle=2
for i = 0, n_elements(spikes)-1 do $
oplot,[spikes(i),spikes(i)],[bz(spikes(i))+20,60000],linestyle=2
wset,1
for k = 0, n_elements(spikes)-1 do begin
print,spikes(k)
if (spikes(k) gt 101 and spikes(k) lt 86298) then begin
plot,bx(spikes(k)-100:spikes(k)+100),max_val=30000,/ystyle
oplot,[90,90],[-20000,20000],linestyle=2
oplot,[110,110],[-20000,20000],linestyle=2
plot,by(spikes(k)-100:spikes(k)+100),max_val=30000,/ystyle
plot,bz(spikes(k)-100:spikes(k)+100),min_val=40000,/ystyle
cursor,x,y,/up
endif
; read,junk
endfor
endif
;plot,bx(spikes(25)-100:spikes(25)+100),max_val=30000,/ystyle
;oplot,[spikes(25),spikes(25)],[-20000,20000],linestyle=2
;read,junk
for i = 0, n_elements(spikes)-1 do begin
bx(spikes(i)-10:spikes(i)+10)=32767
by(spikes(i)-10:spikes(i)+10)=32767
bz(spikes(i)-10:spikes(i)+10)=32767
bx2(spikes(i)-10:spikes(i)+10)=32767
by2(spikes(i)-10:spikes(i)+10)=32767
bz2(spikes(i)-10:spikes(i)+10)=32767
endfor
files(j)=strmid(files(j),strpos(files(j),'.')-7,10)
; OLD print,out_dir+files(j)
; OLD write_maccs,out_dir+files(j),bx,by,bz,bx2=bx2,by2=by2,bz2=bz2,tf1=tf1,type='s2'
PRINT,FILEPATH(files(j), ROOT_DIR = out_dir)
write_maccs,FILEPATH(files(j), ROOT_DIR = out_dir) ,bx,by,bz,bx2=bx2,by2=by2,bz2=bz2,tf1=tf1,type='s2'
next:
ENDFOR
;stop
!p.multi=0
END

