```
c=======================================================================

subroutine locate(xx,n,x,j) 10
c
c purpose:  given an array xx of length n, and given a value X, returns
c           a value J such that X is between xx(j) and xx(j+1). xx must
c           be monotonic, either increasing of decreasing. this function
c           returns j=1 or j=n-1 if x is out of range.
c
c input:
c   xx      monitonic table
c   n       size of xx
c   x       single floating point value perhaps within the range of xx
c
c output:
c           function returns index value j, such that
c
c            for an increasing table
c
c                xx(j) .lt. x .le. xx(j+1),
c                j=1    for x .lt. xx(1)
c                j=n-1  for x .gt. xx(n)
c
c            for a decreasing table
c                xx(j) .le. x .lt. xx(j+1)
c                j=n-1  for x .lt. xx(n)
c                j=1    for x .gt. xx(1)
c
integer j,n
real x,xx(n)
integer jl,jm,ju

c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

if(x.eq.xx(1)) then
j=1
return
endif
if(x.eq.xx(n)) then
j=n-1
return
endif
jl=1
ju=n
10    if(ju-jl.gt.1) then
jm=(ju+jl)/2
if((xx(n).gt.xx(1)).eqv.(x.gt.xx(jm)))then
jl=jm
else
ju=jm
endif
goto 10
endif
j=jl
return
end
```