c
c     file: /export/software/puddle0/dems/mindem.f
c     Last Modified: 11/06/96
c
c     Written by: Glenn E. Moglen
c
c     Program reads in old elevation and isolated area
c     files and prints out new elevation file that is
c     the smallest DEM that completely covers the isolated
c     basin
c
      program main
      parameter (igrid = 2404)
      integer*4 nx, ny
      real*4 dx, dy
      integer*4 ain(igrid, igrid)
      integer*2 ein(igrid, igrid), eout(igrid, igrid)
      character*80 areafile, elevfileold, elevfilenew
      open (15, file = 'mindem.in', form = 'formatted', 
     +          status = 'old')
      read (15, 100) areafile
      read (15, 100) elevfileold
      read (15, 100) elevfilenew
      close (15, status = 'keep')
      call demread (ain, areafile, nx, ny, igrid, dx, dy)
      call demread (ein, elevfileold, nx, ny, igrid, dx, dy)
      print *, 'Area file read...'
      mincol = nx + 1
      maxcol = 0
      minrow = ny + 1
      maxrow = 0
      do i = 1, ny
         do j = 1, nx
            if (ain(i, j) .gt. 0 .and. j .lt. mincol) mincol = j
            if (ain(i, j) .gt. 0 .and. j .gt. maxcol) maxcol = j
            if (ain(i, j) .gt. 0 .and. i .lt. minrow) minrow = i
            if (ain(i, j) .gt. 0 .and. i .gt. maxrow) maxrow = i
         end do
      end do
      nnx = maxcol - mincol + 3
      nny = maxrow - minrow + 3
      do i = minrow - 1, maxrow + 1
         it = i - minrow + 2
         do j = mincol - 1, maxcol + 1
            jt = j - mincol + 2
            eout(it, jt) = ein(i, j)
         end do
      end do
      close (16, status = 'keep')
      call demwrite (eout, elevfilenew, nnx, nny, igrid, dx, dy)
 100  format (a80)
      end
