def clamp(val, lim):
    """ Limit val to between 2 (optional, if nan, because no value is < or > nan) limits """
    if val < lim[0]:
        return lim[0]
    if val > lim[1]:
        return lim[1]
    return val
