import math


def extract_source_at_pos(MAP, x_pos, y_pos, PSF_npix_x, PSF_npix_y):
    """
    Extract a sub-MAP around the position (x_pos, y_pos)
    The shape of the sub-MAP is similar to the PSF_MAP

    Parameters
    ----------
    MAP : numpy masked array
        The original MAP in which the source as to be extracted
    x_pos : float.
        The x (first dimension) position of the source
    y_pos : float
        The y (second dimension) position of the source
    PSF_npix_x : integer
        The x (fisrt dimension) size of the Point Spread Map
    PSF_npix_y : integer
        The y (second dimension) size of the Point Spread Map

    Returns
    -------
    SCR_MAP : numpy masked array
        The SouRCe MAP. A map of size (npix_x, npix_y) with a source
        created according to the PSF_MAP at the position (x_pos, y_pos)
    """
    #
    cpix_x = int(math.floor(PSF_npix_x / 2))
    cpix_y = int(math.floor(PSF_npix_y / 2))
    # corner pixels
    x_inf = int(round(x_pos)) - cpix_x; x_sup = x_inf + PSF_npix_x
    y_inf = int(round(y_pos)) - cpix_y; y_sup = y_inf + PSF_npix_y
    # extract map
    SRC_MAP = MAP[x_inf:x_sup, y_inf:y_sup]
    #
    return SRC_MAP