Exemplo n.º 1
0
    def setUp(self):
        usescratch=False
        mosweight=False
        if (os.path.exists(self.msfile)):
            os.system('rm -rf ' + self.msfile)
  
        shutil.copytree(self.datapath+self.msfile, self.msfile)

        self.imset = cleanhelper(im, self.msfile, (usescratch or mosweight))
        self.imset.datsel(field=self.field, spw=self.spw, nchan=self.nchan,
                         start=self.start, width=self.width,
                         timerange='', uvrange='', antenna='', scan='', 
                         observation='',intent='', usescratch=usescratch)
Exemplo n.º 2
0
def clean(vis, imagename,outlierfile, field, spw, selectdata, timerange,
          uvrange, antenna, scan, observation, intent, mode, resmooth,gridmode,
          wprojplanes, facets, cfcache, rotpainc, painc, aterm, psterm, mterm, wbawp, conjbeams,
          epjtable, interpolation,
          niter, gain, threshold, psfmode, imagermode, ftmachine, mosweight,
          scaletype, multiscale, negcomponent, smallscalebias,
          interactive, mask, nchan, start, width, outframe,
          veltype, imsize, cell, phasecenter, restfreq, stokes, weighting,
          robust, uvtaper, outertaper, innertaper, modelimage, restoringbeam,
          pbcor, minpb, usescratch, noise, npixels, npercycle, cyclefactor,
          cyclespeedup, nterms, reffreq, chaniter, flatnoise, allowchunk):

    #Python script
    casalog.origin('clean')
    casalog.post('nchan='+str(nchan)+' start='+str(start)+' width='+str(width))  
    #If using new FT-Machines, do not use the on-the-fly model_data columns.
    # if (gridmode == 'advancedaprojection'):
    #     raise Exception, 'This mode is not yet ready for use'
    
    if gridmode == 'advancedaprojection' and usescratch==False:
        casalog.post('Forcing usescratch=True for new FTMs. This is temporary.', 'WARN')
        usescratch=True
    
    #######################################################################  
    #
    # start of the big cube treatment
    #
    #######################################################################  
    #paralist=[vis, imagename,outlierfile, field, spw, selectdata, \
    #         timerange, uvrange, antenna, scan, observation, intent, mode, gridmode, \
    #         wprojplanes, facets, cfcache, painc, epjtable, \
    #         interpolation, niter, gain, threshold, psfmode, \
    #         imagermode, ftmachine, mosweight, scaletype, multiscale, \
    #         negcomponent, smallscalebias, interactive, mask, nchan, \
    #         start, width, outframe, veltype, imsize, cell, \
    #         phasecenter, restfreq, stokes, weighting, \
    #         robust, uvtaper, outertaper, innertaper, modelimage, \
    #         restoringbeam, pbcor, minpb, calready, noise, npixels, \
    #         npercycle, cyclefactor, cyclespeedup, nterms, reffreq, \
    #         chaniter, flatnoise, allowchunk]
    #print paralist

    if (spw==''):
        spw='*';
    if allowchunk and (mode=='velocity' or mode=='frequency' or mode=='channel') \
       and not interactive:

	casalog.post('analysing intended channalization...')
        imCln=imtool()
        imset=cleanhelper(imCln, vis, usescratch, casalog)

        (npage, localstart, localwidth)=imset.setChannelizeNonDefault(mode,
                spw, field,nchan,start,width,outframe,veltype,phasecenter, restfreq)
	casalog.post('start='+str(localstart)+' width='+str(localwidth)+' nchan='+str(npage))
        del imCln
 
        try: 
            # estimate the size of the image
            import commands
    
            nstokes=len(stokes)
            casalog.post('imsize='+str(imsize)+' nstokes='+str(nstokes))

            if len(imsize)==1:
                npixel=imsize[0]*imsize[0]
            else:
                npixel=imsize[0]*imsize[1]
    
            volumn=4*nstokes*npage*npixel
            

            av=cu.hostinfo()['memory']['available']
            casalog.post('mem available: '+str(int(av/1024))+'M')

            freemem=commands.getoutput("free")
            for line in freemem.splitlines():
                if line.startswith('Mem'):
                    av=float(line.split()[3])/1024
                    break
            casalog.post('mem free: '+str(int(av))+'M')

            nd=volumn/1024./1024.*9
            casalog.post('mem needed for single chunck clean: '+str(int(nd))+'M')

            chunk=1
            tchan=npage
            fa=2.5
            if not av/nd>fa:
                tchan=npage*av/nd/fa
                chunk=math.ceil(npage/tchan)
                tchan=int(math.floor(npage/chunk))
                chunk=int(chunk)
                #print 'tchan', tchan, ' chunk', chunk

            if chunk>1 and tchan<npage:
                casalog.post('will clean the cube in '+str(chunk)+' chunks')
                bigimg=imagename
                ta=qa.quantity(str(localstart))
                wd=qa.quantity(str(localwidth))
                subimg=[]
                for k in range(chunk):
                    st=0
                    bg=k*tchan
                    ed=bg+tchan-1
                    imname=bigimg+'_'+str(bg)+'-'+str(ed)
                    if mode=='channel':
                        st=localstart+k*tchan
                        ed=st+tchan-1
                    if mode=='frequency':
                        st=qa.convert(ta, 'Hz')['value']
                        ed=qa.convert(wd, 'Hz')['value']
                        st=str(st+k*tchan*ed)+'Hz'
                    if mode=='velocity':
                        st=qa.convert(ta, 'm/s')['value']
                        ed=qa.convert(wd, 'm/s')['value']
                        st=str(st+k*tchan*ed)+'m/s'
                        
                    #print imname, tchan, st, localwidth 

                    os.system('rm -rf '+imname+'*')
                    try:
                        clean(vis=vis,imagename=imname,outlierfile=outlierfile,field=field,
                              spw=spw,selectdata=selectdata,timerange=timerange,uvrange=uvrange,
                              antenna=antenna,scan=scan, observation=str(observation),intent=intent,
                              mode=mode, resmooth=resmooth, gridmode=gridmode, 
                              wprojplanes=wprojplanes,facets=facets,cfcache=cfcache,rotpainc=rotpainc, painc=painc,
                              psterm=psterm,aterm=aterm,mterm=mterm,wbawp=wbawp,conjbeams=conjbeams,
                              epjtable=epjtable,interpolation=interpolation,niter=niter,
                              gain=gain,
                              threshold=threshold,psfmode=psfmode,imagermode=imagermode, 
                              ftmachine=ftmachine,mosweight=mosweight,scaletype=scaletype,
                              multiscale=multiscale,negcomponent=negcomponent,
                              smallscalebias=smallscalebias,interactive=interactive,
                              mask=mask,nchan=tchan,start=st,width=localwidth,outframe=outframe,
                              veltype=veltype,imsize=imsize,cell=cell,phasecenter=phasecenter,
                              restfreq=restfreq,stokes=stokes,weighting=weighting,
                              robust=robust,uvtaper=uvtaper,outertaper=outertaper,
                              innertaper=innertaper,modelimage=modelimage,
                              restoringbeam=restoringbeam,pbcor=pbcor,minpb=minpb,
                              usescratch=usescratch,noise=noise,npixels=npixels,npercycle=npercycle,
                              cyclefactor=cyclefactor,cyclespeedup=cyclespeedup,nterms=nterms,
                              reffreq=reffreq,chaniter=chaniter,flatnoise=flatnoise,
                              allowchunk=False)
                    except Exception, instance:
                        if(string.count(instance.message, 'PSFZero') >0):
                            ia.fromimage(outfile=imname+'.image', infile=imname+'.residual', overwrite=True)
                            ia.done()
                        else:
                            raise instance
                    subimg.append(imname)

                for i in ['.image', '.flux', '.model', '.psf', '.residual', '.mask']:
                    if((len(subimg) > 0) and  os.path.exists(subimg[0]+i)):
                        casalog.post('concate '+bigimg+'_*'+i+' to '+bigimg+i)   
                        os.system('rm -rf '+bigimg+i)
                        inf=''
                        for j in range(len(subimg)):
                            inf+=' '+subimg[j]+i   
                        bigim=ia.imageconcat(outfile=bigimg+i, infiles=inf, relax=True)
                        bigim.done()
                        ia.close()
                        os.system('rm -rf '+inf)

                return    
            else:
                casalog.post('will clean the cube in a single chunk')
Exemplo n.º 3
0
    if (not selectdata):
        timerange=''
        uvrange=''
        antenna=''
        scan=''
        observation = ''
        intent=''
       
    try:
        # Create a new imager tool
        imCln=imtool();

        ###if usescratch open ms with scratch column
        ###if mosweight use scratch columns as there in no
        ###mosweight available for no scratch column /readonly ms yet
        imset=cleanhelper(imCln, vis, usescratch, casalog)

        # multims input only (do sorting of vis list based on spw)
        if  type(vis)==list: imset.sortvislist(spw,mode,width)

        # Check imagename
        if((len(imagename) == 0) or
           ((type(imagename) == str) and imagename.isspace())):
            raise Exception, 'Cannot proceed with blank imagename'

        opim=imset.checkimageusage(imagename)
        if(len(opim) != 0):
            raise Exception, 'image '+str(opim[0])+' is opened by another process' if(len(opim)==1) else  'images '+str(opim)+' are under use by other processes'
        multifield=False
        if (type(imagename)==list) & (len(imagename) > 1):
            multifield=True
Exemplo n.º 4
0
def clean(vis, imagename, outlierfile, field, spw, selectdata, timerange,
          uvrange, antenna, scan, observation, intent, mode, resmooth,
          gridmode, wprojplanes, facets, cfcache, rotpainc, painc, aterm,
          psterm, mterm, wbawp, conjbeams, epjtable, interpolation, niter,
          gain, threshold, psfmode, imagermode, ftmachine, mosweight,
          scaletype, multiscale, negcomponent, smallscalebias, interactive,
          mask, nchan, start, width, outframe, veltype, imsize, cell,
          phasecenter, restfreq, stokes, weighting, robust, uvtaper,
          outertaper, innertaper, modelimage, restoringbeam, pbcor, minpb,
          usescratch, noise, npixels, npercycle, cyclefactor, cyclespeedup,
          nterms, reffreq, chaniter, flatnoise, allowchunk):

    #Python script
    casalog.origin('clean')
    casalog.post('nchan=' + str(nchan) + ' start=' + str(start) + ' width=' +
                 str(width))
    #If using new FT-Machines, do not use the on-the-fly model_data columns.
    # if (gridmode == 'advancedaprojection'):
    #     raise Exception, 'This mode is not yet ready for use'

    if gridmode == 'advancedaprojection' and usescratch == False:
        casalog.post(
            'Forcing usescratch=True for new FTMs. This is temporary.', 'WARN')
        usescratch = True

    #######################################################################
    #
    # start of the big cube treatment
    #
    #######################################################################
    #paralist=[vis, imagename,outlierfile, field, spw, selectdata, \
    #         timerange, uvrange, antenna, scan, observation, intent, mode, gridmode, \
    #         wprojplanes, facets, cfcache, painc, epjtable, \
    #         interpolation, niter, gain, threshold, psfmode, \
    #         imagermode, ftmachine, mosweight, scaletype, multiscale, \
    #         negcomponent, smallscalebias, interactive, mask, nchan, \
    #         start, width, outframe, veltype, imsize, cell, \
    #         phasecenter, restfreq, stokes, weighting, \
    #         robust, uvtaper, outertaper, innertaper, modelimage, \
    #         restoringbeam, pbcor, minpb, calready, noise, npixels, \
    #         npercycle, cyclefactor, cyclespeedup, nterms, reffreq, \
    #         chaniter, flatnoise, allowchunk]
    #print paralist

    if (spw == ''):
        spw = '*'
    if allowchunk and (mode=='velocity' or mode=='frequency' or mode=='channel') \
       and not interactive:

        casalog.post('analysing intended channalization...')
        imCln = imtool()
        imset = cleanhelper(imCln, vis, usescratch, casalog)

        (npage, localstart, localwidth) = imset.setChannelizeNonDefault(
            mode, spw, field, nchan, start, width, outframe, veltype,
            phasecenter, restfreq)
        casalog.post('start=' + str(localstart) + ' width=' + str(localwidth) +
                     ' nchan=' + str(npage))
        del imCln

        try:
            # estimate the size of the image
            import commands

            nstokes = len(stokes)
            casalog.post('imsize=' + str(imsize) + ' nstokes=' + str(nstokes))

            if len(imsize) == 1:
                npixel = imsize[0] * imsize[0]
            else:
                npixel = imsize[0] * imsize[1]

            volumn = 4 * nstokes * npage * npixel

            av = cu.hostinfo()['memory']['available']
            casalog.post('mem available: ' + str(int(av / 1024)) + 'M')

            freemem = commands.getoutput("free")
            for line in freemem.splitlines():
                if line.startswith('Mem'):
                    av = float(line.split()[3]) / 1024
                    break
            casalog.post('mem free: ' + str(int(av)) + 'M')

            nd = volumn / 1024. / 1024. * 9
            casalog.post('mem needed for single chunck clean: ' +
                         str(int(nd)) + 'M')

            chunk = 1
            tchan = npage
            fa = 2.5
            if not av / nd > fa:
                tchan = npage * av / nd / fa
                chunk = math.ceil(npage / tchan)
                tchan = int(math.floor(npage / chunk))
                chunk = int(chunk)
                #print 'tchan', tchan, ' chunk', chunk

            if chunk > 1 and tchan < npage:
                casalog.post('will clean the cube in ' + str(chunk) +
                             ' chunks')
                bigimg = imagename
                ta = qa.quantity(str(localstart))
                wd = qa.quantity(str(localwidth))
                subimg = []
                for k in range(chunk):
                    st = 0
                    bg = k * tchan
                    ed = bg + tchan - 1
                    imname = bigimg + '_' + str(bg) + '-' + str(ed)
                    if mode == 'channel':
                        st = localstart + k * tchan
                        ed = st + tchan - 1
                    if mode == 'frequency':
                        st = qa.convert(ta, 'Hz')['value']
                        ed = qa.convert(wd, 'Hz')['value']
                        st = str(st + k * tchan * ed) + 'Hz'
                    if mode == 'velocity':
                        st = qa.convert(ta, 'm/s')['value']
                        ed = qa.convert(wd, 'm/s')['value']
                        st = str(st + k * tchan * ed) + 'm/s'

                    #print imname, tchan, st, localwidth

                    os.system('rm -rf ' + imname + '*')
                    try:
                        clean(vis=vis,
                              imagename=imname,
                              outlierfile=outlierfile,
                              field=field,
                              spw=spw,
                              selectdata=selectdata,
                              timerange=timerange,
                              uvrange=uvrange,
                              antenna=antenna,
                              scan=scan,
                              observation=str(observation),
                              intent=intent,
                              mode=mode,
                              resmooth=resmooth,
                              gridmode=gridmode,
                              wprojplanes=wprojplanes,
                              facets=facets,
                              cfcache=cfcache,
                              rotpainc=rotpainc,
                              painc=painc,
                              psterm=psterm,
                              aterm=aterm,
                              mterm=mterm,
                              wbawp=wbawp,
                              conjbeams=conjbeams,
                              epjtable=epjtable,
                              interpolation=interpolation,
                              niter=niter,
                              gain=gain,
                              threshold=threshold,
                              psfmode=psfmode,
                              imagermode=imagermode,
                              ftmachine=ftmachine,
                              mosweight=mosweight,
                              scaletype=scaletype,
                              multiscale=multiscale,
                              negcomponent=negcomponent,
                              smallscalebias=smallscalebias,
                              interactive=interactive,
                              mask=mask,
                              nchan=tchan,
                              start=st,
                              width=localwidth,
                              outframe=outframe,
                              veltype=veltype,
                              imsize=imsize,
                              cell=cell,
                              phasecenter=phasecenter,
                              restfreq=restfreq,
                              stokes=stokes,
                              weighting=weighting,
                              robust=robust,
                              uvtaper=uvtaper,
                              outertaper=outertaper,
                              innertaper=innertaper,
                              modelimage=modelimage,
                              restoringbeam=restoringbeam,
                              pbcor=pbcor,
                              minpb=minpb,
                              usescratch=usescratch,
                              noise=noise,
                              npixels=npixels,
                              npercycle=npercycle,
                              cyclefactor=cyclefactor,
                              cyclespeedup=cyclespeedup,
                              nterms=nterms,
                              reffreq=reffreq,
                              chaniter=chaniter,
                              flatnoise=flatnoise,
                              allowchunk=False)
                    except Exception, instance:
                        if (string.count(instance.message, 'PSFZero') > 0):
                            ia.fromimage(outfile=imname + '.image',
                                         infile=imname + '.residual',
                                         overwrite=True)
                            ia.done()
                        else:
                            raise instance
                    subimg.append(imname)

                for i in [
                        '.image', '.flux', '.model', '.psf', '.residual',
                        '.mask'
                ]:
                    if ((len(subimg) > 0) and os.path.exists(subimg[0] + i)):
                        casalog.post('concate ' + bigimg + '_*' + i + ' to ' +
                                     bigimg + i)
                        os.system('rm -rf ' + bigimg + i)
                        inf = ''
                        for j in range(len(subimg)):
                            inf += ' ' + subimg[j] + i
                        bigim = ia.imageconcat(outfile=bigimg + i,
                                               infiles=inf,
                                               relax=True)
                        bigim.done()
                        ia.close()
                        os.system('rm -rf ' + inf)

                return
            else:
                casalog.post('will clean the cube in a single chunk')
Exemplo n.º 5
0
    if (not selectdata):
        timerange = ''
        uvrange = ''
        antenna = ''
        scan = ''
        observation = ''
        intent = ''

    try:
        # Create a new imager tool
        imCln = imtool()

        ###if usescratch open ms with scratch column
        ###if mosweight use scratch columns as there in no
        ###mosweight available for no scratch column /readonly ms yet
        imset = cleanhelper(imCln, vis, usescratch, casalog)

        # multims input only (do sorting of vis list based on spw)
        if type(vis) == list: imset.sortvislist(spw, mode, width)

        # Check imagename
        if ((len(imagename) == 0)
                or ((type(imagename) == str) and imagename.isspace())):
            raise Exception, 'Cannot proceed with blank imagename'

        opim = imset.checkimageusage(imagename)
        if (len(opim) != 0):
            raise Exception, 'image ' + str(
                opim[0]) + ' is opened by another process' if (
                    len(opim) == 1) else 'images ' + str(
                        opim) + ' are under use by other processes'
Exemplo n.º 6
0
def autoclean(vis, imagename, field, spw, selectdata, timerange, uvrange,
              antenna, scan, mode, nchan, start, width, interpolation,
              doconcat, psfmode, imagermode, cyclefactor, cyclespeedup, imsize,
              cell, phasecenter, restfreq, stokes, weighting, robust, noise,
              npixels, interactive, mask, modelimage, uvtaper, outertaper,
              innertaper, niter, npercycle, npercycle_speedup, gain, pbcor,
              minpb, clean_threshold, Nrms, eps_maxres, useabsresid,
              allow_maxres_inc, island_rms, diag, peak_rms, gain_threshold,
              Npeak, shape, boxstretch, irregsize):

    casalog.origin('autoclean')

    orig_npercycle = npercycle

    # set up initial mask values if input by user
    inputmask = None
    if (mask):
        casalog.post('Using input mask from user.')
        imCln = imtool()
        imset = cleanhelper(imCln, vis)
        imset.defineimages(imsize=imsize,
                           cell=cell,
                           stokes=stokes,
                           mode=mode,
                           spw=spw,
                           nchan=1,
                           start=start,
                           width=width,
                           restfreq=restfreq,
                           field=field,
                           phasecenter=phasecenter)
        imset.datselweightfilter(
            field=field,
            spw=spw,
            timerange=timerange,
            uvrange=uvrange,
            antenna=antenna,
            scan=scan,
            wgttype=weighting,
            robust=robust,
            noise=noise,
            npixels=npixels,
            #mosweight=mosweight,
            innertaper=innertaper,
            outertaper=outertaper,
            calready=False)
        inputmask = '__temporary.mask'
        imset.makemaskimage(outputmask=inputmask, maskobject=mask)
        imCln.close()
        ia.open(inputmask)
        maskVals = ia.getchunk()
        ia.close()
        ia.removefile(inputmask)

    # begin loop over all channels
    for ichan in xrange(nchan):

        if imagermode != 'mfs':
            casalog.post('***** Beginning channel %d of %d' % (ichan, nchan))

        if (mode == 'mfs') or (nchan == 1):
            thisImage = imagename
            thisStart = start
        else:
            thisImage = imagename + '.channel.' + str(ichan)
            if mode == 'channel':
                thisStart = start + ichan * width
            else:
                thisStart = qa.add(start, qa.mul(width, qa.quantity(ichan)))

        # create dirty image from which to choose initial clean regions
        clean(vis=vis,
              imagename=thisImage,
              field=field,
              spw=spw,
              selectdata=selectdata,
              timerange=timerange,
              uvrange=uvrange,
              antenna=antenna,
              scan=scan,
              mode=mode,
              nchan=1,
              start=thisStart,
              width=width,
              interpolation=interpolation,
              psfmode=psfmode,
              imagermode=imagermode,
              cyclefactor=cyclefactor,
              cyclespeedup=cyclespeedup,
              imsize=imsize,
              cell=cell,
              phasecenter=phasecenter,
              restfreq=restfreq,
              stokes=stokes,
              weighting=weighting,
              robust=robust,
              noise=noise,
              npixels=npixels,
              modelimage=modelimage,
              uvtaper=uvtaper,
              outertaper=outertaper,
              innertaper=innertaper,
              niter=0,
              gain=gain,
              minpb=minpb)
        casalog.origin('autoclean')

        # make the mask image
        ia_tool = ia.newimagefromimage(infile=thisImage + '.image',
                                       outfile=thisImage + '.mask',
                                       overwrite=True)
        if (mask):
            ia_tool.putchunk(pixels=maskVals)
        else:
            ia_tool.set(pixels=0.0)
        ia_tool.close()

        # keep track of changes in maximum residual over iterations
        maxres_increased = 0
        post_max = 5000
        # iterations variable stores number of clean internal iterations
        iterations = 0
        # new_npercycle: number of minor cycles this iteration (can change)
        new_npercycle = orig_npercycle
        while iterations < niter:
            cleanmore, nregions = \
                       autowindow(imagename=thisImage, Npeak=Npeak,
                                  shape=shape, irregsize=irregsize,
                                  boxstretch=boxstretch, Nrms=Nrms,
                                  island_rms=island_rms, peak_rms=peak_rms,
                                  gain_threshold=gain_threshold,
                                  clean_threshold=clean_threshold, diag=diag,
                                  useabsresid=useabsresid, ichan=ichan)

            # if first iteration, make sure CLEAN is cleaning
            if not (cleanmore):
                if not (iterations):
                    casalog.post('Input parameters do not induce cleaning of '
                                 'channel %d.' % ichan)
                break

            # did autowindow make new clean regions?
            if (nregions):
                new_npercycle = orig_npercycle
            else:
                casalog.post(
                    'Will continue cleaning with current clean regions.')
                new_npercycle = int(round(new_npercycle * npercycle_speedup))

            # maximum absolute residual before next clean iteration
            if (eps_maxres) or (allow_maxres_inc >= 0):
                pre_max = post_max

            # niter-iterations is the *total* number of iterations left, and might
            # be less than npercycle.  Use the minimum of (npercycle, niter-iterations)
            casalog.post('Starting clean at iteration %d' % iterations)

            #TMP# CAS-1623: an improvement will be made to CLEAN to allow it to
            # start from an existing residual image.  The call to clean may need
            # to be changed to reflect this.
            clean(vis=vis,
                  imagename=thisImage,
                  field=field,
                  spw=spw,
                  selectdata=selectdata,
                  timerange=timerange,
                  uvrange=uvrange,
                  antenna=antenna,
                  scan=scan,
                  mode=mode,
                  nchan=1,
                  width=width,
                  start=thisStart,
                  interpolation=interpolation,
                  psfmode=psfmode,
                  imagermode=imagermode,
                  imsize=imsize,
                  cyclefactor=cyclefactor,
                  cyclespeedup=cyclespeedup,
                  cell=cell,
                  phasecenter=phasecenter,
                  restfreq=restfreq,
                  stokes=stokes,
                  weighting=weighting,
                  robust=robust,
                  noise=noise,
                  npixels=npixels,
                  modelimage=modelimage,
                  uvtaper=uvtaper,
                  outertaper=outertaper,
                  innertaper=innertaper,
                  niter=min(new_npercycle, niter - iterations),
                  mask=thisImage + '.mask',
                  gain=gain,
                  minpb=minpb,
                  npercycle=niter,
                  interactive=bool(nregions and interactive))
            casalog.origin('autoclean')
            iterations += new_npercycle

            # stop if we've reached maximum number of clean minor cycles
            if iterations >= niter:
                casalog.post('Reached maximum number of CLEAN cycles (%d)' %
                             niter)
                break

            # check maximum residual value
            if (eps_maxres) or (allow_maxres_inc >= 0):
                residualImage = thisImage + '.residual'
                ia.open(residualImage)
                stats = ia.statistics(list=False, verbose=True)
                ia.close()
                if (useabsresid):
                    post_max = max([stats['max'][0], abs(stats['min'][0])])
                else:
                    post_max = stats['max'][0]
                # check fractional change of maximum residual
                if (eps_maxres):
                    if abs(post_max - pre_max) / pre_max < eps_maxres:
                        casalog.post('Maximum residual has changed by less '
                                     'than %.3f; stopping' % eps_maxres)
                        break
                if post_max >= pre_max:
                    maxres_increased += 1
                    casalog.post(
                        'Maximum residual has increased.')  #, 'INFO1')
                    if maxres_increased > allow_maxres_inc:
                        casalog.post(
                            'Maximum residual has increased %d times; '
                            'stopping.' % maxres_increased)
                        break

    # finished with all channels: concatenate if there are multiple channels
    if (mode != 'mfs') and (nchan != 1) and (doconcat):
        concat_images(imagename, '.image', nchan)
        concat_images(imagename, '.mask', nchan)
        concat_images(imagename, '.flux', nchan)
        concat_images(imagename, '.psf', nchan)
        concat_images(imagename, '.model', nchan)
        concat_images(imagename, '.residual', nchan)
        concat_regions(imagename, '.rgn', nchan)
        # The individual .channel. tables are no longer needed
        shutil.rmtree(imagename + '.channel.*')

    if (pbcor):
        # user wants primary beam corrected .image file and .residual file
        ia.open(imagename + '.image')
        ia.calc(imagename + '.image/' + imagename + '.flux')
        ia.open(imagename + '.residual')
        ia.calc(imagename + '.residual/' + imagename + '.flux')
        ia.close()
Exemplo n.º 7
0
def widefield(
    vis,
    imagename,
    outlierfile,
    field,
    spw,
    selectdata,
    timerange,
    uvrange,
    antenna,
    scan,
    mode,
    niter,
    gain,
    threshold,
    psfmode,
    ftmachine,
    facets,
    wprojplanes,
    multiscale,
    negcomponent,
    interactive,
    mask,
    nchan,
    start,
    width,
    imsize,
    cell,
    phasecenter,
    restfreq,
    stokes,
    weighting,
    robust,
    npixels,
    noise,
    cyclefactor,
    cyclespeedup,
    npercycle,
    uvtaper,
    outertaper,
    innertaper,
    restoringbeam,
    calready,
):
    """Calculate a wide-field deconvolved image with selected algorithm:

	"""
    ms1 = mstool()
    ###
    # Python script
    try:
        casalog.origin("widefield")
        multims = False
        if type(vis) == list:
            if len(vis) > 1:
                multims = True
            else:
                vis = vis[0]
        multifield = False
        if multims:
            print " multiple ms not handle for now"
            return

        if (type(imagename) == list) & (len(imagename) > 1):
            multifield = True
        else:
            if (type(phasecenter) == list) and (len(phasecenter) > 1):
                raise TypeError, "Number of phasecenters has be equal to number of images"

        ###Set image parameters
        casalog.origin("widefield")
        im1 = imtool()
        if multims:
            imset = cleanhelper()
        else:
            imset = cleanhelper(im1, vis, calready)
        #
        #                if(multims):
        # 			visi=vis[len(vis)-1]
        # 		else:
        # 			visi=vis

        # 		if(multifield):
        # 			if(type(phasecenter) != list):
        # 				raise TypeError, 'Number of phasecenters has be equal to number of images'
        # 			for k in range(len(phasecenter)):
        # 				pc=phasecenter[k]
        # 				phasecenter[k]=test_phasecenter(pc, visi)
        # 		else:
        # 			phasecenter=test_phasecenter(phasecenter, visi)
        #
        # 		spwindex=-1;
        # 		spws=spw
        # 		if(multims & (type(spw)==list)):
        # 			spws=spw[0]
        # 		if( (spws==-1) or (spws=='-1')  or (spws=='') or (spws==[-1])):
        #                        spwindex=-1
        # 			spws=''
        #                else:
        # 			thems=vis
        # 			if(multims):
        # 				thems=vis[0]
        #                        spwindex=ms1.msseltoindex(thems, spw=spws)['spw'].tolist()

        #####
        imageids = []
        imsizes = []
        phasecenters = []
        rootname = ""
        if len(outlierfile) != 0:

            imsizes, phasecenters, imageids = imset.readoutlier(outlierfile)
            if type(rootname) == list:
                rootname = imagename[0]
            else:
                rootname = imagename
            if len(imageids) > 1:
                multifield = True

        else:
            imsizes = imsize
            phasecenters = phasecenter
            imageids = imagename

        imset.definemultiimages(
            rootname,
            imsizes,
            cell,
            stokes,
            mode,
            spw,
            nchan,
            start,
            width,
            restfreq,
            field,
            phasecenters,
            imageids,
            facets,
        )

        ############Data Selection
        if multims:
            if type(spw) == list:
                if len(spw) != len(vis):
                    raise ValueError, "Number of spw selection should match number of data sets"
            if type(field) == list:
                if len(field) != len(vis):
                    raise ValueError, "Number of field selection should match number of data sets"
            for k in range(len(vis)):
                spws = spw
                if type(spw) != list:
                    spws = spw
                else:
                    spws = spw[k]
                fields = field
                if type(field) != list:
                    fields = field
                else:
                    fields = field[k]
                if not os.path.exists(vis[k]):
                    raise Exception, "Visibility data %s set not found - please verify the name" % vis[k]
                im1.selectvis(vis=vis[k], spw=spws, field=fields)
        elif (type(vis) == str) & (os.path.exists(vis)):
            imset.datselweightfilter(
                field=field,
                spw=spw,
                timerange=timerange,
                uvrange=uvrange,
                antenna=antenna,
                scan=scan,
                wgttype=weighting,
                robust=robust,
                noise=noise,
                npixels=npixels,
                mosweight=False,
                innertaper=innertaper,
                outertaper=outertaper,
                calready=calready,
            )
            ############End of Data Selection
        else:
            raise Exception, "Visibility data set not found - please verify the name"

        ###deal with masks
        maskimage = ""
        if (mask == []) or (mask == ""):
            mask = [""]
        if interactive:
            if (mask == "") or (mask == [""]) or (mask == []):
                if type(imagename) == list:
                    maskimage = []
                    for namoo in imagename:
                        maskimage.append(namoo + ".mask")
                        imset.maskimages[namoo] = namoo + ".mask"
                elif (type(imagename) == str) and (len(outlierfile) != 0):
                    maskimage = imagename + ".mask"
                    imset.maskimages[imagename] = imagename + ".mask"

        ####
        padd = 1.0
        if facets > 1:
            padd = 1.2
        im1.setoptions(ftmachine=ftmachine, padding=padd, wprojplanes=wprojplanes)
        # im1.setmfcontrol(scaletype=scaletype,minpb=minpb)
        im1.setmfcontrol(stoplargenegatives=negcomponent, cyclefactor=cyclefactor, cyclespeedup=cyclespeedup)

        ##do themask
        if (maskimage == "") and (mask != [""]):
            maskimage = imset.imagelist[0] + ".mask"

        if not multifield:
            imset.makemaskimage(outputmask=maskimage, imagename=imagename, maskobject=mask)

        else:
            imset.makemultifieldmask(mask)
            maskimage = []
            for k in range(len(imset.maskimages)):
                maskimage.append(imset.maskimages[imset.imagelist[k]])

        imset.setrestoringbeam(restoringbeam)

        alg = "mfclark"
        #####
        ###Determine algorithm to use
        if (psfmode == "clark") or (psfmode == "hogbom"):
            if facets > 1:
                alg = "wf" + psfmode
            else:
                alg = "mf" + psfmode

        if multiscale == [0]:
            multiscale = []
        if (type(multiscale) == list) and (len(multiscale) > 0):
            if facets > 1:
                raise Exception, "multiscale with facets > 1 not allowed for now"
            alg = "mfmultiscale"
            im1.setscales(scalemethod="uservector", uservector=multiscale)

        modelimage = []
        restoredimage = []
        residualimage = []
        for k in range(len(imset.imagelist)):
            modelimage.append(imset.imagelist[k])
            restoredimage.append(imset.imagelist[k] + ".image")
            residualimage.append(imset.imagelist[k] + ".residual")

            ##will have to enable entropy later
        if alg == "mfentropy" or alg == "mfemptiness":
            im1.mem(
                algorithm=alg,
                niter=niter,
                sigma=sigma,
                targetflux=targetflux,
                constrainflux=constrainflux,
                keepfixed=[False],
                prior=prior,
                model=modelimage,
                residual=residualimage,
                image=restoredimage,
                mask=maskimage,
            )
        else:
            im1.clean(
                algorithm=alg,
                niter=niter,
                gain=gain,
                threshold=qa.quantity(threshold, "mJy"),
                model=modelimage,
                residual=residualimage,
                image=restoredimage,
                mask=maskimage,
                interactive=interactive,
                npercycle=npercycle,
            )

        im1.done()
        del im1
        # 	del ms1

    except Exception, instance:
        print "*** Error *** ", instance
        raise Exception, instance
Exemplo n.º 8
0
        'name': 'fr.3l',
        'mode': 'frequency',
        'shift': wid_ms / 3,
        'int': 'linear',
        'desc': 'freq start=%5.3fkHz lin' % (wid_ms / 3)
    }
]

# a subset for the plot
toplot = ['avel', 'fel', 'chan', 'freq', 'fr.5']

if pyonly:
    from cleanhelper import *
    imCln = imtool()
    vis = rt + '.ms'
    imset = cleanhelper(imCln, vis, False, casalog)
    outframe = ''
else:
    # size of image to make
    n = 64

#debug:
#tests=[tests[18]]

j = 0
for te in tests:
    if te.has_key('spw'):
        spw = te['spw']
    else:
        spw = ''
    if te.has_key('nchan'):
    {'name':'frm',  'mode':'frequency','wid':"-%fkHz" % wid_ms,'sta':'%fkHz' % chn_ms, 'desc':'freq wid=-%6.1fKHz' % wid_ms},
    {'name':'freq', 'mode':'frequency',                                'desc':'freq default'},
    {'name':'fr.3', 'mode':'frequency','shift':wid_ms/3,               'desc':'freq start=%5.3fkHz' % (wid_ms/3)},
    {'name':'fr.5', 'mode':'frequency','shift':wid_ms/2,               'desc':'freq start=%5.3fkHz' % (wid_ms/2)},
    {'name':'fr.7', 'mode':'frequency','shift':wid_ms/3*2,             'desc':'freq start=%5.3fkHz' % (wid_ms*2/3)},
    {'name':'fr.3l','mode':'frequency','shift':wid_ms/3,'int':'linear','desc':'freq start=%5.3fkHz lin' % (wid_ms/3)}]


# a subset for the plot
toplot=['avel','fel','chan','freq','fr.5']

if pyonly:
    from cleanhelper import *
    imCln=imtool()
    vis=rt+'.ms'
    imset=cleanhelper(imCln, vis, False, casalog)
    outframe=''
else:
    # size of image to make
    n=64


#debug:
#tests=[tests[18]]


j=0
for te in tests:
    if te.has_key('spw'):
        spw=te['spw']
    else:
Exemplo n.º 10
0
def autoclean(vis, imagename, field, spw, selectdata, timerange, uvrange,
              antenna, scan, mode, nchan, start, width, interpolation, doconcat,
              psfmode, imagermode, cyclefactor, cyclespeedup, imsize, cell,
              phasecenter, restfreq, stokes, weighting, robust, noise, npixels,
              interactive, mask, modelimage, uvtaper, outertaper, innertaper,
              niter, npercycle, npercycle_speedup, gain, pbcor, minpb,
              clean_threshold, Nrms, eps_maxres, useabsresid, allow_maxres_inc,
              island_rms, diag, peak_rms, gain_threshold, Npeak, shape,
              boxstretch, irregsize):

    casalog.origin('autoclean')

    orig_npercycle = npercycle

    # set up initial mask values if input by user
    inputmask = None
    if(mask):
        casalog.post('Using input mask from user.')
        imCln = imtool()
        imset = cleanhelper(imCln, vis)
        imset.defineimages(imsize=imsize, cell=cell, stokes=stokes,
                           mode=mode, spw=spw, nchan=1,
                           start=start,  width=width,
                           restfreq=restfreq, field=field,
                           phasecenter=phasecenter)
        imset.datselweightfilter(field=field, spw=spw, timerange=timerange,
                                 uvrange=uvrange, antenna=antenna, scan=scan,
                                 wgttype=weighting, robust=robust, noise=noise,
                                 npixels=npixels,
                                 #mosweight=mosweight,
                                 innertaper=innertaper, outertaper=outertaper,
                                 calready=False)
        inputmask = '__temporary.mask'
        imset.makemaskimage(outputmask=inputmask, maskobject=mask)
        imCln.close()
        ia.open(inputmask)
        maskVals = ia.getchunk()
        ia.close()
        ia.removefile(inputmask)

    # begin loop over all channels
    for ichan in xrange(nchan):

        if imagermode!='mfs':
            casalog.post('***** Beginning channel %d of %d' % (ichan, nchan))

        if (mode=='mfs') or (nchan==1):
            thisImage = imagename
            thisStart = start
        else:
            thisImage = imagename + '.channel.' + str(ichan)
            if mode=='channel':
                thisStart = start + ichan*width
            else:
                thisStart = qa.add(start, qa.mul(width, qa.quantity(ichan)))

        # create dirty image from which to choose initial clean regions
        clean(vis=vis, imagename=thisImage, field=field, spw=spw, 
              selectdata=selectdata, timerange=timerange, uvrange=uvrange,
              antenna=antenna, scan=scan, mode=mode, nchan=1, start=thisStart,
              width=width, interpolation=interpolation, psfmode=psfmode,
              imagermode=imagermode, cyclefactor=cyclefactor,
              cyclespeedup=cyclespeedup, imsize=imsize, cell=cell,  
              phasecenter=phasecenter, restfreq=restfreq, stokes=stokes,
              weighting=weighting, robust=robust, noise=noise, npixels=npixels,
              modelimage=modelimage, uvtaper=uvtaper, outertaper=outertaper,
              innertaper=innertaper, niter=0, gain=gain, minpb=minpb)
        casalog.origin('autoclean')

        # make the mask image
        ia_tool = ia.newimagefromimage(infile=thisImage+'.image',
                                       outfile=thisImage+'.mask',
                                       overwrite=True)
        if(mask):
            ia_tool.putchunk(pixels=maskVals)
        else:
            ia_tool.set(pixels=0.0)
        ia_tool.close()

        # keep track of changes in maximum residual over iterations
        maxres_increased = 0
        post_max = 5000
        # iterations variable stores number of clean internal iterations
        iterations = 0
        # new_npercycle: number of minor cycles this iteration (can change)
        new_npercycle = orig_npercycle
        while iterations < niter:
            cleanmore, nregions = \
                       autowindow(imagename=thisImage, Npeak=Npeak,
                                  shape=shape, irregsize=irregsize,
                                  boxstretch=boxstretch, Nrms=Nrms,
                                  island_rms=island_rms, peak_rms=peak_rms,
                                  gain_threshold=gain_threshold,
                                  clean_threshold=clean_threshold, diag=diag,
                                  useabsresid=useabsresid, ichan=ichan) 

            # if first iteration, make sure CLEAN is cleaning
            if not(cleanmore):
                if not(iterations):
                    casalog.post('Input parameters do not induce cleaning of '
                                 'channel %d.' % ichan)
                break

            # did autowindow make new clean regions?
            if(nregions):
                new_npercycle = orig_npercycle
            else:
                casalog.post('Will continue cleaning with current clean regions.')
                new_npercycle = int(round(new_npercycle * npercycle_speedup))

            # maximum absolute residual before next clean iteration
            if (eps_maxres) or (allow_maxres_inc >= 0):
                pre_max = post_max
                
            # niter-iterations is the *total* number of iterations left, and might
            # be less than npercycle.  Use the minimum of (npercycle, niter-iterations)
            casalog.post('Starting clean at iteration %d' % iterations)

            #TMP# CAS-1623: an improvement will be made to CLEAN to allow it to
            # start from an existing residual image.  The call to clean may need
            # to be changed to reflect this.
            clean(vis=vis, imagename=thisImage, field=field, spw=spw, 
                  selectdata=selectdata, timerange=timerange, uvrange=uvrange,
                  antenna=antenna, scan=scan, mode=mode, nchan=1, width=width, 
                  start=thisStart, interpolation=interpolation, psfmode=psfmode,
                  imagermode=imagermode, imsize=imsize,
                  cyclefactor=cyclefactor, cyclespeedup=cyclespeedup,
                  cell=cell, phasecenter=phasecenter, restfreq=restfreq,
                  stokes=stokes, weighting=weighting, robust=robust,
                  noise=noise, npixels=npixels, modelimage=modelimage,
                  uvtaper=uvtaper, outertaper=outertaper, innertaper=innertaper,
                  niter=min(new_npercycle,niter-iterations),
                  mask=thisImage+'.mask',  gain=gain, minpb=minpb, npercycle=niter,
                  interactive=bool(nregions and interactive))
            casalog.origin('autoclean')
            iterations += new_npercycle

            # stop if we've reached maximum number of clean minor cycles
            if iterations >= niter:
                casalog.post('Reached maximum number of CLEAN cycles (%d)' % niter)
                break

            # check maximum residual value
            if (eps_maxres) or (allow_maxres_inc >= 0):
                residualImage = thisImage+'.residual'
                ia.open(residualImage)
                stats = ia.statistics(list=False, verbose=True)
                ia.close()
                if(useabsresid):
                    post_max = max([ stats['max'][0], abs(stats['min'][0]) ])
                else:
                    post_max = stats['max'][0]
                # check fractional change of maximum residual
                if(eps_maxres):
                    if abs(post_max-pre_max)/pre_max < eps_maxres:
                        casalog.post('Maximum residual has changed by less '
                                     'than %.3f; stopping' % eps_maxres)
                        break
                if post_max >= pre_max:
                    maxres_increased += 1
                    casalog.post('Maximum residual has increased.')#, 'INFO1')
                    if maxres_increased > allow_maxres_inc:
                        casalog.post('Maximum residual has increased %d times; '
                                     'stopping.' % maxres_increased)
                        break

    # finished with all channels: concatenate if there are multiple channels
    if (mode!='mfs') and (nchan!=1) and (doconcat):
        concat_images(imagename, '.image', nchan)
        concat_images(imagename, '.mask', nchan)
        concat_images(imagename, '.flux', nchan)
        concat_images(imagename, '.psf', nchan)
        concat_images(imagename, '.model', nchan)
        concat_images(imagename, '.residual', nchan)
        concat_regions(imagename, '.rgn', nchan)
        # The individual .channel. tables are no longer needed
        shutil.rmtree(imagename + '.channel.*')

    if(pbcor):
        # user wants primary beam corrected .image file and .residual file
        ia.open(imagename+'.image')
        ia.calc(imagename+'.image/'+imagename+'.flux')
        ia.open(imagename+'.residual')
        ia.calc(imagename+'.residual/'+imagename+'.flux')
        ia.close()