function valid_pass2,event,dsn_num,dsn_select,dsn_time,og_table, $
	str_select=str_select
;+
;  Name:
;    valid_pass2
;  Purpose:
;    Retain dsn contacts if they are with dsn_time of the times in the
;    structure dsn_select.
;
;    If pause preceeds rep time, use ANT CHNG TO A (or B) instead.
;
;  Inputs:
;    dsn_time	= Time in minutes to satisfy the selection criterion
;
;    if dsn_num = -1, return
;    if dsn_num =  0, remove all dsn contacts.
;  Optional Output Keyword:
;    str_select = String array of DSN contacts selected
;
;  History:
;    15-nov-91, Written, J. Lemen
;    27-jan-92, Updated, J. Lemen -- Added check for Pause before REP
;     1-jul-92, Updated, J. Lemen -- Fixed check for Pause before REP
;     4-may-93, JRL, Change timing logic for DSN Rep/Ant change during night
;    27-nov-93, JRL, Improved logic to detect simultaneous BDR and Ant changes
;    28-mar-95, JRL, Check station and time to try to get the correct DSN
;		     station.  Change necessitated by addition of Wallops.
;-
n_eve = n_elements(event)
event2 = event
nmax = max(event.pass_num)			; Max number of passes
str_select = '-1'				; Return this string array
if dsn_num eq -1 then return,event2		; Don't do anything

for i=0,nmax do begin
  jj = where( event.pass_num eq i, npts )
  if npts gt 0 then begin
     if event(jj(0)).station ne 'U' then begin	; Only do DSN contacts
  	pass_remove = 1				; Assume we will remove
	if dsn_num eq 0 then pass_remove = 1 else begin
 	   timdiff = fltarr(dsn_num)		; Differences in minutes
	   for j=0,dsn_num-1 do $
	     timdiff(j) = addtime(event(jj(0)).timarr,diff=dsn_select(j).timarr)
	   kk = where((abs(timdiff) lt dsn_time) and 		$
		((strmid(strtrim(dsn_select.station,2),0,1) eq 	$
			strtrim(event(jj(0)).station,2)) or	$
		 (strtrim(dsn_select.station,2) eq '')), npts2)
	   if npts2 gt 0 then pass_remove = 0
	endelse 

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;     If Antenna switch occurs before REP, make it an Change Antenna command
         q_rep = 0				; Until REP commands issued
	 q_ant = 0				; Until change command issued
	 dn_flag = -1				; Rep in day = 1; in night = 0
         for j=0,npts-1 do begin
	   if event2(jj(j)).og eq og_table(19).og then begin
	     q_rep = 1				; Found the rep command
	     tim_rep = event2(jj(j)).timarr	; Time of Rep
	     dn_flag = event2(jj(j)).DN		; Day/Night flag
	   endif
	   if event2(jj(j)).og eq og_table(20).og then begin
	     q_ant = 1					; A to B
	     tim_ant = event2(jj(j)).timarr
	     j_sub = j
	   endif else if event2(jj(j)).og eq og_table(21).og then begin
	     q_ant = 2					; B to A
	     j_sub = j
	     tim_ant = event2(jj(j)).timarr
	   endif
         endfor
         if q_rep ne 0 then begin
	    if q_ant ne 0 then begin
	      t_delt1 = addtime(tim_rep, diff = tim_ant)	; Rep - Ant Change
	      t_delt2 = addtime(addtime(tim_rep,del=32./60.), $	; (Rep +32) - Ant Change
					 diff = tim_ant)	
	      t_delt3 = addtime(addtime(tim_rep,del=64./60.), $	; (Rep +64) - Ant Change
					 diff = tim_ant)	
	      if dn_flag then t_delt = t_delt1 else t_delt = t_delt2
	      if t_delt ge 0. then begin
		if q_ant eq 1 then begin
		  event2(jj(j_sub)).og = og_table(25).og	; Change to B
		  event2(jj(j_sub)).com = og_table(25).og_comm
		endif else if q_ant eq 2 then begin
		  event2(jj(j_sub)).og = og_table(24).og	; Change to A
		  event2(jj(j_sub)).com = og_table(24).og_comm
		endif						; q_ant eq 2
	      endif 						; t_del ge 0.
	      if dn_flag eq 0 then begin		; This is night time	
	; Assume Repro is at Time = T
	; Check if ant change between T and T+32s (move to T-32s)
	         if (t_delt2 ge 0) and (t_delt1 le 0) then begin
		    event2(jj(j_sub)).timarr = addtime(tim_rep,del=(-32./60))
	; Check if ant change between T+32 and T+64s (move to T+64s)
		 endif else if (t_delt3 ge 0) and (t_delt2 le 0) then begin
		    event2(jj(j_sub)).timarr = addtime(tim_rep,del=(64./60))
		 endif
	      endif
; -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
;  This next bit of code tries to force the antenna change to come slightly
;  earlier than the REP in case they are scheduled for the same time
	      t_delt = addtime(tim_rep,diff=event2(jj(j_sub)).timarr)	; Rep - Ant Change
	      if (t_delt ge 0.) and (t_delt le 30./60) then		$
		event2(jj(j_sub)).timarr = addtime(event2(jj(j_sub)).timarr,delta=-32./60)
; -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
            endif
         endif
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       if pass_remove then begin
       	 for j=0,npts-1 do begin
   ;  Take abs() in case it is already eliminated:
           event2(jj(j)).og = - abs( event(jj(j)).og )
	   if event2(jj(j)).og eq 0 then event2(jj(j)).og = -99
	 endfor
       endif else begin
	  temp_str = '  DSN  '+fmt_tim(event2(jj(0)).timarr)+	 $
				'  '+event2(jj(0)).station
	  str_select = [str_select, temp_str]
       endelse
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     endif					; event(jj(0).station ne 'U'
  endif						; npts gt 0
endfor

print,' '
if n_elements( str_select ) gt 1 then begin
  str_select = str_select(1:*)
  print,' Selected  DSN Station times:'
  print,str_select,format='(x,a)'
endif else print,'  No DSN contacts selected'

return,event2
end
