Generate mask avoiding undesired values at the borders.

    Set to True image borders with values equal to 'sought_value'

    Parameters
    ----------
    image2d : numpy array
        Initial 2D image.
    sought_value : int, float, bool
        Pixel value that indicates missing data in the spectrum.
    nadditional : int
        Number of additional pixels to be masked at each border.

    Returns
    -------
    mask2d : numpy array
        2D mask.
    borders : list of tuples
        List of tuples (jmin, jmax) with the border limits (in array
        coordinates) found by find_pix_borders.

def define_mask_borders(image2d, sought_value, nadditional=0):
    """Generate mask avoiding undesired values at the borders.

    Set to True image borders with values equal to 'sought_value'

    Parameters
    ----------
    image2d : numpy array
        Initial 2D image.
    sought_value : int, float, bool
        Pixel value that indicates missing data in the spectrum.
    nadditional : int
        Number of additional pixels to be masked at each border.

    Returns
    -------
    mask2d : numpy array
        2D mask.
    borders : list of tuples
        List of tuples (jmin, jmax) with the border limits (in array
        coordinates) found by find_pix_borders.

    """

    # input image size
    naxis2, naxis1 = image2d.shape

    # initialize mask
    mask2d = np.zeros((naxis2, naxis1), dtype=bool)

    # initialize list to store borders
    borders = []

    for i in range(naxis2):
        # only spectra with values different from 'sought_value'
        jborder_min, jborder_max = find_pix_borders(
            image2d[i, :],
            sought_value=sought_value
        )
        borders.append((jborder_min, jborder_max))
        if (jborder_min, jborder_max) != (-1, naxis1):
            if jborder_min != -1:
                j1 = 0
                j2 = jborder_min + nadditional + 1
                mask2d[i, j1:j2] = True
            if jborder_max != naxis1:
                j1 = jborder_max - nadditional
                j2 = naxis1
                mask2d[i, j1:j2] = True

    return mask2d, borders