	PROGRAM FSIREXAMPLE_EZ
C
C	THIS PROGRAM ILLUSTRATES THE READING OF A FILE IN THE BYU MERS SIR
C       FILE FORMAT USING THE RECORD-BASED SIREZ INTERFACE.  THE PROGRAM
C       READS THE FILE, TESTS THE LOCATION TRANSFORMATIONS,
C       WRITES OUT A BYTE-ARRAY COPY OF THE IMAGE, AND WRITES A SIR FORMAT
C       OUTPUT FILE
C
C	WRITTEN BY D.LONG: MARCH 1997
C       REVISED BY D.LONG: Nov. 2000  +Version 3.0 header
C       REVISED BY D.LONG: Nov. 2000  +SIREZ
C
C       Link with SIR fortran library routines and SIREZ.f
C
        CHARACTER*70 FNAME,ONAME
C
C	IMAGE STORAGE VARIABLES
C
	PARAMETER (MAXSIZE=4500000)       ! MAXIMUM PIXELS
	REAL 	STVAL(MAXSIZE)            ! READ ARRAY
	BYTE	B(MAXSIZE)                ! OUTPUT BYTE ARRAY
C
C	IMAGE FILE HEADER INFORMATION
C
	INCLUDE 'SIREZ.inc'
	RECORD /SIRHEAD/ HEAD
C
C	PROMPT USER FOR FILE NAME INPUT
C
	WRITE(*,*)
	WRITE(*,15)
15	FORMAT(' Enter Input SIR File Name: ',$)
	READ(*,20) FNAME
20	FORMAT(A70)
C
40	CONTINUE
	WRITE(*,*) 'SIR In= "',FNAME(1:LENGTH(FNAME)),'"'
C
C	READ SIR FILE HEADER
C
	IU=10
	CALL GETSIRHEAD(FNAME,IU,HEAD,IERR)
C
C     READ IMAGE CONTENTS
C
	IF (IERR.GE.0) THEN
	   CALL GETSIRDATA(IU,HEAD,STVAL,IERR,SMIN,SMAX)
	ELSE
	   WRITE (*,*) '*** ERROR OPENING FILE',IERR
	ENDIF
	IF (IERR.LT.0) STOP
C
C	WRITE OUT IMAGE HEADER INFORMATION
C
	WRITE (*,*)
	WRITE (*,*) 'SIR File Header Information:'
	CALL PRINTSIRHEAD(HEAD)
	WRITE (*,*)
C
C     TEST THE TRANSFORMATIONS
C
	NSX=HEAD.NSX
	NSY=HEAD.NSY
        DO IX=1,NSX,NSX-1
           DO IY=1,NSY,NSY-1
              X=IX
              Y=IY
              CALL SIRPIX2LATLON(X,Y,ALON,ALAT,HEAD)
              J=ISIRLATLON2PIX(ALON,ALAT,X1,Y1,HEAD)
              J=ISIRPIX(X1,Y1,IX1,IY1,HEAD)
	      VALUE=HEAD.ANODATA
	      IF (J.GT.0) VALUE=STVAL(J)
              WRITE (*,400) IX,IY,ALAT,ALON,X1,Y1,IX1,IY1,VALUE
 400	      FORMAT('Pix(',I4,',',I4,') Lat/Lon (',F6.2,',',F7.2,
     *		   ') (',F6.1,',',F6.1,')(',I4,',',I4,') ',F7.3)
          END DO
        END DO
C
C	REDETERMINE MAX/MIN OF DATA ARRAY
C
	SMIN=1.E25
	SMAX=-1.E25
	DO NN1=0,NSY-1
	   DO NN2=1,NSX
	      INDEX = ISIRLEX(NN2,NN1,HEAD)
	      IF (STVAL(INDEX).NE.HEAD.ANODATA) THEN
		 SMIN=MIN(SMIN,STVAL(INDEX))
		 SMAX=MAX(SMAX,STVAL(INDEX))
	      ENDIF
	   END DO
	END DO
C
	SMIN1=SMIN
	SMAX1=SMAX
C
	WRITE (*,*)
	WRITE (*,*) 'Array min, max values:',SMIN1,SMAX1
C
    	WRITE (*,34) 'Saturation Max?'
34	FORMAT(X,'New ',A,X,$)
        READ (*,*) SMAX
	WRITE (*,34) 'Saturation Min?'
        READ (*,*) SMIN
C
C	SCALE ARRAY AND PUT INTO BYTE ARRAY
C
C     NOTE THAT SIR IMAGE DATA HAS THE ORIGIN AT THE LOWER-LEFT OF THE
C     IMAGE.  THE BYTE ARRAY HAS THE ORIGIN AT THE UPPER-LEFT.
C
	SS=(SMAX-SMIN)/255.0
	IF (SS.GT.0.0) THEN
	   SS=1.0/SS
	ELSE
	   SS=1.0
	ENDIF
	DO NN1=0,NSY-1
	   DO NN2=1,NSX
	      INDEX = NN1*NSX + NN2
	      INDEX2= (NSY-NN1)*NSX + NN2
	      VAL=STVAL(INDEX)
	      IVAL=(VAL-SMIN)*SS
	      IF (IVAL.LT.0) IVAL=0
	      IF (IVAL.GT.255) IVAL=255
	      IF (IVAL.GT.127) IVAL=IVAL-256
	      B(INDEX2)=IVAL
	   END DO
	END DO
C
C     GET OUTPUT FILE NAME FOR BYTE FILE
C
	WRITE(*,25)
25	FORMAT(' Enter BYTE Output File Name: ',$)
	READ(*,20) ONAME
	WRITE(*,*) 'BYTE Out="',ONAME(1:LENGTH(ONAME)),'"'
C
C	NOW WRITE OUT BYTE FILE
C
	WRITE(*,*) 'Output Image Size: (X x Y)',NSX,NSY
	NS=NSX*NSY
C
	OPEN(UNIT=1,FILE=ONAME,STATUS='NEW',FORM='UNFORMATTED')
C
	WRITE (1) (B(I),I=1,NS)
C
C     GET OUTPUT FILE NAME FOR SIR FILE
C
	WRITE(*,30)
30	FORMAT(' Enter SIR Output File Name: ',$)
	READ(*,20) ONAME
	WRITE(*,*) 'SIR Out="',ONAME(1:LENGTH(ONAME)),'"'
C
C	WRITE SIR FORMAT FILE
C
        HEAD.IDATATYPE=2     ! MAKE SURE OUTPUT IMAGE IS IN STANDARD I*2 FORM
	HEAD.TITLE='Re-written '//fname
C
C     WRITE OUT SIR FILE
C
	CALL PUTSIRFILE(ONAME,IU,HEAD,STVAL,IERR)
C
	IF (IERR.LT.0)
     *	    WRITE (*,*) '*** ERROR WRITING SIR FILE ***'
C
360	STOP
	END

