def calculate_peak_fluxes(coords, imagenames=[], stampsize=32, searchradius=None): for i, coord in enumerate(coords): coord.index = i if coords.coord_type == 'physical': pixcoords = stacker.getPixelCoords(coords, imagenames) else: pixcoords = coords _allocate_buffers(pixcoords.imagenames, stampsize, len(pixcoords)) _load_stack(pixcoords) pixcoords = _calculate_peak_fluxes(pixcoords, searchradius) if coords.coord_type == 'physical': for coord in coords: norm_peak_flux = 0. coord.peak_flux = 0. for pixcoord in pixcoords: if coord.index == pixcoord.index: norm_peak_flux += pixcoord.weight coord.peak_flux += pixcoord.peak_flux*pixcoord.weight if norm_peak_flux > 0.: coord.peak_flux /= norm_peak_flux else: coord.peak_flux = 0. else: coords = pixcoords return coords
def noise(coords, nrandom = 50, imagenames=[], stampsize=32, method = 'mean', weighting = 'simga2', maskradius=None, psfmode = 'point'): import stacker import numpy as np from taskinit import ia, qa ia.open(imagenames[0]) beam = qa.convert(ia.restoringbeam()['major'], 'rad')['value'] ia.done() # if coords.coord_type == 'physical': # coords = stacker.getPixelCoords(coords, imagenames) _allocate_buffers(imagenames, stampsize, len(coords)*len(imagenames)) dist = [] for i in range(nrandom): random_coords = stacker.randomizeCoords(coords, beam=beam) random_coords = stacker.getPixelCoords(random_coords, imagenames) _load_stack(random_coords, psfmode) if method == 'mean' and weighting == 'sigma2': random_coords = _calculate_sigma2_weights(random_coords, maskradius) elif method == 'mean' and weighting == 'sigma': random_coords = _calculate_sigma_weights(random_coords, maskradius) stacked_im = _stack_stack(method, random_coords) dist.append(stacked_im[int(stampsize/2+0.5), int(stampsize/2+0.5),0,0]) return np.std(dist)
def calculate_flux_weights(coords, imagenames=[]): for i, coord in enumerate(coords): coord.index = i if coords.coord_type == 'physical': pixcoords = stacker.getPixelCoords(coords, imagenames) else: pixcoords = stacker.CoordList(coords.imagenames, 'pixel', unit='pix') # This works for now. But it is not good if more atrributes exists from somewhere else. for coord in coords: c = stacker.Coord(coord.x, coord.y, coord.weight) c.image = coord.image c.index = coord.index pixcoords.append(c) pixcoords = _calculate_flux_weights(pixcoords) for coord in coords: coord.weight = 0. for pixcoord in pixcoords: if coord.index == pixcoord.index and pixcoord.weight > coord.weight: coord.weight = pixcoord.weight return coords
def calculate_pb_weights(coords, primarybeam, imagenames=[]): import stacker from scipy.constants import c from taskinit import ia, qa import numpy as np for i, coord in enumerate(coords): coord.index = i if coords.coord_type == 'physical': pixcoords = stacker.getPixelCoords(coords, imagenames) else: pixcoords = coords # _allocate_buffers(pixcoords.imagenames, stampsize, len(pixcoords)) # _load_stack(pixcoords) for coord in pixcoords: ia.open(imagenames[coord.image]) cs = ia.coordsys() freqaxis = cs.findaxisbyname('freq') restfreq = cs.referencevalue()['numeric'][freqaxis] dx = ((coord.x-cs.referencepixel()['numeric'][0]) *cs.increment()['numeric'][0]) dy = ((coord.y-cs.referencepixel()['numeric'][1]) *cs.increment()['numeric'][1]) coord.weight = primarybeam(dx,dy,restfreq)**2 if coords.coord_type == 'physical': for coord in coords: coord.weight = 0. for pixcoord in pixcoords: if coord.index == pixcoord.index and pixcoord.weight > coord.weight: coord.weight = pixcoord.weight else: coords = pixcoords return coords
def calculate_sigma2_weights(coords, imagenames=[], stampsize=32, maskradius=0): for i, coord in enumerate(coords): coord.index = i if coords.coord_type == 'physical': pixcoords = stacker.getPixelCoords(coords, imagenames) else: pixcoords = coords _allocate_buffers(pixcoords.imagenames, stampsize, len(pixcoords)) _load_stack(pixcoords) pixcoords = _calculate_sigma2_weights(pixcoords, maskradius) if coords.coord_type == 'physical': for coord in coords: coord.weight = 0. for pixcoord in pixcoords: if coord.index == pixcoord.index and pixcoord.weight > coord.weight: coord.weight = pixcoord.weight else: coords = pixcoords return coords
def stack(coords, outfile, stampsize = 32, imagenames= [], method = 'mean', weighting = None, maxmaskradius=None, psfmode = 'point', primarybeam = None): """ Performs stacking in the image domain. coords -- A coordList object of all target coordinates. outfile -- Target name for stacked image. stampsize -- size of target image in pixels imagenames -- Name of images to extract flux from. method -- 'mean' or 'median', will determined how pixels are calculated weighting -- only for method 'mean', if set to None will use weights in coords. maxmaskradius -- allows blanking of centre pixels in weight calculation psfmode -- Allows application of filters to stacking, currently not supported. primarybeam -- only applies if weighting='pb' returns: Estimate of stacked flux assuming point source. """ from ..interval import interval import os import shutil import numpy as np from taskinit import ia, casalog casalog.origin('stacker') casalog.post('#'*42,'INFO') casalog.post('#'*5 + ' {0: <31}'.format("Begin Task: Stacker")+'#'*5, 'INFO') global skymap global data global oldimagenames if coords.coord_type == 'physical': coords = stacker.getPixelCoords(coords, imagenames) # Important that len(coords) here is for the pixel coordinates, not physical! _allocate_buffers(coords.imagenames, stampsize, len(coords)) ia.open(coords.imagenames[0]) cs = ia.coordsys() outnchans = ia.boundingbox()['trc'][2]+1 outnstokes = ia.boundingbox()['trc'][3]+1 ia.done() for imagename in coords.imagenames: ia.open(imagename) if ia.shape()[2] != outnchans or ia.shape()[3] != outnstokes: print('Channels/polarisations do not match in all images! You probably want to stacking do stacking on continuum data and not on spectral cube.') return ia.done() _load_stack(coords, psfmode) if method == 'mean' and weighting == 'sigma2': coords = _calculate_sigma2_weights(coords, maxmaskradius) elif method == 'mean' and weighting == 'sigma': coords = _calculate_sigma_weights(coords, maxmaskradius) elif method == 'mean' and weighting == 'pb': coords = calculate_pb_weights(coords, primarybeam, imagenames) npos = len([c.weight for c in coords if c.weight > 1e-6]) casalog.post('Number of stacking positions: {0}'.format(npos), priority='INFO') stacked_im = _stack_stack(method, coords) _write_stacked_image(outfile, stacked_im, coords.imagenames[0], stampsize) casalog.post('#'*5 + ' {0: <31}'.format("End Task: stacker")+'#'*5) casalog.post('#'*42) return stacked_im[int(stampsize/2), int(stampsize/2),0,0]