	PROGRAM BYTE2SIR3_map
C
C	THIS PROGRAM CONVERTS A BYTE FILE TO A SIR FILE BY USING
C       THE HEADER INFORMATION FROM ANOTHER SIR FILE
C
C	WRITTEN: OCT 1994	DGL
C       REVISED: APR 1999       DGL
C       + new header format
C       REVISED: NOV 2000       DGL
C       + version 3.0 header format
C
	CHARACTER*70 FNAME,ONAME,BNAME,LINE
C
C	IMAGE DISPLAY VARIABLES
C
	PARAMETER (MAXSIZE=35000000)
	REAL 	STVAL(MAXSIZE)
	BYTE	B(MAXSIZE)
C
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(*,*) 'BYU MERS BYTE2SIR_map Program'
C
C
        NARG=IARGC()  ! GET NUMBER OF COMMAND LINE ARGUMENTS
        IF (NARG.LT.1) THEN
           PRINT *,'Command line: byte2sir template-file-in in-file <out_file> <min> <max>'
           STOP
        ENDIF
C
        IF (NARG.GT.0) THEN
           CALL GETARG(1,FNAME)
	   CALL GETARG(2,BNAME)
        ELSE
           PRINT *,'What TEMPLATE sir file to read from?'
           READ(*,15) FNAME
 15        FORMAT(A80)
           PRINT *,'What BYTE input file to read from?'
           READ(*,15) BNAME
        ENDIF
C
C	GET OUTPUT FILE
C
        IF (NARG.GT.0) THEN
           IF (NARG.GT.2) THEN
              CALL GETARG(3,ONAME)
           ELSE
              ONAME=' '
           ENDIF
        ELSE
 533       PRINT 255
 255       FORMAT(' Output BYTE File: [def=<input_file>.sir] ',$)
           READ(*,260) ONAME
        ENDIF
        IF (ONAME.EQ.' ') ONAME=BNAME(1:LENGTH(BNAME))//'.sir'
260	FORMAT(A80)
        WRITE(*,216) ONAME(1:LENGTH(ONAME))
216     FORMAT(' Output file "',A,'"')
C
	WRITE(*,*),'SIR Template In= "',FNAME(1:LENGTH(FNAME)),'"'
	WRITE(*,*),'BYTE In="',BNAME(1:LENGTH(BNAME)),'"'
	WRITE(*,*),'SIR Out="',ONAME(1:LENGTH(ONAME)),'"'
C
C	READ INPUT IMAGE FILE
C
        WRITE(*,16) FNAME(1:LENGTH(FNAME))
16      FORMAT(' Reading file "',A,'"')
	MAXDES=LEN(DESCRIP)
        IU=20
	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	ECHO INPUT FILE HEADER TO USER
C
	PRINT *,'Input file header information:',NFILE
	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)
	PRINT *
C
C	NOW READ BYTE FILE
C
	NS=NSX*NSY
c	ns=ns-8  ! eases photoshops job
C
	print *,ns,nsx,nsy
	OPEN(UNIT=1,FILE=BNAME,STATUS='OLD',FORM='UNFORMATTED',
	1    access='direct',recl=ns)
C
	READ (1,REC=1) (B(I),I=1,NS)
	CLOSE(1)
C
C	DETERMINE MAX/MIN OF DATA ARRAY
C
	IBMIN=1024
	IBMAX=-1024
	DO I=1,NS
	   IVAL=B(I)
	   IF (IVAL.LT.0) IVAL=IVAL+256
	   IBMIN=MIN(IBMIN,IVAL)
	   IBMAX=MAX(IBMAX,IVAL)
	END DO
	PRINT *,'BYTE array Min, Max:',IBMIN,IBMAX
	IF (IBMAX.EQ.IBMIN) IBMAX=IBMIN+1
C
	SMIN=VMIN
	SMAX=VMAX
	IBNONE=-1
C
c    	PRINT 34,'Max?'
c34	FORMAT(1X,'New ',A,1X,$)
c	SMAX = READVAL1(SMAX,IERR)
c	PRINT 34,'Min?'
c	SMIN = READVAL1(SMIN,IERR)
c	PRINT 34,'Byte value for nodata? (0..255, -1=none)'
c	IBNONE = IREADVAL1(IBNONE,IERR)
C
C	SCALE ARRAY AND PUT INTO BYTE ARRAY
C
c	SS=(SMAX-SMIN)/(IBMAX-IBMIN)
c	print *,ibmin,ibmax,smin,smax,ss
C
C       CONVERT BYTES TO SIR VALUES -- customized for land mask files
C
	IBMIN1=IBMIN
	SX=-1.E25
	SN=1.E25
	IBMAX=-1000
	IBMIN=1000
C
	DO NN1=0,NSY-1
	   DO NN2=1,NSX
	      INDEXV = NN1*NSX+NN2
	      I = (NSY-NN1)*NSX + NN2
	      IVAL=B(I)
	      IF (IVAL.LT.0) IVAL=IVAL+256
	      if (ival.eq.0) ival=256
	      val=0.0
	      if (ival.ge.255) val=1.0
	      STVAL(INDEXV)=val
c	      IF (IVAL.EQ.IBNONE) STVAL(INDEXV)=ANODATA
	      SN=MIN(SN,STVAL(INDEXV))
	      SX=MAX(SX,STVAL(INDEXV))
	      IBMIN=MIN(IBMIN,IVAL)
	      IBMAX=MAX(IBMAX,IVAL)
	   END DO
	END DO
	PRINT *,'Output array min, max:',SN,SX,IBMIN,IBMAX
	TITLE='Mod '//title(1:length(title))
	print *
	print *,'Please modify file header strings'
C
C       CHANGE FILE HEADER STRINGS
C
        PRINT 310,'Title:',TITLE(1:LENGTH(TITLE))
 310    FORMAT(A,' "',A,'"'/'New: ',$)
        READ (*,'(A)') LINE
        IF (LINE.NE.' ') TITLE=LINE(1:LENGTH(LINE))
C
        PRINT 310,'Sensor:',SENSOR(1:LENGTH(SENSOR))
        READ (*,'(A)') LINE
        IF (LINE.NE.' ') SENSOR=LINE(1:LENGTH(LINE))
C
        PRINT 310,'Type:',TYPE(1:LENGTH(TYPE))
        READ (*,'(A)') LINE
        IF (LINE.NE.' ') TYPE=LINE(1:LENGTH(LINE))
C
        PRINT 310,'Tag:',TAG(1:LENGTH(TAG))
        READ (*,'(A)') LINE
        IF (LINE.NE.' ') TAG=LINE(1:LENGTH(LINE))
C
        PRINT 310,'CrProc:',CRPROC(1:LENGTH(CRPROC))
        READ (*,'(A)') LINE
        IF (LINE.NE.' ') CRPROC=LINE(1:LENGTH(LINE))
C
	itype=0  ! land mask type
	vmin=0.0
	vmax=1.0
        IDATATYPE=2		! MAKE SURE OUTPUT IMAGE IS IN STANDARD I*2 FORM
C
C     WRITE OUT SIR FILE
C
	WRITE(*,*)
	WRITE(*,*),'SIR Output file="',ONAME(1:LENGTH(ONAME)),'"'
C
C	WRITE SIR FORMAT FILE
C
	CALL WRITESIR3(ONAME,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
	   PRINT *,'*** ERROR TRYING TO WRITE OUT SIR FILE'
	   STOP
	ENDIF
C
	STOP
	END


