	PROGRAM SAODEMO9
C
C  Note:  This is the UNIX version
C
C	This program demonstrates the control of SAOimage from a
C  standalone program.  Here we see the elliptical orbital motion of a
C  binary system.
C
C			Donald H. Gudehus, August, 1999
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=72
	REAL ARRAY(NCOLS,NROWS,NIMAGES)
C
	CHARACTER*40 ARRAY_NAME
	INTEGER*4 X_OFFSET,Y_OFFSET
	REAL*8 SEMIMAJAX,ECCENTR,MEANANOM,TRUEANOM,ECCENTRANOM,RADIUS
C
	CALL GET_ENDIAN
	DELAY_TIME=0.0
	CALL USRINP(DELAY_TIME,1,'F',4,1,'DELAY_TIME=',
	1'Enter an interframe delay time in seconds:  (default = 0)')
C  Don't prompt the user for the size of the buffer, just use a big
C  enough one.
	IFB_SAO=11 !  Gives a 128 by 128 buffer
	CALL SAOSTART
	CALL SAOFIRST
C
C  Preset the lookup table to a convenient range of colors
	IVAL='A00'X
	CALL SAOCONVLT(IVAL)
C
	BRIGHTNESS_MIN=6.0
	BRIGHTNESS_MAX=1000.0
	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
C  Center the images within the frame. 
C  The frame has (0,0) in the upper left;  
C  any image has (1,1) in the lower left.
	IXFRAME=MAXFB_COLS_SAO/2-NC1
	IYFRAME=MAXFB_ROWS_SAO/2+NR1
	ARRAY_NAME='Elliptical motion demonstration'
	MODE=0 !  Allows loading several pictures into frame
C  Create the images
	a=2.0
	SEMIMAJAX=20.0
	NPICTURE=1
	ECCENTR=0.7
	MEANANOM=0.0
	X_OFFSET=-10
	Y_OFFSET=-10
	ANG_OFFSET=45.0
	DO K=1,NIMAGES
	  MEANANOM=K*360.0/NIMAGES
	  CALL KEPLER(MEANANOM,ECCENTR,ECCENTRANOM)
	  TRUEANOM=2.0*DATAND(DSQRT((1.0+ECCENTR)/(1.0-ECCENTR))*DTAND(
	1 ECCENTRANOM/2.0))
	  RADIUS=SEMIMAJAX*(1.0-ECCENTR*DCOSD(ECCENTRANOM))
	  X1=RADIUS*DCOSD(TRUEANOM+ANG_OFFSET)+NC1+X_OFFSET
	  X2=-2.2*RADIUS*DCOSD(TRUEANOM+ANG_OFFSET)+NC1+X_OFFSET
	  Y1=RADIUS*DSIND(TRUEANOM+ANG_OFFSET)+NR1+Y_OFFSET
	  Y2=-2.2*RADIUS*DSIND(TRUEANOM+ANG_OFFSET)+NR1+Y_OFFSET
C  The lower left corner of each data array is considered to have 
C  coordinates 1, 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+350.0/(1+R2/a)**2
	    END DO
	  END DO
	END DO
C  Now load the images into SAOimage.
	DO NREPEAT=1,8
	  DO K=1,NIMAGES
	    CALL SAOLOAD(ARRAY(1,1,K),NCOLS,NROWS,IXFRAME,IYFRAME,
	1   BRIGHTNESS_MIN,BRIGHTNESS_MAX,ARRAY_NAME,NPICTURE,MODE)
	    CALL DELAY(DELAY_TIME)
	  END DO
	END DO
C
	END
