def nearest_index(index_array, value):
    """Find the position in an increasing array nearest a given value."""

    # find the index of the last data point that is smaller than the 'value'
    # we are looking for ....
    ind1 = len(index_array.compress(index_array < value))

    # if we are at the very end of the array then this is our best estimate ...
    if ind1 == len(index_array)-1:
        ind = ind1
    # otherwise, which of the two points is closer?
    else:
        val1 = index_array[ind1]
        val2 = index_array[ind1+1]
        if val2-value > value-val1:
            ind = ind1
        else:
            ind = ind1+1

    return ind