c   INSERT.FOR
c   this pgm inserts infil into the larger std grid infil2.
c    output file iofil is of identical size to infil2.
c
      character*20 infil,infil2,iofil
      character*1 ans
      write(6,1)
    1 format('$ enter embedded filename:  ')
      read(5,2) infil
    2 format(a)
      write(6,3)
    3 format('$ enter master filename:  ')
      read(5,2) infil2
      write(6,4)
    4 format('$ enter output filename:  ')
      read(5,2) iofil
      write(6,5)
    5 format('$ insert dvals?:  ')
      read(5,2) ans
      if(ans.eq.'Y') ans='y'
      call insrt(infil,infil2,iofil,ans)
      stop
      end
      subroutine insrt(infil,infil2,iofil,ans)
c
c   this pgm inserts infil into the larger std grid infil2.  the
c    output file will be clipped to be of exactly the same size
c    as the input file.
c
      dimension ihdr(14),ipgm(2),ihdr2(14),ipgm2(2)
      dimension data(4000),data2(4000)
      character*20 infil,infil2,iofil,itmp
      character*1 ans
      data dval/1.0e30/
      open(unit=10,file=infil,status='old',form='unformatted')
      open(unit=11,file=infil2,status='old',form='unformatted')
      open(unit=12,file=iofil,status='new',form='unformatted')
      read(10,end=140) ihdr,ipgm,nc,nr,nz,xo,dx,yo,dy
      read(11,end=150) ihdr2,ipgm2,nc2,nr2,nz2,xo2,dx2,yo2,dy2
      y=yo-yo2
      iskpr=y/dy+sign(0.5,y)
      x=xo-xo2
      iskpc=x/dx+sign(0.5,x)
      iskpcs=iskpc+1
      iskpce=iskpc+nc
c
c   check columns for overlap.
c
      if(iskpc) 5,9,8
c
c   west edge of infil left of master file.
c   check for infil left of master file.
c
    5 if(iskpce.lt.1) go to 100
c
c   column overlap exists between infil & master file.
c
      ibeg=2-iskpcs
      iskpcs=1
      go to 10
c
c   check for infil right of master file.
c
    8 if(iskpcs.gt.nc2) go to 110
c
c   column overlap exists between infil & master file.
c
    9 ibeg=1
      if(iskpce.gt.nc2) iskpce=nc2
c
c   check rows for overlap.
c
   10 if(iskpr) 11,30,20
c
c   bottom of infil below master file.
c   check for infil below master file.
c
   11 iendr=iskpr+nr
      if(iendr.lt.1) go to 120
c
c   row overlap exists between infil & master file.
c   skip iskpr rows on inserted file.
c
      iskpr=-iskpr
      do 12 i=1,iskpr
      call rdbin(10,dum,1,data,nc,*140)
   12 continue
      irow=0
      write(12) ihdr2,ipgm2,nc2,nr2,nz2,xo2,dx2,yo2,dy2
      go to 31
c
c   infil & master file begin at same row coordinate.
c
   30 write(12) ihdr2,ipgm2,nc2,nr2,nz2,xo2,dx2,yo2,dy2
      irow=0
      iendr=nr
      go to 31
c
c   check for infil above master file.
c
   20 if(iskpr.ge.nr2) go to 130
c
c   row overlap exists. skip iskpr rows on master file.
c
      write(12) ihdr2,ipgm2,nc2,nr2,nz2,xo2,dx2,yo2,dy2
      do 25 i=1,iskpr
      call rdbin(11,dum,1,data2,nc2,*150)
      call iorow(12,dum,1,data2,nc2)
   25 continue
      irow=iskpr
      iendr=nr
   31 if(iendr.gt.nr2) iendr=nr2
      do 40 i=1,iendr
      call rdbin(10,dum,1,data,nc,*140)
      call rdbin(11,dum2,1,data2,nc2,*150)
      ipos=ibeg
c
c   replace master file data by non-null infil data.
c
      do 33 j=iskpcs,iskpce
      if(data(ipos).gt.dval.and.ans.ne.'y') go to 33
      data2(j)=data(ipos)
   33 ipos=ipos+1
      irow=irow+1
c     write(6,65) i,ibeg,nc,infil,irow,iskpcs,iskpce,infil2
c  65 format(1x,'-row ',i4,' cols ',i4,'-',i4,' from file ',a20,/,2x,
c    1'copied to',/,2x,'row ',i4,' cols ',i4,'-',i4,' from file ',a20)
      call iorow(12,dum2,1,data2,nc2)
      if(irow.eq.nr2) go to 45
   40 continue
      iend=nr2-irow
      do 43 i=1,iend
      call rdbin(11,dum2,1,data2,nc2,*150)
      call iorow(12,dum2,1,data2,nc2)
   43 irow=irow+1
   45 write(6,46) irow,iofil
   46 format(2x,i4,' rows written to output file ',a20)
      go to 502
  100 write(6,101)
  101 format(2x,'****inserted file west of master file.  no overlap.')
      go to 500
  110 write(6,111)
  111 format(2x,'****inserted file east of master file.  no overlap.')
      go to 500
  120 write(6,121)
  121 format(2x,'****inserted file below master file.  no overlap.')
      go to 500
  130 write(6,131)
  131 format(2x,'****inserted file above master file.  no overlap.')
      go to 500
  140 itmp=infil
      go to 490
  150 itmp=infil2
  490 write(6,491) itmp
  491 format(2x,'****unexpected end of file read of file ',a20)
  500 write(6,501)
  501 format(6x,'execution terminating....')
  502 close(10)
      close(11)
      close(12)
      return
      end
      subroutine iorow(io,dat1,n1,dat2,n2)
      dimension dat1(n1),dat2(n2)
      write(io) dat1,dat2
      return
      end
      subroutine rdbin(iu,dum,i1,data,i2,*)
      dimension dum(i1),data(i2)
      read(iu,end=100) dum,data
      return
  100 return 1
      end
