	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       REVIZED BY D.LONG: Nov. 2000  +Version 3.0 header
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
	CHARACTER SENSOR*40,TITLE*80
        CHARACTER TAG*40,TYPE*138,CRPROC*100,CRTIME*28
        INTEGER   NSX,NSY,IOPT,IDATATYPE,IOFF,ISCALE 
        INTEGER   IXDEG_OFF,IYDEG_OFF,IDEG_SC,ISCALE_SC,IA0_OFF,IB0_OFF,IO_SC
        INTEGER   NHEAD,NDES,NHTYPE,LDES,NIA,IPOL,IFREQHM,ISPARE1
        INTEGER   IREGION,ITYPE,IYEAR,ISDAY,ISMIN,IEDAY,IEMIN
        REAL      XDEG,YDEG,ASCALE,BSCALE,A0,B0,ANODATA,VMIN,VMAX
C
C     OPTIONAL HEADER INFO
C
        PARAMETER (MAXI=256)
        CHARACTER DESCRIP*1024
	DIMENSION IAOPT(MAXI)
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
	MAXDES=LEN(DESCRIP)
        IU = 10
	CALL READSIRHEAD3(FNAME,IU,IERR,NHEAD,NDES,NHTYPE,IDATATYPE,
     *     NSX,NSY,XDEG,YDEG,ASCALE,BSCALE,A0,B0,IOPT,IOFF,ISCALE,
     *     IXDEG_OFF,IYDEG_OFF,IDEG_SC,ISCALE_SC,IA0_OFF,IB0_OFF,IO_SC,
     *     IYEAR,ISDAY,ISMIN,IEDAY,IEMIN,IREGION,ITYPE,
     *     IPOL,IFREQHM,ISPARE1,ANODATA,VMIN,VMAX,
     *     SENSOR,TITLE,TYPE,TAG,CRPROC,CRTIME,
     *     MAXDES,DESCRIP,LDES,MAXI,IAOPT,NIA)
C
C	WRITE OUT IMAGE HEADER INFORMATION
C
	WRITE (*,*)
	WRITE (*,*) 'SIR File Header Information:'
	CALL PRINTHEAD3(NHEAD,NDES,NHTYPE,IDATATYPE,
     *     NSX,NSY,XDEG,YDEG,ASCALE,BSCALE,A0,B0,IOPT,IOFF,ISCALE,
     *     IXDEG_OFF,IYDEG_OFF,IDEG_SC,ISCALE_SC,IA0_OFF,IB0_OFF,IO_SC,
     *     IYEAR,ISDAY,ISMIN,IEDAY,IEMIN,IREGION,ITYPE,
     *     IPOL,IFREQHM,ISPARE1,ANODATA,VMIN,VMAX,
     *     SENSOR,TITLE,TYPE,TAG,CRPROC,CRTIME,
     *     DESCRIP,LDES,IAOPT,NIA)
	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
        DO IX=1,NSX
           DO IY=1,NSY
              X=IX+HALF
              Y=IY+HALF
              CALL PIX2LATLON(X,Y,THELON,THELAT,ALON,ALAT,
     $		   IOPT,XDEG,YDEG,ASCALE,BSCALE,A0,B0)
              NI=(IY-1)*NSX+IX
              STVAL(NI)=ALAT
          END DO
        END DO
C
C       SET SIR HEADER SCALE FACTORS
C
        IOFF=-91
        ISCALE=360
	ANODATA=-91.0
	VMIN=-90.0
	VMAX=90.0
C
        TYPE='Lat. of '//FNAME(1:LENGTH(FNAME))
        FNAME2=FNAME(1:LENGTH(FNAME))//'_lat'
C
C	WRITE SIR FORMAT FILE
C
        IDATATYPE=2     ! MAKE SURE OUTPUT IMAGE IS IN STANDARD I*2 FORM
	ITYPE=31        ! SET FILE TYPE CODE TO LATITUDE
C
        WRITE (*,*) 'Writing "',FNAME2(1:LENGTH(FNAME2)),'"'
	CALL WRITESIR3(FNAME2,40,IERR,NHEAD,NDES,NHTYPE,IDATATYPE,
     *     NSX,NSY,XDEG,YDEG,ASCALE,BSCALE,A0,B0,IOPT,IOFF,ISCALE,
     *     IXDEG_OFF,IYDEG_OFF,IDEG_SC,ISCALE_SC,IA0_OFF,IB0_OFF,IO_SC,
     *     IYEAR,ISDAY,ISMIN,IEDAY,IEMIN,IREGION,ITYPE,
     *     IPOL,IFREQHM,ISPARE1,ANODATA,VMIN,VMAX,
     *     SENSOR,TITLE,TYPE,TAG,CRPROC,CRTIME,
     *     STVAL,DESCRIP,LDES,IAOPT,NIA)
C
	IF (IERR.LT.0) THEN
	   WRITE (*,*) '*** ERROR WRITING SIR FILE ***'
	ENDIF
C
C     COMPUTE THE LONGITUDE IMAGE
C
        DO IX=1,NSX
           DO IY=1,NSY
              X=IX+HALF
              Y=IY+HALF
              CALL PIX2LATLON(X,Y,THELON,THELAT,ALON,ALAT,
     $		   IOPT,XDEG,YDEG,ASCALE,BSCALE,A0,B0)
              NI=(IY-1)*NSX+IX
              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
        IOFF=-181
        ISCALE=180
	ANODATA=-181.0
	VMIN=-180.0
	VMAX=180.0
C
        TYPE='Long. of '//FNAME(1:LENGTH(FNAME))
        FNAME2=FNAME(1:LENGTH(FNAME))//'_lon'
	ITYPE=30        ! SET FILE TYPE CODE TO LONGITUDE
C
C	WRITE SIR FORMAT FILE
C
        WRITE (*,*) 'Writing "',FNAME2(1:LENGTH(FNAME2)),'"'
	CALL WRITESIR3(FNAME2,40,IERR,NHEAD,NDES,NHTYPE,IDATATYPE,
     *     NSX,NSY,XDEG,YDEG,ASCALE,BSCALE,A0,B0,IOPT,IOFF,ISCALE,
     *     IXDEG_OFF,IYDEG_OFF,IDEG_SC,ISCALE_SC,IA0_OFF,IB0_OFF,IO_SC,
     *     IYEAR,ISDAY,ISMIN,IEDAY,IEMIN,IREGION,ITYPE,
     *     IPOL,IFREQHM,ISPARE1,ANODATA,VMIN,VMAX,
     *     SENSOR,TITLE,TYPE,TAG,CRPROC,CRTIME,
     *     STVAL,DESCRIP,LDES,IAOPT,NIA)
C
	IF (IERR.LT.0) THEN
	   WRITE (*,*) '*** ERROR WRITING SIR FILE ***'
	ENDIF
C
360	STOP
	END

