;+
; Purpose: To make mission overview plots of all instruments
;
; Inputs:  PROBES: spacecraft ('a','b','c','d','e')
;          DATE: the date string or seconds since 1970 ('2007-03-23')
;          DUR: duration (default units are days)
;          DAYS: redundant keyword to set  the units of duration (but its comforting to have)
;          HOURS: keyword to make the duration be in units of hours
;          DEVICE: sets the device (x or z) (default is x)
;          MAKEPNG: keyword to generate 5 png files
;          DIRECTORY: sets the directory where the above pngs are placed (default is './')
;	   FEARLESS: keyword that prevents program from quitting when it fears its in an infinite loop
;			(infinite loop is feared when catch statement has been call 1000 times)
;	   DONT_DELETE_DATA:  keyword to not delete all existing tplot variables before loading data in for
;			        the overview plot (sometimes old variables can interfere with overview plot)
;
; Example: thm_gen_overplot,probe='a',date='2007-03-23',dur=1
;	   The above example will produce a full day plot in the X window.
;
;
;Version:
; $LastChangedBy$
; $LastChangedDate$
; $LastChangedRevision$
; $URL$
;-

pro thm_gen_overplot, probes=probes, date=date, dur=dur, days=days, hours=hours, device=device, directory=directory, makepng=makepng,fearless=fearless,dont_delete_data=dont_delete_data

;check some inputs
;-------------------------------------------------------

if keyword_set(probes) then sc=probes  ; quick change of variable name
vsc = ['a','b','c','d','e']
if not keyword_set(sc) then begin
  print,'You did not enter a spacecraft into the program call.'
  print, "Valid inputs are: 'a','b','c','d','e'  (ie, sc='b')"
  return
endif
if total(strmatch(vsc,strtrim(strlowcase(sc)))) gt 1 then begin
  print, 'This program is only designed to accept a single spacecraft as input.'
  print, "Valid inputs are: 'a','b','c','d','e'  (ie, sc='b')"
  return
endif
if total(strmatch(vsc,strtrim(strlowcase(sc)))) eq 0 then begin
  print,"The input sc= '",strtrim(sc),"' is not a valid input."
  print, "Valid inputs are: 'a','b','c','d','e'  (ie, sc='b')"
  return
endif

if not keyword_set(date) then begin
  print,'You did not enter a date into the program call.'
  print,"Example: thm_gen_overplot,sc='b',date='2007-03-23'"
  return
endif

if not keyword_set(dur) then dur=1

;-------------------------------------------------------


common overplot_position,load_position,error_count

!quiet=0
error_count=0


;catch statement to allow program to recover from errors
;-------------------------------------------------------

catch,error_status

if error_status ne 0 then begin

   error_count++
   if error_count ge 1000. and not keyword_set(fearless) then begin
     print, ' '
     print, 'The program is quitting because it fears its in an infinite loop.'
     print, 'To eliminate this fear add the keyword /fearless to the call.'
     return
   endif

   print, '***********Catch error**************'
   help, /last_message, output = err_msg
   For j = 0, n_elements(err_msg)-1 Do print, err_msg[j]
   print, 'load_position: ' , load_position
   
   case load_position of 
   	
   	'fgm'		: goto, SKIP_FGM_LOAD
   	'fbk'		: goto, SKIP_FBK_LOAD
   	'sst'		: goto, SKIP_SST_LOAD
   	'esa'		: goto, SKIP_ESA_LOAD
   	'esa_calc'	: goto, SKIP_ESA_CALC
   	'gmag'		: goto, SKIP_GMAG_LOAD
   	'asi'		: goto, SKIP_ASI_LOAD
   	'pos'		: goto, SKIP_POS_LOAD
   	'mode'		: goto, SKIP_SURVEY_MODE
   	'bound'		: goto, SKIP_BOUNDS
   	else		: goto, SKIP_DAY
   	
  endcase
  
endif

;-------------------------------------------------------

if keyword_set(hours) then dur=dur/24.
date=time_string(date)

if not keyword_set(dont_delete_data) then del_data,'*'   ; give ourselves a clean slate

timespan,date,dur

;load magnetic field fit data
;-----------------------------

load_position='fgm'

thm_load_state,probe=sc,/get_support
thm_load_fit,lev=1,probe=sc,/get_support

SKIP_FGM_LOAD:

;kluge to prevent missing data from crashing things
index_fit=where(strjoin('th'+sc+'_fgs') eq tnames())
index_state=where(strjoin('th'+sc+'_state_spinras') eq tnames())
if (index_fit(0) eq -1 or index_state(0) eq -1) then begin
  filler=fltarr(2,3)
  filler(*,*)=float('NaN')
  store_data,strjoin('th'+sc+'_fgs_gse'),data={x:time_double(date)+findgen(2),y:filler}
  ylim,strjoin('th'+sc+'_fgs_gse'),-100,100,0
endif else begin
  thm_cotrans,strjoin('th'+sc+'_fgs'),out_suf='_gse', in_c='dsl', out_c='gse'
endelse

;clip data
get_data,strjoin('th'+sc+'_fgs_gse'),data=d
index=where(d.y gt 100 or d.y lt -100)
if (index(0) ne -1) then d.y(index)=float('NaN')
store_data,strjoin('th'+sc+'_fgs_gse'),data=d

name='th'+sc+'_fgs_gse'
options,name,'ytitle','B FIT!CGSE (nT)'
options,name,'labels',['Bx','By','Bz']
options,name,'labflag',1
options,name,'colors',[2,4,6]


;load FBK data
;--------------

load_position='fbk'

thm_load_fbk,lev=1,probe=sc

SKIP_FBK_LOAD:

del_data,'th'+sc+'_fb_h*'  ; del thx_fb_hff as it is not used and gets in the way later
fbk_tvars=tnames('th'+sc+'_fb_*') ; this should give us two tplot variables (but sometimes more)

if n_elements(fbk_tvars) eq 1 then fbk_tvars=[fbk_tvars,'filler']  

for i=0,n_elements(fbk_tvars)-1 do begin


  ;kluge to prevent missing data from crashing things 
  get_data,fbk_tvars(i),data=dd
  if size(dd,/type) ne 8 then begin
    filler=fltarr(2,6)
    filler(*,*)=float('NaN')
    name=strcompress(strjoin('th'+sc+'_fb_'+string(i+1)),/remove_all)
    store_data,name,data={x:time_double(date)+findgen(2),y:filler,v:findgen(6)}
    options,name,'spec',1
    ylim,name,1,1000,1
    zlim,name,1,1000,1
  endif else begin
;    store_data,fbk_tvars(i),data={x:dd.x,y:dd.y,v:[2.,8.,32.,128.,512.,2048.]}
    store_data,fbk_tvars(i),data={x:dd.x,y:dd.y,v:[2048.,512.,128.,32.,8.,2.]}
    options,fbk_tvars(i),'spec',1
    options,fbk_tvars(i),'zlog',1
;    ylim,fbk_tvars(i),1,3000,1
    options,fbk_tvars(i),'ytitle','th'+sc+'!CFBK '+strmid(fbk_tvars(i),7)
;for ztitle, we need to figure out which type of data is there
;      for V channels, <|V|>.
;      for E channels, <|mV/m|>.
;      for SCM channels, <|nT|>.
    x1 = strpos(fbk_tvars[i], 'scm')
    If(x1[0] Ne -1) Then options, fbk_tvars[i], 'ztitle', '<|nT|>'
    xv = strpos(fbk_tvars[i], 'v')
    If(xv[0] Ne -1) Then options, fbk_tvars[i], 'ztitle', '<|V|>'
    xe = strpos(fbk_tvars[i], 'e')
    If(xe[0] Ne -1) Then options, fbk_tvars[i], 'ztitle', '<|mV/m|>'
  endelse
 
endfor


;load SST spectrograms 
;----------------------

load_position='sst'

thm_load_sst,probe=sc,lev=1
;mtypes = ['psif','psef']
;thm_part_spec_calc,probe=sc,types=mtypes, $
;  comps = ['density', 'velocity', 't3']
thm_part_moments, probe = sc, instrument = ['psif', 'psef'], $
  moments = ['density', 'velocity', 't3']

SKIP_SST_LOAD:

;kluge to prevent missing data from crashing things 
index_sst=where(strjoin('th'+sc+'_psif_en_eflux') eq tnames())
if index_sst eq -1 then begin
  filler=fltarr(2,16)
  filler(*,*)=float('NaN')
  store_data,strjoin('th'+sc+'_psif_en_eflux'),data={x:time_double(date)+findgen(2)*86400.,y:filler,v:findgen(16)}
  name=strjoin('th'+sc+'_psif_en_eflux')
  options,name,'spec',1
  ylim,name,1,1000,1
  zlim,name,1,1000,1
  options,name,'ytitle','th'+sc+'!CSST ions!Cenergy spec'
  options,name,'ztitle','Eflux !C eV/cm!U2!N!C-s-sr-eV'
  goto,SKIP_SST_IONS 
endif

;SST ion panel
name='th'+sc+'_psif_en_eflux'
options,name,'spec',1
options,name,'ytitle','th'+sc+'!CSST ions!Cenergy spec'
options,name,'ztitle','Eflux !C eV/cm!U2!N!C-s-sr-eV'
options,name,'y_no_interp',1
options,name,'x_no_interp',1

SKIP_SST_IONS: 

;kluge to prevent missing data from crashing things 
index_sst=where(strjoin('th'+sc+'_psef_en_eflux') eq tnames())
if index_sst eq -1 then begin
  filler=fltarr(2,16)
  filler(*,*)=float('NaN')
  store_data,strjoin('th'+sc+'_psef_en_eflux'),data={x:time_double(date)+findgen(2),y:filler,v:findgen(16)}
  name=strjoin('th'+sc+'_psef_en_eflux')
  options,name,'spec',1
  ylim,name,1,1000,1
  zlim,name,1,1000,1
  options,name,'ytitle','th'+sc+'!CSST elec!Cenergy spec'
  options,name,'ztitle','Eflux !C eV/cm!U2!N!C-s-sr-eV'
  goto,SKIP_SST_ELECTRONS  
endif

;SST electron panel
name='th'+sc+'_psef_en_eflux'
options,name,'spec',1
options,name,'ytitle','th'+sc+'!CSST elec!Cenergy spec'
options,name,'ztitle','Eflux !C eV/cm!U2!N!C-s-sr-eV'
options,name,'y_no_interp',1
options,name,'x_no_interp',1

SKIP_SST_ELECTRONS:

;load ESA spectrograms and moments
;----------------------------------

load_position='esa'

	thm_load_state,probe=sc
	thm_load_esa_pkt,probe=sc
	
SKIP_ESA_LOAD:

load_position='esa_calc'

; choose type of data - f-full, r-reduced, b-burst
	typ='f'
	if typ eq 'f' then gap_time=1000. else gap_time=10.

; decide if you want magnetosheath (sheath=1) or plasmasheet (sheath=0) limits, default is sheath=1
;	sheath=0
;	if sheath then nmax=100. else nmax=10.
	sheath=1
	nmax=100.
	emin=15

		
; ion plots
	
	;kluge to prevent missing data from crashing things
	index_esai=where(strjoin('th'+sc+'_pei'+typ+'_en_counts') eq tnames())
	if index_esai(0) eq -1 then begin 
	  filler=fltarr(2,32)
	  filler(*,*)=float('Nan')
	  name1=strjoin('th'+sc+'_pei'+typ+'_en_eflux')
	  store_data,name1,data={x:time_double(date)+findgen(2),y:filler,v:findgen(32)} 
	   zlim,name1,1.e3,1.e7,1
	   ylim,name1,3.,40000.,1
	   options,name1,'ztitle','Eflux !C!C eV/cm!U2!N!C-s-sr-eV'
	   options,name1,'ytitle','ESA i+ th'+sc+'!C!C eV'
	   options,name1,'spec',1
	  filler=fltarr(2)
	  filler(*)=float('Nan')
	  store_data,strjoin('th'+sc+'_pei'+typ+'_N'),data={x:time_double(date)+findgen(2),y:filler}
	   options,strjoin('th'+sc+'_pei'+typ+'_N'),'ytitle','Ni th'+sc+'!C!C1/cm!U3'
	  filler=fltarr(2,3)
	  filler(*,*)=float('Nan')
	  store_data,strjoin('th'+sc+'_pei'+typ+'_V'),data={x:time_double(date)+findgen(2),y:filler}
	   options,strjoin('th'+sc+'_pei'+typ+'_V'),'ytitle','Vi th'+sc+'!C!Ckm/s'
	  filler=fltarr(2,6)
	  filler(*,*)=float('Nan')
	  store_data,strjoin('th'+sc+'_pei'+typ+'_T'),data={x:time_double(date)+findgen(2),y:filler}
	   options,strjoin('th'+sc+'_pei'+typ+'_T'),'ytitle','Ti th'+sc+'!C!CeV'
	  goto,SKIP_ESA_IONS
	endif 

	get_dat=strjoin('th'+sc+'_pei'+typ)
	name1=strjoin('th'+sc+'_pei'+typ+'_en_eflux')
	get_en_spec,get_dat,units='eflux',retrace=1,name=name1,gap_time=gap_time,t1=t1,t2=t2
	zlim,name1,1.e3,1.e7,1
	ylim,name1,3.,40000.,1
	options,name1,'ztitle','Eflux !C!C eV/cm!U2!N!C-s-sr-eV'
	options,name1,'ytitle','ESA i+ th'+sc+'!C!C eV'
	options,name1,'spec',1
	options,name1,'x_no_interp',1
	options,name1,'y_no_interp',1

	name1=strjoin('th'+sc+'_pei'+typ+'_N')
	get_2dt,'n_3d',get_dat,name=name1,gap_time=gap_time,t1=t1,t2=t2,energy=[20.,21000.]
	ylim,name1,.1,nmax,1
	options,name1,'ytitle','Ni th'+sc+'!C!C1/cm!U3'
	
	name1=strjoin('th'+sc+'_pei'+typ+'_V')
	get_2dt,'v_3d',get_dat,name=name1,gap_time=gap_time,t1=t1,t2=t2,energy=[20.,21000.]
	ylim,name1,0,0,0
	options,name1,'colors',[2,4,6]
	options,name1,'labflag',1
	options,name1,'ytitle','Vi th'+sc+'!C!Ckm/s'
	options,name1,labels=['Vi!dx!n', 'Vi!dy!n', 'Vi!dz!n'],constant=0.

	name1=strjoin('th'+sc+'_pei'+typ+'_T')
	get_2dt,'t_3d',get_dat,name=name1,gap_time=gap_time,t1=t1,t2=t2,energy=[20.,21000.]
	ylim,name1,100,10000.,1
	if sheath then ylim,name1,10,10000.,1
	options,name1,'colors',[2,4,6,0]
	options,name1,'ytitle','Ti th'+sc+'!C!CeV'

 	SKIP_ESA_IONS:

; electron plots

	index_esae=where(strjoin('th'+sc+'_pee'+typ+'_en_counts') eq tnames())
	if index_esae(0) eq -1 then begin 
	  filler=fltarr(2,32)
	  filler(*,*)=float('Nan')
	  name1=strjoin('th'+sc+'_pee'+typ+'_en_eflux')
	  store_data,name1,data={x:time_double(date)+findgen(2),y:filler,v:findgen(32)} 
	   zlim,name1,1.e5,1.e9,1
	   ylim,name1,3.,40000.,1
	   options,name1,'ztitle','Eflux !C!C eV/cm!U2!N!C-s-sr-eV'
	   options,name1,'ytitle','ESA e- th'+sc+'!C!C eV'
	   options,name1,'spec',1
	  filler=fltarr(2)
	  filler(*)=float('Nan')
	  store_data,strjoin('th'+sc+'_pee'+typ+'_N'),data={x:time_double(date)+findgen(2),y:filler}
	   options,strjoin('th'+sc+'_pee'+typ+'_N'),'ytitle','Ne th'+sc+'!C!C1/cm!U3'
	  filler=fltarr(2,3)
	  filler(*,*)=float('Nan')
	  store_data,strjoin('th'+sc+'_pee'+typ+'_V'),data={x:time_double(date)+findgen(2),y:filler}
	   options,strjoin('th'+sc+'_pee'+typ+'_V'),'ytitle','Ve th'+sc+'!C!Ckm/s'
	  filler=fltarr(2,6)
	  filler(*,*)=float('Nan')
	  store_data,strjoin('th'+sc+'_pee'+typ+'_T'),data={x:time_double(date)+findgen(2),y:filler}
	   options,strjoin('th'+sc+'_pee'+typ+'_T'),'ytitle','Te th'+sc+'!C!CeV'
	  goto,SKIP_ESA_ELECTRONS
	endif 


	get_dat=strjoin('th'+sc+'_pee'+typ)
	name1=strjoin('th'+sc+'_pee'+typ+'_en_eflux')
	get_en_spec,get_dat,units='eflux',retrace=1,name=name1,gap_time=gap_time,t1=t1,t2=t2
	zlim,name1,1.e5,1.e9,1
	ylim,name1,3.,40000.,1
	options,name1,'ztitle','Eflux !C!C eV/cm!U2!N!C-s-sr-eV'
	options,name1,'ytitle','ESA e- th'+sc+'!C!C eV'
	options,name1,'spec',1
	options,name1,'x_no_interp',1
	options,name1,'y_no_interp',1

	name1=strjoin('th'+sc+'_pee'+typ+'_N')
	get_2dt,'n_3d',get_dat,name=name1,gap_time=gap_time,t1=t1,t2=t2,energy=[emin,27000.]
	ylim,name1,.1,nmax,1
	options,name1,'ytitle','Ne th'+sc+'!C!C1/cm!U3'
	
	name1=strjoin('th'+sc+'_pee'+typ+'_V')
	get_2dt,'v_3d',get_dat,name=name1,gap_time=gap_time,t1=t1,t2=t2,energy=[emin,27000.]
	ylim,name1,-500,500.,0
	ylim,name1,-200,200.,0
	if sheath then ylim,name1,-500,200.,0
	options,name1,'ytitle','Ve th'+sc+'!C!Ckm/s'
	;options,name1,'colors',[cols.blue,cols.green,cols.red]
	options,name1,labels=['V!dex!n', 'V!dey!n', 'V!dez!n'],constant=0.

	name1=strjoin('th'+sc+'_pee'+typ+'_T')
	get_2dt,'t_3d',get_dat,name=name1,gap_time=gap_time,t1=t1,t2=t2,energy=[emin,27000.]
	ylim,name1,100,10000.,1
	if sheath then ylim,name1,10,10000.,1
	options,name1,'colors',[2,4,6,0]
	options,name1,'ytitle','Te th'+sc+'!C!CeV'

	SKIP_ESA_ELECTRONS:

; plot quantities (manipulating the plot quantities for the sake of plot aesthetics)

	;kluge for labeling the density
	get_data,strjoin('th'+sc+'_pee'+typ+'_N'),data=d
	dummy=fltarr(n_elements(d.y),2)
	dummy(*,0)=d.y
	dummy(*,1)=d.y
	Ne_kluge_name=strjoin('Ne_th'+sc+'_pee'+typ+'_kluge')
	store_data,Ne_kluge_name,data={x:d.x,y:dummy}
	options,Ne_kluge_name,labels=['Ni','Ne']
	options,Ne_kluge_name,colors=[0,6]
	options,Ne_kluge_name,'labflag',1

	store_data,strjoin('th'+sc+'_Nie'),data=[strjoin('th'+sc+'_pei'+typ+'_N'),Ne_kluge_name]
	options,strjoin('th'+sc+'_Nie'),'ytitle','Ni,e th'+sc+'!C1/cm!U3'
	
    
	store_data,strjoin('th'+sc+'_Tie'),data=[strjoin('th'+sc+'_pei'+typ+'_T'),strjoin('th'+sc+'_pee'+typ+'_T')]
	options,strjoin('th'+sc+'_Tie'),'ytitle','Ti,e th'+sc

SKIP_ESA_CALC:

; load gmag data
;----------------
 
load_position='gmag'

thm_load_gmag,/subtract_median
split_vec,'thg_mag_????'

superpo_histo,'thg_mag_????_x', dif='thg_pseudoAE', res=60.0

tdespike_AE,-2000.0,1500.0
clean_spikes, 'thg_pseudoAE_despike', new_name = 'thg_pseudoAE', thresh = 5

options,'thg_pseudoAE',ytitle='THEMIS!CAE Index'   

SKIP_GMAG_LOAD:  

; load ASK data and plot 3 specific ones (can be changed)
;---------------------------------------------------------

load_position='asi'

	thm_load_ask,/verbose

SKIP_ASI_LOAD:	
	
	asi_sites=tnames('*ask*')
	
	filler=fltarr(2,10)      ; (10 chosen arbitrarily)
	filler(*,*)=float('NaN')
	
	if asi_sites(0) ne '' then copy_data,asi_sites(0),'Keogram' else store_data,'Keogram',data={x:time_double(date)+findgen(2),y:filler,v:findgen(10)}





; Get position info
;---------------------------------------------------------

load_position='mode'

thm_cotrans,strjoin('th'+sc+'_state_pos'),out_suf='_gse',in_coord='gei',out_coord='gse'
get_data,strjoin('th'+sc+'_state_pos_gse'),data=tmp
store_data,strjoin('th'+sc+'_state_pos_gse_x'),data={x:tmp.x,y:tmp.y(*,0)/6370.}
	options,strjoin('th'+sc+'_state_pos_gse_x'),'ytitle','th'+sc+'_X-GSE'
store_data,strjoin('th'+sc+'_state_pos_gse_y'),data={x:tmp.x,y:tmp.y(*,1)/6370.}
	options,strjoin('th'+sc+'_state_pos_gse_y'),'ytitle','th'+sc+'_Y-GSE'
store_data,strjoin('th'+sc+'_state_pos_gse_z'),data={x:tmp.x,y:tmp.y(*,2)/6370.}
	options,strjoin('th'+sc+'_state_pos_gse_z'),'ytitle','th'+sc+'_Z-GSE'

SKIP_POS_LOAD:

load_position='mode'

; make tplot variable tracking the sample rate (0=SS,1=FS,2=PB,3=WB)
;-------------------------------------------------------------------

thm_load_hsk,probe=sc,varformat='th'+sc+'*issr_mode*'
if tnames('th'+sc+'*issr_mode*') eq '' then store_data,strjoin('th'+sc+'_hsk_issr_mode_raw'),data={x:[time_double(date)],y:float('NaN')}
get_data,strjoin('th'+sc+'_hsk_issr_mode_raw'),data=d
ss_data=float(d.y)
index_ss_fill=where(d.y ne 0)
index_ss=where(d.y eq 0)
if (index_ss_fill(0) ne -1) then ss_data(index_ss_fill)=float('NaN')
if (index_ss(0) ne -1) then ss_data(index_ss)=0.

fs_data=float(d.y)
index_fs_fill=where(d.y ne 0 and d.y ne 1 and d.y ne 2 and d.y ne 3)
index_fs=where(d.y eq 0 or d.y eq 1 or d.y eq 2 Or d.y eq 3)
if (index_fs_fill(0) ne -1) then fs_data(index_fs_fill)=float('NaN')
if (index_fs(0) ne -1) then fs_data(index_fs)=0.

store_data,'slow_survey_bar',data={x:d.x,y:ss_data}
store_data,'fast_survey_bar',data={x:d.x,y:fs_data}
store_data,'aesthetic_bar',data={x:d.x,y:ss_data}

;get particle burst data from fgh level 2 data, jmm, 27-aug-2007
thm_load_fgm, probe = sc, level = 'l2', datatype = 'fgh'
tn = tnames('th'+sc+'*fgh*')
if tn[0] eq '' then begin ;no data
  store_data, 'particle_burst_bar', data = {x:time_double(date), y:float('NaN')}
  store_data, 'particle_burst_sym', data = {x:time_double(date), y:float('NaN')}
endif else begin
  get_data, strjoin('th'+sc+'_fgh_dsl'), data = d
  test_y = d.x
  pb_data = float(test_y)
  index_pb_fill = where(finite(test_y) Eq 0)
  index_pb = where(finite(test_y))
  if (index_pb_fill(0) ne -1) then pb_data(index_pb_fill) = float('NaN')
  if (index_pb(0) ne -1) then pb_data(index_pb) = 0.0

  pb_data2 = pb_data            ; pb_data2 is for symbols below bar
  if (index_pb(0) ne -1) then pb_data2(index_pb) = -1.0
  store_data, 'particle_burst_bar', data = {x:d.x, y:pb_data}
  store_data, 'particle_burst_sym', data = {x:d.x, y:pb_data2}
endelse

;wave bursts from level 1 ffw data
thm_load_fft, probe = sc, level = 'l1', varformat='th'+sc+'*ffw*'
tn = tnames('th'+sc+'*ffw*')
if tn[0] eq '' then begin
  store_data, 'wave_burst_bar', data = {x:time_double(date), y:float('NaN')}
  store_data, 'wave_burst_sym', data = {x:time_double(date), y:float('NaN')}
endif else begin
  tn = tn[0]                    ;making the assumption that all ffws will have the same time range?
  get_data, tn, data = d
  test_y = d.x                  ;use the times here
  wb_data = float(test_y)
  index_wb_fill = where(finite(test_y) Eq 0)
  index_wb = where(finite(test_y))
  if (index_wb_fill(0) ne -1) then wb_data(index_wb_fill) = float('NaN')
  if (index_wb(0) ne -1) then wb_data(index_wb) = 0.0
  
  wb_data2 = wb_data            ; wb_data2 is for symbols above bar
  if (index_wb(0) ne -1) then wb_data2(index_wb) = 1.0
  store_data, 'wave_burst_bar', data = {x:d.x, y:wb_data}
  store_data, 'wave_burst_sym', data = {x:d.x, y:wb_data2}
endelse

options,'aesthetic_bar','color',255
options,'slow_survey_bar','color',5
options,'fast_survey_bar','color',6
options,'particle_burst_bar','color',3
options,'particle_burst_sym','color',0
options,'wave_burst_bar','color',0
options,'wave_burst_sym','color',0

options,'slow_survey_bar','thick',5
options,'fast_survey_bar','thick',5
;options,'particle_burst_bar','thick',5
options,'particle_burst_bar','psym',6
options,'particle_burst_bar','symsize',0.1
options,'particle_burst_sym','psym',6
options,'particle_burst_sym','symsize',0.2
;options,'wave_burst_bar','thick',5
options,'wave_burst_bar','psym',6
options,'wave_burst_bar','symsize',0.1
options,'wave_burst_sym','psym',6
options,'wave_burst_sym','symsize',0.2

;store_data,'sample_rate',data=['aesthetic_bar','fast_survey_bar','slow_survey_bar','wave_burst_bar','particle_burst_bar','particle_burst_sym','wave_burst_sym']

store_data,'sample_rate',data=['aesthetic_bar','fast_survey_bar','slow_survey_bar','particle_burst_sym','wave_burst_sym']

ylim,'sample_rate',-1.1,1.1,0
options,'sample_rate','panel_size',0.2


SKIP_SURVEY_MODE:
load_position='bound'

; final tplot preparations
;--------------------------

;calculate ESA and SST min/max values on various intervals
esai_name=strjoin('th'+sc+'_pei'+typ+'_en_eflux')
esae_name=strjoin('th'+sc+'_pee'+typ+'_en_eflux')
ssti_name=strjoin('th'+sc+'_psif_en_eflux')
sste_name=strjoin('th'+sc+'_psef_en_eflux')

esasst_types=[esai_name,esae_name,ssti_name,sste_name]

particle_minmax_z=dblarr(4,5,2)  ; ([esai,esae,ssti,sste],[t0,t1,t2,t3,t4],[min,max])

for j=0,3 do begin

  get_data,esasst_types(j),data=d

  for i=0,4 do begin
  
    starttime=time_double(date)+i*3600.*6.
    endtime=starttime+3600.*6.
    
    if i eq 4 then begin
      starttime=time_double(date)
      endtime=starttime+3600.*24.
    endif

    index1=where(d.x ge starttime and d.x le endtime)
    if index1(0) ne -1 then begin
      data_clip=d.y(index1,*) 
      index2=where(data_clip gt 1)
    endif else index2=-1
  
    if index2(0) ne -1 then begin
      d_min=min(data_clip(index2)) 
      d_max=max(data_clip(index2))
    endif else begin 
      d_min=1. 
      d_max=100000. 
    endelse
    
    d_min=10.^round(alog10(d_min))
    d_max=10.^round(alog10(d_max))
  
    particle_minmax_z(j,i,0)=d_min
    particle_minmax_z(j,i,1)=d_max
    

  endfor ;i

endfor ;j

;make ESA and SST ions on same scale

for i=0,4 do begin
  particle_minmax_z(0,i,0)=min([particle_minmax_z(0,i,0),particle_minmax_z(2,i,0)])
  particle_minmax_z(0,i,1)=max([particle_minmax_z(0,i,1),particle_minmax_z(2,i,1)])
  particle_minmax_z(2,i,0)=particle_minmax_z(0,i,0)
  particle_minmax_z(2,i,1)=particle_minmax_z(0,i,1)
endfor


SKIP_BOUNDS:
load_position='plot'

; plot it!

;zlim,esai_name,particle_minmax_z(0,4,0),particle_minmax_z(0,4,1),1
;zlim,esae_name,particle_minmax_z(1,4,0),particle_minmax_z(1,4,1),1
;zlim,ssti_name,particle_minmax_z(2,4,0),particle_minmax_z(2,4,1),1
;zlim,sste_name,particle_minmax_z(3,4,0),particle_minmax_z(3,4,1),1

zlim,esai_name,1e3,1e7,1
zlim,esae_name,1e5,1e9,1
zlim,ssti_name,1e0,1e5,1
zlim,sste_name,1e0,1e5,1


tplot_options,'lazy_ytitle',0  ; prevent auto formatting on ytitle (namely having carrage returns at underscores)


if not keyword_set(device) then begin
  help,/device,output=plot_device
  plot_device=strtrim(strlowcase(strmid(plot_device(1),24)),2)
  if plot_device eq 'z' then device, set_resolution = [750, 800]
endif else begin 
  set_plot,device
  help,/device,output=plot_device
  plot_device=strtrim(strlowcase(strmid(plot_device(1),24)),2)
  if plot_device eq 'z' then device, set_resolution = [750, 800]
endelse


!p.background=255.
!p.color=0.
time_stamp,/off
loadct2,43
!p.charsize=0.8


	tplot,['thg_pseudoAE','Keogram', $
	       'th'+sc+'_fgs_gse','th'+sc+'_Nie','th'+sc+'_pei'+typ+'_V','th'+sc+'_Tie','sample_rate', $
	       'th'+sc+'_psif_en_eflux','th'+sc+'_pei'+typ+'_en_eflux','th'+sc+'_psef_en_eflux', $
	       'th'+sc+'_pee'+typ+'_en_eflux', 'th'+sc+'_fb_*','th'+sc+'_pos_gse_z'], $
	title='THEMIS '+strupcase(sc),var_label=['th'+sc+'_state_pos_gse_z','th'+sc+'_state_pos_gse_y','th'+sc+'_state_pos_gse_x']


; make pngs
;-----------


if keyword_set(makepng) then begin

  year=string(strmid(date,0,4))
  month=string(strmid(date,5,2))
  day=string(strmid(date,8,2))
  hour=string(strmid(date,11,2))
  if keyword_set(directory) then dir=directory else dir='./'
;  dir='/disks/danaid/home/www/html/mattd/THEMIS_WEB/PLOTS/'

  makepng,dir+'th'+sc+'_l2_overview_'+year+month+day+'_0024',/no_expose
  ;zlim,esai_name,particle_minmax_z(0,0,0),particle_minmax_z(0,0,1),1
  ;zlim,esae_name,particle_minmax_z(1,0,0),particle_minmax_z(1,0,1),1
  ;zlim,ssti_name,particle_minmax_z(2,0,0),particle_minmax_z(2,0,1),1
  ;zlim,sste_name,particle_minmax_z(3,0,0),particle_minmax_z(3,0,1),1

  tplot, trange=[time_double(date),time_double(date)+3600.*6.]
  makepng,dir+'th'+sc+'_l2_overview_'+year+month+day+'_0006',/no_expose

  ;zlim,esai_name,particle_minmax_z(0,1,0),particle_minmax_z(0,1,1),1
  ;zlim,esae_name,particle_minmax_z(1,1,0),particle_minmax_z(1,1,1),1
  ;zlim,ssti_name,particle_minmax_z(2,1,0),particle_minmax_z(2,1,1),1
  ;zlim,sste_name,particle_minmax_z(3,1,0),particle_minmax_z(3,1,1),1

  tplot, trange=[time_double(date)+3600.*6,time_double(date)+3600.*12.]
  makepng,dir+'th'+sc+'_l2_overview_'+year+month+day+'_0612',/no_expose

  ;zlim,esai_name,particle_minmax_z(0,2,0),particle_minmax_z(0,2,1),1
  ;zlim,esae_name,particle_minmax_z(1,2,0),particle_minmax_z(1,2,1),1
  ;zlim,ssti_name,particle_minmax_z(2,2,0),particle_minmax_z(2,2,1),1
  ;zlim,sste_name,particle_minmax_z(3,2,0),particle_minmax_z(3,2,1),1

  tplot,trange=[time_double(date)+3600.*12,time_double(date)+3600.*18.]
  makepng,dir+'th'+sc+'_l2_overview_'+year+month+day+'_1218',/no_expose

  ;zlim,esai_name,particle_minmax_z(0,3,0),particle_minmax_z(0,3,1),1
  ;zlim,esae_name,particle_minmax_z(1,3,0),particle_minmax_z(1,3,1),1
  ;zlim,ssti_name,particle_minmax_z(2,3,0),particle_minmax_z(2,3,1),1
  ;zlim,sste_name,particle_minmax_z(3,3,0),particle_minmax_z(3,3,1),1

  tplot,trange=[time_double(date)+3600.*18,time_double(date)+3600.*24.]
  makepng,dir+'th'+sc+'_l2_overview_'+year+month+day+'_1824',/no_expose

endif ; makepng


SKIP_DAY:

end