def normalize(val, minval, maxval):
    """Scale a value between 0 and 1."""
    if val >= maxval:
        return 1
    elif val <= minval:
        return 0
    normed = float(val - minval) / float(maxval - minval)
    return normed