      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)
C
      real*8 deltax,deltay
      real*8 floatmatrix
      integer nr1,nc1,nr2,nc2
      dimension floatmatrix1(nrowmax,ncolmax)
      dimension floatmatrix2(nrowmax,ncolmax)
      dimension floatmatrix3(nrowmax,ncolmax)
      character*10 ch1,ch2,ch3,ch4,ch5,ch6,ch7
      character*20 content1
      open(unit = 51, file = 'elevation-array-mf.txt')
      open(unit = 52, file = 'boundary-array-mf.txt')
      rewind(51)
      rewind(52)
C
C assume redirection is used for I/O 
C
      write(*,*)'read first array'
      read(51,*)content1
      write(*,*)content1
      read(51,*)nr1
      write(*,*)nr1
      read(51,*)nc1
      write(*,*)nc1
      read(51,*)nr1
      write(*,*)nr1
      read(51,*)nc1
      write(*,*)nc1
      read(51,*)deltay
      write(*,*)deltay
      read(51,*)deltax
      write(*,*)deltax
      read(51,*)content1
      write(*,*)content1
      do 1001 irow=1,nr1
        read(unit=51,fmt=*,end=8001)(floatmatrix1(irow,jcol),jcol=1,nc1)
 1001 continue
 8001 write(*,*)'exit because EOF marker'
      close(51)
      write(*,*)'irow value ',irow
      write(*,*)'jcol value ',jcol
      write(*,*)'nrows :',nr1
      write(*,*)'ncols :',nc1
      write(*,*)'spacing between rows    :',deltay
      write(*,*)'spacing between columns :',deltax
      write(*,*)'read second array'
      read(52,*)content1
      write(*,*)content1
      read(52,*)nr1
      write(*,*)nr1
      read(52,*)nc1
      write(*,*)nc1
      read(52,*)nr1
      write(*,*)nr1
      read(52,*)nc1
      write(*,*)nc1
      read(52,*)deltay
      write(*,*)deltay
      read(52,*)deltax
      write(*,*)deltax
      read(52,*)content1
      write(*,*)content1
      do 1002 irow=1,nr1
        read(unit=52,fmt=*,end=8002)(floatmatrix2(irow,jcol),jcol=1,nc1)
 1002 continue    
 8002 write(*,*)'exit because EOF marker'
      close(52)

      do 1011 irow=1,nr1
        do 1012 jcol=1,nc1
           if(floatmatrix2(irow,jcol) .EQ. -1)then
              floatmatrix3(irow,jcol)= 1882
           else if(floatmatrix2(irow,jcol) .EQ. 1)then
              floatmatrix3(irow,jcol)= floatmatrix1(irow,jcol)
           else
              floatmatrix3(irow,jcol)= -999
           end if
 1012   continue
 1011 continue  

      write(*,*) irow,'irow value '
      write(*,*) jcol,'jcol value '
      write(*,*) nr1,'nrows :'
      write(*,*) nc1,'ncols :'
      write(*,*) deltay,'spacing between rows    :'
      write(*,*) deltax,'spacing between columns :'

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 1003 irow=1,nr1
        write(*,9001)(floatmatrix3(irow,jcol),jcol=1,nc1)
 1003 continue

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