;+
;                SUMMARY.PRO                                           
;                                                                      
;  Program to produce summary plots of BDA data files                  
;                                                                      
;                                                                      
;                                                                      
;  Use:                                                                
;          IDL> .run test_rd              (include dp_sync data)  
;          IDL> .run summary     
;
;
;  Based on original pp_cratex.pro   RDB   Fall '91                    
;                                    CDP   Dec '91                     
;
;
;  Bug fix to cope with roadmap and dp_sync data starting on different 
;  sides of 00:00 hrs UT                    Jan 92  CDP                
;                                                                      
;  Generalize extraction of file name and extension   Mar 92   CDP
;  Fix to bypass the yohkoh_events                    May 92
; 
;-                                                                     
;
;   if to PS printer, make it larger
;
if !d.name eq 'PS'  then device,ysiz=25.,yoffset=3.
if !d.name eq 'X'   then window,ysiz=800,retain=2
;
;  if tek, interactive and top panel takes for ever
;
ans = 'Y'
if !d.name ne 'PS' then begin
   read,'Do you want the top panel (housekeeping data)? ',ans
endif
;
;  temp fix while events not working
;
ans = 'N'  
ymax=10000.


;
;   dp_sync time scale, allowing for day rollover
;
ttime = dp_sync.time + (dp_sync.day - dp_sync(0).day)*86400L*1000L
ttime = ttime/3600./1000.
;
;  roadmap time scale
;
rtime = roadmap.time/3600./1000. + (roadmap.day - roadmap(0).day)*24.

;
;  dp_sync and roadmap start on different days
;
if roadmap(0).day lt dp_sync(0).day then begin
   ttime = ttime + 24.0
endif 
if roadmap(0).day gt dp_sync(0).day then begin
   rtime = rtime + 24.0
endif 

hr_start_idx = 0
!x.style=0 & !x.range = 0 & x_style = 0
if max(ttime gt 2.) then begin
  hr_start_dp = fix(min(ttime))
  hr_stop_dp  = fix(max(ttime)+1)
  hr_start_rd = fix(min(rtime))
  hr_stop_rd  = fix(max(rtime)+1)
  hr_start = min([hr_start_dp,hr_start_rd])
  hr_stop  = max([hr_stop_dp,hr_stop_rd])
  !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
print,'Hour range is ',!x.range

if strupcase(ans) eq "Y" then begin

;
;	  get night/day and saa cycles
;
;   yohkoh_events,all_vals(dp_sync.day),tonit,today,tesaa,txsaa,/hours
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
	tonit = tonit-24 & today = today-24
	tesaa = tesaa-24 & txsaa = txsaa-24
endif
	
ss = Where(dp_sync.acc_interval eq 0)
qqss = Where(dp_sync.acc_interval ne 0)

dpm = float(dp_sync.dp_mode and '1f'x)/'f'x & dpm(ss)=0		;DP mode
rbm = dp_sync.rbm_status/128 & rbm(ss)=0			;RBM flag
spa = float(dp_sync.specstatus and 'e0'x)/'e0'x & spa(ss)=0	;BCS-A status
spb = float(dp_sync.specstatus and '0e'x)/'0e'x & spb(ss)=0	;BCS-B status
cab = float(dp_sync.specstatus and '01'x)/'01'x & cab(ss)=0	;CAL-B status
nn = n_elements(dp_sync.all_cnts(3,*))
mm = n_elements(dp_sync.rbm_status)

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

!mtitle='BCS Encoded Events Counts'
!xtitle=''
!ytitle=''

if strupcase(ans) eq "Y" then begin

   plot,ttime,cab(0,*)*2.5,psym=3 $
       	   ,xstyle=8+4+x_style $
	   ,yrange=[0,22], yticks=12, yticklen=0.005, yminor=-1 $
	,ytickname=[' ','CALB',' ','SAA',' ','NIGHT',' ','DPM',' ','SPA',' ','SPB',' ']

   if max([tesaa]) gt -1 then begin
      for j=0,n_elements(tesaa)-1 do oplot,[tesaa(j),txsaa(j)],[7,7],thick=3
   endif 
   oplot,ttime,rbm*1.5 + 4,psym=3
   if max([tonit]) gt -1 then $
      for j=0,n_elements(tonit)-1 do oplot,[tonit(j),today(j)],[10,10],thick=4
   oplot,ttime,dpm*2.5 + 12,psym=3
   oplot,ttime(qqss),spa(0,qqss)*2.5 + 16,psym=3
   oplot,ttime(qqss),spb(0,qqss)*2.5 + 20,psym=3
endif

!mtitle=''
!x.style = 8+x_style
if dp_sync(hr_start_idx).day-dp_sync(0).day eq 1 then begin
	rtime = rtime-24
endif

;
;	  PLot the header counters in the DP synchronous data CaXIX
;         ---------------------------------------------------------

chekov,2,3,dp_sync,crt,acc1
ymax = max(crt) & !c=0
ymin = 0.0
qqss = where(dp_sync.acc_interval ne 0 and crt gt ymin)
plot,ttime(qqss),crt(qqss),ytitle='CaXIX dp_sync',yrange=[ymin,ymax],psym=3
;
;  output file name
;

break_file,infil(0),disk_log,dir,filnam,ext,fver
xyouts,0.75,0.0,filnam+ext,/normal,siz=1.2


;
;  plot roadmap data for SXV
;  -------------------------
;
qq = where(((roadmap.blockid eq 0) or (roadmap.blockid eq 1)) and       $
        (rtime ge !x.range(0)) and (rtime le !x.range(1)))
cnts = roadmap(qq).total_cnts(3)*10./(roadmap(qq).dgi*0.125)
cymax = max(cnts)
xx = where(cnts ge cymax)
if n_elements(xx) le 2 then cnts(xx) = 0.1
cymax = max(cnts)

ymax = (cymax +35)/25*25
plot,rtime(qq), cnts, yrange=[ymin,ymax] $
	,ytitle='SXV Roadmap',psym=3

;
;  plot roadmap data for Ca XIX
;  -----------------------------------
;
qq = where(((roadmap.blockid eq 0) or (roadmap.blockid eq 1)) and       $
        (rtime ge !x.range(0)) and (rtime le !x.range(1)))
cnts = roadmap(qq).total_cnts(2)*10./(roadmap(qq).dgi*0.125)
cymax = max(cnts)
xx = where(cnts ge cymax)
if n_elements(xx) le 2 then cnts(xx) = 0.1
cymax = max(cnts)

ymax = (cymax +35)/25*25

plot,rtime(qq), cnts, yrange=[ymin,ymax]        $
	,ytitle='Ca XIX Roadmap',psym=3

;
;  plot roadmap data for FeXXV
;  -----------------------------------
;
qq = where(((roadmap.blockid eq 0) or (roadmap.blockid eq 1)) and       $
        (rtime ge !x.range(0)) and (rtime le !x.range(1)))
cnts = roadmap(qq).total_cnts(1)*10./(roadmap(qq).dgi*0.125)
cymax = max(cnts)
xx = where(cnts ge cymax)
if n_elements(xx) le 2 then cnts(xx) = 0.1
cymax = max(cnts)

ymax = (cymax +35)/25*25

plot,rtime(qq), cnts, yrange=[ymin,ymax]        $
	,ytitle='FeXXV Roadmap' ,psym=3

;
;  plot roadmap data for FeXXVI
;  -----------------------------------
;
qq = where(((roadmap.blockid eq 0) or (roadmap.blockid eq 1)) and       $
        (rtime ge !x.range(0)) and (rtime le !x.range(1)))
cnts = roadmap(qq).total_cnts(0)*10./(roadmap(qq).dgi*0.125)
cymax = max(cnts)
xx = where(cnts ge cymax)
if n_elements(xx) le 2 then cnts(xx) = 0.1
cymax = max(cnts)

ymax = (cymax +35)/25*25
!xtitle = 'Time (UT) on ' + sday
plot,rtime(qq), cnts, yrange=[ymin,ymax]        $
	,ytitle='FeXXVI Roadmap' ,psym=3


;
;	  save the graphics structures for reuse
;
if !d.name eq 'X' then begin
	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.


ans = 'Y'
end
