;+
pro rdsci, fn
; 
; Reads and displays one data block from 5k data files.
; The argument specifies the base file name, without the number and extension.
;
; 5k data files contain upto 50 records of 424 bytes each.
; The records contain a 42 byte header followed by a 382 byte data block.
;-

hdr = bytarr(32)
pkt = bytarr(392)
num = 1
status = 0
recnum = 0

filename = '$MDI_SCI5K_DIR/'+ fn + string (num, format = '("_", i2.2, ".sci")')
openr, lun, filename, /get_lun, error = status

i_end2 = 0
while (status eq 0) do begin
    recnum = 0
    while (not eof(lun)) do begin
;       print, filename, recnum
        readu, lun, hdr, pkt
;       print, hdr, format = '(16z4)'
;       print, ' '
;       print, pkt(0:3) and 'ffff'x, format = '(4z5.4)'
;       print, ' '
;       print, pkt(4:*) and 'ffff'x, format = '(12z5.4)'
;       print, ' '
       
        i = 4
        if (i_end2 ne 0) then begin
            print, recnum, i, pkt(i:i_end2) and 'ffff'x, format = '(i6, ":", i4, ">", 19(z2.2, z2.2, 1x))'
            i_end2 = 0
        endif

        while (i lt 390) do begin
            if ((pkt(i) eq 'aa'x) and (pkt(i+1) eq '55'x)) then begin
                i_end = i + 38
                if (i_end ge 392) then begin
                    i_end2 = i_end - 389
                    i_end = 391
                endif
                print, recnum, i, pkt(i:i_end-1) and 'ffff'x, format = '(i6, ":", i4, ">", 19(z2.2, z2.2, 1x))'
            endif
            i = i + 1
        endwhile

        ; r = get_kbrd (1)
        recnum = recnum + 1
    endwhile
    close, lun
    num = num + 1
    filename = '$MDI_SCI5K_DIR/'+ fn + $
               string (num, format = '("_", i2.2, ".sci")')
    openr, lun, filename, error = status
endwhile

exit:
return
end

