pro print_bad_cmds,hk 
; 
;       prints all of the commands received by the LEB in the HK stream 
; 
; @(#)print_bad_cmds.pro	1.1 01/23/98 :LASCO IDL LIBRARY
;
 
c=get_bad_cmds(hk) 
npage=0 
nlinepage=66 
nline=nlinepage+1 
s=size(c) 
nc=s(2) 
print,'Number of commands = ',nc 
 
nrepeats=-1 
lastda=-1 
lastcod=-1 

getcputime,hk,first,time,cdstime=utc_all
utc=utc_all(0)
utc_str=STRMID(UTC2STR(utc), 0, 10) 
openw,lun,'badcmds'+utc_str+'.lst',/get_lun 
date=utc_str 
 
IF (c(0) EQ -1) THEN BEGIN 
   printf,lun,"NO BAD COMMANDS FOR DATE = "+date 
   print,"NO BAD COMMANDS FOR DATE = "+date 
ENDIF ELSE BEGIN 
for ic=0,nc-1 do begin 
    if nrepeats gt 0 then begin         ; into repeats 
       if ((c(9,ic) eq lastda) and (c(8,ic) eq lastcod)) then begin 
          nrepeats=nrepeats+1 
       endif else begin 
          nline = nline+1 
          if nline gt nlinepage then begin 
                npage=npage+1 
                if npage ne 1 then printf,lun,string(byte(12)) 
                printf,lun,date,npage, $ 
                        format='(5x,"BAD COMMAND LIST FOR DATE = ",a10,5x,"PAGE ",i4)' 
                printf,lun 
                printf,lun,'N','TIME','SEQ','DEST','COMMAND', 'ERROR', $ 
                        format='(5x,a1,6x,a4,5x,a3,5x,a4,10x,a7,10x,a5)' 
                printf,lun 
                nline=4 
          endif 
          if nrepeats gt 2 then begin 
             printf,lun,'        Command repeated a total of '+ $ 
                strtrim(string(nrepeats),2)+' times' 
             nline = nline+1 
          endif 
          if nrepeats ge 2 then begin 
             cmdtime = utc2str(tai2utc(obt2tai(byte(c(0:5,ic-1)))), $
                /ecs,/time_only,/truncate)                              
             s = cnvrt_cmd_dest(c(9,ic-1)) 
             t = cnvrt_cmd_code(c(8,ic-1)) 
             e = cnvrt_cmd_err(c(11,ic-1)) 
             printf,lun,ic,cmdtime,d,s,t,e, $ 
                format='(i7,3x,a8,a12,a12,3x,a12,5x,a)' 
             nline = nline+1 
           endif 
           s = cnvrt_cmd_dest(c(9,ic)) 
           t = cnvrt_cmd_code(c(8,ic)) 
           e = cnvrt_cmd_err(c(11,ic)) 
           cmdtime = utc2str(tai2utc(obt2tai(byte(c(0:5,ic)))), $
                /ecs,/time_only,/truncate)                              
           nline = nline+1 
           printf,lun,ic+1,cmdtime,d,s,t,e, $ 
                format='(i7,3x,a8,a12,a12,3x,a12,5x,a)' 
           nrepeats = 0 
           lastda=c(9,ic) 
           lastcod=c(8,ic) 
       endelse 
    endif else begin 
       if ((c(9,ic) eq lastda) and (c(8,ic) eq lastcod)) then begin 
          nrepeats=2 
        endif else begin 
       nline = nline+1 
       if nline gt nlinepage then begin 
          npage=npage+1 
          if npage ne 1 then printf,lun,string(byte(12)) 
          printf,lun,date,npage,$  
                format='(5x,"BAD COMMAND LIST FOR DATE = ",a10,5x,"PAGE ",i4)' 
          printf,lun 
          printf,lun,'N','TIME','SEQ','DEST','COMMAND', 'ERROR', $ 
                        format='(5x,a1,6x,a4,5x,a3,5x,a4,10x,a7,10x,a5)' 
          printf,lun 
          nline=5 
        endif 
        s = cnvrt_cmd_dest(c(9,ic)) 
        t = cnvrt_cmd_code(c(8,ic)) 
        e = cnvrt_cmd_err(c(11,ic)) 
        cmdtime = utc2str(tai2utc(obt2tai(byte(c(0:5,ic-1)))), $
                /ecs,/time_only,/truncate)                              
        printf,lun,ic+1,cmdtime,d,s,t,e, $ 
                format='(i7,3x,a8,a12,a12,3x,a12,5x,a)' 
        nrepeats = 0 
        lastda=c(9,ic) 
        lastcod=c(8,ic) 
       endelse 
    endelse 
endfor  
ENDELSE 
close,lun 
free_lun,lun 
return 
end 
 
