;+
; NAME:
;	CPU_DIAG
; PURPOSE:
;	Plots some telemetry points from te BCS CPU against time
; CALLING SEQUENCE:  
;	.run CPU_DIAG
; INPUTS:
;	Data is extracted from the DP_SYNC field
;	The DP_SYNC field is first matched with the file header.
;	The time range is derived from the variable X_XHR_RANGE
; OUTPUTS:
;
; OPTIONAL OUTPUTS:
;
; RESTRICTIONS:
;	Variable X_HR_RANGE must have been set. This is either done by
;	running PP_CRATEX or by running SET_XHR using the PLOTR output 
;	from the TEST_RD routine.
; PROCEDURE:
;
; MODIFICATION HISTORY:
;	RDB	Oct-91
;	RDB  03-Jul-92	Asks questions about time limits
;			Only does filecheck for songle dataset
;	RDB  09-Jul-92	Uses common block files_24hr for source of data
;	RDB  11-May-94  Also plot when Yohkoh is night/saa
;	RDB  16-May-94	Check that clock is ticking 
;	RDB  17-May-94  Revised chack value for clock error
;       RDB   3-Sep-94  Print start and end times of dp_sync data
;-
common files_24hr,infilq,nffq,ffq

if n_elements(nffq) gt 0 then begin
   if nffq ge 1 then begin	;and n_elements(dp_sync) le 1 then begin
      print,'Default files are:
      print,ffq
      yesnox,'* Read DP sync data for files',ansr
      if ansr then rd_bda_dp,ffq(0:nffq-1),dp_sync
      break_file,ffq,disk,dir,filnam,ext
   endif
endif

if n_elements(dp_sync) le 1 then stop,'*** No DP_SYNC data ***'


again:
print,' '
print,'** Time range of DP_SYNC data is:'
fmt_timer,dp_sync
if n_elements(x_hr_range) le 1 then begin
   x_hr_range = fltarr(2)
   read,'* Enter start and stop time: ',x_hr_range
endif else begin
   print,'Current time range is ',x_hr_range
   yesnox,'* Re-enter time limits?: ',ans
   if ans then begin
      x_hr_range = 0
      goto, again
   endif   
endelse

if n_elements(fheader) eq 1 then match_header,fheader,dp_sync

;	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

!p.multi = 0
!p.multi = [0,1,6]
!x.range = x_hr_range

;	  time of data, allowing for day rollover
ttime = dp_sync.time + (dp_sync.day - dp_sync(0).day)*86400L*1000L
ttime = ttime/3600./1000.
rqdays = all_vals(dp_sync.day)
yohkoh_events,rqdays,tonit,today,tesaa,txsaa,/hours

fi = dp_sync.dp_time(3)
ss0   = where(fi eq 0)
ss64  = where(fi eq 64)
ss128 = where(fi eq 128)
ss192 = where(fi eq 192)

qfree   = dp_sync(ss64).cpu(3,1)
qpages  = dp_sync(ss64).cpu(2,1)
qsects  = dp_sync(ss64).cpu(1,1)
fqsects = dp_sync(ss64).cpu(0,1)
bcs_que_sts = (dp_sync(ss64).cpu(1,0) and 64)/64

out_pgs  = (qpages and '0f'x) + (qsects and '0f'x)*1/16.
;aar = [where(out_pgs(1:*) - out_pgs(0:*) lt 0),n_elements(out_pgs)]
;print,aar
;wwo = [intarr(aar(0)+1)]
;for j=1,n_elements(aar)-1 do wwo = [wwo, intarr(aar(j)-aar(j-1))+j*11.]

in_pgs = (qpages/16) + (qsects/16)*1/16.
;aar = [where(in_pgs(1:*) - in_pgs(0:*) lt 0),n_elements(in_pgs)]
;print,aar
;wwi = [intarr(aar(0)+1)]
;for j=1,n_elements(aar)-1 do wwi = [wwi, intarr(aar(j)-aar(j-1))+j*11.]

;qroom = ((out_pgs+wwo) - (in_pgs+wwi))*8
;wwx = fix(qroom-qfree-4)/8
;wwix = wwi + wwx

;icc = 0
;if (wwix(0) lt 0) or (wwo(0) lt 0) then icc = 11

int2ex,dp_sync(0).time,dp_sync(0).day,tarr
sstm =  fmt_tim(tarr,sday,stime)
!xtitle='Time (UT) on ' + sday

!ytitle=''
loadct,3		;colors for plots

;	  Queue Free
plot, ttime(ss64), qfree,title='QFREE',yrange=[0,100],psym=3
print,'Out is solid line, In is dotted'
;;plot, ttime(ss64), out_pgs+wwo+icc, title='Queue Pointers'
;;oplot, ttime(ss64), in_pgs+wwix+icc, color=210
plot, ttime(ss64), out_pgs, title='Queue Pointers'	$
	,yrange=[0,16],ystyle=1
oplot, ttime(ss64), in_pgs, color=210


;	  Sequence Phase, Fast Queue sectors
phase = (dp_sync(ss192).cpu(1,1) and 'c0'x)/64
fqs_out = (fqsects and '0f'x)
fqs_in  = (fqsects/16)
plot,ttime(ss192),phase/3.*4 + 11  $
	,title='SEQN Phase, Fast Queue Sectors',ystyle=1	$
	,yrange=[0,16], yticks=8, yminor=-1, yticklen=0.005 	$
	,ytickname=[' ','FQ_IN',' ',' ','FQ_OUT',' ',' ','PHASE',' '],psym=3
oplot,ttime(ss64),fqs_in/16.*4.5
oplot,ttime(ss64),fqs_out/16.*4.5 + 6

;modeid = (dp_sync(ss128).cpu(1,1))
phswop = dp_sync(ss128).cpu(3,0) and '0f'x
accswop = dp_sync(ss128).cpu(3,0)/16
;oplot,ttime(ss128),modeid/256.*2.5


;	  Queue Clash and Margin Flags
qclash = (dp_sync(ss64).cpu(0,0) and 16)/16
qmarg  = (dp_sync(ss64).cpu(0,0) and 32)/32
mem_checksum  = (dp_sync(ss64).cpu(0,0) and 64)/64
clock = dp_sync(ss0).cpu(0,0) + dp_sync(ss0).cpu(1,0)*256L

plot,ttime(ss64),qclash*2.5 $
	,title='Memory Checksum, Clock, Queue Clash and Margin flags',ystyle=1	$
	,yrange=[0,16], yticks=8, yminor=-1, yticklen=0.005 	$
	,ytickname=[' ','CLASH',' ','MARGIN',' ','CLOCK',' ','MEM CHK ERR',' ']
oplot,ttime(ss64),qmarg*2.5 + 4
oplot,ttime(ss0),float(clock)/(2.^16)*2.5 + 8, psym=3
oplot,ttime(ss64),mem_checksum*2.5 + 12

;	Check high byte of clock to see if it is changing (i.e. ticking...)
;	(low byte will tick evcen if micro in Kernel mode)
nvclk = n_elements(all_vals(clock/256))
if nvclk eq 1 then begin
  print,' '
  print,'*** Clock bad all way through; High byte constant ***
  print,' '
endif
if nvclk gt 1 then begin
  qclk = histogram(clock/256)
  mxclk = max(qclk)
  if mxclk gt 20 then begin
    print,' '
    help,clock
    print,'*** Possible clock problem; Some frames of high byte constant ***'
    print,'*** No. of occurances, value =',mxclk,where(qclk eq mxclk)
    print,' '
  endif
endif


;	  RBM, NIGHT, DP and BCS FF
rbm = dp_sync.rbm_status/128
wrbm = where(rbm eq 1)
night = (dp_sync(ss64).cpu(1,0) and 16)/16
wnight = where(night eq 1)
dp_flare = (dp_sync.rbm_status and '30'x)/16
bcs_flare_exceed = (dp_sync(ss64).cpu(1,0) and 2)/2
bcs_flare_flag = (dp_sync(ss64).cpu(1,0))/128
plot,ttime,dp_flare/3.*2.5	$
	,title='Orbit Night, SAA, DP and BCS Flare',ystyle=1	$
	,yrange=[0,20], yticks=10, yminor=-1, yticklen=0.005 	$
	,ytickname=[' ','DP FLARE',' ','BCS FLR EXC',' ', $
		'BCS-FLR-FLG',' ','SAA',' ','NIGHT',' ']
oplot,ttime(ss64),bcs_flare_exceed*2.5 + 4
oplot,ttime(ss64),bcs_flare_flag*2.5 + 8
;	plot expected times of SAA and Night
if max([tesaa]) gt -1 then  $
  for j= 0,n_elements(tesaa)-1 do oplot,[tesaa(j),txsaa(j)],[14,14],thick=3
if max([tonit]) gt -1 then  $
  for j= 0,n_elements(tonit)-1 do oplot,[tonit(j),today(j)],[18,18],thick=4
;	overplot actual status from spacecraft
color=255 & if !d.name eq 'X' then color=210
oplot,ttime(wrbm),rbm(wrbm)+12,psym=3,color=color
oplot,(ttime(ss64))(wnight),night(wnight)+16,psym=3,color=color

;	  DP mode
dpmode = (dp_sync.dp_mode and '1f'x)
dprate = (dp_sync.dp_rate)/32
plot,ttime,dpmode/16.*2.5	$
	,title='DP Mode, Rate',ystyle=1	$
	,yrange=[0,8], yticks=4, yminor=-1, yticklen=0.005 	$
	,ytickname=[' ','DP Mode',' ','DP Rate',' '],psym=3
oplot,ttime,dprate/4.*2.5+4,psym=3


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


;	  reset PS plot size
if !d.name eq 'PS' then device,/inches,ysize=5.,yoffset=5.
!p.multi=0
!x.range=0

end
