c SPLIT3.FOR
c
c  To quantize a grid into three levels: below L1, between L1 and L2, and
c  above L2.  Note that output grid values are either 1,2, or 3.  
c  max are assigned a value of 4.  Use GRDMAX or GHIST to choose L1 and L2.
c
c  Lin Cordell
c  August 1993
c
      character*50 ifile,ofile
      dimension f(2000),id(14),pgm(2)
      data dval/0.170412e+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 L1,L2:'/' *'$)
      read*, f1,f2
      if(f1.gt.f2)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
      fout=1
      if(f(i).gt.f1)fout=2
      if(f(i).gt.f2)fout=3
      if(f(i).gt.dval2)fout=dval
      f(i)=fout
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
