pro print_cmds,hk
;
;	prints all of the commands received by the LEB in the HK stream
;       modified for hk in bytes
;
; @(#)print_cmds.pro	1.1 01/23/98 :LASCO IDL LIBRARY
;
c=get_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

sz=size(hk)
if (sz(sz(0)+1) eq 1) then byteflag=0 else byteflag=1
if (byteflag eq 0) then begin
   lobt_tai = obt2tai(hk(6:11,0))
   utc=tai2utc(lobt_tai)
endif else begin
   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), 0, 10)
date=utc_str(0)
openw,lun,'cmds'+date+'.lst',/get_lun

;
;   get time for each command
;
lobt_tai = obt2tai(byte(c(0:5,*)))
utc = tai2utc(lobt_tai)

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,"COMMAND LIST FOR DATE = ",a10,5x,"PAGE ",i4)'
		printf,lun
		printf,lun,'N','TIME','SEQ','DEST','COMMAND', $
			format='(5x,a1,6x,a4,5x,a3,5x,a4,10x,a7)'
		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
             is = utc(ic-1).time/1000
             ih = is/3600
             im = (is-ih*3600)/60
             is = is-ih*3600-im*60
	     cmdtime = string(ih,im,is, format='(i2,":",i2,":",i2)')
	     s = cnvrt_cmd_dest(c(9,ic-1))
	     t = cnvrt_cmd_code(c(8,ic-1))
	     printf,lun,ic,cmdtime,c(10,ic-1),s,t, $
		format='(i7,3x,a8,i5,a12,3x,a)'
	     nline = nline+1
	   endif
	   s = cnvrt_cmd_dest(c(9,ic))
	   t = cnvrt_cmd_code(c(8,ic))
           is = utc(ic).time/1000
           ih = is/3600
           im = (is-ih*3600)/60
           is = is-ih*3600-im*60
	   cmdtime = string(ih,im,is, format='(i2,":",i2,":",i2)')
	   nline = nline+1
	   printf,lun,ic+1,cmdtime,c(10,ic),s,t, $
		format='(i7,3x,a8,i5,a12,3x,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,"COMMAND LIST FOR DATE = ",a10,5x,"PAGE ",i4)'
	  printf,lun
	  printf,lun,'N','TIME','SEQ','DEST','COMMAND', $
			format='(5x,a1,6x,a4,5x,a3,5x,a4,10x,a7)'
	  printf,lun
	  nline=5
	endif
	s = cnvrt_cmd_dest(c(9,ic))
	t = cnvrt_cmd_code(c(8,ic))
        is = utc(ic).time/1000
        ih = is/3600
        im = (is-ih*3600)/60
        is = is-ih*3600-im*60
	cmdtime = string(ih,im,is, format='(i2,":",i2,":",i2)')
	printf,lun,ic+1,cmdtime,c(10,ic),s,t, $
		format='(i7,3x,a8,i5,a12,3x,a)'
        nrepeats = 0
	lastda=c(9,ic)
	lastcod=c(8,ic)
       endelse
    endelse
endfor 
wrapup:
close,lun
free_lun,lun
return
end

