def interp(val, array_value, array_ref):
    """
    Interpolate the array_value from the array_ref with val. The array_ref
    must be in an increasing order!
    """
    if val <= array_ref[0]:
        return array_value[0]
    elif val > array_ref[len(array_ref)-1]:
        return array_value[len(array_ref)-1]
    else:
        i = 1
        while val > array_ref[i]:
            i += 1
        delta = array_ref[i] - array_ref[i-1]
        return ((array_ref[i]-val)*array_value[i-1]+array_value[i]*(val-array_ref[i-1]))/delta