pro pass_sheets,contacts,version=version,qtest=qtest,comment=comment,	$
		orbit_params=orbit_params,file=file,cover_file=cover_file,	$
                weekend=weekend,ksc_pass_num=ksc_pass_num
;+
;  Name:
;     pass_sheets
;  Purpose:
;     Called by op_first_guess to generate the pass sheets.
;
;  Calling Sequence:
;     pass_sheets,contacts,version=version,comment=comment,	$
;			orbit_params=orbit_params  [,/qtest,file=file]
;
;  Optional Input Keywords:
;     file = 	The IDL plot file.  If not specified, defaults to idl.ps
;
;  This routine will create the plot file, but not print it.
;
;  Modification History:
;     12-aug-92, written, JRL
;     23-sep-92, Modified, JRL. Fixed some bugs; Added TMX H REP for Night entry
;     27-apr-93, JRL,  Call Dave Pike's cover_page routine.  Removed MOD CTL AT
;     30-nov-93, JRL,  Always force the entry comment (KSC AOS) to be printed
;		       if the AOS antenna doesn't match the actual antenna, put
;		       the actual antenna in ( ) (Kosugi san's suggestion).
;      3-dec-93, JRL,  Fixed the DAY OG position (bec of previous change)
;      6-dec-93, JRL,  Fixed the "non-printing" of SXT CHK DIS when AOS is -NT.
;     30-jul-94, JRL,  Pass ksc_pass_num and weekend keywords to cover_page
;-

if n_elements(cover_file) eq 0 then cover_file = 'cover.ps'
cover_page,contacts,qtest=qtest,outfile=cover_file,weekend=weekend,ksc_pass_num=ksc_pass_num

if not keyword_set(qtest) then begin
  if n_elements(file) eq 0 then file = 'idl.ps'	; Default output file
  file_delete,file,status			; Delete the previous plot file
  if not status then begin
	print,'**  Possible problem in pass_sheets: ' & tbeep
 	print,'**  Could not delete file =',file
   endif
  save_device = !d.name				; Save the plotting device type
  set_plot,'PS'
  xpage = 8.  & ypage = 11.			; Plot page size in inches
  device,/inches,xsize=xpage,ysize=ypage,xoffset=0.,yoffset=0.,file=file
endif else begin
    ans = '' & read,'Pause ',ans
endelse

x0 = 0.
y0 = .11
x1 = .91
y1 = .790
xlen = x1-x0
ylen = y1-y0
ch_size = 1.2			; Size of characters in D/N and ANT boxes

; --------------------------------------------------------
; This next section is from Dave Pike's cover_page program
;
; Select the KSC contacts that are on the first day:

pass = contacts
pass = pass(where(pass.station eq 'U'))
;  check for a change of day
;
np = n_elements(pass) & diff = fltarr(np-1)
for i=0,np-2 do begin
   diff(i) = addtime(pass(i+1).aos_time,diff=pass(i).aos_time)
endfor
next_day = where(diff gt 500)

;
;  just in case there are more than one
;
next_day = next_day(0)
;  if no change of day found the use all passes
if next_day lt 0 then next_day = n_elements(pass)-1

;
;  only retain passes that are on the original start day
;
pass = pass(0:next_day)
np = next_day+1

Pass_nn = 0				; Pass number

for i=0,n_elements(pass)-1 do begin

;;  if (pass(i).station eq 'U') and (pass_nn lt 5) then begin

;   Pass_nn = Pass_nn + 1		; This is the Pass number
;   if n_elements(ksc_pass_num) ne 0 then pass_num = ksc_pass_num(pass_nn)

   pass_num = i + 1			; This is the Pass number
   if n_elements(ksc_pass_num) ne 0 then pass_num = ksc_pass_num(i)
   zz_num = 0				;=1 if there is A/B or D/N
;					;=2 if both an A/B and D/N

;  -- Draw the outer box	---------------------------------------

   plot,[0,0],[1,1],xstyle=4,ystyle=4		; Suppress axis labels
   oplot,[x0,x0,x1,x1,x0],[y0,y1,y1,y0,y0]	; Outer box
   oplot,[x0,x1],y0+[ylen,ylen]*.898		; Header box
   oplot,x0+[xlen,xlen]*.147,[y0,y1]		
   oplot,x0+[xlen,xlen]*.206,[y0,y1]
   oplot,x0+[xlen,xlen]*.413,[y0,y1]
   oplot,x0+[xlen,xlen]*.469,[y0,y1]
   oplot,x0+[xlen,xlen]*.797,[y0,y1]
   xx0 = x0+[.147,.206]*xlen			; Coordinates of day/night box
   xx1 = x0+[.413,.469]*xlen			; Coordinates of Ant box
   yy = y0+[0,.898]*ylen

   start_y = .845
   end_y   = .029

   tt = .034		; One vertical line
   c0 = .008		; Column 0 x start
   h0 = .953		; Header 0 y value
   h1 = .92
   c1 = .1735		; Column 1 x start
   cc1= .165		; Column 1 x Header start
   c2 = .225
   c3 = .42
   c4 = .481
   c5 = .80		; Column 5

   aos_time = pass(i).aos_time
   los_time = pass(i).los_time
   fac = ylen*(end_y-start_y)/pass(i).length
   yyy = y0 + start_y*ylen	; yyy = the position of the first line in the 
				;       "body" of the command sheet (at AOS)

;  -- Overall Title Line	---------------------------------------

   xyouts,0,.98,		$
	'!6YOHKOH OPERATION REAL-TIME COMMAND SHEET!3       Signature',size=1.2

;  -- Signature Box 		---------------------------------------

   oplot,x0+[.7,1.,1.,.7,.7]*xlen,[.9,.9,.96,.96,.9]
   oplot,x0+[.797,.797]*xlen,[.9,.96]
   xyouts,x0+.705*xlen,.925,'SSOC'

;  -- Pass ID	 		---------------------------------------

   xyouts,x0,.92,string('19',pass(i).aos_time([6,5,4]),Pass_num,pass(i).passid, $
		format="(a,i2,2('/',i2.2),' Pass',i2,';  Pass No. ',a)")

   oplot,x0+[.08,.92]*xlen,[1,1]*.856
   xyouts,x0+.08*xlen,.86+.5*tt*ylen,'object:'
   xyouts,x0+.08*xlen,.86-1.2*tt*ylen,'remark:'

;  -- Labels in the box		---------------------------------------

   xyouts,x0+c0*xlen,y0+h0*ylen,'Time(UT)'
   xyouts,x0+cc1*xlen,y0+h0*ylen,'D'
   xyouts,x0+cc1*xlen,y0+(h1+.3*tt)*ylen,'/'
   xyouts,x0+cc1*xlen,y0+h1*ylen,' N'
   xyouts,x0+c3*xlen,y0+(h0+.3*tt)*ylen,'A'
   xyouts,x0+c3*xlen,y0+(h0+h1)/2*ylen,' N'
   xyouts,x0+c3*xlen,y0+(h1-.3*tt)*ylen,'  T'
   xyouts,x0+.27*xlen,y0+h0*ylen,'Event'
   xyouts,x0+.56*xlen,y0+h0*ylen,'Commands'
   xyouts,x0+.55*xlen,y0+h1*ylen,'(BC in HEX)'
   xyouts,x0+.84*xlen,y0+h0*ylen,'Status,'
   xyouts,x0+.84*xlen,y0+h1*ylen,'comment'

   xyouts,x0+c2*xlen,y0+(start_y-0*tt)*ylen,'AOS'
   xyouts,x0+c2*xlen,y0+(start_y-1*tt)*ylen,'AOS check'
   xyouts,x0+c2*xlen,y0+(start_y-2*tt)*ylen,'uplink start'
   xyouts,x0+c0*xlen,y0+(start_y-2*tt)*ylen,'X +1.5 min'
   ylim = y0+(start_y-2*tt)*ylen
   xyouts,x0+c0*xlen,y0+(end_y+2*tt)*ylen,'Z -2 min'
   xyouts,x0+c2*xlen,y0+(end_y+2*tt)*ylen,'uplink end'
   xyouts,x0+c2*xlen,y0+(end_y+1*tt)*ylen,'LOS check'
   xyouts,x0+c2*xlen,y0+(end_y+0*tt)*ylen,'LOS'

;  -- AOS/LOS times 		---------------------------------------

   xyouts,x0+c0*xlen,y0+start_y*ylen,'X='+gt_time(aos_time,/str)
   xyouts,x0+c0*xlen,y0+end_y  *ylen,'Z='+gt_time(los_time,/str)

;  -- Max Antenna time	 	---------------------------------------
   
   ant_max_time = pass(i).ant_max_time
   abx = addtime(ant_max_time,diff=aos_time)
   yy2 = yyy+abx*fac			; Position of Max Antenna
   xyouts,x0+c0*xlen,yy2,'  '+gt_time(ant_max_time,/str)
   xyouts,x0+c2*xlen,yy2,'max. el.='+string(pass(i).ant_max,format='(f4.1)')

;  -- 10 deg Antenna time 	---------------------------------------

   ant_10_time = pass(i).ant_10_time
   yy3 = y0+(end_y+3*tt)*ylen
   xyouts,x0+c2*xlen,yy3,'el.=10 deg'
   xyouts,x0+c0*xlen,yy3,'  '+gt_time(ant_10_time,/str)
   ylim = [yy3,ylim]

;  -- Antenna Change times 	---------------------------------------

   if strtrim(pass(i).antenna,2) eq 'A' then begin
	xyouts,x0+c3*xlen,total(yy)/2,' A',size=ch_size
   endif else if strtrim(pass(i).antenna,2) eq 'B' then begin
	xyouts,x0+c3*xlen,total(yy)/2,' B',size=ch_size
   endif else begin			; either A/B or B/A
	ab_time = pass(i).ab_time
	abx = addtime(ab_time,diff=aos_time)
        abz = addtime(los_time,diff=ab_time)
	yy1 = yyy+abx*fac			; Position of actually switch
	oplot,xx1,yy1*[1,1]
	yy1x = yyy+abx/2.*fac			; Position of 1st Antenna
	yy1y = yyy + (abx+abz/2.)*fac		; Position of 2nd Antenna
	if yy1x gt ylim(1) then yy1x = yy1x + .8*tt*ylen
	if yy1y lt ylim(0) then yy1y = yy1y - .8*tt*ylen
	xyz = strtrim(pass(i).antenna,2)
	first_ant = strmid(xyz,0,1)
        first_act = strmid(pass(i).aos_com,strpos(pass(i).aos_com,'-')+1,1)
	if first_ant ne first_act then 	first_ant = '('+first_ant+')' else 	$
					first_ant = ' '+first_ant
	xyouts,x0+c3*xlen,yy1x,first_ant,size=ch_size
	xyouts,x0+c3*xlen,yy1y,' '+strmid(xyz,2,1),size=ch_size
	zz1 = yy1
	zz_text = '  '+gt_time(ab_time,/str)+'!c   ('+xyz+')'
	zz_num = zz_num + 1			; count A/B & D/N transitions
   endelse

;  -- Day/Night times 		---------------------------------------

   day_og_time = pass(i).day_og
   if day_og_time(6) eq 0 then day_og = 0 else day_og = 1
   xyz = strtrim(pass(i).daynight,2)    ; 'DAY' or 'NIGHT' or 'D/N' or 'N/D'

   if (xyz ne 'DAY') and (xyz ne 'NIGHT') then begin	; D/N or N/D

	dn_time = pass(i).dn_time
	abx = addtime(dn_time,diff=aos_time)
        abz = addtime(los_time,diff=dn_time)
	yy0 = yyy+abx*fac			; Position of actually D/N
	yy0x = yyy+abx/2.*fac			; Position of 1st D or N
	yy0y = yyy + (abx+abz/2.)*fac		; Position of 2nd D or N
	if yy0x gt ylim(1) then yy0x = yy0x + .8*tt*ylen
	if yy0y lt ylim(0) then yy0y = yy0y - .8*tt*ylen
	if strmid(xyz,0,1) eq 'D' then Day_entry = 1 else Day_entry = 0
	zz_num = zz_num + 1			; count A/B & D/N transitions
	if zz_num eq 2 then zz1 = [zz1,yy0] else zz1 = yy0
	rr = '  '+gt_time(dn_time,/str)+'!c   ('+xyz+')'
	if zz_num eq 2 then zz_text = [zz_text,rr] else zz_text = rr
	yx = yy				; yy=Limits of top/bot of whole D/N box
	if strmid(xyz,0,1) eq 'N' then yx(0) = yy0 else yx(1) = yy0
; Look for the case where the Day OG comes between N/D and AOS
        if (Day_entry eq 0) and Day_og then begin
	    if addtime(aos_time,diff=day_og_time) ge 0. then xyz = 'DAY' 

; Look for the case where the D/N comes before AOS
	endif else			 		$
        if (Day_entry eq 1) and 			$
	      (addtime(aos_time,diff=dn_time) ge 0.) then xyz = 'NIGHT'
   endif						; D/N or N/D

; Write the Day or Night in the box and polyfill the night part

   if xyz eq 'D/N' or xyz eq 'N/D' then begin
	if yy0 gt yy(1) then begin
	  if xyz eq 'D/N' then xyz = 'NIGHT' else xyz = 'DAY'
	endif
	if yy0 lt yy(0) then begin
	  if xyz eq 'D/N' then xyz = 'DAY' else xyz = 'NIGHT'
	endif
   endif
   
   if xyz eq 'DAY' then begin
	for j=0,2 do 						$
	  xyouts,x0+c1*xlen,y0+(.55-j*tt)*ylen,strmid(xyz,j,1),size=ch_size
   endif else if xyz eq 'NIGHT' then begin
	for j=0,4 do						$
	  xyouts,x0+c1*xlen,y0+(.55-j*tt)*ylen,strmid(xyz,j,1),size=ch_size
        polyfill,xx0([0,1,1,0,0]),yy([0,0,1,1,0]),orient=45,spacing=.4
   endif else begin					; D/N or N/D
	oplot,xx0,yy0*[1,1]
	xyouts,x0+c1*xlen,yy0x,strmid(xyz,0,1),size=ch_size
	xyouts,x0+c1*xlen,yy0y,strmid(xyz,2,1),size=ch_size
        polyfill,xx0([0,1,1,0,0]),yx([0,0,1,1,0]),orient=45,spacing=.4
   endelse

;  -- Entry and DAY/OG Comment  ---------------------------------------------

   Entry_comment = pass(i).aos_com		; Always turn on entry comment (30-nov-93)
   if strlen(strtrim(entry_comment,2)) gt 0 then begin
	xyouts,x0+c5*xlen,yyy,entry_comment	; Put this at the "Top"
	ylim2 = yyy - 1.5*tt*ylen		; New start position for Day OG
   endif else ylim2 = yyy 
   if day_og then begin
	abx = addtime(day_og_time,diff=aos_time)
        abz = addtime(los_time,diff=day_og_time)
	yy1 = yyy+abx*fac			; Position of Day OG
	ylim3 = yyy + addtime(los_time,diff=aos_time)*fac + tt	; Bottom of page
        yy1 = ylim3 > yy1 < ylim2		; ylim2 = top of page

	xyouts,x0+c5*xlen,yy1,'Day OG at'
	xyouts,x0+c5*xlen,yy1 - tt*ylen,gt_time(day_og_time,/str)
   endif

;  -- Print the D/N and A/B times -------------------------------------
;  -- this is the hardest part:  Try not to have times that overwrite
;  -- each other.

   if zz_num gt 0 then begin
;    Check if there will be a conflict with AOS(2), LOS(1), or Max Ant(3)
        conflict = intarr(zz_num)
	for j=0,zz_num-1 do 						$
	   if zz1(j) le ylim(0)+2*tt*ylen then conflict(j) = 1 else 	$
  	   if zz1(j) ge ylim(1) 	  then conflict(j) = 2 else	$
	   if (zz1(j) ge yy2-.5*tt*ylen) and (zz1(j) le yy2+2*tt*ylen)	$
					  then conflict(j) = 3

;    Check which half transition falls in: top(0) or bottom(1)
	half = intarr(zz_num)
	for j=0,zz_num-1 do					$
	   if zz1(j) ge yy2 then half(j) = 0 else half(j) = 1
        if (zz_num gt 0) and (keyword_set(qtest)) then		$	;***	
	  for j=0,zz_num-1 do print,zz1(j),' half=',half(j),' con=',conflict(j) ;***

	
;    Establish the limits for each half of the time box:
	yylim = [[ylim],[ylim]]		; break up ylim into 2 parts
	yylim(0,0) = yy2		; ylim(*,0) = top half
	yylim(1,1) = yy2		; ylim(*,1) = bottom half

;    Time order the transitions.  Reverse if following conditions apply:
	kk = [0,1]
	rev=0
	if zz_num gt 1 then begin
	  kk = reverse(sort(zz1))	; time order (later time is smaller zz1)
	  if (conflict(0) eq 1) or (conflict(1) eq 1) or 	$
	     ((conflict(0) eq 3) and (half(0) eq 0))  or	$
	     ((conflict(1) eq 3) and (half(1) eq 0)) then rev = 1 else rev=0
	  if (total(conflict) eq 0) and (half(0) eq half(1)) and $
		(max(zz1) le total(yylim(*,half(0)))/2) then rev = 1
	  if rev then kk = reverse(kk)
	endif 

;    Now write transition times in the correct locations:
	for j=0,zz_num-1 do begin
	   k = kk(j)
	   yout = zz1(k)
	   if (j eq 1) and 						$
	      (yylim(1,half(k))-yylim(0,half(k)) lt 3*tt*ylen) then begin
	      yylim(0,half(k)) = yylim(*,1-half(k))
	   endif
	   if yout le yylim(0,half(k))+2*tt*ylen then begin
		yout = yylim(0,half(k)) + 2*tt*ylen
		yylim(0,half(k)) = yout
  	   endif else if yout ge yylim(1,half(k))-1*tt*ylen then begin
	 	yout = yylim(1,half(k)) - tt*ylen
		yylim(1,half(k)) = yout - tt*ylen
	   endif else yylim(1-rev,half(k)) = yout - (1-rev)*tt*ylen ; sub goin positive
	   xyouts,x0+c0*xlen,yout,zz_text(k)
	   if keyword_set(qtest) then print,zz_text(k)		;***
	endfor
   endif

;  -- Start/End commands	---------------------------------------
   buf = strupcase(strtrim(pass(i).aos_com,2))
   if strpos(buf,'-DAY') ge 0 then begin
;;   	xyouts,x0+c4*xlen,y0+(start_y-0*tt)*ylen,'MOD CTL AT'
   	xyouts,x0+c4*xlen,y0+(start_y-0*tt)*ylen,'SXT CHK DIS'
   	xyouts,x0+c4*xlen,y0+(start_y-1*tt)*ylen,'TMX H REP'
   endif else xyouts,x0+c4*xlen,y0+(start_y-0*tt)*ylen,'TMX H REP'
   xyouts,x0+c4*xlen,y0+(end_y+2*tt)*ylen,'BDR REC'

;  -- Version			---------------------------------------
   if n_elements(version) ne 0 then xyouts,x0+.6*xlen,0,version,size=.8
   if n_elements(comment) ne 0 then xyouts,x0,0,comment,size=.8
   if n_elements(orbit_params) ne 0 then xyouts,x0,tt*ylen,	$
		'Orb. El.: '+strtrim(orbit_params,2),size=.8

    if (keyword_set(qtest)) and (i lt np-1) then begin
      ans = '' & read,'Pause ',ans
    endif
;  endif

endfor

if not keyword_set(qtest) then begin
  device,/close				; Close the plotting device
  spawn,'chmod 666 '+file		; Try to make the file deleteable
  set_plot,save_device			; Reset the plotting device
endif
end
