      character*120 input_line,substring
      character*1 dimchar
      character*4 iform,jform,kform
      character*80 file_out,file_form
      logical finished, OK
      logical check(5)
      logical check_and_parse
      integer ii(20),ighost(5),icenter(5),ileft(5),iright(5) 
      data check/5*.false./
c
c
      finished = .false.
c
      nvar = 7
      do while ( .not. finished )
         read (5,'(A)',end=1001) input_line
         finished = check_and_parse(input_line,
     $   'param.h:',ii,nvar)
      enddo
      ni = ii(1)
      nj = ii(2)
      nk = ii(3)
      norder = ii(4)
      num_procs = ii(5)
      num_mhd = ii(6)
      num_ion = ii(7)
c
      ini = alog10( float(ni) ) + 1
      write(iform,899) ini,ini
 899  format ('i',i1,'.',i1)
      inj = alog10(float(nj)) + 1
      write(jform,899) inj,inj
      ink = alog10(float(nk)) + 1
      write(kform,899) ink,ink
      
*      write(file_out,900) ni,nj,nk,num_procs
* 900  format ('param_',i3.3,'x',i3.3,'x',i3.3,'_',i3.3,'.inc')
      open (unit=12,file='ion-param-tmp.inc',form='formatted')
      nii0 = max(norder/2-1,2)
      write (12,912) nii0,nj,nk
 912  format('C ion-param.inc'/7x,'PARAMETER (NI=',i3,',  NJ=',
     $     i3,',   NK=',i3,'  )'/'#include "ion-rest-of-param.inc"')
      close (unit=12)
      open (unit=11,file='param-tmp.inc',form='formatted')
c
c
      finished = .false.
c 
      nvar = 1
      do while ( .not. finished )
         read (5,'(A)',end=1001) input_line
      if ( .not. check(1)) then
          check(1) = check_and_parse(input_line,
     $   'Number of Dimensions (nDims) =',ii,1)
          nvar = ii(1)
      endif
c
c
      
        if ( check(1)) then
           ncdim = nvar -1
           write(dimchar,'(I1)') ncdim
c
c   have dim info
c
         if (.not. check(2)) check(2) = check_and_parse(input_line,
     $'number of internal ghost cells in each dim (ghostCells[0-'//
     $          dimchar//']):',
     $        ighost,nvar)

         if (.not. check(3)) check(3) = check_and_parse(input_line,
     $'local size of each dim for central pieces (dimVecL[0-'//
     $        dimchar//']):',
     $        icenter,nvar)
         if (.not. check(4)) check(4) = check_and_parse(input_line,
     $'local size of each dim for left most piece (dimVecL_L[0-'//
     $        dimchar//']):',
     $        ileft,nvar)
         if (.not. check(5)) check(5) = check_and_parse(input_line,
     $'local size of each dim for right most piece (dimVecL_R[0-'//
     $        dimchar//']):',
     $        iright,nvar)

         endif
         finished = check(1) .and. check(2) .and. check(3) .and.
     $        check(4) .and. check(5)
         
       enddo
 1001  continue
c

       write (6,*) ' number of dims',nvar
       write (6,*) 'ghost',ighost
       write (6,*) 'center',icenter
       write (6,*) 'left',ileft
       write (6,*) 'right',iright
c
       ni_local = max(icenter(1),ileft(1),iright(1))
       nj_local = max(icenter(2),ileft(2),iright(2))
       nk_local = max(icenter(3),ileft(3),iright(3))
       no = norder
       li = max(ni_local,nj_local,nk_local)
       lj = li
       lk = li
       OK = .true.
       if ( ighost(1) .ge. min(icenter(1),ileft(1),iright(1)))
     $      OK = .false.
       if ( ighost(2) .ge. min(icenter(2),ileft(2),iright(2)))
     $      OK = .false.
       if ( ighost(3) .ge. min(icenter(3),ileft(3),iright(3)))
     $      OK = .false.
c
c
       if ( .not. OK ) then
          write (6,*) 'not enough space on individual processors for',
     $         ' guard cells'
          stop
       endif
       llow = -no/2+1
       lihigh = li + no/2
       ljhigh = lj + no/2
c
c
c
      write (11,901) ni_local,nj_local,nk_local,no
 901  format('c param.inc'/
     $     '       PARAMETER ( NI=',I4,',  NJ=',I4,', NK=',I4,','/
     $     '     $       no=',I4,',')
      write (11,902) li,lj,lk
c
 902  format('     $       li=',i4,
     $     ',  lj=',i4,',  lk=',i4,',')
      write (11,911) llow,lihigh,ljhigh
c
 911  format('     $       llow=',i4,
     $     ',  lihigh=',i4,',  ljhigh=',i4,',')
c
      write (11,903) num_procs,num_mhd,num_ion
 903  format ('     $     num_procs=',i4,',  num_mhd=', i4,
     $     ',  num_ion=', i4, ' )' )
      write (11,905) 
 905  format(
     $     '#include "rest_of_param.inc"')
c
      close(unit=11)

       end
c
c
      logical function check_and_parse(input_line,instring,ii,nvar)
      character*(*) input_line
      character*(*) instring
      character*120 substring
      dimension ii(1)
      logical found
c
#ifdef DEBUG_MODE_ON
      write(*,*) "DEBUG:  in write-param.F::check_and_parse(...)"
#endif
      found = .false.
         ll = index(input_line,instring)
         if ( ll .ne. 0 ) then
            length = len(input_line)
            lenstart = ll + len(instring) 
            lensub = length - lenstart +1 
            substring(1:lensub) = input_line(lenstart:length)
            read (substring,*) (ii(i),i=1,nvar)
*            write (6,*) 'll,len',ll,length
          found = .true.
          endif
c
          check_and_parse = found
*          write (6,*) found, check_and_parse
          return
          end

            
