function rd_srspas, infil, qdebug=qdebug, outfil=outfil, sort=sort, barr=barr, to_inf=to_inf
;
;+
;NAME:
;	rd_srspas
;PURPOSE:
;	To read the SIRIUS database listing file contents into
;	a structure
;CALLING SEQUENCE:
;	out = rd_srspas()
;OPTIONAL KEYWORD INPUT:
;	infil	- The input file name.  If not set, then it
;		  uses /ys/site/logs/srspas.new
;	sort	- If set, return the data sorted by time
;OPTIONAL KEYWORD OUTPUT:
;	barr	- The byte array 80xN of the input file
;HISTORY:
;	Written 4-Jan-93 by M.Morrison
;	13-Jan-93 (MDM) - made infil a positional parameter instead
;			  of a keyword parameter
;	 8-Feb-93 (MDM) - added BARR output 
;	 6-May-93 (MDM) - Added /TO_INF option to read TO_INF script results
;	22-May-93 (MDM) - Added check to see if the file existed
;	 7-Oct-93 (MDM) - Replaced /ys with $ys
;	 7-may-95 (SLF) - problem if nulls in srspas (shuto "fix")
;-
;
if (not keyword_set(infil)) then infil = '$ys/site/logs/srspas.new'
;
if (not file_exist(infil)) then return, 0		;Added 22-May-93
;
spawn, 'wc -c ' + infil, result
arr = str2arr(strtrim(strcompress(result(0)),2), delim=' ')
nchar = long(arr(0))
;
openr, lun, infil, /get_lun
lin = ' '
on_ioerror, skip
readf, lun, lin
skip:
on_ioerror,null
free_lun, lun
if (strlen(lin) eq 50) then ncharlin=51 else ncharlin=80	;SRSPAS results have 80 characters per line (no CR)
								;The full mission listing has 50 characters + CR
if (keyword_set(to_inf)) then ncharlin=81
n = nchar/ncharlin
;
if (n eq 0) then begin
    message, 'Input file: ' + infil + ' has nothing in it', /info
    return, 0b
end
;
;
openr, lun, infil, /get_lun
barr = bytarr(ncharlin, n)
readu, lun, barr
str = string(barr)
free_lun, lun
;
chk=strcompress(str,/remove)
nnull=where(chk ne '',nncnt)
if nncnt gt 0 then str=str(nnull)
n=n_elements(str)
out0 = {rd_srspas, line: ' ',  source: ' ', path: ' ', nrec: 0L, time: 0L, day: 0, duration: 0.}
out = replicate(out0, n)

for i=0,n-1 do begin
    str0 = str(i)
    ;SA229 9212220900 92/12/22 12:44:53 12:53:25 016384
    ;0123456789012345678901234567890123456789012345678901234567890123456789
    ; 14   SA222    9304241400   93/04/24 20:09:57 - 20:18:29    9  16384  1 0 0 0 1
    if (not keyword_set(to_inf)) then begin
	out(i).line 	= strmid(str0, 0, 50)
	out(i).source 	= strmid(str0, 0, 5)
	out(i).path	= strmid(str0, 6, 10)
	out(i).nrec	= long(strmid(str0, 44, 6))

	date_slash	= strmid(str0, 17, 8)
	st_time 	= strmid(str0, 26, 8)
	en_time		= strmid(str0, 35, 8)
    end else begin
	out(i).line 	= strmid(str0, 6, 74)
	out(i).source 	= strmid(str0, 6, 5)
	out(i).path	= strmid(str0, 15, 10)
	out(i).nrec	= long(strmid(str0, 62, 6))

	date_slash	= strmid(str0, 28, 8)
	st_time 	= strmid(str0, 37, 8)
	en_time		= strmid(str0, 48, 8)
    end
    time1 	= anytim2ints(date_slash+' '+st_time)
    time2 	= anytim2ints(date_slash+' '+en_time)

    if (int2secarr(time2, time1) lt 0) then time2 = anytim2ints(time2, off=86400.)	;wrap around - add one day
    out(i).time = time1.time
    out(i).day	= time1.day
    out(i).duration = int2secarr(time2, time1)
end
;
if (keyword_set(qdebug)) then stop
;
if (keyword_set(outfil)) then begin
    openw, lun, outfil, /get_lun
    xx = int2secarr(out)
    ss = sort(xx)
    for i=0,n-1 do printf, lun, out(ss(i)).line
    free_lun, lun
end
;
if (keyword_set(sort)) then begin
    xx = int2secarr(out)
    ss = sort(xx)
    out = out(ss)
    barr = barr(*,ss)
end

return, out
end
