	PROGRAM FSIR_DUMP
C
C	THIS PROGRAM READS A BYU SIR-FORMAT FILE AND DUMPS CONTENTS TO AN
C       ASCII FILE.  ILLUSTRATES THE READING FILE FORMAT AND MAKING 
C       GEOMETRIC TRANSFORMATIONS
C
C	WRITTEN BY D.LONG: 9 Feb 2002
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)
	WRITE(*,*) 'SIR In= "',FNAME(1:LENGTH1(FNAME)),'"'
C
	WRITE(*,25)
 25	FORMAT(' Enter ASCII Output File Name: ',$)
	READ(*,20) FNAME2
	WRITE(*,*) 'Out= "',FNAME2(1:LENGTH1(FNAME2)),'"'
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     READ IMAGE CONTENTS
C
        IF (IERR.GE.0) THEN
           CALL READSIRF(IU,IERR,NHEAD,NHTYPE,IDATATYPE,STVAL,NSX,NSY,
     *           IOFF,ISCALE,SMIN,SMAX,NCNT,ANODATA,VMIN,VMAX)
        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 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 (*,30)
 30	FORMAT(' Lat,Lon for Lower-Left corner (L) or Center (C) of pixel? ',$)
        READ (*,'(A1)') CHAR_INPUT
	WRITE (*,*)
        IF (CHAR_INPUT.EQ.'C' .OR. CHAR_INPUT.EQ.'c') HALF=0.5
	IF (HALF.GT.0.1) THEN
	   WRITE (*,*) 'Lat,Lon coordinates correspond to center of pixels'
	ELSE
	   WRITE (*,*) 'Lat,Lon coordinates correspond to pixel lower-left corners'
	ENDIF
	WRITE (*,*) 'Pixels: ',NSX,' x ',NSY,' = ',NSX*NSY,'  No Data: ',ANODATA
	WRITE (*,*) 'One pixel/line: x_index y_index image_value latitude longitude'

C
C       OPEN OUTPUT FILE
C
	IU2=11
	OPEN(UNIT=IU2,FILE=FNAME2,STATUS='UNKNOWN',FORM='FORMATTED')
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
	      WRITE(IU2,*) IX,IY,STVAL(NI),ALAT,ALON
          END DO
        END DO
	CLOSE(IU2)
C
	STOP
	END

