def create_image_from_array(data: numpy.array, wcs: WCS, polarisation_frame: PolarisationFrame) -> Image: """ Create an image from an array and optional wcs The output image preserves a reference to the input array. :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
def copy_image(im: Image): """ Create an image from an array Performs deepcopy of data_models, breaking reference semantics :param im: :return: Image """ if im is None: return im assert isinstance(im, Image), 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
def cImage(image_in, new=False): "Convert an Image* into ARL Image structure" new_image = Image() size = image_in.size data_shape = tuple(image_in.data_shape) new_image.data = numpy.frombuffer(ff.buffer(image_in.data, size * 8), dtype='f8', count=size) # frombuffer only does 1D arrays.. new_image.data = new_image.data.reshape(data_shape) # New images don't have pickles yet if new: new_image.wcs = numpy.frombuffer(ff.buffer(image_in.wcs, 2996), dtype='b', count=2996) new_image.polarisation_frame = numpy.frombuffer(ff.buffer( image_in.polarisation_frame, 117), dtype='b', count=117) else: new_image.wcs = pickle.loads(ff.buffer(image_in.wcs, 2996)) new_image.polarisation_frame = pickle.loads( ff.buffer(image_in.polarisation_frame, 117)) return new_image
def import_image_from_fits(fitsfile: str) -> Image: """ Read an Image from fits :param fitsfile: :return: Image """ fim = Image() warnings.simplefilter('ignore', FITSFixedWarning) hdulist = fits.open(fitsfile) fim.data = hdulist[0].data fim.wcs = WCS(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 ValueError: 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
def create_empty_image_like(im: Image) -> Image: """ Create an empty image like another in shape and wcs :param im: :return: Image """ assert isinstance(im, Image), 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 isinstance(fim, Image), "Type is %s" % type(fim) return fim
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 :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