        dimension x(2000),icumfreq(256),id(14),pgm(2)
        dimension bound(10),bound2(10),jcount(11)
        character ifile*80
        data ddval/1.e30/
        print *,'enter name of input grid file:'
        read(5,100) ifile
  100 format(a)
        open(10,file=ifile,form='unformatted',status='old')
        print *,'enter name of output grid file:'
        read(5,100) ifile
        open(11,file=ifile,form='unformatted',status='unknown')
        read(10) id,pgm,nc,nr,nz,xo,dx,yo,dy
        write(11) id,pgm,nc,nr,nz,xo,dx,yo,dy
        xmin=1.e38
        xmax=-1.e38
        icount=0
        do 10 j=1,nr
        read(10) xo,(x(i),i=1,nc)
        do 10 i=1,nc
        if(x(i).ge.ddval) go to 10
        if(x(i).gt.xmax) xmax=x(i)
        if(x(i).lt.xmin) xmin=x(i)
        icount=icount+1
   10 continue
        rewind(10)
        read(10) id
        xdel=(xmax-xmin)/255.
        do 20 i=1,256
        icumfreq(i)=0
   20 continue
        do 30 j=1,nr
        read(10) xo,(x(i),i=1,nc)
        do 30 i=1,nc
        if(x(i).ge.ddval) go to 30
        inc=(x(i)-xmin)/xdel+1.0
        icumfreq(inc)=icumfreq(inc)+1
   30 continue
        isum=0
        j=1
        jcount(1)=0
        bound(1)=xmin
        bound(10)=xmax
        icount=int(float(icount)/9.+.5)
        do 40 i=1,256
        isum=isum+icumfreq(i)
        if(isum.lt.icount) go to 40
        j=j+1
        jcount(j)=isum
        bound(j)=xmin+(i-1)*xdel
        isum=isum-icount
   40 continue
        jcount(j+1)=isum
        del2=(xmax-xmin)/9.
        print *,'            mapping pairs         # in interval'
        print *,'        old             new'
        do 50 i=1,10
        bound2(i)=xmin+float(i-1)*del2
        print *,bound(i),bound2(i),jcount(i)
   50 continue
        rewind(10)
        read(10) id
        do 80 j=1,nr
        read(10) xo,(x(i),i=1,nc)
        do 70 i=1,nc
        if(x(i).ge.ddval) go to 70
        do 60 k=2,10
        if(x(i).lt.bound(k)) then
          x(i)=bound2(k-1)+del2*(x(i)-bound(k-1))/(bound(k)-bound(k-1))
          go to 70
        endif
   60 continue
   70 continue
        write(11) xo,(x(i),i=1,nc)
   80 continue
        stop
        end

