c DVAL.FOR
c
c  To set dvals in a 2-D function (defined on a grid) in the range [fmin,fmax].
c  Lin Cordell, September 1992
c
      character*50 ifile,ofile
      dimension f(2000),id(14),pgm(2)
      data dval/0.1e+39/
      dval2=1.e20
      write(6,1)
1     format(' Enter input file name:'/' *'$)
      read(5,100)ifile
100	format(a50)
	write(6,201)
201   format(1x,'Enter output file name:'/' *'$)
      read(5,100)ofile
      open(10,file=ifile,form='unformatted',status='old')
      read(10)id,pgm,ncol,nrow,nz,x0,dx,y0,dy
      if(ncol.gt.2000.or.nz.ne.1.or.dx.eq.0.e0) go to 900
      open(11,file=ofile,form='unformatted',status='new')
      write(6,202)
202   format(' Enter new id:'/' *'$)
      read(5,203)(id(i),i=1,14)
203   format(14a4)
      write(6,204)
204   format(' Enter fmin, fmax:'/' *'$)
      read*, fmin,fmax
      if(fmin.gt.fmax)go to 900
      write(11)id,pgm,ncol,nrow,nz,x0,dx,y0,dy
      do 20 j=1,nrow
	call rowio(f,y,1,ncol,10,1)
      do 10 i=1,ncol
      if(f(i).gt.fmax)go to 10
      if(f(i).lt.fmin)go to 10
      f(i)=dval
10    continue
      call rowio(f,y,1,ncol,11,2)
20	continue
      go to 998
900   write(6,901)
901   format(' Can''t handle it.')
998   close(11)
999   close(10)
      stop
      end
      subroutine rowio(a,y,nz,ncol,ld,key)
      dimension a(nz,ncol)
      go to (1,2),key
1     read(ld) y,a
      go to 90
2     write(ld)y,a
90    return
      end
