def __init__(self, ftmachine='ft', wprojplanes=10, facets=1, pixels=[3600, 3600], cell=['3arcsec', '3arcsec'], spw='', field='', phasecenter='', weight='natural', robust=0.0, stokes='I', npixels=0, uvtaper=False, outertaper=[], timerange='', uvrange='', baselines='', scan='', observation='', gain=0.1, numthreads=-1, pbcorr=False, minpb=0.2, cyclefactor=1.5, painc=360., pblimit=0.1, dopbcorr=True, applyoffsets=False, cfcache='cfcache.dir', epjtablename='',mterm=True,wbawp=True,aterm=True,psterm=True,conjbeams=True): self.im=casac.imager() self.imperms={} self.dc=casac.deconvolver() self.ft=ftmachine self.origms='' self.wprojplanes=wprojplanes self.facets=facets print 'cell', cell self.pixels=pixels self.cell=cell if(spw==''): spw='*' self.spw=spw self.field=field self.phCen=phasecenter self.weight=weight self.imageparamset=False self.robust=robust self.weightnpix=npixels self.stokes=stokes self.numthreads=numthreads self.imagetilevol=1000000 self.visInMem=False self.cyclefactor=cyclefactor self.novaliddata={} self.gain=gain self.uvtaper=uvtaper self.outertaper=outertaper self.timerange=timerange self.uvrange=uvrange self.baselines=baselines self.scan=scan self.observation=observation self.pbcorr=pbcorr self.minpb=minpb ###a default common cube coordsys self.cubecoordsys=casac.coordsys().torecord() self.painc=painc; self.pblimit=pblimit; self.dopbcorr=dopbcorr; self.applyoffsets=applyoffsets; self.cfcache=cfcache; self.epjtablename=epjtablename; self.mterm=mterm; self.aterm=aterm; self.psterm=psterm; self.wbawp=wbawp; self.conjbeams=conjbeams;
def imagecont(self, msname='spw00_4chan351rowTile.ms', start=[0], numchan=[-1], spw='', field=0, freq='1.20GHz', band='200MHz', imname='newmodel', nterms=1, scales=[0],timerange=''): #casalog.post('KEYS '+str(self.imperms.keys())) if(not self.imperms.has_key(msname)): self.imageparamset=False im=casac.imager() self.imperms[msname]=im self.novaliddata[msname]=False #casalog.post('MSNAME '+msname) else: #casalog.post('reMSNAME '+msname) im=self.imperms[msname] self.imageparamset=True ###either psf 0 or no channel selected if(self.novaliddata[msname]): return #j=start #end=start+numchan-1 #spwstring=str(spw)+':'+str(start)+'~'+str(end) #print 'spwstring', spwstring if(not self.imageparamset): self.origms=msname try: im.selectvis(vis=msname, field=field, spw=spw, nchan=numchan, start=start, step=1, datainmemory=self.visInMem, time=timerange, uvrange=self.uvrange, baseline=self.baselines, scan=self.scan, observation=self.observation, writeaccess=False) if(self.uvtaper): im.filter(type='gaussian', bmaj=self.outertaper[0], bmin=self.outertaper[1], bpa=self.outertaper[2]) except Exception, instance: ###failed to selectdata self.novaliddata[msname]=True #### #imname=imname+'_%02d'%(j) self.setparamcont(im, freq, band) if(self.ft=='mosaic'): self.setextraoptions(im, fluxscaleimage=imname+'.flux', scaletype='SAULT') if((len(numchan)==0) or (np.sum(numchan)==0)): self.novaliddata[msname]=True
# other functions the engines do not need # we load tools and tasks explicitly ## ## finally load tools #### # jagonzal: Import tasks but don't load task manager and dbus os.environ['CASA_ENGINE']="YES" 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()
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 imagechan_selfselect(self, msname='spw00_4chan351rowTile.ms', spwids=[0], field=0, imroot='newmodel', imchan=0, niter=100, alg='clark', thr='0.0mJy', mask='', majcycle=1, scales=[0], chanchunk=1, startchan=0): ###need generate fstart, width from 1 channel width, start, numchan and spw origname=msname retval={} retval['maxresidual']=0.0 retval['iterations']=0 retval['converged']=False #pdb.set_trace() ####This causes a lock problem..randomly gets exception while waiting #ia.open(imroot+'.model') #csys=ia.coordsys() #ia.done() #### csys=casac.coordsys() ###hopefully this has been set by caller csys.fromrecord(self.cubecoordsys) im=casac.imager() fstart=csys.toworld([0,0,0,startchan],'n')['numeric'][3] fstep=csys.toworld([0,0,0,startchan+1],'n')['numeric'][3]-fstart fend=fstep*(chanchunk-1)+fstart print 'fstat bef findchansel ', fstart spw, start, nchan=self.findchanselLSRK(msname=msname, spw=spwids, field=field, numpartition=1, beginfreq=fstart, endfreq=fend, chanwidth=fstep) print 'spw, start, nchan', spw, start, nchan if((len(spw[0])==0) or (len(nchan[0])==0) or (len(start[0]) ==0) ): return retval imname=imroot+str(imchan) maskname='' if(mask != ''): maskname=imname+'.mask' if( not self.makemask(inmask=mask, outmask=maskname, imchan=imchan, chanchunk=chanchunk, startchan=startchan)): maskname='' # a.getchanimage(cubeimage=imroot+'.model', outim=imname+'.model', chan=imchan) im.selectvis(vis=msname, field=field, spw=spw[0], nchan=nchan[0], start=start[0], step=1, datainmemory=self.visInMem, time=self.timerange, uvrange=self.uvrange, baseline=self.baselines, scan=self.scan, observation=self.observation, writeaccess=False) print 'fstart bef def', fstart if(self.uvtaper): im.filter(type='gaussian', bmaj=self.outertaper[0], bmin=self.outertaper[1], bpa=self.outertaper[2]) im.defineimage(nx=self.pixels[0], ny=self.pixels[1], cellx=self.cell[0], celly=self.cell[1], phasecenter=self.phCen, facets=self.facets, mode='frequency', nchan=chanchunk, start=str(fstart)+'Hz', step=str(fstep)+'Hz', outframe='LSRK') im.weight(type=self.weight, rmode='norm', npixels=self.weightnpix, robust=self.robust) im.setoptions(ftmachine=self.ft, wprojplanes=self.wprojplanes, imagetilevol=self.imagetilevol, singleprecisiononly=True, numthreads=self.numthreads) im.setscales(scalemethod='uservector', uservector=scales) fluxscaleimage=imname+'.flux' if(self.ft=='mosaic') else '' majcycle = majcycle if (niter/majcycle) >0 else 1 self.setextraoptions(im, fluxscaleimage=fluxscaleimage, cyclefactor=(0.0 if(majcycle >1) else self.cyclefactor)) #### non simple ft machine...should use mf if((self.ft != 'ft') or ((majcycle <= 1) and (self.cyclefactor > 0))): alg='mf'+alg try: for kk in range(majcycle): retval=im.clean(algorithm=alg, gain=self.gain, niter= (niter/majcycle), threshold=thr, model=imname+'.model', image=imname+'.image', residual=imname+'.residual', mask=maskname, psfimage=imname+'.psf') if(not os.path.exists(imname+'.image')): ##all channels flagged for example...make 0 images for concat later im.defineimage(nx=self.pixels[0], ny=self.pixels[1], cellx=self.cell[0], celly=self.cell[1], phasecenter=self.phCen, facets=self.facets, mode='frequency', nchan=chanchunk, start=str(fstart)+'Hz', step=str(fstep)+'Hz', outframe='LSRK') im.make(imname+'.image') im.make(imname+'.model') im.make(imname+'.residual') im.make(imname+'.psf') if(self.ft=='mosaic'): im.make(imname+'.flux' ) im.make(imname+'.flux.pbcoverage') im.done() del im except Exception as instance: if(not os.path.exists(imname+'.image')): ##all channels flagged for example...make 0 images for concat later im.defineimage(nx=self.pixels[0], ny=self.pixels[1], cellx=self.cell[0], celly=self.cell[1], phasecenter=self.phCen, facets=self.facets, mode='frequency', nchan=chanchunk, start=str(fstart)+'Hz', step=str(fstep)+'Hz', outframe='LSRK') im.make(imname+'.image') im.make(imname+'.model') im.make(imname+'.residual') im.make(imname+'.psf') if(self.ft=='mosaic'): im.make(imname+'.flux' ) im.make(imname+'.flux.pbcoverage') im.done() del im if(string.count(str(instance), 'PSFZero') <1): raise Exception(instance) if(maskname != ''): shutil.rmtree(maskname, True) #self.putchanimage(imroot+'.model', imname+'.model', startchan, False) #self.putchanimage(imroot+'.residual', imname+'.residual', startchan, False) #self.putchanimage(imroot+'.image', imname+'.image', startchan, False) #self.putchanimage(imroot+'.psf', imname+'.psf', startchan, False) return retval
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']