pro ip_que_dmpver, date, src_infil, mem=mem, map=map, src=src, $
		sci_pkt=sci_pkt, dir=dir, qdebug=qdebug, window_hr=hrs, recs=out, $
		qstop=qstop
;+
;NAME:
;	ip_que_dmpver
;PURPOSE:
;	To verify an IP queue dump
;SAMPLE CALLING SEQUENCE:
;	ip_que_dmpver, '12-JUN-95  18:24:32', mem=mem
;	ip_que_dmpver, '12-Jun-95  18:24:32', 'dlimg.src', dir='/mdisw/cfl/eof/src'
;INPUT:
;	date_time - The approximate date and time of the dump is needed
;		    to know what portion of the SCI5K data file to read.
;		    The default is to check a 2 hour window centered on
;		    the requested time.
;	src_infil - The file to check for in the memory dump
;OPTIONAL KEYWORD INPUT:
;	window	- The number of hours to check on either side of the input time
;		  (the default is 1 hour)
;	dir	- The directory to look for the .src file (defaults to
;		  /mdisw/cfl/eof/src)
;OPTIONAL KEYWORD OUTPUT:
;	mem	- The memory dump in a byte array
;	sci_pkt	- The sci5k packets with IP buffer dump data in it
;	out	- All sci5k packets over the time span
;	src	- The binary IP queue load (as decoded from the .src file)
;	map	- A 8192 word map of the IP queue memory space
;	recs	- The SCI5K packets read from the file
;RESTRICTIONS:
;	If the time period spans midnight GMT, it will be a problem
;HISTORY:
;	Written 18-Sep-95 by M.Morrison (taking interactive work of Jun-95)
;	31-Oct-95 (MDM) - Added RECS keyword output
;	 3-Nov-95 (MDM) - Added informational statements
;	 8-Nov-95 (MDM) - Added /QSTOP
;	12-Nov-96 (MDM) - Modified the word delimiting the load command
;-
;
;-------- Getting the dump from the SCI5K telemetry stream
;
if (n_elements(date) eq 0) then date = !stime
if (n_elements(hrs) eq 0) then hrs = 1
;
tarr = anytim2ex(date)
fid = ex2fid(tarr)
sttim = anytim2ints(date, off=-hrs*60*60.)
entim = anytim2ints(date, off= hrs*60*60.)
strec = gt_time(sttim)/1000.  *24/15.		;24 packets per major frame (15sec)
enrec = gt_time(entim)/1000.  *24/15.		;24 packets per major frame (15sec)
;
strec = strec > 0 < 138239
enrec = enrec > 0 < 138239
infil = concat_dir(getenv('MDI_SCI5K_DIR'), strmid(fid, 0, 6) + '.sci')
;
out = rd_hk(infil, strec, enrec, /new)
if (data_type(out) ne 8) then begin
    message, 'SCI5K read resulted in no data at all.', /info
    return
end
;
pkt_types = [out.hk(0)]
upkt_types = pkt_types(uniq(pkt_types,sort(pkt_types)))
print, 'Packet types for that period (IP Queue Dump = 0xFB) :'
print, upkt_types, format='(5x, 8z3)'
ss0 = where(out.hk(0) eq 'fb'x, nss0)
if (nss0 eq 0) then begin
    print, 'No IP buffer dumps during the period from ' + fmt_tim(sttim) + ' to ' + fmt_tim(entim)
    return
end
sci_pkt = out(ss0)
mem = sci_pkt.hk(2:*)
;
print, 'Found ', nss0, ' packets which are IP Queue dumps'
times = fmt_tim(anytim2ints('1-jan-70', off=double(sci_pkt.time)))
fmt_timer, times
;
if (keyword_set(qdebug)) then print, nss0, ' SCI5K packets with IP buffer dumps'
;
;--------- Decoding the load file
;
if (n_elements(dir) eq 0) then dir = '/mdisw/cfl/eof/src'
if (n_elements(src_infil) eq 0) then src_infil = 'dlimg.src'
infil = concat_dir(dir, src_infil)
if (not file_exist(infil)) then begin
    print, 'Cannot find file: ' + infil
    return
end
;
mat = rd_tfile(infil,nocomm='#')
stadd = '1f00'
;
arr = arr2str(mat, delim=' ')
arr = str_replace(arr, '\',  '')
;;arr = str2arr(arr, 'tcblock mbipcmd 0x0091')
arr = str2arr(arr, 'tcblock mbipmacl')		;MDM 12-Nov-96
arr = strtrim(strcompress(arr), 2)
arr = arr(1:*)          ;drop the first junk
n=n_elements(arr)
arr(n-1) = strmid(arr(n-1), 0, strpos(arr(n-1)-1, 'tcblock'))
;
ss = where(arr ne '')
arr = arr(ss)
n=n_elements(arr)
arr = str_replace(arr, '0x', '')
st_addr = 0L
reads, arr(0), st_addr, format='(z)'
;
if (n_elements(map) ne 8192) then map = intarr(8192)
for i=0,n-1 do begin
    lin = arr(i)
    arr2 = str2arr(lin, delim=' ')
    n2 = n_elements(arr2)
    v = intarr(n2)
    reads, arr2, v, format='(z)'
    map(v(0)) = v(1:*)          ;insert into map array
    if (i eq 0) then src = v(1:*) else src = [src, v(1:*)]
end
;
;------- Verification checks
;
bsrc = byte(src, 0, n_elements(src)*2)
ss=where_pattern(mem, bsrc(0:12), nss)
if (nss eq 0) then begin
    print, infil, ' pattern match not found in buffer dump
    return
end
;
n = n_elements(sci_pkt)
dt = deriv_arr(sci_pkt.time)
ss_tim = where(dt gt 10, nss_tim)	;if several dumps, mark where each one starts
st_rec = lonarr(n)
for i=0,nss_tim-1 do st_rec( ss_tim(i):* ) = ss_tim(i)

print, 'Source file: ' + infil
print, 'Expected start address = ', st_addr, format='(a, z4.4)'
print, '    BEWARE: We can be missing single SCI5K packets which will put the address
print, '            off by multiples of 384 bytes (180 hex) or 191 words (0C0 hex).
print, ' Word#_In_Dump      Delta_From_Exp         Date/Time
print, ' '
       ;  7552   1d80        384   0180        12-JUN-95  18:25:09
for i=0,nss-1 do begin
    diff = mem(ss(i):ss(i)+n_elements(bsrc)-1)  - fix(bsrc)
    if (min(diff)+max(diff) eq 0) then begin
	irec = ss(i) / 384
	ibyte0 = ss(i)/2				;not corrected for multiple dump case
	ibyte = ibyte0 - st_rec(irec)/2
	tim = fmt_tim(anytim2ints('1-jan-70', off=double(sci_pkt(irec).time)))
	print, ibyte, ibyte, st_addr-ibyte, st_addr-ibyte, tim, format='(2(i6, 3x, z4.4,5x), 3x, a)'
    end else begin
	print, 'Found a partial match but not complete match
	print, n_elements(where(diff eq 0)), ' match out of ', n_elements(bsrc), ' BYTES'
	if (keyword_set(qstop)) then stop
    end
end
;
;;tim=out2.time + out2.ccsds2(0)/256D
;;daytim = anytim2ints('1-jan-70', off=tim)
;
end