pro scan_sc_hdr,sc
;+
; NAME:
;	SCAN_SC_HDR
;
; PURPOSE:
;	This procedure identifies the subpacket headers in the science packets
;
; CATEGORY:
;	LASCO PACKETS
;
; CALLING SEQUENCE:
;	SCAN_SC_HDR,Sc
;
; INPUTS:
;	Sc:	A 2D byte array of the packet data as read in by
;		READ_TM_PACKET
;
; OUTPUTS:
;	This procedure writes the subpacket information to a file in 
;	the current directory.  The file name is scan_YYYY-MM-DD,
;	where the date is today's date.
;
; PROCEDURE:
;	The science packet is scanned for the 4-byte subpacket headers.
;	The first byte is the number of 2-byte data words in the 
;	subpacket.  The second byte is the subpacket type.  The 
;	packet data are only valid when the OBE is running.  WHen
;	OBE is not running the packets will contain 'FF'X.
;
; MODIFICATION HISTORY:
; 	Written by:	R.A. Howard, NRL, 1993
;	Sep, 1999	RAH, The input data is now a byte array
;
;	@(#)scan_sc_hdr.pro	1.4 09/08/99 LASCO IDL LIBRARY
;-

s=size(sc)
nsc=s(2)	; number of science packets
;
;     get today's date for the file name
;
get_utc,date,/ccsds,/date_only
;
;     open a file for the scan listing
;
openw,lu,'scan_'+date+'.lst',/get_lun
printf,lu,'          Scan of Science Sub-packet Types'
printf,lu,'             Date = '+date
printf,lu
printf,lu,'  PCKT        DATE       TIME        TYPE          DESC              NREP'
printf,lu
;
;  the for loop is for each packet
;
old = -1
nrep = 0
for pno=0L,nsc-1 do begin
;
; 	pktsize is the size of the packet and takes care of high / low rate
;
    pktsize= sc(4,pno)*256+sc(5,pno)-5

    pkidwd = 12		; the byte of the sub packet length in the science tlm
    REPEAT BEGIN
       IF (sc(pkidwd,pno) EQ 'ff'xl) THEN BEGIN			; is sc tlm on?
          pkttype = 255						; no
          nwds = pktsize
       ENDIF ELSE BEGIN
          nwds = sc(pkidwd,pno)*2 				; yes
          pkttype = sc(pkidwd+1,pno)
       ENDELSE
       IF (old EQ pkttype) THEN nrep=nrep+1 ELSE BEGIN		; count # repeats
;
;    if new pk id then print old one
;
          IF (old NE -1) THEN $
             PRINTF,lu,op,tim,old,t,nrep,format='(i6,a28,i6,6x,a17,i9)'
          tim = UTC2STR(TAI2UTC(OBT2TAI(sc(6:11,pno))),/ecs)
          old = pkttype					; old packet type
          op = pno					; old packet #
          nrep = 0					; # repeats
          CASE old OF					; decode the type
             1:   t='Image Data       '
             2:   t='Image Header     '
             3:   t='Image Block Start'
             4:   t='Image Continue   '
             5:   t='Image Block End  '
             6:   t='Pad Data         '
             7:   t='Activity Buffer  '
             8:   t='M1 Data          '
             10:  t='FP Results 1     '
             11:  t='FP Results 2     '
             12:  t='FP Results 3     '
             13:  t='FP Results 4     '
             14:  t='FP Results 5     '
             15:  t='FP Data Cont     '
             16:  t='FP Data End      '
             17:  t='Memory Dump Start'
             18:  t='Memory Dump Cont '
             19:  t='Memory Dump End  '
             20:  t='Driver Data Start'
             21:  t='Driver Data Cont '
             22:  t='Driver Data End  '
             23:  t='Image End        '
             24:  t='Periph Resp Start'
             25:  t='Periph Resp Cont '
             26:  t='Periph Resp End  '
             255: t='Power Off        '
             ELSE: t='Unknown          '
          ENDCASE
       ENDELSE
       pkidwd=pkidwd+nwds+4			; go to next packet id
    ENDREP UNTIL (pkidwd GE pktsize)		; until packet is complete
ENDFOR
;
;   now print out the last header type
;
PRINTF,lu,op,tim,old,t,nrep,format='(i6,a28,i6,6x,a17,i9)'
CLOSE,lu
FREE_LUN,lu
RETURN
END
