예제 #1
0
def create_image_from_array(
    data: numpy.array,
    wcs: WCS = None,
    polarisation_frame=PolarisationFrame('stokesI')) -> Image:
    """ Create an image from an array and optional wcs

    :param data: Numpy.array
    :param wcs: World coordinate system
    :param polarisation_frame: Polarisation Frame
    :return: Image
    
    """
    fim = Image()
    fim.polarisation_frame = polarisation_frame

    fim.data = data
    if wcs is None:
        fim.wcs = None
    else:
        fim.wcs = wcs.deepcopy()

    if image_sizeof(fim) >= 1.0:
        log.debug(
            "create_image_from_array: created %s image of shape %s, size %.3f (GB)"
            % (fim.data.dtype, str(fim.shape), image_sizeof(fim)))

    assert isinstance(fim, Image), "Type is %s" % type(fim)
    return fim
예제 #2
0
파일: base.py 프로젝트: Luffky/arl_backup
def create_image(ny,
                 nx,
                 frequency,
                 phasecentre,
                 cellsize=0.001,
                 polarisation_frame=PolarisationFrame('linear')):
    '''
        创建和上个函数等效的非并行的image
    :param ny:
    :param nx:
    :param frequency:
    :param phasecentre:
    :param cellsize:
    :param polarisation_frame:
    :return:
    '''
    wcs4 = WCS(naxis=4)
    wcs4.wcs.crpix = [ny // 2, nx // 2 + 1.0, 1.0, 1.0]
    wcs4.wcs.cdelt = [
        -180.0 * cellsize / np.pi, +180.0 * cellsize / np.pi, 1.0,
        frequency[1] - frequency[0]
    ]
    wcs4.wcs.crval = [
        phasecentre.ra.deg, phasecentre.dec.deg, 1.0, frequency[0]
    ]
    wcs4.wcs.ctype = ["RA---SIN", "DEC--SIN", 'STOKES', 'FREQ']
    wcs4.wcs.radesys = 'ICRS'
    wcs4.wcs.equinox = 2000.00
    nchan = frequency.shape[0]
    npol = polarisation_frame.npol
    image = Image()
    image.wcs = wcs4
    image.data = np.zeros([nchan, npol, ny, nx])
    image.polarisation_frame = polarisation_frame
    return image
예제 #3
0
def import_image_from_fits(fitsfile: str, mute_warnings=True) -> Image:
    """ Read an Image from fits
    
    :param fitsfile:
    :return: Image
    """
    fim = Image()
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        hdulist = fits.open(arl_path(fitsfile))
        fim.data = hdulist[0].data
        fim.wcs = WCS(arl_path(fitsfile))
        hdulist.close()

    if len(fim.data) == 2:
        fim.polarisation_frame = PolarisationFrame('stokesI')
    else:
        try:
            fim.polarisation_frame = polarisation_frame_from_wcs(
                fim.wcs, fim.data.shape)
        except:
            fim.polarisation_frame = PolarisationFrame('stokesI')

    log.debug(
        "import_image_from_fits: created %s image of shape %s, size %.3f (GB)"
        % (fim.data.dtype, str(fim.shape), image_sizeof(fim)))
    log.debug("import_image_from_fits: Max, min in %s = %.6f, %.6f" %
              (fitsfile, fim.data.max(), fim.data.min()))

    assert isinstance(fim, Image)
    return fim
예제 #4
0
def create_empty_image_like(im: Image) -> Image:
    """ Create an empty image like another in shape and wcs

    :param im:
    :return: Image
    
    """
    assert type(im) == Image, "Type is %s" % type(im)
    fim = Image()
    fim.polarisation_frame = im.polarisation_frame
    fim.data = numpy.zeros_like(im.data)
    if im.wcs is None:
        fim.wcs = None
    else:
        fim.wcs = copy.deepcopy(im.wcs)
    if image_sizeof(im) >= 1.0:
        log.debug("create_empty_image_like: created %s image of shape %s, size %.3f (GB)" %
                  (fim.data.dtype, str(fim.shape), image_sizeof(fim)))
    assert type(fim) == Image, "Type is %s" % type(fim)
    return fim
예제 #5
0
def copy_image(im: Image) -> Image:
    """ Create an image from an array
    
    Performs deepcopy of data, breaking reference semantics

    :param im:
    :return: Image
    
    """
    assert isinstance(im, Image), "Type is %s" % type(im)
    fim = Image()
    fim.polarisation_frame = im.polarisation_frame
    fim.data = copy.deepcopy(im.data)
    if im.wcs is None:
        fim.wcs = None
    else:
        fim.wcs = copy.deepcopy(im.wcs)
    if image_sizeof(fim) >= 1.0:
        log.debug("copy_image: copied %s image of shape %s, size %.3f (GB)" %
                  (fim.data.dtype, str(fim.shape), image_sizeof(fim)))
    assert type(fim) == Image
    return fim
예제 #6
0
def replicate_image(im: Image, polarisation_frame=PolarisationFrame('stokesI'), frequency=numpy.array([1e8])) \
        -> Image:
    """ Make a new canonical shape Image, extended along third and fourth axes by replication.

    The order of the data is [chan, pol, dec, ra]


    :param frequency:
    :param im:
    :param polarisation_frame: Polarisation_frame
    :param nchan: Number of spectral channels
    :return: Image
    """

    if len(im.data.shape) == 2:
        fim = Image()

        newwcs = WCS(naxis=4)

        newwcs.wcs.crpix = [
            im.wcs.wcs.crpix[0] + 1.0, im.wcs.wcs.crpix[1] + 1.0, 1.0, 1.0
        ]
        newwcs.wcs.cdelt = [im.wcs.wcs.cdelt[0], im.wcs.wcs.cdelt[1], 1.0, 1.0]
        newwcs.wcs.crval = [
            im.wcs.wcs.crval[0], im.wcs.wcs.crval[1], 1.0, frequency[0]
        ]
        newwcs.wcs.ctype = [
            im.wcs.wcs.ctype[0], im.wcs.wcs.ctype[1], 'STOKES', 'FREQ'
        ]

        nchan = len(frequency)
        npol = polarisation_frame.npol
        fim.polarisation_frame = polarisation_frame

        fim.wcs = newwcs
        fshape = [nchan, npol, im.data.shape[1], im.data.shape[0]]
        fim.data = numpy.zeros(fshape)
        log.info("replicate_image: replicating shape %s to %s" %
                 (im.data.shape, fim.data.shape))
        for i3 in range(nchan):
            fim.data[i3, 0, :, :] = im.data[:, :]
        return fim
    else:
        return im
예제 #7
0
파일: base.py 프로젝트: Luffky/arl_backup
def image_para_to_image(ims: List[image_for_para],
                        image_share: image_share) -> Image:
    '''
        将并行image_for_para还原为原本的Image类,验证算法正确性用
    :param ims:  只有一个facet的并行Image list 类型:list[image_para]
    :param image_share: 并行Image的共享消息
    :return: 还原后的image
    '''
    image = Image()
    datatype = None
    if type(ims[0]) == tuple:
        datatype = ims[0][1].data.dtype
    else:
        datatype = ims[0].data.dtype

    data = np.zeros(
        [image_share.nchan, image_share.npol, image_share.ny, image_share.nx],
        dtype=datatype)
    dy = 0
    dx = 0
    if type(ims[0]) == tuple:
        dy = ims[0][1].ny
        dx = ims[0][1].nx
    else:
        dy = ims[0].ny
        dx = ims[0].nx

    assert image_share.ny // dy == image_share.nx // dx
    facet = image_share.ny // dy

    for im in ims:
        if type(im) == tuple:
            im = im[1]
        nchan = im.channel
        npol = im.polarisation
        y = im.facet // facet
        x = im.facet % facet
        data[nchan, npol, y * dy:(y + 1) * dy, x * dx:(x + 1) * dx] = im.data
    image.data = data
    image.wcs = image_share.wcs
    image.polarisation_frame = image_share.polarisation_frame
    return image