	SUBROUTINE SAO_DRAW(ACTION,CURSOR_TYPE,COLOR_TYPE,X,Y,
	1NPIC_SELECTED,a,b,INCLINA,EXTRA,NEXTRA)
C
C  Note:  This is the UNIX version
C
C       This subroutine sends commands to SAOimage via the Imtool protocol
C  in order to carry out various drawing tasks.  One can color, size, and
C  position various types of erasable cursors (circle, rectangle, etc.) and
C  various types of nonerasable cursors.  The erasable cursors are useful
C  for marking an area for possible editing or measuring action, and the
C  nonerasble ones are useful for marking the area after an editing action
C  has taken place.  When the image is reloaded, the nonerasble cursors will
C  be erased, of course.  Rotation and annuli are not yet implemented for the 
C  nonerasable cursors.
C
C				Donald H. Gudehus, August, 1993
C				Version 2, D. Gudehus, September 18, 1994
C				Changed some variable names and added case
C				of ACTION=3
C				Version 2U, D. Gudehus, September, 1994
C				Modified code for UNIX
C				Version 3, D. Gudehus, October 1, 1994
C				Allowed for an arbitrary number of annuli
C				rather than just 2 extra.
C				Version 4, D. Gudehus, July 25, 2006
C				Incorporated new saofacts.for
C
C	ACTION		(C)	The proposed action:
C				0  = erases an erasable cursor
C				1  = draws an erasable cursor
C				2  = draws a nonerasable cursor
C				3  = draws a nonerasable straight line with
C				     immediate display.  The preferred
C				     routine for drawing nonerasable straight 
C				     lines, however, is SAO_VECT.
C				4  = draws a nonerasable straight line with
C			 	     delayed display.  You should call 
C				     SAO_DISPLAY_VECT to see it in the frame 
C				     buffer or call this subroutine with 
C				     ACTION=3 and a Y range large enough to 
C				     include the vector.
C				5  = reserved for SAO_DISPLAY_VECT
C				10 = erases an erasable region cursor
C				11 = draws an erasable region cursor
C	CURSOR_TYPE	(C)	The type of cursor:
C				256  = dot cursor (100 hexidecimal)
C				       (only erasable).  For a nonerasable
C				       dot, call SAODOT.
C				768  = rectangle cursor (300 hexidecimal)
C				1024 = circle (or ellipse) cursor 
C				       (400 hexidecimal)
C				1536 = Arrow cursor (600 hexidecimal)
C				       (only erasable)
C	COLOR_TYPE	(C)	The color of the cursor:
C				201 = green
C				202 = black
C				203 = white
C				204 = red
C				206 = blue
C				207 = yellow
C				For the nonerasable cursors additional colors
C				in the range 0 to 199 are available depending
C				on the current color lookup table.
C	X		(C)	The central x location of the object in map 
C				coordinates.  It is up to the calling routine
C				to ensure that the fractional part of X is 0.5
C				in the case where the side of a rectangle 
C				is an odd number of pixels and 0.0 for an 
C				even number.  If a circle is to be centered
C				on a pixel, the fractional part should be 0.5.
C	Y		(C)	Same as X but in the vertical direction
C	NPIC_SELECTED	(C)	The picture (which of several possible in 
C				frame buffer).  This determines which 
C				picture's coordinate system will apply to 
C				X and Y.
C	a		(C)	One half the horizontal cursor length measured
C				from pixel center to pixel center.  It is up 
C				to the calling routine to ensure that the 
C				fractional part is 0.0 for an odd number of 
C				pixels for the total cursor length and 0.5 
C				for an even number.  In the case of drawing 
C				a single straight line (ACTION = 3 or 4), a 
C				is to be interpreted as the second X position.
C	b		(C)	Same as a, but in the vertical direction.
C				When a and b are unequal for the circle, an 
C				ellipse is drawn.  In the case of drawing
C				a single straight line, b is interpreted as
C				the second Y position.
C	INCLINA		(C)	The inclination of the long axis of the cursor.
C				Note this is not yet implemented for 
C				nonerasable cursors.
C	EXTRA		(C)	An array containing additional dimensions
C				or positions for the cursor.  This is useful
C				for creating annuli or polygons.  In the case
C				of annuli, each quantity gives the long half
C				axis length of each additional annulus.  In 
C				the case of polygons, the x and y coordinates
C				of the additional vertexes are given in this 
C				array.  Note:  polygons are not yet 
C				implemented.  Polygons can however be drawn
C				by successive calls to SAO_VECT.
C	NEXTRA		(C)	The number of additional dimensions.  For
C				a single circle or rectangle, its value would
C				be 0.
C
	INTEGER ACTION,CURSOR_TYPE,COLOR_TYPE
	REAL INCLINA,EXTRA(NEXTRA+1)
C
	INCLUDE 'saofacts.for'
C
	INTEGER*2 ICOUNT
	INTEGER CURSOR_TYPE2
	LOGICAL ANNULUS
C
	IF (NEXTRA.GT.0) THEN
	  ANNULUS=.TRUE.
	  ICOUNT=67+NEXTRA*10
	ELSE
	  ANNULUS=.FALSE.
	  ICOUNT=67
	END IF
	IF (ACTION.EQ.1.AND.CURSOR_TYPE.EQ.1024.AND.a.NE.b) THEN
	  CURSOR_TYPE2=1280 !  Ellipse
	ELSE
	  CURSOR_TYPE2=CURSOR_TYPE !  Nonerasable cursors can draw
				   !  ellipses with CURSOR_TYPE=1024
	END IF
	IBUFFER_SAO(1)='40000'O !  = PACKED;  since is not IIS_READ, is write
	IBUFFER_SAO(2)=-ICOUNT !  imhead.thingct
	IBUFFER_SAO(3)=13 !  = imhead.subunit
        IBUFFER_SAO(4)=0
	IBUFFER_SAO(5)=0
        IBUFFER_SAO(6)=0
        IBUFFER_SAO(7)=1 !  Frame number
        IBUFFER_SAO(8)=0
	CALL IMTOOLCHECKSUM(IBUFFER_SAO)
	NBYTES=16
C  NPIC_SAO is the most recent picture number loaded.
	CALL SI_WRITE(BUFFER_SAO,NBYTES)
	XSAO=X-IMSX_SAO(NFRAME_SAO,NPIC_SELECTED)+
	1IMSX_SAO(NFRAME_SAO,NPIC_SAO)+XLOC_SAO(NPIC_SELECTED)
	2-XLOC_SAO(NPIC_SAO)
	YSAO=Y-IMSY_SAO(NFRAME_SAO,NPIC_SELECTED)+
	1IMSY_SAO(NFRAME_SAO,NPIC_SAO)-YLOC_SAO(NPIC_SELECTED)
	2+YLOC_SAO(NPIC_SAO)
        ENCODE (2,2002,CBUFFER_SAO(1:2)) ACTION
2002	FORMAT(I2)
	CBUFFER_SAO(3:3)=' '
        ENCODE (4,2004,CBUFFER_SAO(4:7)) CURSOR_TYPE2
2004	FORMAT(I4)
	CBUFFER_SAO(8:8)=' '
        ENCODE (8,2008,CBUFFER_SAO(9:16)) COLOR_TYPE
2008	FORMAT(I8)
	CBUFFER_SAO(17:17)=' '
	ENCODE (9,1009,CBUFFER_SAO(18:26)) XSAO
1009	FORMAT(F9.3)
	CBUFFER_SAO(27:27)=' '
	ENCODE (9,1009,CBUFFER_SAO(28:36)) YSAO
	CBUFFER_SAO(37:37)=' '
	ENCODE (9,1009,CBUFFER_SAO(38:46)) a
	CBUFFER_SAO(47:47)=' '
	ENCODE (9,1009,CBUFFER_SAO(48:56)) b
	CBUFFER_SAO(57:57)=' '
	ENCODE (9,1009,CBUFFER_SAO(58:66)) INCLINA
	CBUFFER_SAO(67:67)=' '
	IF (ANNULUS) THEN
	  DO I=0,NEXTRA-1
	    ENCODE (9,1009,CBUFFER_SAO(68+I*10:76+I*10)) EXTRA(I+1)
	    CBUFFER_SAO(77+I*10:77+I*10)=' '
	  END DO
	END IF
	NBYTES=ICOUNT
	CALL SI_WRITE(BUFFER_SAO,NBYTES)
C
	RETURN
	END
