	PROGRAM SAODEMO10
C
C  Note:  This is the Unix version
C
C       This program demonstrates opening a FITS file and displaying
C  it on "Enhanced" SAOimage.  File formats of BYTE, INTEGER*2, INTEGER*4,
C  REAL*4, REAL*8, COMPLEX*8 (real and imaginary parts are each REAL*4)
C  and COMPLEX*16 (real and imaginary parts are each REAL*8) are 
C  supported.  The entire image is automatically scaled so as to
C  fit within the frame.
C		Donald H. Gudehus, March 18, 2000
C		Version 2, D. H. Gudehus, June 1, 2005
C		Added support for 64-bit floating point FITS
C		Version 3, D. Gudehus, July, 2006
C		Rewrote it to make use of FITS Library routines
C
	INCLUDE 'fitsfacts.for'
	INCLUDE 'saofacts.for'
C
	CHARACTER*1 COMMAND
	CHARACTER*68 CHFITSMHV
	CHARACTER*80 FITS_FILENAME
C
	CHARACTER*3 DATFMT
	CHARACTER*38 MESS
	INTEGER BLUE(0:199),GREEN(0:199),RED(0:199)
	INTEGER ADDRESS
C
	CALL GET_ENDIAN
        IBLKLEN=2880
	LU=11
	CALL USRINP(%REF(FITS_FILENAME),1,'A',80,0,'INPUT_FILENAME=',
	1'Open the FITS file (include extension)') 
	CALL ALPHA(%REF(FITS_FILENAME),1,80,NC,NF,NR,2)
	OPEN(LU,STATUS='OLD',ACCESS='DIRECT',RECL=2880,
	1FORM='UNFORMATTED',FILE=FITS_FILENAME)
C
	CALL GET_FITS_INFO(LU)
C  Check for a valid file
	DO M=1,NUMMAPS(LU)
	  IF (NAXIS(M,LU).NE.2.AND.NAXIS(M,LU).NE.3) THEN
	    CALL ERROR(-4,'Sorry, NAXIS must be 2 or 3')
	  END IF
	END DO
C  Obtain the map number from the user.
	IMNUM=1
	IF (NUMMAPS(LU).EQ.1) THEN
	  CALL MESSAGE('There is one map in the file')
	ELSE IF (NUMMAPS(LU).GT.1) THEN
	  ENCODE(37,1215,MESS) NUMMAPS(LU)
1215	  FORMAT('There are'I4,' maps in the file'7X)
	  CALL MESSAGE(MESS)
	  CALL USRINP(IMNUM,1,'I',4,1,'MAP=',
	1 'Enter the map number:  (default = 1)')
	END IF
	NMAPS=NUMMAPS(LU)
C
	NPLNS=NAXIS3(IMNUM,LU)
	NROWS=NAXIS2(IMNUM,LU)
	NCOLS=NAXIS1(IMNUM,LU)
	DATFMT=CHFITSMHV(LU,IMNUM,'BITPIX','?')
	IF (DATFMT.EQ.'?') THEN
	  CALL ERROR(-3,'Error getting data format')
	  STOP
	END IF
	CALL MESSAGE('Reading from the file')
	ENCODE(38,1250,MESS) NROWS,NCOLS,DATFMT(1:3)
1250	FORMAT(I4,' rows of 'I4,' pixels in 'A3,' format')
	CALL MESSAGE(MESS)
	IF (NPLNS.EQ.1) THEN
	  WRITE (6,1254) NPLNS
1254	  FORMAT(' ','There is ',I1,' plane in this map')
	  WRITE (7,1254) NPLNS
	  IPNUM=1
	ELSE IF (NPLNS.GT.1) THEN
	  WRITE (6,1255) NPLNS
1255	  FORMAT(' ','There are ',I4,' planes in this map')
	  WRITE (7,1255) NPLNS
	  IPNUM=1
	  IF (NPLNS.EQ.2) THEN
	    CALL USRINP(IPNUM,1,'I',4,1,'PLANE=',
	1   'Enter the plane number (real = 1, imaginary =2):  (default = 1)')
	  ELSE
	    CALL USRINP(IPNUM,1,'I',4,1,'PLANE=',
	1   'Enter the plane number:  (default = 1)')
	  END IF
	END IF
C
	WRITE (6,1200) BZERO(IMNUM,LU)
1200	FORMAT(' ','A value of BZERO = ',E,' will be used')
	WRITE (7,1200) BZERO(IMNUM,LU)
	WRITE (6,1300) BSCALE(IMNUM,LU)
1300	FORMAT(' ','A value of BSCALE = ',E,' will be used')
	WRITE (7,1300) BSCALE(IMNUM,LU)
C
	IFB_SAO=0 !  Lets user be prompted for size of buffer in
		  !  Enhanced SAOimage
C  Initialize Enhanced SAOimage
	CALL SAOSTART
	CALL SAOFIRST
C
C  Reduce the size of the loaded image by the smallest integer value
C  that allows it to fit within the chosen frame
	IREDUCTX=(NCOLS-1)/MAXFB_COLS_SAO+1
	IREDUCTY=(NROWS-1)/MAXFB_ROWS_SAO+1
	IREDUCT=MAX(IREDUCTX,IREDUCTY)
	ICOLS=MAX(1,NCOLS/IREDUCT)
	IROWS=MAX(1,NROWS/IREDUCT)
C
	NWORDS=ICOLS*IROWS
	CALL DYNAMIC(ADDRESS,NWORDS,'  R4',.TRUE.)
	CALL FITS_TO_SAO1(%VAL(ADDRESS),NCOLS,NROWS,ICOLS,IROWS,IREDUCT,
	1IMNUM,IPNUM,LU,FITS_FILENAME,DATFMT,
	2BZERO(IMNUM,LU),BSCALE(IMNUM,LU))
	CALL DYNAMIC(ADDRESS,NWORDS,'  R4',.FALSE.)
C
	DO WHILE(.TRUE.)
	  COMMAND='K'
	  CALL MESSAGE('Enter a command:  (default = K)')
	  CALL MESSAGE('   J = Loads a high contrast spectrum lookup table')
	  CALL MESSAGE('   K = Loads a spectrum lookup table including white')
	  CALL MESSAGE('   Q = Quits')
	  CALL MESSAGE('   W = Loads a black and white triangle look-up table')
	  CALL MESSAGE('   X = Loads a black and white sawtooth look-up table')
	  CALL USRINP(%REF(COMMAND),1,'A',1,1,'COMMAND=',' ')
	  IF (COMMAND.EQ.'J'.OR.COMMAND.EQ.'K'.OR.COMMAND.EQ.'W') THEN
	    IF (COMMAND.EQ.'J') IVAL='900'X
	    IF (COMMAND.EQ.'K') IVAL='A00'X
	    IF (COMMAND.EQ.'W') IVAL='B00'X
	    CALL SAOCONVLT(IVAL)
	  ELSE IF (COMMAND.EQ.'Q') THEN
	    STOP
	  ELSE IF (COMMAND.EQ.'X') THEN
	    DO J=1,10
	      DO I=0,19
	        RED(I+20*(J-1))=I*255/19
	        GREEN(I+20*(J-1))=I*255/19
	        BLUE(I+20*(J-1))=I*255/19
	      END DO
	    END DO
	    CALL SAO_LOAD_VLT(BLUE,GREEN,RED)
	  END IF
	END DO
C
	CALL CLOSEFITS(LU)
	END
C
	SUBROUTINE FITS_TO_SAO1(ARRAY,NCOLS,NROWS,ICOLS,IROWS,IREDUCT,
	1IMNUM,IPNUM,LU,FITS_FILENAME,DATFMT,BZERO,BSCALE)
C
C       This subroutine helps display a FITS file on Enhanced
C  SAOimage.  The file is assumed to have been previously opened, the
C  map selected, and the plane selected.
C			Donald H. Gudehus, Aug., 2006
C
C	ARRAY	      (C) An array created by dynamic memory, and used to
C			  hold the data given to SAOLOAD
C	NCOLS	      (C) The number of columns in the file
C	NROWS	      (C) The number of rows in the file
C	ICOLS	      (C) The X dimension of ARRAY
C	IROWS	      (C) The Y dimension of ARRAY
C	IRECT	      (C) An automatically determined integer scaling factor
C			  to allow the image to fit within the frame.
C	IMNUM	      (C) The selected map number
C	IPNUM	      (C) The selected plane number
C	LU	      (C) The logical unit used for reading
C	FITS_FILENAME (C) The FITS filename which will appear on
C			  SAOimage.
C	DATFMT	      (C) The file data format
C	BZERO	      (C) The data zero point from the FITS header
C	BSCALE	      (C) The data scaling factor from the FITS header
C
	CHARACTER*3 DATFMT
	CHARACTER*80 FITS_FILENAME
	REAL*4 ARRAY(ICOLS,IROWS)
	REAL*8 BSCALE,BZERO
C
	INCLUDE 'saofacts.for'
	PARAMETER (MAXSIZE=4096)
	REAL*4 DATA(MAXSIZE),MIN_MAX(2)
	REAL*8 DATADP(MAXSIZE)
C
	IF (DATFMT.EQ.'I1'.OR.DATFMT.EQ.'I2'.OR.DATFMT.EQ.'I4'.OR.
	1 DATFMT.EQ.'R4'.OR.DATFMT.EQ.'C8') THEN
	  JJ=1
	  DO J=1,NROWS,IREDUCT
	    CALL RDFITSROW(LU,IMNUM,IPNUM,DATA,MAXSIZE,J)
	    II=1
	    DO I=1,NCOLS,IREDUCT
	      ARRAY(II,JJ)=DATA(I)
	      II=II+1
	    END DO
	    JJ=JJ+1
	  END DO
	ELSE IF (DATFMT.EQ.'R8'.OR.DATFMT.EQ.'C16') THEN
	  JJ=1
	  DO J=1,NROWS,IREDUCT
	    CALL RDDPFITSROW(LU,IMNUM,IPNUM,DATADP,MAXSIZE,J)
	    II=1
	    DO I=1,NCOLS,IREDUCT
	      ARRAY(II,JJ)=DATADP(I)
	      II=II+1
	    END DO
	    JJ=JJ+1
	  END DO
	END IF
C  Position the image in the center of the frame
 999	IXFRAME=(MAXFB_COLS_SAO-ICOLS)/2
	IYFRAME=(MAXFB_ROWS_SAO+IROWS)/2-1
	BRIGHTNESS_MIN=1.0E38
	BRIGHTNESS_MAX=-1.0E38
	DO J=1,IROWS
	  DO I=1,ICOLS
	    IF (ARRAY(I,J).LT.BRIGHTNESS_MIN) BRIGHTNESS_MIN=ARRAY(I,J)
	    IF (ARRAY(I,J).GT.BRIGHTNESS_MAX) BRIGHTNESS_MAX=ARRAY(I,J)
	  END DO
	END DO
	MIN_MAX(1)=BRIGHTNESS_MIN
	MIN_MAX(2)=BRIGHTNESS_MAX
	CALL USRINP(MIN_MAX,2,'F',4,1,'MINIMUM_MAXIMUM=','Enter the '//
	1'minimum and maximum for the display (default = values in file)')
	BRIGHTNESS_MIN=MIN_MAX(1)
	BRIGHTNESS_MAX=MIN_MAX(2)
	NPICTURE=1
C  Load SAOimage in `fast mode'
	MODE=0
	CALL SAOLOAD(ARRAY,ICOLS,IROWS,IXFRAME,IYFRAME,
	1BRIGHTNESS_MIN,BRIGHTNESS_MAX,FITS_FILENAME,NPICTURE,MODE)
C
	END
