pro stlethet_get_spec, sat, sep_sdaytim, sep_edaytim, bkg_stime, bkg_etime, used_channel, display=display, evid=evid, comment=comment

display=keyword_set(display)


usedch_filehead='_ch'
for j=0, n_elements(used_channel)-1 do begin
   case used_channel[j] of
      10: usedch_filehead=usedch_filehead+'A'
      11: usedch_filehead=usedch_filehead+'B'
      12: usedch_filehead=usedch_filehead+'C'
      13: usedch_filehead=usedch_filehead+'D'
      14: usedch_filehead=usedch_filehead+'E'
      else: usedch_filehead=usedch_filehead+string(used_channel[j], format='(i1)')
   endcase
endfor


if size(evid,/type) eq 0 then evid='000'

;sat='sta'
;sep_sdaytim='2011/03/21 01:30'
;sep_edaytim='2011/03/28 12:00'
;bkg_stime='2011/03/20 00:00'
;bkg_etime='2011/03/20 12:00'
;used_channel=[4,5,6,7,8,9,10,11,12,13,14]


sdaytim=y_time_keisan(min([sep_sdaytim,bkg_etime]), -24, form=11)
edaytim=y_time_keisan(sep_edaytim, +24, form=11)
trange=[sdaytim, edaytim]


p0=read_stereolethet_genx(trange,sat)


for t=2,2 do begin
   etime_ok=1
   sep_stime=sep_sdaytim   
   case t of
      0: begin
         fext='_et001p'
         ss=where(p0.daytim ge sep_sdaytim and p0.daytim le sep_edaytim)
         i3max=max(p0(ss).p10int, maxindex)
         max_daytim=p0(ss(maxindex)).daytim
         ss=where(p0.daytim ge max_daytim and p0.p10int le i3max*0.01, count)
         if count ne 0 then begin
            sep_etime=min(p0(ss).daytim)
         endif else begin
            sep_etime=sep_edaytim
            etime_ok=0
         endelse
      end
      1: begin
         fext='_et010p'
         ss=where(p0.daytim ge sep_sdaytim and p0.daytim le sep_edaytim)
         i3max=max(p0(ss).p10int, maxindex)
         max_daytim=p0(ss(maxindex)).daytim
         ss=where(p0.daytim ge max_daytim and p0.p10int le i3max*0.10)

         if count ne 0 then begin
            sep_etime=min(p0(ss).daytim)
         endif else begin
            sep_etime=sep_edaytim
            etime_ok=0
         endelse
      end
      2: begin
         fext='_etbkg'
         sep_etime=sep_edaytim
      end
      3: begin
         fext='_et050p'
         ss=where(p0.daytim ge sep_sdaytim and p0.daytim le sep_edaytim)
         i3max=max(p0(ss).p10int, maxindex)
         max_daytim=p0(ss(maxindex)).daytim
         ss=where(p0.daytim ge max_daytim and p0.p10int le i3max*0.50)
         sep_etime=min(p0(ss).daytim)
      end
   endcase

   sep_dur=round((anytim2tai(sep_etime)-anytim2tai(sep_stime))/3600.)

for eps=0,1 do begin

   xs=18
   ys=8

   outfil=evid+'_'+sat+'_lethet__'+anytim2yymmdd(sep_stime, form='yyyymmdd_hhmm')+'_'+strtrim(string(sep_dur, format='(i03)'),2)+'h'+usedch_filehead+fext+'.ps'
   pset, xs=xs, ys=ys, eps=eps, outfil=outfil
   
   !p.charsize=0.8
   !p.thick=3
   !x.thick=3
   !y.thick=3

yrange=[1e-5, 1.0e4]

xm=8
!p.multi=[0,2,1]
!x.margin=[8,0.5-xm]
!y.margin=[3.5,1.5]

case sat of
   'sta': title='STEREO-A'
   'stb': title='STEREO-B'
endcase


xtitle='Start Time ('+trange(0)+')'
ytitle='Intensity [cm!U-2!N s!U-1!N sr!U-1!N MeV!U-1!N]'

utplot_io, trange, yrange, xs=1, ys=1, timerange=trange, yr=yrange,/nodata,$
           ytitle=ytitle, xtitle=xtitle, title=title

outplot, bkg_stime+['',''],yrange, lines=2
outplot, bkg_etime+['',''],yrange, lines=2

outplot, sep_stime+['',''],yrange, lines=0
if etime_ok then begin
   outplot, sep_etime+['',''],yrange, lines=0
endif else begin
   outplot, sep_etime+['',''],yrange, lines=3
endelse


ct_rainbow, r0, g0, b0, /load
step=18.1818
col3=0.0*step
col4=0.6*step
col5=2.2*step
col6=2.8*step
col7=4.0*step
col8=5.0*step
col9=6.0*step
col10=7.0*step
col11=8.0*step
col12=9.0*step
col13=10.0*step
col14=11.0*step
ss3_14=round([col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14])
loadct,0
tvlct, r, g, b,/get
r3to14=r0[ss3_14]
g3to14=g0[ss3_14]
b3to14=b0[ss3_14]
r[1:15]=[128, 96+r3to14[0]*0.40, r3to14[0]*0.80,r3to14]
g[1:15]=[128, 96+g3to14[0]*0.40, g3to14[0]*0.80,g3to14]
b[1:15]=[128, 96+b3to14[0]*0.40, b3to14[0]*0.80,b3to14]
tvlct,r,g,b




for i=0,14 do outplot, p0.daytim, smooth(p0.pflux[i],5), color=i+1, lines=0

fluence=dblarr(15)
bkg=dblarr(15)

;for i=0,14 do begin
;   ii=strtrim(string(i, format='(i2)'),2)
;   tmp=execute('ss=where(p0.daytim ge bkg_stime and p0.daytim lt bkg_etime and p0.pflux['+ii+'] ge 0, count)')
;   tmp=execute('if count ne 0 then bkg'+ii+'=mean(p0(ss).pflux['+ii+'])')
;   tmp=execute('ss=where(p0.daytim ge sep_stime and p0.daytim lt sep_etime and p0.pflux['+ii+'] ge 0, count)')
;   tmp=execute('if count ne 0 then flux'+ii+'=300*total(p0(ss).pflux['+ii+']-bkg'+ii+')')
;   tmp=execute('if count ne 0 then fluence['+i+']=300*total(p0(ss).pflux['+ii+']-bkg'+ii+')')
;endfor

for i=0,14 do begin
   ss=where(p0.daytim ge bkg_stime and p0.daytim lt bkg_etime and p0.pflux[i] ge 0, count)
   if count ne 0 then bkg[i]=mean(p0(ss).pflux[i])
   ss=where(p0.daytim ge sep_stime and p0.daytim lt sep_etime and p0.pflux[i] ge 0, count)
   if count ne 0 then fluence[i]=300*total(p0(ss).pflux[i]-bkg[i])
endfor;i

for i=3,14 do outplot, trange, [bkg[i], bkg[i]], color=i+1

;outplot, trange, [bkg3, bkg3], color=col3
;outplot, trange, [bkg4, bkg4], color=col4
;outplot, trange, [bkg5, bkg5], color=col5
;outplot, trange, [bkg6, bkg6], color=col6
;outplot, trange, [bkg7, bkg7], color=col7
;outplot, trange, [bkg8, bkg8], color=col8
;outplot, trange, [bkg9, bkg9], color=col9
;outplot, trange, [bkg10, bkg10], color=col10
;outplot, trange, [bkg11, bkg11], color=col11
;outplot, trange, [bkg12, bkg12], color=col12
;outplot, trange, [bkg13, bkg13], color=col13
;outplot, trange, [bkg14, bkg14], color=col14

;clip_xyouts, 0.73, 0.95, 'F3: '+string(flux3, format='(e10.3)'), charsize=0.7, color=col3
;clip_xyouts, 0.73, 0.91, 'F4: '+string(flux4, format='(e10.3)'), charsize=0.7, color=col4
;clip_xyouts, 0.73, 0.87, 'F5: '+string(flux5, format='(e10.3)'), charsize=0.7, color=col5
;clip_xyouts, 0.73, 0.83, 'F6: '+string(flux6, format='(e10.3)'), charsize=0.7, color=col6
;clip_xyouts, 0.73, 0.79, 'F7: '+string(flux7, format='(e10.3)'), charsize=0.7, color=col7
;clip_xyouts, 0.73, 0.75, 'F8: '+string(flux8, format='(e10.3)'), charsize=0.7, color=col8
;clip_xyouts, 0.73, 0.71, 'F9: '+string(flux9, format='(e10.3)'), charsize=0.7, color=col9
;clip_xyouts, 0.73, 0.67, 'F10: '+string(flux10, format='(e10.3)'), charsize=0.7, color=col10
;clip_xyouts, 0.73, 0.63, 'F11: '+string(flux11, format='(e10.3)'), charsize=0.7, color=col11
;clip_xyouts, 0.73, 0.59, 'F12: '+string(flux12, format='(e10.3)'), charsize=0.7, color=col12
;clip_xyouts, 0.73, 0.55, 'F13: '+string(flux13, format='(e10.3)'), charsize=0.7, color=col13
;clip_xyouts, 0.73, 0.51, 'F14: '+string(flux14, format='(e10.3)'), charsize=0.7, color=col14

for i=3,14 do begin 
   clip_xyouts, 0.73, 0.95-(i-3)*0.04, 'F'+string(i, format='(i2)')+': '+string(fluence[i], format='(e10.3)'), charsize=0.7, color=i+1
endfor


;=====(b)=====

;loadct,0,/s
;stretch,255,0
!p.multi=[1,2,1]
!x.margin=[8+xm,0.5]

xtitle='Energy [MeV]'
ytitle='Fluence [cm!U-2!N sr!U-1!N MeV!U-1!N]'

;flu_yr=[100,1e7]
;flu_yr_max=10.^ceil(0.5+alog10(max([flux3, flux4, flux5, flux6])))
flu_yr_max=10.^ceil(0.5+alog10(max([fluence])))
flu_yr_min=flu_yr_max/1.0e7
flu_yr=[flu_yr_min,flu_yr_max]


flu_xr=[1,999]

plot_oo, flu_xr, flu_yr,xs=1, ys=1,/nodata,$
         ytitle=ytitle, xtitle=xtitle, title=comment



fit3_14=stereo_lethet_powerlawfit(fluence, used_channel=used_channel, mean_e=mean_e)

tmpx=def_tmpx()
oplot, tmpx, 10.^poly(alog10(tmpx), fit3_14), lines=0, color=1


for i=0,14 do begin 
   if total(where(i eq used_channel)) eq -1 then plots, mean_e[i], fluence[i], color=i+1, psym=1 else plots, mean_e[i], fluence[i], color=i+1, psym=8
endfor

print, mean_e

;clip_xyouts, 0.72, 0.90, '!Ma!4='+string(yround(fit35(1),2), format='(f5.2)'), charsize=1.0
clip_xyouts, 0.72, 0.90, '!Ma!4='+string(yround(fit3_14(1),2), format='(f5.2)'), charsize=1.0


pend,display=display, bbox=0

endfor;eps
endfor;t

end
