FUNCTION locate(xx,x) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: xx REAL(SP), INTENT(IN) :: x INTEGER(I4B) :: locate INTEGER(I4B) :: n,jl,jm,ju LOGICAL :: ascnd n=size(xx) ascnd = (xx(n) >= xx(1)) jl=0 ju=n+1 do if (ju-jl <= 1) exit jm=(ju+jl)/2 if (ascnd .eqv. (x >= xx(jm))) then jl=jm else ju=jm end if end do if (x == xx(1)) then locate=1 else if (x == xx(n)) then locate=n-1 else locate=jl end if END FUNCTION locate