import math


def sanitize_ndim(d):
    """Sanity check and sanitize the number of space dimensions.

    In: raw value (number). Out: sanitized value.
    Raises ValueError() if the given value is invalid.

    """
    d = math.floor(d)
    if d < 1  or  d > 2:
        raise ValueError("Number of dimensions out of range. Valid values are 1 and 2.")
    return d