;+
;
; NAME: 
;       READ_EPHEM
;
; PURPOSE:
;
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;	READ_EPHEM, FILENAME, DAYTIMES, NDAYS, NITETIMES, NNITES
;
;
; CALLS:
;	RD_ASCII, UTIME
;
; INPUTS:
;       Filename
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;	Daytimes - Times of entry into orbital days in seconds from 1-jan-1979.
;	Ndays    - Number of day crossings.
;	Nitetimes- Times of entry into orbital night.
;	Nnites   - Number of night crossings.
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
; 	READ_EPHEM reads a GRO ephemeris file copied from the GROSSC and returns
; 	arrays of day and night start times.
; 	(The files are copied from grossc::public:[plan_dat.batse]*.*)
;	
;	On 12-March-1997 the default source of the ephemeris files is from MSFC
;	here is a message on this subject from Anne Shaver:
;	I spoke with Bill Henze about the ephemeris files.  They will be sending them 
;	directly to us via email in the future.
;
;	Anne
;
; MODIFICATION HISTORY:
; 	Kim Tolbert  May 1992
;
;-
pro read_ephem, filename, daytimes, ndays, nitetimes, nnites

; Changed 9/24/93 by akt.  On alpha, can't use on_ioerror, so call
; read_seqfile instead.
;openr, lun, filename, /get_lun
;filestat = fstat(lun)
;nrec = filestat.size / 80 + 100
;rec = strarr(nrec)
;readfile:
;on_ioerror, goterror
;readf, lun, rec
;nrec = nrec + 500
;close, lun & free_lun, lun
;openr, lun, filename, get_lun
;goto,readfile
;
;goterror:
;close, lun & free_lun, lun
;if (strpos(strmessage(!err),'End of file') eq -1) then goto,errorexit

;read_seqfile, rec, filename
rec = rd_ascii( filename )

days = where (strmid(rec,0,4) eq 'ODAY')
ndays = n_elements (days)
daytimes=dblarr(ndays)
if (strmid(rec(days(0)),13,3)*1 lt $
    strmid(rec(days(ndays-1)),13,3)*1) then begin 
   daytimes(*) = utime(strmid(rec(days(0)),10,2) + '/1/1,0')
endif else begin
   for i=0,ndays-1 do daytimes(i) = utime(strmid(rec(days(i)),10,2) + $
       '/1/1,0') 
endelse
daytimes = daytimes  + (strmid(rec(days),13,3)-1)*86400.d0 + $
                       strmid(rec(days),17,2) * 3600.d0 + $
                       strmid(rec(days),20,2) * 60.d0 + $
                       strmid(rec(days),23,2)
nites = where (strmid(rec,0,4) eq 'ONIT')
nnites = n_elements (nites)
nitetimes=dblarr(nnites)
if (strmid(rec(nites(0)),13,3)*1 lt $
    strmid(rec(nites(nnites-1)),13,3)*1) then begin 
   nitetimes(*) = utime(strmid(rec(nites(0)),10,2) + '/1/1,0')
endif else begin
   for i=0,nnites-1 do nitetimes(i) = utime(strmid(rec(nites(i)),10,2) + $
       '/1/1,0') 
endelse
nitetimes = nitetimes  + (strmid(rec(nites),13,3)-1)*86400.d0 + $
                       strmid(rec(nites),17,2) * 3600.d0 + $
                       strmid(rec(nites),20,2) * 60.d0 + $
                       strmid(rec(nites),23,2)

if nitetimes(0) lt daytimes(0) then begin
   nitetimes = nitetimes(1:*)
   nnites = nnites - 1
endif
if ndays ne nnites then begin
   ndays = (ndays < nnites) & nnites = ndays
   days = days(0:ndays-1) & nites = nites(0:nnites-1)
endif

print,' file=',filename
;for i=0,(ndays<nnites)-1 do print,atime(daytimes(i)),'  ',atime(nitetimes(i))
goto,getout

errorexit:
print,'Error reading file ',filename
error=1

getout:
return&end
