pro print_ab,hk, LATEST=latest
;
;  prints the tce and obe activity buffers as a time-stamped string
;  hk  is an array containing all of the housekeeping telemetry packets
;
; @(#)print_ab.pro	1.1 07/24/01 :LASCO IDL LIBRARY
;

;** added 26 Jun 97 - print the latest hour 
outname = ''
IF KEYWORD_SET(LATEST) THEN BEGIN
  tmpckts = GETENV('TMPCKTS')
  IF (tmpckts EQ '') THEN tmpckts = '.'
  match_str = tmpckts+'/LASHK*'
  files = FINDFILE(match_str)
  len = N_ELEMENTS(files)
  IF (len EQ 0) THEN BEGIN
     PRINT, '%%PRINT_AB: No packet files found matching: ', match_str
     RETURN
  ENDIF
  file = files(len-1)
  pos = STRPOS(file, '.recs')
  hk = READALLHK(STRMID(file,pos-9,9))
  outname = tmpckts+'/latest_ab.lst'
ENDIF

sz=size(hk)
if (sz(sz(0)+1) eq 1) then byteflag=0 else byteflag=1
if (byteflag eq 0) then begin
   offset=12				; offset to sync word
   utc = tai2utc(obt2tai(hk(offset-6:offset-1,0)))
endif else begin
   offset=15				; offset to sync word
   year=hk(1)
   IF year LT 100 THEN year = ((year + 50) MOD 100) + 1950
   doy=hk(2)+1
   ihh=hk(3)
   imm=hk(4)
   iss=hk(5)
   utc = {cds_int_time, mjd:0L, time:0L}
   utc.mjd = DATE2MJD(year, doy)
   utc.time = ( (ihh*60L*60L)+(imm*60L)+iss ) * 1000L
endelse
utc_str=STRMID(UTC2STR(utc,/date_only), 0, 16)
IF (outname EQ '') THEN outname = 'acb'+utc_str+'.lst'
openw,lu,outname,/get_lun
date = strmid(utc_str,0,10)

hk1 = gethkn(hk,1)
s=size(hk1)
n=s(2)
nlinpage=60
nlines=61
npage=0
for i=0,n-1 do begin
    for j=0,20,2 do begin		; for bytes
        if nlines>nlinpage then begin
           npage=npage+1
           bl = '        '
           if npage eq 1 then t=bl else t=string(byte(12))+bl
           t=t+'TCE Activity Buffer for Date = '+date
           printf,lu,t+bl+'Page '+strtrim(string(npage),2)
           printf,lu
           nlines=3
        endif
        if (byteflag eq 1) then begin
           off = offset+(131-1)
           s=cnvrt_abc(hk1(j+off,i)) 
        endif else begin
           off = offset+2*(131-1)
           tmval = hk1(j+off,i)*256l + hk1(j+off+1,i)
           s=cnvrt_abc(tmval)
        endelse
        if ((s ne '') AND (s ne 'not to be printed - nop')) then begin
           if (byteflag eq 0) then begin
              tai = obt2tai(byte(hk1(offset-6:offset-1,i)))
              utc = tai2utc(tai)
              t = utc2str(utc,/ecs,/time_only,/truncate)
           endif else begin
              t = string(hk1(3,i),hk1(4,i),hk1(5,i), $
		format='(i2.2,":",i2.2,":",i2.2,"   ")')
           endelse
           printf,lu,t,'  ',s
           nlines=nlines+1
        endif
    endfor
endfor
hk3 = gethkn(hk,3)
s=size(hk3)
n=s(2)
nlines=61
npage=0
if (byteflag eq 0) then begin
   even=indgen(10)*2+(offset+2*(99+34-2))
   odd = even+1
   obe_ab = hk3(even,*)*256L + hk3(odd,*)
endif else $
   obe_ab = hk3(146:*,*)
for i=0,n-1 do begin
    nskip = 0
    for j=0,9 do begin		
        if (nlines>nlinpage) then begin
           npage=npage+1
           bl = '        '
           t=string(byte(12))+bl
           t=t+'OBE Activity Buffer for Date = '+date
           printf,lu,t+bl+'Page '+strtrim(string(npage),2)
           printf,lu
           nlines=3
        endif
	if nskip gt 0 then nskip=nskip-1 else begin
	   if (j lt 5) then begin
	      if ((obe_ab(j,i) eq 137) and (obe_ab(1+j,i) eq 143) and $
		   (obe_ab(2+j,i) eq 75) and (obe_ab(3+j,i) eq 60) and $ 
		   (obe_ab(4+j,i) eq 144)) then begin
		       s='Default Status Request (Normal Execution)'
		       nskip=4
	         endif 
	   endif 
	   if (nskip eq 0) then s=cnvrt_abc(obe_ab(j,i))
           if ((s ne '') AND (s ne 'not to be printed - nop')) then begin
              if (byteflag eq 0) then begin
                 tai = obt2tai(byte(hk3(6:11,i)))
                 utc = tai2utc(tai)
                 t = utc2str(utc,/ecs,/time_only,/truncate)
              endif else t = string(hk3(3,i),hk3(4,i),hk3(5,i), $
		format='(i2.2,":",i2.2,":",i2.2,"   ")')
              printf,lu,t,'  ',s
              nlines=nlines+1
           endif
	endelse
    endfor
endfor
close,lu
free_lun,lu
PRINT, '%%PRINT_AB: Saved output to file: ', outname
return
end
