def map_val(val, cmin=0.0, cmax=1.0, mn=1.0, mx=-1.0):
    """Linearly map a set of values to a new range

    Parameters
    ----------
    val : numpy.array_like 
        Array containing the values to be mapped
    cmin : float, optional
        Minimum value for the new range
    cmax : float, optional
        Maximum value for the new range
    mn : float, optional
        The value to be considered as the minimum in the range of 'val'.
        If this parameter is not provided, it is computed as min(val).
        Setting a minimum that is different from the actual data minimum
        allows to clamp values that are too small. Note that, if 'mn' is
        provided, then both 'mn' and 'mx' need to be provided
    mx : float, optional
        The value to be considered the maximum in the range of 'val'. If
        this parameter is not provided, it is computed as max(val).
        Setting a maximum that is different from the actual data maximum
        allows to clamp values that are too big. Note that, if 'mn' is
        provided, then both 'mn' and 'mx' need to be provided

    Returns
    -------
    new_val : numpy.array_like 
        Array containing 'val' mapped to the new range [cmin, cmax]

    Notes
    -----
    The purpose of this function is to linearly map a set of values
    'val' in the range [mn, mx] to a new set of values 'new_val' in the
    range [cmin, cmax]. This can be utilized, for example, to map data
    values to color values.
    """
 
    # Check if minimum and maximum were provided:
    if mx < mn:
        mx = val.max()
        mn = val.min()

    # Clamp values
    temp = val.copy()
    temp[temp > mx] = mx
    temp[temp < mn] = mn

    # Map values
    return cmin + ((temp - mn)/(mx - mn))*(cmax - cmin)