;+
; NAME:
;	PLOT_ERATE
;
; PURPOSE:
;	Plots BCS all_cnts, lim_cnts and acc_cnts for a selected channel 
;	and time interval
;
; CATEGORY:
; CALLING SEQUENCE:
;	Use YODAT to get dp_sync data
;	.run plot_erate
; INPUTS:
; OPTIONAL INPUT PARAMETERS:
; KEYWORD PARAMETERS:
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;  	RDB  Oct '91	Written
;	RDB  11-Jun-93	Made Channels 1-4
;	RDB  14-Jun-93	Changed to use UTPLOT
;	RDB  18-Jun-93	Put reset of pmulti and clearplot, etc at end
;	RDB  08-Jul-93	Added call to fmt_timer
;-

break_file,infil(0),disk_log,dir,filnam,ext,fver	;!!!!

;	if to PS printer, make it larger
;;if !d.name eq 'PS'  then device,ysiz=25.,yoffset=3.
ps_long
if !d.name eq 'X'   then window,ysiz=800,retain=2

ymax=10000.
;Read,'Enter ymax: ',ymax


get_times:
stimes = ''
print,' '
print,'Name of data file: ',filnam+ext
print,'Times of DP_SYNC data are'
fmt_timer,dp_sync
read,'* Enter start and stop time: ',stimes

times = anytim2ex(str2arr(stimes,delim=','))
if n_elements(times) ne 14 then goto,get_times

sttime = anytim2ex(dp_sync(0))
if max(times(4:6,0)) eq 0 then times(4:6,0) = sttime(4:6)
if max(times(4:6,1)) eq 0 then times(4:6,1) = sttime(4:6)

print,times
x_style=1
sday=''
goto, qqxx

;	  time of data, allowing for day rollover
ttime = dp_sync.time + (dp_sync.day - dp_sync(0).day)*86400L*1000L
ttime = ttime/3600./1000.

hr_start_idx = 0
!x.style=0 & !x.range = 0 & x_style = 0
print,'Hour range is ',min(ttime),max(ttime)
if max(ttime gt 2.) then begin
  read,'* Enter start hour, end hour: ',hr_start,hr_stop
  !x.range = [hr_start,hr_stop] & x_style = 1
  x_hr_range = !x.range
  hr_start_idx = min(where(ttime ge hr_start))
  hr_start_idx = hr_start_idx(0)
  hr_stop_idx = min(where(ttime ge hr_stop))	;for use in other progs!
  hr_stop_idx = hr_stop_idx(0) -1
  endif
;	  start day of plot
int2ex,dp_sync(hr_start_idx).time,dp_sync(hr_start_idx).day,tarr
sstm =  fmt_tim(tarr,sday,stime)
;	  if day has changed before start of plot, use show
;	  hours on the new day.
if dp_sync(hr_start_idx).day-dp_sync(0).day eq 1 then begin
	ttime = ttime-24
	!x.range = [hr_start-24,hr_stop-24] & x_style = 1
	endif

qqxx:
print,' '
Read,'* Enter channel no.[1-4]: ',channel
ichn = channel-1
	

ss = Where(dp_sync.acc_interval eq 0)


tek

!p.multi = [0,1,3]
!fancy=6

!x.style = 8+x_style

!xtitle = 'Time (UT) on ' + sday
;!mtitle = bcs_channels(ichn)
!mtitle=' '

;
;	  PLot the counters in the DP synchronous data 

crt = unsign(dp_sync.all_cnts(ichn,*),16)
crt = float(crt)/(dp_sync.acc_interval)
crt = crt(0,0,*) & crt(ss) = 0. & crt(where(crt le 0)) = 0.1
cymax = max(crt) & !c=0
ymax = 10000. & ymin = 1
if cymax gt 5.10e3 then begin 
  ymax = 100000 & ymin = 10. 
  endif
cymax = good_max(crt)
;plot_io,ttime,crt,ytitle='ALL_CNTS',yrange=[ymin,ymax]
;;plot,ttime,crt,ytitle='All Event Counts',yrange=[0,cymax]
utplot,dp_sync,reform(crt),timer=fmt_tim(times),xsty=1, $
	ytitle='All Event Counts',yra=[0,cymax]
xyouts,0.1*(!x.crange(1)-!x.crange(0))+!x.crange(0),	$
	0.95*cymax,bcs_channels(ichn),siz=1.5

crt = unsign(dp_sync.lim_cnts(ichn,*),16)
crt = float(crt)/(dp_sync.acc_interval)
crt = crt(0,0,*) & crt(ss) = 0. & crt(where(crt le 0)) = 0.1
cymax = max(crt) & !c=0
ymax = 10000. & ymin = 1
if cymax gt 10e4 then begin 
  ymax = 10e5 & ymin = 10
  endif
cymax = good_max(crt)
;plot_io,ttime,crt,ytitle='LIM_CNTS',yrange=[ymin,ymax]
;;plot,ttime,crt,ytitle='Limited Event Counts',yrange=[0,cymax]
utplot,dp_sync,reform(crt),timer=fmt_tim(times),xsty=1, $
	ytitle='Limited Event Counts',yra=[0,cymax]

ymax = 10000.
crt = unsign(dp_sync.acc_cnts(ichn,*),16)
crt = float(crt)/(dp_sync.acc_interval)
crt = crt(0,0,*) & crt(ss) = 0. & crt(where(crt le 0)) = 0.1
cymax = max(crt) & !c=0
cymax = good_max(crt)
;plot_io,ttime,crt,ytitle='ACC_CNTS',yrange=[1,ymax]
;;plot,ttime,crt,ytitle='Accepted Event Counts',yrange=[0,cymax]
utplot,dp_sync,reform(crt),timer=fmt_tim(times),xsty=1, $
	ytitle='Accepted Event Counts',yra=[0,cymax]


xyouts,0.9,0.05,filnam+ext,/device,siz=1.2

if !d.name eq 'X' then begin
;	  save the graphics structures for reuse
	pp_x = !x & pp_y = !y & pp_z = !z & pp_p = !p
	endif

;	  reset PS plot size
;;if !d.name eq 'PS' then device,/inches,ysize=5.,yoffset=5.
ps_reset

!p.multi=0
clearplot
clear_utplot

end
