def locate(xx, iis, iie, x):
    """Return value `j` such that `x` is between `xx[j]` and `xx[j+1]`, where
    `xx` is an array of length `n`, and `x` is a given value. `xx` must be
    monotonic, either increasing or decreasing (GSLIB version).
    :param xx: array
    :param iis: start point
    :param iie: end point
    :param x: given value
    :return: TODO
    """
    n = len(xx)
    if iis <= 0:
        iis = 0
    if iie >= n:
        iie = n - 1
    jl = iis - 1
    ju = iie
    if xx[n - 1] <= x:
        j = iie
        return j
    while ju - jl > 1:
        jm = int((ju + jl) / 2)
        if (xx[iie] > xx[iis]) == (x > xx[jm]):
            jl = jm
        else:
            ju = jm
    j = jl
    return j
