pro dsn_input, dsn_num, dsn_select, start_time=start_time, end_time=end_time
;+
;  Name:
;    dsn_input
;
;  Purpose:
;    Allow user to input DSN stations for op_first_guess
;
;  Inputs:
;    start_time	= Start time in Yohkoh format
;    end_time   = End   time in Yohkoh format
;
;  Outputs:
;    dsn_num	= Number of selections
;    dsn_select = Structure of times, day of years
;
;  Method:
;
;  History:
;    Written, 14-nov-91, J. Lemen
;    Updated, 12-dec-91, J. Lemen - Added start_time keyword
;    Updated, 22-sep-92, JRL:  Read the contacts from RASM final schedule file.
;    Updated, 13-nov-92, JRL:  Search for PASS (instead of TKG PASS since it
;			       sometimes can be TRK PASS instead
;    24-apr-93, JRL, Read the solass file instead.  Use rd_rasm.pro
;    29-apr-93, JRL, Identify passes using WRK CAT field
;    26-nov-93, JRL, Fixed bug which prevented all DSN passes from being manually deleted.
;    11-mar-95, JRL, Add station contact to the output dsn_select structure
;		     Added temporary patch to eliminate Wallops passes
;    28-mar-95, JRL, Enabled Wallops passes to be accepted
;    15-may-95, JRL, Enabled Santiago passes to be accepted
;-

dsn_num = 0 & rd_rasm = 1
ans = 'A'			; Append more cases
start_time = anytim2ex(start_time)
end_time   = anytim2ex(end_time)


repeat begin
  if rd_rasm then begin
    dd = {dsn_select, doy: 0, timarr: intarr(7), station: '   '}
    dsn_select = dd

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  Try to read RASM file if path is defined and start and end times.
    if (n_elements(start_time) ne 0) and (n_elements(end_time) ne 0) then begin
       anytim2weeks, start_time, end_time, weeks, years
; (-2) bec JPL week diff from Yohkoh Week  (-1) because DSN week begins on Mon.
       weeks = [weeks(0)-2, weeks(0)-1, weeks]		; Bec. DSN week begins on Monday
       years = [years(0), years(0),   years]
       for i=0,1 do begin
         if weeks(i) eq 0 then begin
 		weeks(i) = 53 & years(i) = years(i) - 1
         endif
       endfor
       if weeks(0) eq -1 then begin
	  weeks(0) = 52 & years(0) = years(0) - 1
       endif
       
       for j=0,n_elements(weeks)-1 do begin		; Loop through weeks
          rd_rasm,'solass',weeks(j),pass,status,sel='sola',rasmfile=fn
  	  if status eq 1 then begin
	     print,'Reading RASM file: ',fn
	     if n_elements(pass) gt 0 then begin
		ijk = where(strlowcase(strtrim(pass.wrk,2)) eq '1a1', npass)
		if npass gt 0 then pass = pass(ijk)
		for k=0,npass-1 do begin
		   BOT  = doytim2ex(string(pass(k).doy,pass(k).BOT),year=pass(k).year)
		   START= doytim2ex(string(pass(k).doy,pass(k).Start),year=pass(k).year)
;	DOY is for Start time.  BOT might be on the next day:
		   if (addtime(BOT,diff=START) lt 0) then begin
			pass(k).doy = pass(k).doy + 1
                        BOT = doytim2ex(string(pass(k).doy,pass(k).BOT),year=pass(k).year)
		   endif
		   if (addtime(BOT,diff=start_time) ge 0) and	$
			     (addtime(end_time,diff=BOT)   ge 0) then begin
		       dsn_num = dsn_num + 1
		       dd.doy = pass(k).doy & dd.timarr = BOT 
                       case pass(k).DSS of 
			 16: dd.station = 'G16'	; G16
			 17: dd.station = 'G17'	; G17
			 46: dd.station = 'C  '	; Canberra
			 66: dd.station = 'M  '	; Madrid
			 74: dd.station = 'S  '	; Santiago
			 76: dd.station = 'W  '	; Wallops
			 else: dd.station= '   '
		       endcase
		       dsn_select = [dsn_select, dd]
		    endif		; start_time < timarr < end_time
		endfor			; k=0,n_elements(pass)-1
	     endif			; n_elements(pass) gt 0
	  endif				; status eq 1
       endfor				; j=0,n_elements(weeks)
       if dsn_num gt 0 then dsn_show, dsn_num, dsn_select(1:*), /index
; ---------------------------------------
; 11-Mar-95:  Temporary patch begins here
;*if dsn_num gt 0 then begin
;*   ii = wc_where(dsn_select.station,'W*',mcount)
;*   if mcount gt 0 then begin
;*      message,'*** Warning: op_first_guess does not include Wallops passes',/info
;*      ii=reverse(ii(sort(ii)))		; Make sure the vector is biggest to smallest
;*      for i=0,mcount-1 do begin
;*          kk = indgen(n_elements(dsn_select))
;*          dsn_select = dsn_select(where(kk ne ii(i)))
;*          dsn_num = dsn_num - 1
;*      endfor
;*      if dsn_num gt 0 then dsn_show, dsn_num, dsn_select(1:*), /index
;*   endif
;*endif
;             Temporary patch ends here
; ---------------------------------------
    endif
    rd_rasm = 0				; Don't read RASM again
; - - - - - - - - End of RASM File Reading  - - - - - - - - - - - - - - - - - -
    if n_elements(start_time) eq 0 then start_time = timstr2ex( !stime )
    ttt = addtime(start_time,del=9.*60)			; Add about 9 hours
    date2doy,string(ttt([6,5,4]),format='(3i2.2)'),doy
    examp = strtrim(string(doy,', ',ttt(0:1),format='(i3,a,i2,":",i2.2)'),2)
    examp1 = string('Note that',doy,' corresponds to '+gt_day(ttt,/str),  $
		format='(a,i4,a)')
  endif						; dsn_num eq 0

;;  if dsn_num gt 0 then ans = ''			; Got some contacts from rasm
  if ans eq 'A' then begin
	print,' '
	print,'  Enter DSN requested stations '
	print,'  Enter as doy, time, such as ',examp 
	print,'  ',examp1 & print,' '

      repeat begin
	buff = ' '
	cyear = fix(strmid(!stime,9,2))			; Current year
	read,'* Enter doy, time (or <cr> to quit): ',buff
	timarr = doytim2ex(buff,doy=doy)
	if (start_time(5) eq 12) and (doy lt 31) then begin
		cyear = cyear + 1
		timarr = doytim2ex(buff,doy=doy,year=cyear)
	endif
	if total(timarr) ne 0 then begin
	  dsn_num = dsn_num + 1
     	  dd.doy = doy & dd.timarr = timarr & dd.station = '   '
	  dsn_select = [dsn_select, dd]
	endif
      endrep until total(timarr) eq 0
      if dsn_num gt 0 then dsn_select = dsn_select(1:*)
  endif						; ans eq 'A'


  dsn_show, dsn_num, dsn_select, /index

  ans = ' ' & print,' '
  print,'* Enter A to add more stations, '
  print,'* Enter S to start over,'
  print,'* Enter D to delete an entry,'
  read, '* Enter <cr> if finished entering DSN contacts: ', ans

  ans = strmid(strupcase(ans),0,1)
  if ans eq 'S' then begin
	dsn_num=0 & ans = 'A' & rd_rasm = 1
  endif else			 				$
  if ans eq 'A' then dsn_select = [dd,dsn_select] else		$ 
  if ans eq 'D' then begin
       nstrip = dsn_num
       input,'* Enter index of station to delete or 0 for no action',	$
			nstrip, nstrip, 0, dsn_num
       if nstrip ne 0 then begin
         ij = where(indgen(dsn_num)+1 ne nstrip,dsn_num)
         if dsn_num gt 0 then dsn_select = dsn_select(ij)
       endif 
  endif
endrep until ans eq ''

xx = (dsn_num-1) > 0
dsn_select = dsn_select(0:xx)

return
end
