	PROGRAM FSIR_LOCMAP
C
C	THIS PROGRAM ILLUSTRATES THE READING OF A FILE IN THE BYU MERS SIR
C       FILE FORMAT.  IT 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 version
C
C       Link with SIR fortran library routines
C
        CHARACTER*120 FNAME,FNAME2
C
C	IMAGE STORAGE VARIABLES
C
	PARAMETER (MAXSIZE=4500000)       ! MAXIMUM PIXELS/IMAGE
	REAL 	STVAL(MAXSIZE)            ! READ ARRAY
C
	CHARACTER*1 CHAR_INPUT
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(A120)
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       WE DON'T NEED THE IMAGE DATA SO WE DON'T READ IT
C
C	WRITE OUT IMAGE HEADER INFORMATION
C
	WRITE (*,*)
	WRITE (*,*) 'SIR File Header Information:'
	CALL PRINTSIRHEAD(HEAD)
	WRITE (*,*)
C
C     DETERMINE IF PIXEL CENTER OR CORNER DESIRED
C
        HALF=0.0
        WRITE (*,140)
 140    FORMAT(' Lat,Lon for Lower-Left corner (L) or Center (C) of pixel? ',$)
        READ (*,'(A1)') CHAR_INPUT
        IF (CHAR_INPUT.EQ.'C' .or. CHAR_INPUT.EQ.'c') HALF=0.5
C
C     COMPUTE THE LATTITUDE IMAGE
C
	NSX=HEAD.NSX
	NSY=HEAD.NSY
C
        DO IX=1,NSX
           DO IY=1,NSY
              X=IX+HALF
              Y=IY+HALF
              CALL SIRPIX2LATLON(X,Y,ALON,ALAT,HEAD)
	      NI=ISIRLEX(IX,IY,HEAD)
              STVAL(NI)=ALAT
          END DO
        END DO
C
C       SET SIR HEADER SCALE FACTORS
C
        HEAD.IOFF=-91
        HEAD.ISCALE=360
	HEAD.ANODATA=-91.0
	HEAD.VMIN=-90.0
	HEAD.VMAX=90.0
C
        HEAD.TYPE='Lat. of '//FNAME(1:LENGTH(FNAME))
        FNAME2=FNAME(1:LENGTH(FNAME))//'_lat'
C
C	WRITE SIR FORMAT FILE
C
        HEAD.IDATATYPE=2     ! MAKE SURE OUTPUT IMAGE IS IN STANDARD I*2 FORM
	HEAD.ITYPE=31        ! SET FILE TYPE CODE TO LATITUDE
C
        WRITE (*,*) 'Writing "',FNAME2(1:LENGTH(FNAME2)),'"'
	CALL PUTSIRFILE(FNAME2,IU,HEAD,STVAL,IERR)
C
	IF (IERR.LT.0)
     *	    WRITE (*,*) '*** ERROR WRITING SIR FILE ***'
C
C     COMPUTE THE LONGITUDE IMAGE
C
        DO IX=1,NSX
           DO IY=1,NSY
              X=IX+HALF
              Y=IY+HALF
              CALL SIRPIX2LATLON(X,Y,ALON,ALAT,HEAD)
	      NI=ISIRLEX(IX,IY,HEAD)
              IF (ALON.GT.180.0) ALON=ALON-360.0
              IF (ALON.LT.-180.0) ALON=ALON+360.0
              STVAL(NI)=ALON
          END DO
        END DO
C
        HEAD.IOFF=-181
        HEAD.ISCALE=180
	HEAD.ANODATA=-181.0
	HEAD.VMIN=-180.0
	HEAD.VMAX=180.0
C
        HEAD.TYPE='Long. of '//FNAME(1:LENGTH(FNAME))
        FNAME2=FNAME(1:LENGTH(FNAME))//'_lon'
	HEAD.ITYPE=30        ! SET FILE TYPE CODE TO LONGITUDE
C
C	WRITE SIR FORMAT FILE
C
        WRITE (*,*) 'Writing "',FNAME2(1:LENGTH(FNAME2)),'"'
	CALL PUTSIRFILE(FNAME2,IU,HEAD,STVAL,IERR)
C
	IF (IERR.LT.0)
     *	    WRITE (*,*) '*** ERROR WRITING SIR FILE ***'
C
360	STOP
	END

