def makecontimage(self, im, novaliddata, imname): makeblanks=lambda imn,im: [im.make(imn+eltypo) for eltypo in ['.image', '.residual', '.model', '.psf', '.wgt'] ] if(novaliddata==True): ###make blanks ###The images may still be open...as we don't have ###a reset function; setscales is the closest which will destroy ###the skyequation without much of anything else. im.setscales() makeblanks(imname, im) return novaliddata if(not self.imageparamset): try: im.clean(algorithm='mfclark', gain=self.gain, niter=0, threshold='0.05mJy', model=imname+'.model', image=imname+'.image', residual=imname+'.residual', psfimage=imname+'.psf') ia=casac.image() for ima in [imname+'.residual', imname+'.psf']: ia.open(ima) statout=ia.statistics(verbose=False, list=False) ia.done() if(statout['max'][0]==0.0): novaliddata=True except Exception, instance: if(string.count(instance.message, 'PSFZero') >0): novaliddata=True ###The images may still be open...as we don't have ###a reset function; setscales is the closest which will destroy ###the skyequation without much of anything else. im.setscales() ###make a blank image makeblanks(imname, im) else: raise instance
def compImages(im0,im1,keys=['flux','min','max','maxpos','rms'],tol=1e-4,verbose=False): """ compare two images using imstat and the specified keys, to a tolerance tol, and printing the comparison if verbose==True note that the string keys like 'blcf' will fail """ from os import F_OK if isinstance(tol,float): tol=tol+np.zeros(len(keys)) myia=casac.image() ims=[im0,im1] s=[] for i in range(2): if not os.access(ims[i],F_OK): print ims[i]+" not found" return False myia.open(ims[1]) s.append(myia.statistics()) myia.done() status=True for ik in range(len(keys)): k=keys[ik] s0=s[0][k][0] s1=s[1][k][0] if abs(s0-s1)*2/(s0+s1)>tol[ik]: status=False if verbose: print ("%7s: "%k),s0,s1 return status
def concatimages(cubeimage, inim, csys=None, removeinfile=True): if((csys==None) and os.path.exists(cubeimage)): ia.open(cubeimage) csys=ia.coordsys() ia.done() if(type(inim) != list): inim=[inim] ###Temporary bypass of CAS-4423 #elbeamo={} #for elim in inim: # ia.open(elim) # beam=ia.restoringbeam() #if( (len(beam) > 0) and (not beam.has_key('beams'))): # ia.setrestoringbeam(remove=True) # nchan=ia.shape()[3] # for k in range(nchan): # ia.setrestoringbeam(major=beam['major'], minor=beam['minor'], pa=beam['positionangle'], channel=k, polarization=0) # ia.setrestoringbeam(remove=True) # elbeamo=beam if(not beam.has_key('beams')) else beam['beams']['*0']['*0'] # ia.done() #### if(len(inim)==1): ib=ia.fromimage(outfile=cubeimage, infile=inim[0], overwrite=True) else: #ib=ia.imageconcat(outfile=cubeimage, infiles=inim, # axis=3, relax=True, overwrite=True) ############ strnames='"' for k in range(len(inim)): strnames=strnames+' '+inim[k] if (k > 0) else strnames+inim[k] strnames=strnames+'"' comm='imageconcat '+ strnames + ' '+ cubeimage casalog.post('Command '+comm) exitval=os.system(comm) #print "exitval", exitval if(exitval > 0): raise Exception('Failed to concat '+str(inim)) ib=casac.image() ib.open(cubeimage) ############## ia.done() ##### CAS-4423 temp #if(len(elbeamo) >0): # ib.setrestoringbeam(beam=elbeamo) ##### if(csys != None): if(type(csys)==dict): ib.setcoordsys(csys=csys) else: ib.setcoordsys(csys=csys.torecord()) ib.done() if(removeinfile): for k in range(len(inim)): ia.removefile(inim[k])
def pixelmask2cleanmask(imagename='', maskname='mask0', maskimage='', usemasked=False): """ convert pixel(T/F) mask (in a CASA image) to a mask image (1/0) used for clean imagename - input imagename that contain a mask to be used maskname - mask name in the image (default: mask0) maskimage - output mask image name usemasked - if True use masked region as a valid region """ ia = casac.image() ia.open(imagename) masks = ia.maskhandler('get') ia.close() inmaskname = '' if type(masks) != list: masks = [masks] for msk in masks: if maskname == msk: inmaskname = msk break if inmaskname == '': raise Exception, "mask %s does not exist. Available masks are: %s" % ( maskname, masks) tb = casac.table() tb.open(imagename + '/' + maskname) dat0 = tb.getcol('PagedArray') tb.close() #os.system('cp -r %s %s' % (imagename, maskimage)) shutil.copytree(imagename, maskimage) ia.open(maskimage) # to unset mask ia.maskhandler('set', ['']) # make all valid if (usemasked): ia.set(1) else: ia.set(0) ia.close() # tb.open(maskimage, nomodify=False) imd = tb.getcol('map') # maybe shape check here #by default use True part of bool mask masked = 1 if (usemasked): masked = 0 imd[dat0] = masked tb.putcol('map', imd) tb.close()
def fitCube2(self,x0,y0): result=[] box = str(x0) + ", " + str(y0) + ", " + str(x0) + ", " + str(y0) reg=self.rgTool.box(blc=[x0,y0], trc=[x0,y0]) model = "mymodel.im" resid = "myresid.im" myia = casac.image() for x in ([model, resid]): if (os.path.exists(x)): myia.open(x) myia.remove() myia.close() retval=self.imTool.fitprofile(box=box, ngauss=1, model=model, residual=resid) myia.open(model) theFit = myia.getchunk().flatten() myia.close() myia.open(resid) theResid = myia.getchunk().flatten() myia.done() for x in ([model, resid]): if (os.path.exists(x)): myia.open(x) myia.remove() myia.close() result.append([retval['gs']['amp'].flatten()[0], retval['gs']['center'].flatten()[0], retval['gs']['fwhm'].flatten()[0]]) result.append([retval['gs']['ampErr'].flatten()[0], retval['gs']['centerErr'].flatten()[0], retval['gs']['fwhmErr'].flatten()[0]]) s='fit at [%d,%d]\n\tFWHM: %f \n\peak: %f \t with errors: %f, %f '%(x0,y0, result[0][2], result[0][0], result[1][2], result[1][0]) print s if self.write: self.body2.append('<pre>%s</pre>'%s) data=self.imTool.getchunk(blc=[int(x0),int(y0)], trc=[int(x0),int(y0)], dropdeg=True) pylab.clf() pylab.plot(data,'r-') pylab.plot(theFit, 'bo') pylab.plot(theResid,'g.-.') pylab.xlabel('Layer of image cube @ pixel [%d,%d]'%(x0,y0)) pylab.ylabel('Intensity') pylab.title('Fit on Image '+self.imageName) if self.write: header='Image cube Fit: %s'%self.imageName self.body1=['<pre>Plot of layers @ pixel [%d,%d]:</pre>'%(x0,y0)] saveDir=self.imDir+self.fname[11:-5]+'-cube-min_max%d.png'%self.iterate pylab.savefig(saveDir) self.htmlPub.doBlk(self.body1, self.body2, saveDir,header) self.iterate+=1 XY=[] fwhm=[] for i in range(len(result)): fwhm.append(result[i][2]) XY.append([result[i][0],result[i][1]]) return XY, fwhm
from casac import casac import os import numpy as np import pdb iaim = casac.image() iamask = casac.image() qa = casac.quanta() rg = casac.regionmanager() def automask(image='', maskimage='', fracofpeak=0, rmsthresh=3.0, resolution=None, twopass=False): """ image : dirty image or residual to mask maskimage: name of mask to create. If already existant has to be same shape and coordsys as image fracofpeak : Fraction of peak to use as masking threshold (a number between 0 and 1) if 0 then rmsthresh or a value based on the rms of the image is used rmsthresh : Threshold in sigma to use; default is 3 sigma resolution: if image has no restoring beam ..this value is used for resolution of image (e.g '4arcsec') twopass: set to True if faint fluffy stuff is wanted else false if a less agressive masking is needed especially a multi pass masking after some cleaning """ # print '2pass ', twopass iaim.open(image)
from casac import casac import os import numpy as np import pdb iaim=casac.image() iamask=casac.image() qa=casac.quanta() rg=casac.regionmanager() def automask(image='', maskimage='', fracofpeak=0, rmsthresh=3.0, resolution=None, twopass=False): """ image : dirty image or residual to mask maskimage: name of mask to create. If already existant has to be same shape and coordsys as image fracofpeak : Fraction of peak to use as masking threshold (a number between 0 and 1) if 0 then rmsthresh or a value based on the rms of the image is used rmsthresh : Threshold in sigma to use; default is 3 sigma resolution: if image has no restoring beam ..this value is used for resolution of image (e.g '4arcsec') twopass: set to True if faint fluffy stuff is wanted else false if a less agressive masking is needed especially a multi pass masking after some cleaning """ # print '2pass ', twopass iaim.open(image) stat=iaim.statistics(mask='abs("'+image+'") > 0.0') csys=iaim.coordsys() rb=iaim.restoringbeam() numpix=10 shp=iaim.shape() resol=qa.quantity(resolution, 'arcsec')
from tasks import * casalog = casac.logsink() casalog.setglobal(True) quanta = casac.quanta() measures = casac.measures() imager = casac.imager() calibrater = casac.calibrater() ms = casac.ms() tableplot = casac.tableplot() msplot = casac.msplot() calplot = casac.calplot() table = casac.table() #flagger = casac.flagger() agentflagger = casac.agentflagger() image = casac.image() imagepol = casac.imagepol() simulator = casac.simulator() componentlist = casac.componentlist() coordsys = casac.coordsys() regionmanager = casac.regionmanager() spectralline = casac.spectralline() utils = casac.utils() deconvolver = casac.deconvolver() vpmanager = casac.vpmanager() vlafillertask = casac.vlafillertask() atmosphere = casac.atmosphere() cu = casac.utils() mstransformer = casac.mstransformer() #plotms = __plotmshome__.create( )
from casac import casac from locatescript import copydata quantity = casac.quanta() im = casac.imager() ia = casac.image() def data(): return ['ic2233_1.ms'] def run(fetch=False): #####fetch data if fetch: for f in data(): copydata(f, os.getcwd()) im.open('ic2233_1.ms') npix = 1024 im.selectvis(spw='0', nchan=[6], start=[0], step=[1]) im.defineimage(nx=npix, ny=npix, cellx='3.0arcsec', celly='3.0arcsec', stokes="IV", spw=[0]) im.weight(type='briggs', robust=0.7) im.setoptions(imagetilevol=-1000000) im.setvp(dovp=bool(1),
def imagecontbychan(self, msname='spw00_4chan351rowTile.ms', start=[0], numchan=[1], spw=[0], field=0, freq='1.20GHz', band='200MHz', imname='newmodel'): ia=casac.image() if(type(spw) == int): spw=[spw] start=[start] numchan=[numchan] totchan=np.sum(numchan) if(not self.imageparamset): if(totchan==0): ###make blanks self.im.selectvis(vis=msname, field=field, spw=spw, nchan=numchan, start=start, step=1, time=self.timerange, uvrange=self.uvrange, baseline=self.baselines, scan=self.scan, observation=self.observation,writeaccess=False) self.im.defineimage(nx=self.pixels[0], ny=self.pixels[1], cellx=self.cell[0], celly=self.cell[1], phasecenter=self.phCen, mode='frequency', nchan=1, start=freq, step=band, facets=self.facets) self.im.make(imname+'.image') self.im.make(imname+'.residual') self.im.make(imname+'.model') self.im.make(imname+'.psf') self.imageparamset=True return del self.im self.im=[] del self.novaliddata self.novaliddata=[] selkey={} spwind=0 chancounter=0 for k in range(totchan): if(chancounter==numchan[spwind]): chancounter=0 spwind+=1 selkey[k]= {'spw': spw[spwind], 'start':start[spwind]+chancounter} chancounter += 1 self.im.append(casac.imager()) self.novaliddata.append(False) print 'selkey', selkey else: if(totchan==0): return origname=msname for k in range(totchan): ###either psf 0 or no channel selected if(k==0): image=imname+'.image' residual=imname+'.residual' psf=imname+'.psf' else: image=imname+'_'+str(k)+'.image' residual=imname+'_'+str(k)+'.residual' psf=imname+'_'+str(k)+'.psf' if(not self.novaliddata[k]): msname=origname if(not self.imageparamset): self.im[k].selectvis(vis=msname, field=field, spw=selkey[k]['spw'], nchan=1, start=selkey[k]['start'], step=1, datainmemory=self.visInMem, time=self.timerange, uvrange=self.uvrange, baseline=self.baselines, scan=self.scan, observation=self.observation, writeaccess=False) self.im[k].defineimage(nx=self.pixels[0], ny=self.pixels[1], cellx=self.cell[0], celly=self.cell[1], phasecenter=self.phCen, mode='frequency', nchan=1, start=freq, step=band, facets=self.facets) self.im[k].weight(type=self.weight, rmode='norm', npixels=self.weightnpix, robust=self.robust) self.im[k].setoptions(ftmachine=self.ft, wprojplanes=self.wprojplanes, pastep=self.painc, pblimit=self.pblimit, cfcachedirname=self.cfcache, dopbgriddingcorrections=self.dopbcorr, applypointingoffsets=self.applyoffsets, imagetilevol=self.imagetilevol, singleprecisiononly=True, numthreads=self.numthreads) #im.regionmask(mask='lala.mask', boxes=[[0, 0, 3599, 3599]]) #im.setmfcontrol(cyclefactor=0.0) if(not self.imageparamset): try: self.im[k].clean(algorithm='clark', gain=self.gain, niter=0, model=imname+'.model', image=image, residual=residual, psfimage=psf) if(k > 0): ia.open(imname+'.psf') ia.calc('"'+imname+'.psf'+'" + "'+psf+'"') ia.done() except Exception, instance: if(string.count(instance.message, 'PSFZero') >0): self.novaliddata[k]=True ###make a blank image if(k==0): self.im[k].make(image) self.im[k].make(residual) else: raise instance else: if(not self.novaliddata[k]): self.im[k].restore(model=imname+'.model', image=image, residual=residual) if(k > 0): ia.open(imname+'.residual') ia.calc('"'+imname+'.residual'+'" + "'+residual+'"') ia.done() ia.open(imname+'.image') ia.calc('"'+imname+'.image'+'" + "'+residual+'"') ia.done() tb.showcache()
from casac import casac from locatescript import copydata quantity=casac.quanta() im = casac.imager() ia = casac.image() def data(): return ['ic2233_1.ms'] def run(fetch=False) : #####fetch data if fetch: for f in data( ): copydata( f, os.getcwd( ) ) im.open('ic2233_1.ms') npix=1024 im.selectvis(spw='0', nchan=[6], start=[0], step=[1]) im.defineimage(nx=npix, ny=npix, cellx='3.0arcsec', celly='3.0arcsec', stokes="IV", spw=[0]) im.weight(type='briggs', robust=0.7) im.setoptions(imagetilevol=-1000000); im.setvp(dovp=bool(1), usedefaultvp=bool(1), dosquint=bool(1), parangleinc='5.0deg') im.make('squint_corr') im.clean(algorithm='mfhogbom', niter=1000, model=['squint_corr'], residual=['squint_corr.residual'], image=['squint_corr.restored'], threshold='0Jy') im.done() return ['squint_corr.restored']
def fitCube2(self, x0, y0): result = [] box = str(x0) + ", " + str(y0) + ", " + str(x0) + ", " + str(y0) reg = self.rgTool.box(blc=[x0, y0], trc=[x0, y0]) model = "mymodel.im" resid = "myresid.im" myia = casac.image() for x in ([model, resid]): if (os.path.exists(x)): myia.open(x) myia.remove() myia.close() retval = self.imTool.fitprofile(box=box, ngauss=1, model=model, residual=resid) myia.open(model) theFit = myia.getchunk().flatten() myia.close() myia.open(resid) theResid = myia.getchunk().flatten() myia.done() for x in ([model, resid]): if (os.path.exists(x)): myia.open(x) myia.remove() myia.close() result.append([ retval['gs']['amp'].flatten()[0], retval['gs']['center'].flatten()[0], retval['gs']['fwhm'].flatten()[0] ]) result.append([ retval['gs']['ampErr'].flatten()[0], retval['gs']['centerErr'].flatten()[0], retval['gs']['fwhmErr'].flatten()[0] ]) s = 'fit at [%d,%d]\n\tFWHM: %f \n\peak: %f \t with errors: %f, %f ' % ( x0, y0, result[0][2], result[0][0], result[1][2], result[1][0]) print s if self.write: self.body2.append('<pre>%s</pre>' % s) data = self.imTool.getchunk(blc=[int(x0), int(y0)], trc=[int(x0), int(y0)], dropdeg=True) pylab.clf() pylab.plot(data, 'r-') pylab.plot(theFit, 'bo') pylab.plot(theResid, 'g.-.') pylab.xlabel('Layer of image cube @ pixel [%d,%d]' % (x0, y0)) pylab.ylabel('Intensity') pylab.title('Fit on Image ' + self.imageName) if self.write: header = 'Image cube Fit: %s' % self.imageName self.body1 = [ '<pre>Plot of layers @ pixel [%d,%d]:</pre>' % (x0, y0) ] saveDir = self.imDir + self.fname[ 11:-5] + '-cube-min_max%d.png' % self.iterate pylab.savefig(saveDir) self.htmlPub.doBlk(self.body1, self.body2, saveDir, header) self.iterate += 1 XY = [] fwhm = [] for i in range(len(result)): fwhm.append(result[i][2]) XY.append([result[i][0], result[i][1]]) return XY, fwhm
def __init__(self, imageName, write=True, resultDir='WEBPAGES/imageTest/', imDir='IMAGES/'): import shutil self.imTool = casac.image() self.imTool.open(imageName) #open('tables/squint_corr.restored') # fix up images that don't have CASA-canonical stokes and spec: # assume for now that direction is in 01 at least, mycs = self.imTool.coordsys() findstok = mycs.findcoordinate("stokes") if not findstok['return']: myImagename = imageName + ".k" self.imTool.adddegaxes(stokes=True, outfile=myImagename, overwrite=True) mystokpix = self.imTool.summary()['ndim'] # ct from 0 self.imTool.close() shutil.rmtree(imageName) shutil.move(myImagename, imageName) self.imTool.open(imageName) mycs.done() mycs = self.imTool.coordsys() else: mystokpix = findstok['pixel'] findspec = mycs.findcoordinate("spectral") if not findspec['return']: myImagename = imageName + ".s" self.imTool.adddegaxes(spectral=True, outfile=myImagename, overwrite=True) myspecpix = self.imTool.summary()['ndim'] # ct from 0 self.imTool.close() shutil.rmtree(imageName) shutil.move(myImagename, imageName) self.imTool.open(imageName) mycs.done() mycs = self.imTool.coordsys() else: myspecpix = findspec['pixel'] curr_order = [mystokpix, myspecpix] if curr_order != [2, 3]: myImagename = imageName + ".t" self.imTool.transpose(order="01%1i%1i" % (mystokpix, myspecpix), outfile=myImagename, overwrite=True) shutil.rmtree(imageName) shutil.move(myImagename, imageName) self.imTool.open(imageName) self.rgTool = casac.regionmanager() self.qaTool = casac.quanta() self.getArr() #instead make explicit call to getArr() self.write = write self.imageName = imageName self.iterate = 0 #for multiple cubeFit() tests if self.write: self.resultDir = resultDir + strftime('/%Y_%m_%d/') if os.access(self.resultDir, os.F_OK) is False: print self.resultDir + ' directory DNE, so am making one!' os.mkdir(self.resultDir) else: print self.resultDir + ' directory exists; will add to it!' self.imDir = imDir if os.access(imDir, os.F_OK) is False: print imDir + ' directory DNE, so am making one!' os.mkdir(imDir) else: print imDir + ' directory exists; will add to it!' t = localtime(time()) self.fname = 'Regression-%s-%s-%s-%s-%s-%s.html' % ( t[0], t[1], t[2], t[3], t[4], t[5]) self.html = self.resultDir + self.fname self.body1 = [] self.body2 = [] self.htmlPub = htmlPub(self.html, 'Image tests') else: print 'stats-only mode; will not write to html file!'
def __init__(self, imageName, write=True, resultDir='WEBPAGES/imageTest/', imDir='IMAGES/'): import shutil self.imTool=casac.image() self.imTool.open(imageName) #open('tables/squint_corr.restored') # fix up images that don't have CASA-canonical stokes and spec: # assume for now that direction is in 01 at least, mycs=self.imTool.coordsys() findstok=mycs.findcoordinate("stokes") if not findstok['return']: myImagename=imageName+".k" self.imTool.adddegaxes(stokes=True,outfile=myImagename,overwrite=True) mystokpix=self.imTool.summary()['ndim'] # ct from 0 self.imTool.close() shutil.rmtree(imageName) shutil.move(myImagename,imageName) self.imTool.open(imageName) mycs.done() mycs=self.imTool.coordsys() else: mystokpix=findstok['pixel'] findspec=mycs.findcoordinate("spectral") if not findspec['return']: myImagename=imageName+".s" self.imTool.adddegaxes(spectral=True,outfile=myImagename,overwrite=True) myspecpix=self.imTool.summary()['ndim'] # ct from 0 self.imTool.close() shutil.rmtree(imageName) shutil.move(myImagename,imageName) self.imTool.open(imageName) mycs.done() mycs=self.imTool.coordsys() else: myspecpix=findspec['pixel'] curr_order=[mystokpix,myspecpix] if curr_order != [2,3]: myImagename=imageName+".t" self.imTool.transpose(order="01%1i%1i" % (mystokpix,myspecpix),outfile=myImagename,overwrite=True) shutil.rmtree(imageName) shutil.move(myImagename,imageName) self.imTool.open(imageName) self.rgTool=casac.regionmanager() self.qaTool=casac.quanta() self.getArr() #instead make explicit call to getArr() self.write=write self.imageName=imageName self.iterate=0 #for multiple cubeFit() tests if self.write: self.resultDir=resultDir+strftime('/%Y_%m_%d/') if os.access(self.resultDir,os.F_OK) is False: print self.resultDir+' directory DNE, so am making one!' os.mkdir(self.resultDir) else: print self.resultDir+' directory exists; will add to it!' self.imDir=imDir if os.access(imDir,os.F_OK) is False: print imDir+' directory DNE, so am making one!' os.mkdir(imDir) else: print imDir+' directory exists; will add to it!' t=localtime( time() ) self.fname='Regression-%s-%s-%s-%s-%s-%s.html'%(t[0],t[1],t[2],t[3],t[4],t[5]) self.html=self.resultDir+self.fname self.body1=[] self.body2=[] self.htmlPub=htmlPub(self.html,'Image tests') else: print 'stats-only mode; will not write to html file!'