;+
; Project     :	SOHO - CDS
;
; Name        :	PLOT_DSN_27
;
; Purpose     :	Plot a series of rectangles representing DSN-27 passes.
;
; Explanation : Plot a series of rectangles on the planning timeline 
;		representing DSN-27 passes.  If there is enough room in the
;		retangle, then add a descriptive label to it.
;
; Use         :	PLOT_DSN_27, row, t1, t2
;
; Inputs      :	row	Row number in the planning timeline display in which
;			to plot the rectangles representing the DSN-27 passes.
;
;		t1	Array contianing the starting location of each pass
;			along the timeline in the DATA coordinate system.
;
;		t2	Array contianing the ending location of each pass
;			along the timeline in the DATA coordinate system. 
;
; Opt. Inputs : None.
;
; Outputs     :	None.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	Plots rectangles representing DSN-27 passes to the current
;		display window.
;
; Category    :	Planning
;
; Prev. Hist. :	None.
;
; Written     :	Ronald Yurow (EITI)
;
; Modified:   : Ronald Yurow, EITI, October 5, 2001
;                  Initial Release
;
; Version     :	Version 1, October 5, 2001
;-
	PRO PLOT_DSN_27, row, t1, t2

;
; Find the default average characher width and height.
;

        x_ch_size = !D.X_CH_SIZE
        y_ch_size = FIX (x_ch_size * 1.5)

;
; Set the default label length and label height in terms of the standard
; character size
;

	label_length = x_ch_size * 8
	label_height = y_ch_size + 4

;
; Set chsize to 1.  If we have to use reduced size characters for the 
; the label then chsize will be reset to reflect the size reduction.
;

	chsize = 1.0

;
; Set num to the number of DSN 27 passes that we are plotting.
;

	num = N_ELEMENTS (t1)

;
; Set row_height and offset to the height of the bar representing the 
; the DSN 27 pass and offset at which to place the bar within the row.
;

	row_height = 0.45
	offset = .1

;
; Set y1 and y2 to the y coordinates of the bar representing the DSN 27
; pass in the data coordinate system.
;
	y1 = row + offset
	y2 = y1 + row_height
	        
;
; Convert y1 and y2 to the device coordinate system.  Set dy to hieght of
; the bar representing the DSN 27 pass in pixels.
;

	n = CONVERT_COORD ([0, 0], [y1, y2], /TO_DEVICE)

	y1 = FIX (n (1, 0))
	y2 = FIX (n (1, 1))

	dy = y2 - y1

;
; Check if the bar representing the DSN 27 pass will be tall enough to 
; acomadate the label that will be generated for it.  If it is not, then
; we will have to shrink the label.
;

	IF dy LT label_height + 4 THEN BEGIN

	   chsize = (dy - 4.0) / label_height

           label_height = dy - 4
	   label_length = FIX (label_length * chsize)

	ENDIF

;
; Figure out what the starting and endiny y coordinates of the label area
; are.  These should be centered inside the bar representing a DSN 27 pass.
; 

	y1_lbl = y1 + FIX ((dy - label_height) / 2.0) - 1
	y2_lbl = y1_lbl + label_height

;
; Main loop to write a bar representing each DSN 27 to the graphic display.
; Since y coordinates of the bar and the label are already known, all that
; needs to be calculated are the x axis coordinates for each display element.
;

	FOR i = 0, num - 1 DO BEGIN


;
; Calculate the starting and ending x coordinates of the bar representing
; DSN 27 pass i.  This is taken directly from the arrays t1 and t2 which
; start and ending point of each pass.
;

	   x1 = t1(i) > !x.crange(0)
	   x2 = t2(i) < !x.crange(1)

;
; Convert the x coordinates from the data coordinate system to the device
; (pixels) coordinate system.
;            

	   n = CONVERT_COORD ([x1, x2], [0, 0], /TO_DEVICE)

	   x1 = FIX (n (0, 0))
	   x2 = FIX (n (0, 1))

;
; Find the length of the bar representing DSN 27 pass (i).  
;

	   dx = x2 - x1 + 1

;
; Make sure the end point of the bar is after the starting point of the 
; bar.  If it is, the call the function RECTSTRIPE to plot the bar.
;
       
	   IF x2 GT x1 THEN BEGIN 
           
              RECTSTRIPE, [x1, x2],				$ 
	                  [y1, y2], 				$
	                  COLOR = [3, 2], 			$
	                  WIDTH = 5,				$
	                  /DEVICE,				$
	                  /HORIZONTAL

	   ENDIF 

;
; Check if the bar representing the ith DSN 27 pass long enough for a 
; a label field.  It must be at least 32 pixels to put a label on it.
; If it is not, then we will just continue with the next iteration of
; the loop.
;

	   IF dx LT 48 THEN GOTO, LOOP_CONTINUE

;
; Check if the bar that we just drew is long enough to accomadate the
; desired label field.  If it is, then we can go ahead and plot the
; label field without any further adjustments.
;

	   IF dx GT label_length + 16 THEN BEGIN

;
; Calculate the starting and ending locations of the label field within
; the bar representing the ith DSN 27 pass.
;

	      x1_lbl = x1 + FIX (dx / 2.0) - FIX (label_length / 2.0) + 1
	      x2_lbl = x1_lbl + label_length

;
; Since we didn't need to reduce length of the label field, we can also
; use the standard value of chsize when write the text in the label field. 
;

	      t_chsize = chsize

	   ENDIF ELSE BEGIN

;
; Otherwise, find a label lenth that will fit within the bar that we just
; drew representing the ith DSN 27 pass.
;

	      t_label_length = dx - 16

;
; We also need to temperarily reduce the character size multiplier to 
; accomadate our smaller label field.
;

	      t_chsize = (FLOAT (t_label_length) / label_length) * chsize

;
; Calculate the starting and ending locations of the new, reduced length
; label field within the bar representing the ith DSN 27 pass.
;

	      x1_lbl = x1 + FIX (dx / 2.0) - FIX (t_label_length / 2.0) + 1
	      x2_lbl = x1_lbl + t_label_length

	   ENDELSE

;
; Write the label field on the bar representing the ith DSN 27 pass
;

	   x_lbl_coord = [x1_lbl, x1_lbl, x2_lbl, x2_lbl]
	   y_lbl_coord = [y1_lbl, y2_lbl, y2_lbl, y1_lbl]

	   POLYFILL, x_lbl_coord, y_lbl_coord, COLOR = 0, /DEVICE

;
; Write the actual label itself within the label field.
;


	   XYOUTS, x1 + FIX (dx / 2.0),			$
	           y1_lbl + 4, 				$
	           "DSN 27", 				$
	           ALIGNMENT = 0.5, 			$
	           CHARSIZE = t_chsize,			$
	           /DEVICE

LOOP_CONTINUE:

	ENDFOR

	RETURN

	END
