      program formatagrid
C program to structure an ascii grid for MODFLOW formatted input
C reads a matrix, then assigns correct formatting and write result 
      parameter(nrowmax=200,ncolmax=200)
      real*8 deltax,deltay,deltax2,deltay2
      real*8 topelev
      real*8 storagecoef
      real*8 bottomelev
      real*8 precipitation
      real*8 evaporation
      integer matrixmask,nrows1,ncols1,nrows2,ncols2
      dimension topelev(nrowmax,ncolmax)
      dimension storagecoef(nrowmax,ncolmax)
      dimension bottomelev(nrowmax,ncolmax)
      dimension precipitation(nrowmax,ncolmax)
      dimension evaporation(nrowmax,ncolmax)
      dimension matrixmask(nrowmax,ncolmax)
      character*10 ch1,ch2,ch3,ch4,ch5,ch6,ch7
C
C assume redirection is used for I/O 
C

C 
      open(unit=11,file='masked-boundary-codes-as-grid.txt')
      open(unit=12,file='masked-elevation-as-grid.txt')
      rewind(11)
      rewind(12)

      read(11,*)nrows,ncols
      read(11,*)deltay,deltax

      read(12,*)nrows2,ncols2
      read(12,*)deltay2,deltax2

      if((nrows .NE. nrows2) .OR. (ncols .NE. ncols2))then
         write(*,*)'grid row/col count not compatable'
         close(11)
         close(12)
         stop
      end if
C
      do 1011 irow=1,nrows
        read(unit=11,fmt=*,end=8011)(matrixmask(irow,jcol),jcol=1,ncols)
 1011 continue
 8011 write(*,*)'exit because EOF marker'
      close(11)


      do 1001 irow=1,nrows
        read(unit=12,fmt=*,end=8001)(topelev(irow,jcol),jcol=1,ncols)
 1001 continue
 8001 write(*,*)'exit because EOF marker'
      close(12)
C
C use the mask
C
      do 9011 irow=1,nrows
         do 9012 jcol=1,ncols
         topelev(irow,jcol) = topelev(irow,jcol) - 250.0
         if((matrixmask(irow,jcol) .eq. 1) .and. 
     #      (topelev(irow,jcol) .le. 0.0 ) ) then
            topelev(irow,jcol) = 2800.0 
         end if
         if((matrixmask(irow,jcol) .eq. -1) .and. 
     #      (topelev(irow,jcol) .le. 0.0 ) ) then
            topelev(irow,jcol) = 1822.0 
         end if
         if((matrixmask(irow,jcol) .eq. 0)) then
            topelev(irow,jcol) = 0.0 
         end if
 9012    continue
 9011 continue

      write(*,*)'irow value ',irow
      write(*,*)'jcol value ',jcol
      write(*,*)'nrows :',nrows
      write(*,*)'ncols :',ncols
      write(*,*)'spacing between rows    :',deltay
      write(*,*)'spacing between columns :',deltax
C typical formatting directives for 2Darray reader
C234567890123456789012345678901234567890123456789012345678901234567890
      ch1='        1'
      ch2='      1.00'
      ch3=' (10G10.3)'
      ch4='          '
      ch5='         0'
      ch6='    LABEL-'
      ch7='         1'
      write(*,*)ch1 // ch2 // ch3 // ch4 // ch5 // ch6 // ch7
C234567890123456789012345678901234567890123456789012345678901234567890
      do 1002 irow=1,nrows
        write(*,9001)(topelev(irow,jcol),jcol=1,ncols)
 1002 continue
C typical formatting directives for 2Darray reader

      stop
 9001 format(10(g10.4))
 9002 format(10(1x,i2))
      end
