pro contacts, st_date, en_date, qstop=qstop, outfile=outfile, hc=hc, paths=paths, $
	goldstone=goldstone, canberra=canberra, madrid=madrid, wallops=wallops, $
	santiago=santiago, $
	outarr=outarr, qdebug=qdebug, asca=asca, outstr=outstr, quiet=quiet
;
;+
;NAME:
;	contacts
;PURPOSE:
;	To list the times of the KSC contacts for a given day
;CALLING SEQUENCE:
;	contacts		;contacts for today
;       contacts, +1		;contacts for tomorrow
;       contacts, +2		;contacts for day after tomorrow
;	contacts, '1-jan
;	contacts, '30-oct-91', /hc
;	contacts, '30-oct-91', 7;contacts for a 7-day period
;	contacts, '30-oct-91', '2-nov-91', /hc
;INPUT:
;	st_date	- The JST date to search for.  If no date is entered,
;		  the current date is used.  The year is not
;		  required, but if entered should be of the
;		  form 1-Oct-91 style.  The date should be
;		  entered in string style with date first.
; 		- st_date can also be entered as a number which
;		  will specify a number of days from the current day.
;		  Example, contacts,-1 will print yesterday's contacts.
;OPTIONAL INPUT:
;	en_date	- The ending date to list data for.  If no date is passed,
;		  will default to a 24 hour period of time.
;		- en_date can also be entered as a number.  In this
;		  case it will specify the days+st_date to list.
;OPTIONAL KEYWORD INPUT:
;	outfile	- If a filename is passed, the results will also
;		  be written to that file
;	hc	- If set, write the results to a temporary file, print
;		  it and delete it.
;	paths	- If set, include the path ID in the printed information
;	goldstone- If set, return the station contact times for Goldstone
;	madrid	- If set, return the station contact times for Madrid
;	canberra - If set, return the station contact times for Canberra
;	wallops - If set, return the station contact times for Wallops
;	santiago- If set, return the station contact times for Santiago
;		NOTE: Can only call one station at a time.
;	asca	- If set, then print out the contact information for the
;		  ASCA spacecraft
;	outstr	- The string array of the information that was displayed
;		  to the screen (without the header)
;OUTPUT:
;	The times of the KSC contacts are listed.  The first column of
;	times is the start of the contact in JST.  The second column 
;	(in parenthesis) is the start time in UT.  The third column
;	is the time of the end of the contact in JST.  The D: and N:
;	numbers at the end are the number of minutes of day and night
;	for the S/C during the KSC contact.
;OPTIONAL OUTPUT KEYWORD:
;   outarr - string array containing contact information
;METHOD:
;	RD_FEM is called with the UT times and selects all ORBIT START TIMES
;	which are between the specified start and end times.  
;HISTORY:
;	Written Oct-91 by M.Morrison
;	Updated Oct-91 by J.Lemen; Changed file times to yymmdd* format
;				   so elimnate problems at year boundary.
;	12-Mar-92 (MDM) - Adjusted to use the FEM files instead of 
;			  the SUMM files.
;	13-Mar-92 (MDM) - Added the options "outfile", "hc" and "paths"
;			  as well as "goldstone", "madrid" and "canberra"
;	14-Apr-92 (SLF) - Added outarr output parameters
;	20-Apr-92 (MDM) - Added "en_date" option
;	23-Apr-92 (MDM) - print space out between clustering of station 
;			  contacts when writing to output file.
;	24-Apr-92 (MDM) - Removed the 17 hour correction (new IDL fixed
;			  the problem?)
;	 5-Jun-92 (MDM) - When time is passed with the input date, use it
;	24-Jun-92 (MDM) - Added "qdebug" keyword
;	24-Aug-92 (MDM) - Fixed a bug when listing the number of minutes of
;			  daytime and night time for station contacts.
;			  (case where contact end on the NEXT orbit)
;	 7-Apr-93 (MDM) - Added /ASCA option
;	 2-Jun-93 (MDM) - Added OUTSTR option
;	21-Oct-93 (MDM) - Added check that FEM read worked ok
;	16-jun-94 (JRL) - Added capability of entering start time as a delta-day
;			  from the current time.
;			- Added capability of entering stop  time as a number of day
;			  offset from the start time.
;			- Modified so that only passes between the st_date and en_date
;			  are printed out. Changed to pass in st_date-100 min to rd_fem
;			  to make sure to get the previous orbit.
;			- Fixed the bug which caused contacts to crash if no valid
;			  contacts were in the selected interval.
;	22-sep-94 (SLF) - added QUIET keyword (just want the outarr)
;	28-apr-95 (JRL) - Added the /Wallops option
;	16-May-95 (JRL) - Added the /Santiago option
;-
;
loud=1-keyword_set(quiet)

qhc = 0
qout = 0
qdebug = keyword_set(qdebug)
if (keyword_set(outfile)) then begin
    openw, lunout, outfile, /get_lun
    qout = 1
end
if (keyword_set(hc)) then begin
    openw, lunhc, 'contacts.temporary', /get_lun
    qhc = 1
end
;
                                         station = 'U' & station_name = 'Kagoshima Space Center'
if (keyword_set(goldstone)) then begin & station = 'G' & station_name = '       Goldstone (DSN)' & end
if (keyword_set(canberra)) then begin  & station = 'C' & station_name = '        Canberra (DSN)' & end
if (keyword_set(madrid)) then begin    & station = 'M' & station_name = '          Madrid (DSN)' & end
if (keyword_set(wallops)) then begin   & station = 'W' & station_name = '         Wallops (DSN)' & end
if (keyword_set(santiago))then begin   & station = 'S' & station_name = '        Santiago (DSN)' & end
;
if (n_elements(st_date) eq 0) then begin	; Start time not defined
    st_date1 = strmid(!stime, 0, 6)
endif else begin				; Start time came in as a number (+/- days)
    st_date1 = st_date			; Don't corrupt user input
    sz = size(st_date1)
    if N_elements(st_date1) eq 1 and (sz(sz(0)+1) le 4) then st_date1 = addtime(strmid(!stime,0,6),del=st_date1*60.*24.)
endelse

day_arr = anytim2ex(st_date1)		; Initial start time (JST)
day_arr1= addtime(day_arr,del=-9.*60.)	; JST is 9 hour ahead of UT (RD_FEM accepts UT times)

if (n_elements(en_date) eq 0) then begin
   day_arr2 = addtime(day_arr1,del=24.*60) 	;just end 24 hours later
endif else begin
    en_date1 = en_date			; Don't corrupt user input
    sz = size(en_date1)
    if n_elements(en_date1) eq 1 and (sz(sz(0)+1) le 4) then en_date1 = addtime(st_date1,del=en_date1*60.*24.)
    day_arr2 = addtime(en_date1,del=-9.*60.);JST is 9 hour ahead of UT (RD_FEM accepts UT times)
end
;
;rd_fem, fmt_tim(day_arr1), fmt_tim(day_arr2), fem_data
rd_fem, fmt_tim(addtime(day_arr1,del=-100.)), fmt_tim(anytim2ints(day_arr2, off=100*60.)), fem_data, asca=asca
		;go 100 minutes past requested end time to get one more orbit
		;go 100 minutes earlier to get one earlier orbit (JRL 16-jun-94)
if (qdebug) then print, fmt_tim(day_arr1), fmt_tim(day_arr2)
;
if (get_nbytes(fem_data) lt 10) then begin	;added 21-Oct-93
    print, 'CONTACTS: No FEM data available.  Please check your system
    return
end
;
tit = strarr(5)
                                tit(0) = 'Contact Information for Yohkoh.   Program Run: '+ !stime
if (keyword_set(asca)) then     tit(0) = 'Contact Information for ASCA.   Program Run: '+ !stime
tit(2) = '             '+station_name+' Contacts           Minutes of
tit(3) = '                  Starts                   Ends       Day  Ngt  Tot
tit(4) = '         JST              (UT)             JST  
;
for i=0,n_elements(tit)-1 do begin
    if loud then print, tit(i)
    if (qhc) then printf, lunhc, tit(i)
    if (qout) then printf, lunout, tit(i)
end
;
qfirst = 1
outarr=intarr(7)		; slf, init time array
outstr = ' '
;;for i=0,n_elements(fem_data)-1 do for j=0,2 do if (string(fem_data(i).st$station(j)) eq station) then begin
for i=0,n_elements(fem_data)-2 do for j=0,2 do if (string(fem_data(i).st$station(j)) eq station) then begin
    stday_ut  = fem_data(i).day
    sttime_ut = fem_data(i).time + fem_data(i).st_station(j)*1000L
    check_time, sttime_ut, stday_ut
    int2ex, sttime_ut, stday_ut, sttarr_ut
; Don't include unless this pass is between day_arr1 and day_arr2
  if (addtime(sttarr_ut,diff=day_arr1) ge 0) and 	$ 	; After day_arr1
     (addtime(day_arr2,diff=sttarr_ut) ge 0) then begin  	; Before day_arr2
    outarr=[[outarr],[sttarr_ut]]			; slf 14-apr-92
    ;
    stday_jst  = fem_data(i).day
    sttime_jst = fem_data(i).time + fem_data(i).st_station(j)*1000L + 9*60L*60L*1000	;JST is 9 hour ahead of UT
    check_time, sttime_jst, stday_jst
    int2ex, sttime_jst, stday_jst, sttarr_jst
    ;
    enday_jst  = fem_data(i).day
    entime_jst = fem_data(i).time + fem_data(i).en_station(j)*1000L + 9*60L*60L*1000	;JST is 9 hour ahead of UT
    check_time, entime_jst, enday_jst
    int2ex, entime_jst, enday_jst, entarr_jst
    ;
    stdt_ut = anytim2ints(fem_data(i), off=fem_data(i).st_station(j))
    endt_ut = anytim2ints(fem_data(i), off=fem_data(i).en_station(j))
    ;
    dat_jst = gt_day(sttarr_jst, /str)
    dat_ut = gt_day(sttarr_ut, /str)
    st_jst = gt_time(sttarr_jst, /str)
    st_ut = gt_time(sttarr_ut, /str)
    en_jst = gt_time(entarr_jst, /str)
    ;
    nmin_con   = (fem_data(i).en_station(j) - fem_data(i).st_station(j))/60.
    nmin_day   = ((fem_data(i).night - fem_data(i).st_station(j))/60.) >0<nmin_con
    nmin_night = ((fem_data(i).en_station(j) - fem_data(i).night)/60.) >0<nmin_con
    if (int2secarr(endt_ut, fem_data(i+1)) gt 0) then begin	;end time of station contact is in next orbit
	nmin_night = (int2secarr(fem_data(i+1), stdt_ut)/60.) >0<nmin_con
	nmin_day   = (int2secarr(endt_ut, fem_data(i+1))/60.) >0<nmin_con
    end
    str = dat_jst + ' ' + st_jst + ' (' + dat_ut + ' ' + st_ut + ')   ' + en_jst
    str2 = string(nmin_day, nmin_night, nmin_con, format='(2x, 3f5.1)')

    str3 = ' '
    if (keyword_set(paths)) then begin
	tarr = sttarr_ut	;hh,mm,ss,msec,dd,mm,yy

       ;pnum = fem_data(i).path
       ;str3 = string(tarr(6),tarr(5),tarr(4),'-',pnum,format='(2x, 3i2.2,a,i4.4)')

	pnum = (tarr(0)*60 + tarr(1))/30 + 1		;number of 1/2 hour segments into the day
	ip   = (tarr(0)*60 + tarr(1))/98 + 1		;number of orbits into the day
	str3 = string(tarr(6),tarr(5),tarr(4),'-',ip,pnum,format='(2x, 3i2.2,a,2i2.2)')
    end

    this_one = (stday_ut - fem_data(0).day)*86400 + (sttime_ut - fem_data(0).time)/1000
    if (not qfirst) then begin
	del = (this_one-last_one)/60	;time between contacts in minutes
	if (del gt 180) and loud then print, ' '
	if ((del gt 180) and qhc) then printf, lunhc, ' '
	if ((del gt 180) and qout) then printf, lunout, ' '
    end
    str = str + str2 + str3
    if loud then print, str 
    outstr = [outstr, str]
    if (qhc) then printf, lunhc, str
    if (qout) then printf, lunout, str
;
    utout=dat_ut + ' ' + st_ut
;
    last_one = this_one
    qfirst = 0
  endif				; Check for pass between day_arr1 and day_arr2
end
if n_elements(outstr) gt 1 then begin	; JRL: Fixed for case of no contacts selected
  outarr = outarr(*,1:*)
  outstr = outstr(1:*)
endif else  message,'No contacts during specified interval',/info
;
if (qhc) then begin
    free_lun, lunhc
    dprint, 'contacts.temporary', /delete
end
if (qout) then free_lun, lunout
;
if (keyword_set(qstop)) then stop
end

