	PROGRAM SAODEMO4
C
C  Note:  This is the UNIX version
C
C	This program demonstrates the control of SAOimage from a
C  standalone program.  Here we load 4 images of the binary system 
C  from SAODEMO3 at 4 different phases.  The cursor can then be used to
C  read back positional information from each frame.  The program
C  calculates the desktop, frame, and map coordinates, as well as the
C  picture number.  A message is sent if the cursor is not in a picture.
C  From this information, the pixel intensity value is then obtained from 
C  the data array.
C
C			Donald H. Gudehus, September, 1994
C			Version 1U, D. Gudehus, September 21, 1994
C			Changed code to work under UNIX
C			Version 2, D. Gudehus, August 29, 2006
C			Incorporated changes from saofacts.for and
C			added call to GET_ENDIAN.
C
	INCLUDE 'saofacts.for'
C
	PARAMETER NCOLS=128,NROWS=128,NIMAGES=4
	INTEGER IXFRAME(NIMAGES),IYFRAME(NIMAGES)
	REAL ARRAY(NCOLS,NROWS,NIMAGES)
C
	CHARACTER*40 ARRAY_NAME
	INTEGER*2 IXT,IYT
	INTEGER*4 ACTION,CURSOR_TYPE,COLOR_TYPE
	LOGICAL MESSAG
	REAL INCLINA,EXTRA(1)
C
	CALL GET_ENDIAN
C  Don't prompt the user for the size of the buffer, just use a big
C  enough one.
	IFB_SAO=1 !  Gives a 512 by 512 buffer
	CALL SAOSTART
	CALL SAOFIRST
C
C  Preset the lookup table to a convenient range of colors
	IVAL='A00'X
	CALL SAOCONVLT(IVAL)
C
	CALL MESSAGE('Creating the images')
C  Zero the arrays
	DO K=1,NIMAGES
	  DO J=1,NROWS
	    DO I=1,NCOLS
	      ARRAY(I,J,K)=0.0
	    END DO
	  END DO
	END DO
	NC1=NCOLS/2
	NR1=NROWS/2-1
	BRIGHTNESS_MIN=0.0
	BRIGHTNESS_MAX=1000.0
C  Calculate where the images will be placed in the frame.  The 
C  coordinates below are the where the lower left of each image will be 
C  placed in the frame buffer.  The frame has (0,0) in the upper left;  
C  any image has (1,1) in the lower left.
	IXFRAME(1)=MAXFB_COLS_SAO/4-NC1
	IYFRAME(1)=MAXFB_ROWS_SAO/4+NR1 !  Upper left in frame buffer
	IXFRAME(2)=3*MAXFB_COLS_SAO/4-NC1
	IYFRAME(2)=MAXFB_ROWS_SAO/4+NR1 !  Upper right
	IXFRAME(3)=MAXFB_COLS_SAO/4-NC1
	IYFRAME(3)=3*MAXFB_ROWS_SAO/4+NR1 !  Lower left
	IXFRAME(4)=3*MAXFB_COLS_SAO/4-NC1
	IYFRAME(4)=3*MAXFB_ROWS_SAO/4+NR1 !  Lower right
	ARRAY_NAME='Cursor Demonstration'
	MODE=1 !  Allows loading several pictures into frame
	a=2.0
C  Create the images.
	DO K=1,NIMAGES
	  X1=10.0*COSD(90.0*K)+NC1
	  Y1=10.0*SIND(90.0*K)+NR1
	  X2=-22.0*COSD(90.0*K)+NC1
	  Y2=-22.0*SIND(90.0*K)+NR1
C  The lower left corner of each data array is considered to have 
C  coordinates 1, 1.
	  IMSX_SAO(0,K)=1
	  IMSY_SAO(0,K)=1
	  DO J=1,NROWS
	    DO I=1,NCOLS
	      R1=SQRT((I-X1)**2+(J-Y1)**2)
	      R2=SQRT((I-X2)**2+(J-Y2)**2)
	      ARRAY(I,J,K)=880.0/(1+R1/a)**2+440.0/(1+R2/a)**2+25.0
	    END DO
	  END DO
	END DO
C  Now load the images into SAOimage.  Each will be located at a 
C  different position.
	DO K=1,NIMAGES
	  NPICTURE=K
	  CALL SAOLOAD(ARRAY(1,1,K),NCOLS,NROWS,IXFRAME(K),IYFRAME(K),
	1 BRIGHTNESS_MIN,BRIGHTNESS_MAX,ARRAY_NAME,NPICTURE,MODE)
	END DO
C  Read the cursor coordinates
	KEY=0 !  Initializes the returned ASCII code of the key that will
	      !  be pressed
	CALL MESSAGE('The coordinates printed in the SAOimage window are '//
	1'array or map coordinates,')
	CALL MESSAGE('and are always relative to the last image loaded.')
	CALL MESSAGE('The coordinates you will see below are correct for '//
	1'each individual image')
	CALL MESSAGE(' ')
	DO WHILE (KEY.NE.81.AND.KEY.NE.113)
	  CALL MESSAGE('Be sure SAOimage has focus, position the cursor, '//
	1 'then press <CR> or any key.')
	  CALL MESSAGE('Press Q or q to quit')
	  MESSAG=.TRUE.
	  CALL SAO_CURN(NPICN,IXT,IYT,XF,YF,XM,YM,KEY,MESSAG)
	  WRITE (6,1000) NPICN,TV_FILENAME(NPICN)
	  WRITE (7,1000) NPICN,TV_FILENAME(NPICN)
1000	  FORMAT(' ','Cursor in image ',I2';  ','file name = ',A30)
	  WRITE (6,2000) IXT,IYT !  Position on TV
	  WRITE (7,2000) IXT, IYT
2000	  FORMAT(' ',10X,'(X, Y:  on TV desktop  )= ',I8,2X,I8)
	  WRITE (6,3000) XF,YF !  Position in frame
	  WRITE (7,3000) XF,YF
3000	  FORMAT(' ',10X,'(X, Y:  in frame       )= ',F8.3,2X,F8.3)
	  WRITE (6,4000) XM,YM !  Position in map
	  WRITE (7,4000) XM,YM
4000	  FORMAT(' ',10X,'(X, Y:  in map or array)= ',F8.3,2X,F8.3)
	  IF (XM.GE.1.AND.XM.LE.NCOLS.AND.YM.GE.1.AND.YM.LE.NROWS) THEN
	    WRITE (6,5000) ARRAY(INT(XM),INT(YM),NPICN) !  Pixel intensity
5000	    FORMAT(' ',10X,'Intensity in array      = ',F10.3)
	    WRITE (7,5000) ARRAY(INT(XM),INT(YM),NPICN) !  Pixel intensity
	  END IF
	END DO
C  Display some erasable rectangles 
C  We will reference the rectangles to the last image.
C
	ACTION=1  !  Erasable cursor
	CURSOR_TYPE=768 !  Rectangle
	NPIC1=NPIC_SAO
	X=MAXFB_COLS_SAO/2-XLOC_SAO(NPIC_SAO)+1.0
	Y=-MAXFB_ROWS_SAO/2+2.0+YLOC_SAO(NPIC_SAO)
			!  Center location of rectangle in system of last
			!  image
	DO I=1,257,16 !  Border of frame is 514X514.  We want to draw just
		      !  outside that.
	  INCLINA=(I-1)*22.5/16.0
	  COLOR_TYPE=MOD(I,5)+203
	  a=I+0.5 !  Half the horizontal length of the rectangle.  Since
		  !  our rectangle is drawn through the centers of an even
		  !  number of pixels, its length is odd.  Half of that 
		  !  gives us the 0.5.
	  b=a
	  EXTRA(1)=0.0
	  CALL SAO_DRAW(ACTION,CURSOR_TYPE,COLOR_TYPE,X,Y,NPIC1,
	1 a,b,INCLINA,EXTRA,0)
	END DO
C
	END
