pro decode_sci5k, rec, lun, irec, none_cnt, cnt_minutes, simple=simple, fill_cnt=fill_cnt, $
	roll_lun = roll_lun
;
;INPUT:
;	rec	- the record to decode
;		  199 integer*2 array
;	lun	- The logical unit of the output file to print the ascii informat to
;	i	- counting the number of packets
;	none_cnt - Allowing the routine to count the number of packets in a row
;		   with no information
;	cnt_minutes - an ongoing counter for the number of ref-times
;HISTORY:
;	19-Dec-95 (MDM) - Renamed variable "i" to "irec"
;	24-Jan-96 (MDM) - Display more than just the first 6 arguments 
;			  on the command line echo
;	29-Jan-96 (MDM) - Patched 24-Jan-96 patch which gave b2 subscripts
;			  out of range errors on occasion
;	   Mar-96 (DM)  - Changed format
;	30-Apr-96 (MDM) - Added option to not write to output file
;	13-Jun-96 (MDM) - Added Tuncate flag to the display
;	29-Mar-99 (CED) - Split out from mon_sci5k.  
;	20-Apr-99 (CED) - Added capacity for handling roll file
;
common decode_sci5k_blk, last_reftime
;
;;print, unsign(rec(0:20), 16), format='(25z5)'
;;return
;
l_val = intarr(2)
if (n_elements(last_reftime) eq 0) then last_reftime = -1

    b2 = byte(rec, 0, 398)
    b = unsign(rec, 16)
    ;
    typ = b2(12)
    reftime = long(b2, 6)
    daytim = anytim2ints('1-Jan-58', off=reftime*1D)
    daytim_str = fmt_tim(daytim)
    ;
    if (keyword_set(qstop)) then stop
    ;;print, 'Packet Type = ', typ
    ;;print, b(0:19), format='(20(1x,z4.4))'
    case typ of
	'FD'x: begin	;DEP memory dump
		print,       '<<<<<<<<<<<<<<<< DEP Memory Dump at ' + daytim_str + ' >>>>>>>>>>>>>>>>'
		print,       b, format='(16(1x,z4.4))'
		if (lun ne 0) then printf, lun, '<<<<<<<<<<<<<<<< DEP Memory Dump at ' + daytim_str + ' >>>>>>>>>>>>>>>>'
		if (lun ne 0) then printf, lun, b, format='(16(1x,z4.4))'
		none_cnt = 0
	      end
	'FB'x: begin	;IP buffer dump
		print,       '<<<<<<<<<<<<<<<< IP Buffer Dump ' + daytim_str + '  >>>>>>>>>>>>>>>>'
		print,       b, format='(16(1x,z4.4))'
		if (lun ne 0) then printf, lun, '<<<<<<<<<<<<<<<< DEP Memory Dump at ' + daytim_str + ' >>>>>>>>>>>>>>>>'
		if (lun ne 0) then printf, lun, b, format='(16(1x,z4.4))'
		none_cnt = 0
	      end
	'F8'x: begin	;command echo
		if (n_elements(cmd_table) eq 0) then begin
		;print, '<<<<<<<<<<<<<<<< Command Echo >>>>>>>>>>>>>>>>'
		    table = rd_tfile('/mdisw/dbase/cal/info/cmd_echo.table')
		    ntable = n_elements(table)
		    cmd_table = replicate({cmd: ' ', nwords: 0L, mnem: ' ', descr: ' '}, n_elements(table))
		    for i=0,ntable-1 do begin
			arr = str2arr(strcompress(table(i)), delim=' ')
			cmd_table(i).cmd = str_replace(arr(0), '.', '*') + '*'
			cmd_table(i).nwords = fix(arr(1))
			cmd_table(i).mnem  = arr(2)
			cmd_table(i).descr = arr2str(arr(3:*), delim=' ')
		    end
		end
		if (keyword_set(simple)) then begin
		    print,      b, format='(16(1x,z4.4))'
		    if (lun ne 0) then print, lun, b, format='(16(1x,z4.4))'
		end else begin
		    qdone = 0
		    ist = 16
		    while (not qdone) do begin
		      cmd_check = mask(b2(ist), 2, 4)	;xx10 10xx
		      if (cmd_check eq 10) then begin
			full_cmd = string(b2(ist:ist+5), format='(6z2.2)')
			;if (qdebug) then print, b, format='(16(1x,z4.4))'
			;if (qdebug) then print, full_cmd
			;
			i=-1
			nss = 0
			while ((nss eq 0) and (i lt ntable-1)) do begin
			    i = i + 1
			    ss = wc_where(full_cmd, cmd_table(i).cmd, nss)
			end
			;;ss = wc_where(cmd_table.cmd, full_cmd, nss)
			if (nss ne 0) then begin
			    ;;prstr, cmd_table(i).mnem + ' == ' + cmd_table(i).descr
			    cmd_str = strmid(full_cmd, 0, strlen(cmd_table(i).cmd)-1) + '             '
			    narg = mask(b2(ist+1), 0, 5)
			    arg_str = ' '
			    if (narg gt 1) then arg_str = string(b2(ist+2:ist+2+(narg<6-2)*2+1), format='(5(2z2.2,1x))') + $
						'                                              '
			    fmt = '(a8, 2x, a10, a10, 1x, a25, a35)'
			    out = string(gt_time(daytim,/str), cmd_str, cmd_table(i).mnem+'       ', $
										arg_str, cmd_table(i).descr, format=fmt)
			    print, out
                            if (lun ne 0) then printf, lun, out
			    ;
			    for iargs=5,narg-2,5 do begin
				istarg = ist+2 + iargs*2
				ienarg = ist+2 + ( ((iargs+4)<(narg-2))*2+1)
				arg_str = string(b2(istarg:ienarg), format='(31x, 5(2z2.2,1x))')
			        print, arg_str
                                if (lun ne 0) then printf, lun, arg_str
			    end
			    ;
			    ist = ist + (narg-1)*2 + 4
			    none_cnt = 0
			end else begin
			    qdone = 1
			end
		      end else begin
			qdone = 1
		      end
		    end
		end
	      end
	else: begin	;science data
		ss  = where(b eq 'aa55'x, nss)
		for ii=0,nss-1 do begin
		    if (keyword_set(simple)) then begin
			ist = ss(ii)
			ien = (ist + 20) < 198
			print,       irec, b(ist:ien), format = '(i6,3x, 36(1x,z4.4))'
			if (lun ne 0) then printf, lun, irec, b(ist:ien), format = '(i6,3x, 36(1x,z4.4))'
		    end else begin
		        pkid = b2(12)		;unsign(rec(6), 16)
		        qtrunc = mask(pkid, 6, 1)
			extra = ''
			if (ii eq 0) and (qtrunc) then extra = '^Truncated^'
			;
			ist = ss(ii)
			ien = (ist + 20) < 198
			hdr = intarr(40)-1
			hdr(0) = b(ist:ien)
			reftime_sci = long(hdr, 2)
			;
			qnew_reftime = (reftime_sci ne last_reftime)
			last_reftime = reftime_sci
			if (qnew_reftime) then cnt_minutes = cnt_minutes + 1
			;;if (qnew_reftime) then print, ' '
			tit = "   Frm    Minute  Date    Pkg Time  SegLen   NpixTotal  NpixImage  NpixSeg  Seg#  N K    DPC"
			      ;     10     1  18-SEP-95  23:52:51               7168      14509     3584     3   0 0  bfffffb0
                        tit = $
"  Frame Min     Date      Time    SegL  PixTot PixImg  PixSeg Seg#  N K   DPC"
;     36   1   6-JAN-96  13:25:43  xxxx   3628   14509   3628   4   14 5 03006001

			if (qnew_reftime) then print, tit
			print, irec, cnt_minutes, fmt_tim(daytim), format = '(1x, i6, i4, 2x, a, $)'
			if (lun ne 0) then printf,lun, irec, cnt_minutes, fmt_tim(daytim), format = '(1x, i6, i4, 2x, a, $)'
                        print,       hdr(4), format = '(i6, $)'              ; DP Seg Len
                        if (lun ne 0) then printf, lun, hdr(4), format = '(i6, $)'              ; DP Seg Len
			l_val(0) = hdr(6) &  l_val(1) = hdr(5)
			print,       long(l_val,0), format = '(i7, $)'       ; NpixTotal
			if (lun ne 0) then printf, lun, long(l_val,0), format = '(i7, $)'       ; NpixTotal
			l_val(0) = hdr(18) &  l_val(1) = hdr(17)
			print,       long(l_val,0), format = '(i8, $)'       ; NpixImage
		        if (lun ne 0) then printf,lun,  long(l_val,0), format = '(i8, $)'       ; NpixImage
			;
			print,       hdr(7), format = '(i7, $)'              ; NpixSegment
			print,       hdr(8), format = '(1x, i3, $)'              ; Segment Number
			print,       hdr(12), hdr(13), format = '(3x, 2i2, $)'   ; N, K
			print,       long(hdr,30), extra, format = '(1x, z8.8, 2x, a)'         ; DPC
			if (lun ne 0) then printf, lun, hdr(7), format = '(3x, i4, $)'              ; NpixSegment
	                if (lun ne 0) then printf, lun, hdr(8), format = '(1x, i3, $)'              ; Segment Number
			if (lun ne 0) then printf, lun, hdr(12), hdr(13), format = '(3x, 2i2, $)'   ; N, K
			if (lun ne 0) then printf, lun, long(hdr,30), extra, format = '(1x, z8.8, 2x, a)'         ; DPC

	;
	; Deal with roll -- this duplicates code in DISPLOI_MON5k!
	;
			DPC = long(hdr, 30)
			qloi = mask(dpc,12,4) eq 7  ; LOI uses IRBIN list
			qcont = mask(dpc,24,4) eq 7 ; continuum == not doppler
		
			if(qloi and (qcont eq 0)) then begin
				img = unsign(fix(b(ist+19:ist+19+179))) ; loc. rel. to start of packet
				reftime_sci_str = fmt_tim(anytim2ints('1-Jan-58',off=reftime_sci*1D))
				roll = calc_loi_roll(img,tref=reftime_sci_str,method=rmethod)
				print,"LOI DOPPLER ROLL: ",reftime_sci_str,'      ',roll,'  ('+rmethod+')',format='(A,A,A,G,A)'
				if isvalid(roll_lun) then $
				    if(roll_lun ne 0) then begin
					printf,roll_lun,tref,'      ',roll,'  ('+rmethod+')' $
						,format='(A,A,G,A)'
			end
		    end
		    none_cnt = 0
		  end
	      end

    end

    if (lun ne 0) then flush, lun

end
