*******************************************************************************
C                              Ratint.f
C Given arrays xa and ya of length n (n.le.nmax) and a given value of x, this
C routine returns a value of y and error estimate dy.  See Numerical Recipes,
C pgs 104-107.
C Caution! This program does not pause for failures in ratint.
*******************************************************************************
      subroutine ratint(xa,ya,n,x,y,dy)
      parameter(nmax=10,tiny=1.e-25)
      real xa(n),ya(n),c(nmax),d(nmax)
      ns=1
      hh=abs(x-xa(1))
      do i=1,n
         h=abs(x-xa(i))
         if(h.eq.0.)then
            y=ya(i)
            dy=0.0
            return
         else if (h.lt.hh) then
            ns=i
            hh=h
         end if
         c(i)=ya(i)
         d(i)=ya(i)+tiny
      end do
      y=ya(ns)
      ns=ns-1
      do m=1,n-1
         do i=1,n-m
            w=c(i+1)-d(i)
            h=xa(i+m)-x
            t=(xa(i)-x)*d(i)/h
            dd=t-c(i+1)
            if(dd.eq.0.) then 
               ind=n/2
               y=ya(ind)
               dy=1.
               return
            end if
            dd=w/dd
            d(i)=c(i+1)*dd
            c(i)=t*dd
         end do
         if (2*ns.lt.n-m) then
            dy=c(ns+1)
         else
            dy=d(ns)
            ns=ns-1
         end if
         y=y+dy
      end do
      return
      end


