pro rd_orbit_event, lun_filnam, out, start_rec=start_rec, end_rec=end_rec, $
			orbit_params=orbit_params, eof=eof_flag, 	   $
			max_rec=max_rec, newfmt=newfmt, plus=plus
;+
;  NAME:
;    rd_orbit_event
;
;  PURPOSE:
;    Read orbital predictions file written by dsnfil_XXX (AOSLOS) FORTRAN program.
;
;  CALLING SEQUENCE:
;    rd_orbit_event, lun_filnam, out, start_rec=start_rec, end_rec=end_rec
;    rd_orbit_event, lun_filnam, out, start_rec=start_rec, end_rec=end_rec, $
;				/new,/plus
;
;  INPUTS:
;    lun_filnam	AOSLOS file name or the unit number of an opened data
;		base file.
;  OUTPUTS:
;    out	Structure containing contents of the file.
;
;  OPTIONAL INPUT KEYWORDS:
;    start_rec	  = Starting record number (fixed-length records assumed).
;		    start_rec = 0 is the first non-header record
;    end_rec	  = Ending record number   
;
;  OPTIONAL OUTPUT KEYWORDS:
;    orbit_params = String of the time of the orbital parameters.
;    eof_flag	  = Set to 1 when EOF is reached or the next record is the EOF.
;    max_rec	  = Maximum value of end_rec that is allowed
;    newfmt	  = If set, return the new style format (incs. Wallops columns)
;    plus	  = If set, change  'U+' to '+' to be consistent with earlier
;		    versions of the dsnfil (AOSLOS) format.
;		    If n_elements(newfmt) and n_elements(plus), this will cause 
;		    plus to default to /plus.
;
;  RESTRICTIONS:
;    If start_rec is specified, it must be greater than 0 and less than
;    the number of non-header records in the file.  There are 12 header records.
;
;  MODIFICATION HISTORY:
;    5-Oct-91, Written, MDM/JRL
;	12-Oct-91 MDM	updated "i" to be long word type
;	12-Oct-91 MDM	- Changed array dimension size to
;			  divide the size by 138, not 137
;			  (there is a CR in there)
;	15-oct-91 JRL	Updated: Added START_rec, END_rec, keywords
;			Uses point_lun to get to the starting record in
;			the file.  Changed the time format in the
;			returned structure to reflect the standard
;			Yohkoh External Time format (d,m,s,ms, d,m,y).
;	 6-Mar-95 JRL   Many changes to eliminate format read statments that
;		        were causing problems for OSF IDL V3.6.
;	15-Mar-95 JRL	Changed so that the routine works out the record
;			length automatically in preparation for Wallops.
;	20-Mar-95 JRL   Enable the code to read the columns that contain Wallops info.
;-

if n_params() eq 0 then begin
   print,'rd_orbit_event, lun_filnam, out[, start_rec = start_rec, $'
   print,'        end_rec = end_rec, orbit_params=orbit_params, eof = eof_flag]'
   return
endif

;  Check if lun_filnam is a string (file name) or a number (unit number) 

ss = size(lun_filnam)
if ss(n_elements(ss)-2) ne 7 then begin		; lun_filnam is a unit number
  lun_flag = 0					; Don't close the file on exit
  lun = lun_filnam
endif else begin				; lun_filnam is a file name
  openr, lun, lun_filnam, /get_lun		; Open the file
  lun_flag = 1					; Close the file on exit
  lin = '  '
  readf, lun, lin				; Read the orbital parameters
  i = strpos(lin,' AT ')+4
  orbit_params = strtrim(strmid(lin,i,strlen(lin)),2)
endelse

; Get the size of a record -- assume that the file is fixed length
point_lun,lun,0					; Move to the beginning
buff = '' & readf,lun,buff			; Read a record
f = fstat(lun)					; There are 12 header records
rec_size = f.cur_ptr				; Current position = size
nrec = long(f.size-12*float(rec_size))/rec_size	; Number of non-header records 
max_rec = nrec-1				; Maximum valid record number

if (n_elements(end_rec)   eq 0) then end_rec   = max_rec
if (n_elements(start_rec) eq 0) then start_rec = 0 
if end_rec lt start_rec then end_rec = start_rec

;  Check that 0<= star_rec <= max_rec

if (start_rec lt 0) or (start_rec gt max_rec) then begin
   print,'   ***********   Error in rd_orbit_event   **************'
   print,'                 Illegal value of start_rec = ',start_rec
   print,'                 Must be 0 <= start_rec <=',long(max_rec)
   stop
endif


point_lun,lun,(start_rec+12L)*rec_size		; Position to start record
nsize = (end_rec<max_rec - start_rec + 1)	; Set size of output structure

; ------------------------------
; Set up the control variables
; ------------------------------

; /plus is the default if newfmt is undefined
; plus = 0 is the default if newfmt is defined as either 0 or 1
; This will make this version compatible with previous versions

if keyword_set(newfmt) then qnewfmt = 1 else qnewfmt = 0
if keyword_set(plus) then qplus = 1 else qplus = 0
if (n_elements(newfmt)  eq 0) and (n_elements(plus) eq 0) then qplus = 1

; ------------------------------
; Set up the output variable
; ------------------------------

if qnewfmt then begin
  out = {orbit_event, path:' ', timarr: intarr(7), sat_pos: fltarr(4),     $
        station: strarr(2), aquis: fltarr(3,2), antenna: strarr(2),        $
	sun: fltarr(2), sc_day: ' ', geo_mag: fltarr(2), earth: fltarr(2), $
        rig: float(0), saa:' ', elcano: float(0), canopus: ' '}
endif else begin
  out = {orbit_event_old, path:' ', timarr: intarr(7), sat_pos: fltarr(4), $
        station: ' ', aquis: fltarr(3), antenna: ' ',                      $
	sun: fltarr(2), sc_day: ' ', geo_mag: fltarr(2), earth: fltarr(2), $
        rig: float(0), saa:' ', elcano: float(0), canopus: ' '}
endelse
out = replicate(out, nsize)


if nsize+start_rec-1 lt max_rec then eof_flag = 0 $	; Have not hit the end of file yet
			        else eof_flag = 1	; Will read to the end of the file

;--------  Read the file -----------
buff = bytarr(rec_size,nsize)
readu,lun,buff
if lun_flag then free_lun,lun 		; Close if lun_flag = file name
buff = string(buff)

; -------- Assign the values -------
    out.path		= strmid(buff,1,10)
    out.timarr(6) 	= fix(strmid(out.path,0,2))	; Get the year from the path
    out.timarr(5) 	= fix(strmid(buff,11,3))	; Month
    out.timarr(4) 	= fix(strmid(buff,14,3))	; Day
    out.timarr(2) 	= fix(strmid(buff,23,3))	; sec
    out.timarr(1) 	= fix(strmid(buff,20,3))	; min
    out.timarr(0) 	= fix(strmid(buff,17,3))	; hour
    out.sat_pos(0)	= float(strmid(buff,27,6))
    out.sat_pos(1)	= float(strmid(buff,33,6))
    out.sat_pos(2)	= float(strmid(buff,39,7))
    out.sat_pos(3)	= float(strmid(buff,46,7))
    if qnewfmt then $
      out.station(0)	= strmid(buff,54,2) else $
      out.station	= strmid(buff,54,2)		; Bec. of curious IDL feature
    out.aquis(0,0)	= float(strmid(buff,56,6))
    out.aquis(1,0)	= float(strmid(buff,62,6))
    out.aquis(2,0)	= float(strmid(buff,68,7))
    if qnewfmt then $
      out.antenna(0)	= strmid(buff,77,1) else $
      out.antenna	= strmid(buff,77,1)		; Bec. of curious IDL feature
    out.sun(0)		= float(strmid(buff,78,8))
    out.sun(1)		= float(strmid(buff,86,7))
    out.sc_day		= strmid(buff,93,1)
    out.geo_mag(0)	= float(strmid(buff,96,5))
    out.geo_mag(1)	= float(strmid(buff,101,6))
    out.earth(0)	= float(strmid(buff,107,7))
    out.earth(1)	= float(strmid(buff,114,6))
    out.rig		= float(strmid(buff,120,6))
    out.saa		= strmid(buff,126,1)
    out.elcano		= float(strmid(buff,127,6))
    out.canopus		= strmid(buff,133,1)		; When Canopus is visible to STT
  if qnewfmt and (rec_size ge 137) then begin
    out.station(1)	= strmid(buff,54+81,2)
    out.aquis(0,1)	= float(strmid(buff,56+81,6))
    out.aquis(1,1)	= float(strmid(buff,62+81,6))
    out.aquis(2,1)	= float(strmid(buff,68+81,7))
    out.antenna(1)	= strmid(buff,77+81,1)
  endif

; Change Station ID of low elevation times to "+"
  if qplus then begin
    if (rec_size ge 137) and qnewfmt then j = 1 else j = 0
    for i=0,j do begin
        if qnewfmt then $
	   ii = wc_where(out.station(i),'*+',nc) else $
	   ii = wc_where(out.station   ,'*+',nc)	; Bec. of curious IDL feature

        if nc gt 0 then begin
 	   if qnewfmt then $
             out(ii).station(i) = '+ ' else $
	     out(ii).station    = '+ '			; Bec. of curious IDL feature
        endif
    endfor
  endif
end
