pro rd_ads, infil0, header, data, roll_only=roll_only, qtest=qtest, qprint=qprint
;
;
;
;HISTORY:
;	Written ?, by M.MOrrison
;	23-May-93 (MDM) - Modified to check if the file is compressed
;			  If so, uncompress it, read it, and re-compress it
;	11-Aug-93 (MDM) - Added /ROLL_ONLY option.  The default is to
;			  read the full contents of the ADS file
;			- Added capability to read on non-DEC machines
;	12-Jan-95 (MDM) - Changed read loop from WHILE NOT EOF to a
;			  FOR loop.  Handles case where there is a 
;			  partial record.
;
if (keyword_set(roll_only)) then begin
    data_ref = {proc2_att_data, time: long(0), $
		roll: long(0) }
end else begin
    data_ref = {proc_att_data, time: long(0), $ 
		position: lonarr(3), $
		deviation: lonarr(3), $
		motion: lonarr(3), $
		status: long(0) }
end
;
header = {proc_att_header, id: ' ', day: fix(0), time: long(0)}
;
infil = infil0
;
qcompress = 0
if (strmid(infil, strlen(infil)-1, 1) eq 'Z') then begin
    print, 'Decompressing ', infil
    spawn, 'uncompress "' + infil + '"'
    qcompress = 1
    infil = strmid(infil, 0, strlen(infil)-2)
end
;
get_lun, lun
openr, lun, infil, /block
f = fstat(lun)
nrec = f.size/64 - 3
data = replicate(data_ref, nrec)
;
barr = bytarr(64, 4)
readu, lun, barr
header.id	= str_ebc2asc(barr(2:11), /str)		;does not work any more?!?!
tarr = intarr(7)
tarr(6) = barr( 5,1)	;year	- guessing
tarr(5) = barr( 9,1)	;month
tarr(4) = barr(13,1)	;day
tarr(0) = barr(17,1)	;hour
tarr(1) = barr(21,1)	;minute
tarr(2) = barr(25,1)	;second
tarr(3) = barr(28,1)*100 + barr(29,1)	;millisec
ex2int, tarr, time, day
header.day  = day
header.time = time
;
noswap_os=['vms','ultrix','OSF']                ; list of noswap (extensible)
chk=where(!version.os eq noswap_os,nscount)     ; current OS in noswap list?
qswap = nscount eq 0                            ; assign boolean
;
irec = 0L
;;while not eof(lun) do begin
for irec=0L, nrec-1 do begin
    ibyt = 64*3 + irec*long(64)		;skip header
    point_lun, lun, ibyt
    ;
    barr = bytarr(64)
    readu, lun,barr 
    ;if (keyword_set(qprint)) then print, barr(0:19)
    if (keyword_set(qprint)) then print, barr(20:39)
    barr = barr(2:*)		;trim off first two bytes - why??
    ;
    ii = fix(barr, 0, 30)
    ii2 = ii 
;;  ss = indgen(16)*2		;byte-pair swap
;;  ii2(ss+1) = ii(ss)
;;  ii2(ss) = ii(ss+1)
;;  rec = long(ii2, 0, 15)	;convert to integer*4
    ;
    ss=59-indgen(60)		;59,58,57, ... 3,2,1,0
    rec0 = barr(ss)		;reverse byte order
    rec0 = long(rec0, 0,15)	;make into integers
    ss=14-indgen(15)
    rec = rec0(ss)		;reverse order again
    ;
    if (qswap) then dec2sun, rec
    ;
    if (keyword_set(roll_only)) then begin
	data(irec).time = rec(0)
	data(irec).roll = rec(3)	;roll value
    end else begin
	data(irec).time = rec(0)
	data(irec).position = rec(1:3)
	data(irec).deviation = rec(4:6)
	data(irec).motion = rec(7:9)
	data(irec).status = rec(10)
    end
    ;
    ;;irec = irec + 1
end
;
free_lun, lun
;
if (qcompress) then begin
    print, 'Compressing ', infil
    spawn, 'compress "' + infil + '"'
end

end
