function op_same_time, event, og_table, verbose=verbose
;+
; NAME:
;  op_same_time
; PURPOSE:
;  Called by op_edit (by op_first_guess) to resolve conflicting OG times.
;
; PROCEDURE:
;  There are six rules:
;  1. If the conflict involes OP STOP and anything, 
;		move the OP STOP later by 32 sec
;  2. If the conflict involves the QUIET RATE HIGH command & an LOS command,
;		change the LOS to QT/H and remove the QUIETT RATE HIGH OG.
;  3. If the conflict involves the QUIET RATE HIGH, move earlier 32 sec
;		(unless rules 1 or 2 apply).
;  4. If the conflict involves the OP DAY, move later 32 sec (unless
;		rules 1 or 2 apply).
;  5. If the conflict involves the QUIET RATE MED and an RB HV RDCT command,
;		move the QUIET RATE MED command 32 sec later.
;  6. If the conflict involves the DSN REPRO command and an antenna change 
;     command, back the antenna change command up by 32 sec and make it a
;     "ANT CHNG TO X" command.
;  7. If the conflict involves any other type of conflict, just warn the user.
; HISTORY:
;  30-nov-93, JRL, Written
;  30-jun-94, JRL, When warning the user, give the same event number as
;		   shows up in the printout.
;  27-jul-94, JRL, Make the warning more prominent.  Added OP DAY case.
;  26-feb-97, JRL, Added rule for QUIET RATE HIGH.  This was made necessary because
;		   BDR optimization makes conflicts more likely.
;   8-mar-97, JRL, Added additional rule for QUIET RATE HIGH and for QUIET RATE
;		   MED, which was made necessary by changes to terminator selection.
;-

event2 = event				; Save a copy of the original
repeat begin
; Don't look at out of range og's or night og's
  jj = where((event2.og ge og_table(0).og) and (event2.og le og_table(127).og) 	$
	  and event2.og ne og_table(5).og)
  jj = jj(1:*)				; The first event is the KSC AOS

; ---------------------------------------------------------------------
; This next bit of code is messy.  Its purpose is to try to get the 
; numbering of the warning message to match the printout OP number in case
; of a conflict.  Normally, one should just start at the NOP command.
; However, if a DAY command occurs at the same time as the NOP, it will
; be shifted past the NOP, so one has to check for the case of a DAY OG at
; the same time as the NOP.

  if event2(jj(0)).og ne og_table(0).og then begin	; Is the NOP first jj event?
    kk = where(event2(jj).og eq 0,nc)	; Get indices of all NOPs
    if nc gt 0 then begin		; Could there not be NOPs?
      kk = min(kk)			; Get the first NOP
; The next two lines of code keeps the DAY OG or anything else (what it could 
; it be, though?) if it is at the same time as the first NOP.
      if addtime(event2(jj(kk)).timarr,diff=event2(jj(kk-1)).timarr) ne 0. then jj = jj(1:*)
    endif				; nc gt 0
  endif
; ---------------------------------------------------------------------

  str_event = fmt_og(event2(jj))	; Format the information
  finished = 1
  npts = n_elements(jj)
  for i=0,npts-2 do begin
    xx = addtime(event2(jj(i)).timarr,diff=event2(jj(i+1)).timarr)
    if xx eq 0 then begin
       if keyword_set(verbose) then begin
         print,'*********************************************'
         print,'** Warning identical times for the following:'
         print,str_event(i:i+1),format='(x,a)'
         print,'*********************************************'
       endif
;      Find all occurances of LOS commands (for Rule 2)
       jk = where(event2(jj(i)).og eq og_table([15,16,22.23]).og,njk)
       if njk gt 0 then jk = i else begin
         jk = where(event2(jj(i+1)).og eq og_table([15,16,22.23]).og,njk)
         if njk gt 0 then jk = i+1
       endelse
       qcase = 0
; Case 1:  involving op stop:
       if (event2(jj(i)).og eq og_table(127).og) or $
	  (event2(jj(i+1)).og eq og_table(127).og) then begin
	  ii = (where(event2(jj(i:i+1)).og eq og_table(127).og))(0) + i
	  sgg = +1			; Move later
          if keyword_set(verbose)then print,'   (will move the OP Stop 32 sec later: '+	$
		string((addtime(event2(jj(ii)).timarr,del=sgg*32.d0/60.))(0:2),format='(3i2.2)')+')'
; Case 2:  involving QUIET RATE HIGH and an LOS OG:
       endif else if ((event2(jj(i)).og   eq og_table(28).og) or $
		      (event2(jj(i+1)).og eq og_table(28).og)) and njk gt 0 then begin
	  ii = (where(event2(jj(i:i+1)).og eq og_table(28).og))(0) + i
	  sgg = 0			; Don't move
          qcase = 2			; Remove the Quiet Rate High OG
	  if keyword_set(verbose) then print,'   (will remove QUIET RATE HIGH and change LOS to QT/H ' + $
		fmt_tim(event2(jj(jk)).timarr)
          case event2(jj(jk)).og of
            og_table(16).og:  begin				; KSC change to QT/H
		event2(jj(jk)).og = og_table(15).og
		event2(jj(jk)).com = og_table(15).og_comm
            endcase
            og_table(23).og:  begin				; DSN change to QT/H
		event2(jj(jk)).og = og_table(22).og
                event2(jj(jk)).com = og_table(22).og_comm
            endcase
	    else:					; Must have been QT/H already
          endcase
; Case 3:  involving QUIET RATE HIGH:
       endif else if (event2(jj(i)).og   eq og_table(28).og) or $
		     (event2(jj(i+1)).og eq og_table(28).og) then begin
	  ii = (where(event2(jj(i:i+1)).og eq og_table(28).og))(0) + i
	  sgg = -1			; Move earlier
	  if keyword_set(verbose) then print,'   (will move the QUIET RATE HIGH OG 32 sec earlier: '+	$
		string((addtime(event2(jj(ii)).timarr,del=sgg*32.d0/60.))(0:2),format='(3i2.2)')+')'
; Case 4:  involving Day OG:
       endif else if (event2(jj(i)).og   eq og_table(55).og) or $
		     (event2(jj(i+1)).og eq og_table(55).og) then begin
	  ii = (where(event2(jj(i:i+1)).og eq og_table(55).og))(0) + i
	  sgg = +1			; Move later
	  if keyword_set(verbose) then print,'   (will move the DAY OG 32 sec later: '+	$
		string((addtime(event2(jj(ii)).timarr,del=sgg*32.d0/60.))(0:2),format='(3i2.2)')+')'
; Case 5:  involving QUIET RATE MED:
       endif else if ((event2(jj(i)).og   eq og_table(29).og) or    $; QT RATE HIGH
                      (event2(jj(i+1)).og eq og_table(29).og)) and  $
                     ((event2(jj(i)).og   eq og_table(26).og) or    $; RB HV RDCT ON
                      (event2(jj(i+1)).og eq og_table(26).og) or    $
		      (event2(jj(i)).og   eq og_table(27).og) or    $; RB HV RDCT OFF
                      (event2(jj(i+1)).og eq og_table(27).og)) then begin
	  ii = (where(event2(jj(i:i+1)).og eq og_table(29).og))(0) + i
          sgg = 1			; Move later
	  if keyword_set(verbose) then print,'   (will move the QUIET RATE MED OG 32 sec later: '+	$
		string((addtime(event2(jj(ii)).timarr,del=sgg*32.d0/60.))(0:2),format='(3i2.2)')+')'
; Case 6:  involving BDR REP and an antenna change command.
       endif else if ((event2(jj(i)).og   eq og_table(19).og) or 	$; DSN REPRO
		      (event2(jj(i+1)).og eq og_table(19).og)) and 	$
		     ((event2(jj(i)).og   eq og_table(20).og) or 	$; DSN PAUSE-A/B
		      (event2(jj(i+1)).og eq og_table(20).og) or	$
		      (event2(jj(i)).og   eq og_table(21).og) or 	$; DSN PAUSE-B/A
		      (event2(jj(i+1)).og eq og_table(21).og) or	$
		      (event2(jj(i)).og   eq og_table(24).og) or 	$; ANT CHNG TO A
		      (event2(jj(i+1)).og eq og_table(24).og) or	$
		      (event2(jj(i)).og   eq og_table(25).og) or 	$; ANT CHNG TO B
		      (event2(jj(i+1)).og eq og_table(25).og)) then begin
	  ii = (where(event2(jj(i:i+1)).og ne og_table(19).og))(0) + i
	  sgg = -1			; Move earlier
	  if event2(jj(ii)).og eq og_table(20).og then begin
	      event2(jj(ii)).og  = og_table(25).og
	      event2(jj(ii)).com = og_table(25).og_comm
	  endif else if event2(jj(ii)).og eq og_table(21).og then begin
	      event2(jj(ii)).og  = og_table(24).og
	      event2(jj(ii)).com = og_table(24).og_comm
	  endif
	  if keyword_set(verbose) then print,'   (will move the Ant Change OG 32 sec earlier: '+ $
		string((addtime(event2(jj(ii)).timarr,del=sgg*32.d0/60.))(0:2),format='(3i2.2)')+')'
       endif else begin
	  ii = -1
          if not keyword_set(verbose) then begin
            print,'*********************************************'
            print,'** Warning identical times for the following:'
            print,str_event(i:i+1),format='(x,a)'
            print,'*********************************************'
          endif
          print,'   (you must fix with opogedt)' & tbeep,5
          ans = '' & read,'* Enter <cr> to continue:',ans
       endelse
       if ii ne -1 then begin
	  if qcase eq 2 then begin
	    event2(jj(ii)).og  = - event2(jj(ii)).og	; Remove the QT RATE HIGH
	; Arbitarily shift QT RATE HIGH OG out of the time range
	    del = max(addtime(event2(jj).timarr,diff=event2(jj(0)).timarr, /sec)) + 32.d0
            event2(jj(ii)).timarr = addtime(event2(jj(0)).timarr,del=del,/sec)
	  endif else begin
            event2(jj(ii)).timarr = addtime(event2(jj(ii)).timarr,del=sgg*32.d0/60.)
	    event2(jj(ii)).offset = event2(jj(ii)).offset + sgg*32.d0/60.
	  endelse
	  finished = 0			; We shifted something, so not finished yet
       endif
    endif
  endfor
  if not finished then 		$		; not finished
       event2 = op_time_sort( event2, /no32 )	; Time order; no 32s alignment
  
endrep until finished

return,event2
end
