function get_sunset, sttim, entim, fem_night=fem_night, $
	duration=duration, quiet=quiet
;
;+
;NAME:
;	get_sunset
;PURPOSE:
;	To run the AOSLOS program and to find the times of sunset to
;	a higher accuracy than the FEM files provide.
;CALLING SEQUENCE:
;	times = get_sunset(sttim, entim)
;	prstr, get_sunset('18-aug-93', '20-aug-93')
;INPUT:
;	sttime	- The starting time to search for
;OPTIONAL INPUT:
;	entim	- The ending time to search for.  If entim is not passed,
;		  then entim is set to be "duration" hours after sttim.
;OPTIONAL KEYWORD INPUT:
;	duration- The number of hours to calculate the sunset time starting
;		  from "sttim".  If it is not passed in, and "entim" is not
;		  passed, then duration will default to 12 hours
;OUTPUT:
;	times	- The times of the transition from day to night with
;		  a one second accuracy.
;OPTIONAL KEYWORD INPUT/OUTPUT:
;	fem_night- The sunset times as predicted by FEM
;HISTORY:
;	Written 16-Aug-93 by M.Morrison
;	24-Aug-93 (MDM) - Changed to run AOSLOS program from +/- 120
;			  seconds (instead of +/- 60 sec)
;	16-Mar-94 (MDM) - Modified to not call RD_FEM if the time is
;			  passed in as FEM_NIGHT (made FEM_NIGHT 
;			  optional input as well as output)
;	13-Dec-94 (LWA) - Added quiet=quiet keyword.
;-
;
;
out = ''
;
if (n_elements(fem_night) eq 0) then begin
    if (n_elements(duration) eq 0) then duration = 12
    if (n_elements(entim) eq 0) then entim = anytim2ints(sttim, off=duration*60.*60)

    rd_fem, anytim2ints(sttim, off=-90*60.), entim, fem
if n_elements(quiet) ne 0 then print, n_elements(fem), ' day/night transitions in this time span'
    fem_night = fmt_tim(anytim2ints(fem, off=fem.night))
end
;
for i=0,n_elements(fem_night)-1 do begin	;
    fem_night0 = fem_night(i)
    sttim1 = anytim2ints(fem_night0, off=-120)	;back up 120 seconds
    entim1 = anytim2ints(fem_night0, off=+120)	;
    ;
    outdir = getenv('HOME')
    interval = 1	;1 second steps
    tarr = anytim2ex(sttim1)
    num = int2secarr(entim1, sttim1)/interval
    param = [tarr(6), tarr(5), tarr(4), tarr(0), tarr(1), 0, interval, num]
if n_elements(quiet) ne 0 then print, 'GET_SUNSET: Running AOSLOS program for: ' + fmt_tim(sttim1) + ' to ' + fmt_tim(entim1)
    run_dsnfil, param, path=outdir, file_out=aoslos_file

    openr, lun, aoslos_file, /get_lun
    irec = 0L
    qeof = 0
    while (not qeof) do begin
	rd_orbit_event, lun, orbit, start_rec=irec, end_rec=irec, eof=qeof	
	orbit = orbit(0)
	if (irec eq 0) then lorbit = orbit
  
	timarr_ut = orbit.timarr

	if (orbit.sc_day eq '*') then begin		;start of night
if n_elements(quiet) ne 0 then print, 'S/C Night ', fmt_tim(timarr_ut)
	    out = [out, fmt_tim(timarr_ut)]
	    qeof = 1
	end
	irec = irec + 1
    end

    free_lun, lun
    spawn, 'rm -f ' + aoslos_file
end
;
if (n_elements(out) eq 1) then begin
    message, 'No Day/Night transistion found', /info
end else begin
    out = out(1:*)
end
;
return, out
end

