Pro thm_write_probe_timing_tables, ticks_table_file, sectors_table_file, TEST=TEST, NO_PNG=NO_PNG
if (N_Params() lt 2) then message,'Usage: thm_write_probe_timing_tables, ticks_table_file, sectors_table_file, [/TEST], [/NO_PNG]'
!p.color = 0
!p.background = 16777215
phases_files = file_Search('phases_*.txt')
if keyword_set(test) then begin
p410 = where(phases_files eq 'phases_410.txt')
n = n_elements(phases_files)-1
phases_files = [ phases_files[0:p410 - 1], phases_files[p410 + 1:n] ]
endif
ticks = strarr(256)
for i = 0 , 255 do ticks[i] = 'Tick'+(string(i))+':'+string(9B)
sectors = strarr(n_elements(phases_files))
for j = 0,n_elements(phases_files)-1 do begin
phases_file = phases_files[j]
phases = read_ascii(phases_file)
apid = strmid(phases_file,7,3)
sectors[j] = 'Apid '+ apid +':'+string(9B)
onesecmark = round(256d*phases.field1[1,*])
h_mark = histogram(onesecmark,min=0,max=255)
ticks_found = where(abs(h_mark-mean(h_mark)) gt 1.*stddev(h_mark))
if (min(ticks_found) eq -1) then n = 0 else n = n_elements(ticks_found)
if keyword_set(test) then begin
for i = 0,(n-1) do ticks(ticks_found[i]) = ticks(ticks_found[i]) + apid + ' '
endif else begin
if max(n eq [1,2,8,16,32]) then begin
for i = 0,(n-1) do ticks(ticks_found[i]) = ticks(ticks_found[i]) + apid + ' '
endif
endelse
spin = 32d*phases.field1[2,*]
h_spin = histogram(spin,min=0,max=31)
sectors_found = where(h_spin gt 2.*stddev(h_spin))
n = n_elements(sectors_found)
sectors_found = strtrim(string(sectors_found),2)
if (n lt 20) then begin
for i = 0,(n-1) do sectors[j] = sectors[j] + ' ' + sectors_found[i]
endif
if keyword_set(no_png) then begin dprint, 'Apid: ',strtrim(apid,2),', Ticks: ',strtrim(n_elements(ticks_found),2),', Sectors: ',strtrim(n_elements(sectors_found),2)
endif else begin
time_array = reform(double(phases.field1[0,*]))
store_data,'apid_'+apid+'_1secmark_ticks',data={x:time_array,y:reform(double(round(256d*phases.field1[1,*])))}
store_data,'apid_'+apid+'_spin_phase_sectors',data={x:time_array,y:reform(double(32d*phases.field1[2,*]))}
store_data,'apid_'+apid+'_delta_phi',data={x:time_array,y:reform(double(phases.field1[3,*]))}
tplot_options,title='APID: '+apid+' Phases'
options,'apid_'+apid+'_1secmark_ticks',yrange=[0d,256d],psym=3,ytitle='OneSecMark Ticks [0,255]'
options,'apid_'+apid+'_spin_phase_sectors',yrange=[0d,32d],psym=3,ytitle='Spin Phase Sectors [0,31]'
options,'apid_'+apid+'_delta_phi',psym=3,yrange=[-360d,360d],ytitle='Delta Phi [degrees]'
tplot,['apid_'+apid+'_1secmark_ticks','apid_'+apid+'_spin_phase_sectors','apid_'+apid+'_delta_phi']
makepng,'phases_'+apid
store_data,['apid_'+apid+'_1secmark_ticks','apid_'+apid+'_spin_phase_sectors','apid_'+apid+'_delta_phi'],/delete
endelse
endfor
openw,1,ticks_table_file
for i = 0, 255 do printf,1,ticks[i]
close,1
openw,2,sectors_table_file
for i = 0, (n_elements(phases_files)-1) do printf,2,sectors[i]
close,2
end