def interp(time, list_val, list_time):
    """
    Return the interpolation between the time and list_time for list_val.
    If outside of list_time, return the first or last element of list_val

    """
    if time <= list_time[0]:
        return list_val[0]
    elif time >= list_time[len(list_time)-1]:
        return list_val[len(list_val)-1]
    else:
        i = 0
        while list_time[i+1] < time:
            i += 1
    d1 = time - list_time[i]
    d2 = list_time[i+1]-time
    return (d2 * list_val[i]+d1 * list_val[i+1])/(d1+d2)