Пример #1
0
    def setup_imager(self, imagename=''):
        """
        Cube imaging for major cycles.
        """
        params = ImagerParameters(msname=self.vis,
                                  field=self.field,
                                  spw=self.spw,
                                  imagename=imagename,
                                  imsize=self.imsize,
                                  cell=self.cell,
                                  phasecenter=self.phasecenter,
                                  weighting=self.weighting,
                                  gridder=self.gridder,
                                  pblimit=self.pblimit,
                                  wprojplanes=self.wprojplanes,
                                  specmode='cube',
                                  nchan=self.nchan,
                                  reffreq=self.reffreq,
                                  width=self.width,
                                  interpolation=self.interpolation,
                                  deconvolver='hogbom',
                                  niter=0,
                                  wbawp=True)

        self.imagertool = PySynthesisImager(params=params)
        self.imagertool.initializeImagers()
        self.imagertool.initializeNormalizers()
        self.imagertool.setWeighting()

        self.imagertool.makePSF()
        self.imagertool.makePB()
        self.imagertool.runMajorCycle()

        self.lib.copy_restoringbeam(fromthis=imagename + '.psf',
                                    tothis=imagename + '.residual')
Пример #2
0
    def setup_deconvolver(self,imagename=''):
        """
        Cube or MFS minor cycles. 
        """
        params = ImagerParameters(msname=self.vis, field=self.field,spw=self.spw,timestr=self.timestr,
                                  imagename=imagename,
                                  imsize=self.imsize, cell=self.cell, phasecenter=self.phasecenter, 
                                  weighting=self.weighting,
                                  gridder=self.gridder, pblimit=self.pblimit,wprojplanes=self.wprojplanes,
                                  specmode=self.specmode,nchan=self.nchan,start=self.start, 
                                  reffreq=self.reffreq, width=self.width,interpolation=self.interpolation,
                                  deconvolver=self.deconvolver, scales=self.scales,nterms=self.nterms,
                                  niter=self.niter,cycleniter=self.cycleniter, threshold=self.threshold,
                                  mask=self.mask,interactive=False,datacolumn=self.datacolumn)
        
        self.deconvolvertool = PySynthesisImager(params=params)

        ## Why are we initializing these ? 
        self.deconvolvertool.initializeImagers()
        self.deconvolvertool.initializeNormalizers()
        self.deconvolvertool.setWeighting()

        
        ### These three should be unncessary.  Need a 'makeimage' method for csys generation. 
        self.deconvolvertool.makePSF() ## Make this to get a coordinate system
        self.deconvolvertool.makePB()  ## Make this to turn .weight into .pb maps
        self.deconvolvertool.runMajorCycle() ## Make this to make template residual images.

        ## Initialize deconvolvers. ( Order is important. This cleans up a leftover tablecache image.... FIX!)
        self.deconvolvertool.initializeDeconvolvers()
        self.deconvolvertool.initializeIterationControl()
Пример #3
0
    def __init__(self, params):

        PySynthesisImager.__init__(self, params)

        self.PH = PyParallelImagerHelper()
        self.NF = len(allimpars.keys())
        self.listOfNodes = self.PH.getNodeList()
        #### MPIInterface related changes
        #self.NN = self.PH.NN
        self.NN = len(self.listOfNodes)
        if self.NF != self.NN:
            print 'For now, cannot handle nfields != nnodes. Will implement round robin allocation later.'
            print 'Using only ', self.NN, ' fields and nodes'
    def __init__(self, params=None):

        PySynthesisImager.__init__(self, params)

        self.PH = PyParallelImagerHelper()
        self.NN = self.PH.NN
        self.selpars = self.allselpars
        self.allselpars = self.PH.partitionContDataSelection(self.allselpars)
        # self.allcflist = self.PH.partitionCFCacheList(self.cfcachepars['cflist']);
        # self.allcflist = self.PH.partitionCFCacheList(self.allgridpars['0']);
        self.listOfNodes = self.PH.getNodeList()
        self.coordsyspars = {}
        self.toolsi = None
Пример #5
0
def tclean(
        ####### Data Selection
        vis,  #='', 
        selectdata,
        field,  #='', 
        spw,  #='',
        timerange,  #='',
        uvrange,  #='',
        antenna,  #='',
        scan,  #='',
        observation,  #='',
        intent,  #='',
        datacolumn,  #='corrected',

        ####### Image definition
    imagename,  #='',
        imsize,  #=[100,100],
        cell,  #=['1.0arcsec','1.0arcsec'],
        phasecenter,  #='J2000 19:59:28.500 +40.44.01.50',
        stokes,  #='I',
        projection,  #='SIN',
        startmodel,  #='',

        ## Spectral parameters
    specmode,  #='mfs',
        reffreq,  #='',
        nchan,  #=1,
        start,  #='',
        width,  #='',
        outframe,  #='LSRK',
        veltype,  #='',
        restfreq,  #=[''],
        #    sysvel,#='',
        #    sysvelframe,#='',
    interpolation,  #='',
        ##
        ####### Gridding parameters
    gridder,  #='ft', 
        facets,  #=1,
        chanchunks,  #=1,
        wprojplanes,  #=1,

        ### PB
    vptable,
        aterm,  #=True,
        psterm,  #=True,
        wbawp,  #= True,
        conjbeams,  #= True,
        cfcache,  #= "",
        computepastep,  #=360.0,
        rotatepastep,  #=360.0,
        pblimit,  #=0.01,
        normtype,  #='flatnoise',

        ####### Deconvolution parameters
    deconvolver,  #='hogbom',
        scales,  #=[],
        nterms,  #=1,
        smallscalebias,  #=0.6

        ### restoration options
    restoration,
        restoringbeam,  #=[],
        pbcor,

        ##### Outliers
        outlierfile,  #='',

        ##### Weighting
    weighting,  #='natural',
        robust,  #=0.5,
        npixels,  #=0,
        #    uvtaper,#=False,
    uvtaper,  #=[],

        ##### Iteration control
    niter,  #=0, 
        gain,  #=0.1,
        threshold,  #=0.0, 
        cycleniter,  #=0, 
        cyclefactor,  #=1.0,
        minpsffraction,  #=0.1,
        maxpsffraction,  #=0.8,
        interactive,  #=False, 

        ##### (new) Mask parameters
    usemask,  #='user',
        mask,  #='',
        pbmask,  #='',
        maskthreshold,  #='',
        maskresolution,  #='',
        nmask,  #=0,

        ##### automask by multithresh
    sidelobethreshold,  #=5.0,
        noisethreshold,  #=3.0,
        lownoisethreshold,  #=3.0,
        negativethreshold,  #=0.0,
        smoothfactor,  #=1.0,
        minbeamfrac,  #=0.3, 
        cutthreshold,  #=0.01,
        growiterations,  #=100

        ## Misc
    restart,  #=True,
        savemodel,  #="none",

        #    makeimages,#="auto"
    calcres,  #=True,
        calcpsf,  #=True,

        ####### State parameters
    parallel):  #=False):

    #####################################################
    #### Sanity checks and controls
    #####################################################

    ### Move these checks elsewhere ?

    if specmode == 'cont':
        specmode = 'mfs'

    if specmode == 'mfs' and nterms == 1 and deconvolver == "mtmfs":
        casalog.post(
            "The MTMFS deconvolution algorithm (deconvolver='mtmfs') needs nterms>1.Please set nterms=2 (or more). ",
            "WARN", "task_tclean")
        return

    if specmode != 'mfs' and deconvolver == "mtmfs":
        casalog.post(
            "The MSMFS algorithm (deconvolver='mtmfs') applies only to specmode='mfs'.",
            "WARN", "task_tclean")
        return

    #####################################################
    #### Construct ImagerParameters object
    #####################################################

    imager = None
    paramList = None

    # Put all parameters into dictionaries and check them.
    paramList = ImagerParameters(
        msname=vis,
        field=field,
        spw=spw,
        timestr=timerange,
        uvdist=uvrange,
        antenna=antenna,
        scan=scan,
        obs=observation,
        state=intent,
        datacolumn=datacolumn,

        ### Image....
        imagename=imagename,
        #### Direction Image Coords
        imsize=imsize,
        cell=cell,
        phasecenter=phasecenter,
        stokes=stokes,
        projection=projection,
        startmodel=startmodel,

        ### Spectral Image Coords
        specmode=specmode,
        reffreq=reffreq,
        nchan=nchan,
        start=start,
        width=width,
        outframe=outframe,
        veltype=veltype,
        restfreq=restfreq,
        sysvel='',  #sysvel,
        sysvelframe='',  #sysvelframe,
        interpolation=interpolation,
        gridder=gridder,
        #        ftmachine=ftmachine,
        facets=facets,
        chanchunks=chanchunks,
        wprojplanes=wprojplanes,
        vptable=vptable,

        ### Gridding....
        aterm=aterm,
        psterm=psterm,
        wbawp=wbawp,
        cfcache=cfcache,
        conjbeams=conjbeams,
        computepastep=computepastep,
        rotatepastep=rotatepastep,
        pblimit=pblimit,
        normtype=normtype,
        outlierfile=outlierfile,
        restart=restart,
        weighting=weighting,
        robust=robust,
        npixels=npixels,
        uvtaper=uvtaper,

        ### Deconvolution
        niter=niter,
        cycleniter=cycleniter,
        loopgain=gain,
        threshold=threshold,
        cyclefactor=cyclefactor,
        minpsffraction=minpsffraction,
        maxpsffraction=maxpsffraction,
        interactive=interactive,
        deconvolver=deconvolver,
        scales=scales,
        nterms=nterms,
        scalebias=smallscalebias,
        restoringbeam=restoringbeam,

        ### new mask params
        usemask=usemask,
        mask=mask,
        pbmask=pbmask,
        maskthreshold=maskthreshold,
        maskresolution=maskresolution,
        nmask=nmask,

        ### automask multithresh params
        sidelobethreshold=sidelobethreshold,
        noisethreshold=noisethreshold,
        lownoisethreshold=lownoisethreshold,
        negativethreshold=negativethreshold,
        smoothfactor=smoothfactor,
        minbeamfrac=minbeamfrac,
        cutthreshold=cutthreshold,
        growiterations=growiterations,
        savemodel=savemodel)

    #paramList.printParameters()

    pcube = False
    concattype = ''
    if parallel == True and specmode != 'mfs':
        pcube = True
        parallel = False

    # catch non operational case (parallel cube tclean with interative=T)
    if pcube and interactive:
        casalog.post(
            "Interactive mode is not currently supported with parallel cube CLEANing, please restart by setting interactive=F",
            "WARN", "task_tclean")
        return False

    ## Setup Imager objects, for different parallelization schemes.
    if parallel == False and pcube == False:

        imager = PySynthesisImager(params=paramList)
    elif parallel == True:
        imager = PyParallelContSynthesisImager(params=paramList)
    elif pcube == True:
        imager = PyParallelCubeSynthesisImager(params=paramList)
        # virtualconcat type - changed from virtualmove to virtualcopy 2016-07-20
        concattype = 'virtualcopy'
    else:
        print 'Invalid parallel combination in doClean.'
        return False

    retrec = {}

    try:
        #if (1):
        ## Init major cycle elements
        t0 = time.time()
        imager.initializeImagers()

        # Construct the CFCache for AWProject-class of FTMs.  For
        # other choices the following three calls become NoOps.
        # imager.dryGridding();
        # imager.fillCFCache();
        # imager.reloadCFCache();

        imager.initializeNormalizers()
        imager.setWeighting()
        t1 = time.time()
        casalog.post(
            "***Time for initializing imager and normalizers: " + "%.2f" %
            (t1 - t0) + " sec", "INFO3", "task_tclean")

        ## Init minor cycle elements
        if niter > 0 or restoration == True:
            t0 = time.time()
            imager.initializeDeconvolvers()
            t1 = time.time()
            casalog.post(
                "***Time for initializing deconvolver(s): " + "%.2f" %
                (t1 - t0) + " sec", "INFO3", "task_tclean")

        if niter > 0:
            t0 = time.time()
            imager.initializeIterationControl()
            t1 = time.time()
            casalog.post(
                "***Time for initializing iteration controller: " + "%.2f" %
                (t1 - t0) + " sec", "INFO3", "task_tclean")

        ## Make PSF
        if calcpsf == True:
            t0 = time.time()

            imager.makePSF()

            t1 = time.time()
            casalog.post(
                "***Time for making PSF: " + "%.2f" % (t1 - t0) + " sec",
                "INFO3", "task_tclean")

            imager.makePB()

            t2 = time.time()
            casalog.post(
                "***Time for making PB: " + "%.2f" % (t2 - t1) + " sec",
                "INFO3", "task_tclean")

        if niter >= 0:

            ## Make dirty image
            if calcres == True:
                t0 = time.time()
                imager.runMajorCycle()
                t1 = time.time()
                casalog.post(
                    "***Time for major cycle (calcres=T): " + "%.2f" %
                    (t1 - t0) + " sec", "INFO3", "task_tclean")

            ## In case of no deconvolution iterations....
            if niter == 0 and calcres == False:
                if savemodel != "none":
                    imager.predictModel()

            ## Do deconvolution and iterations
            if niter > 0:

                isit = imager.hasConverged()
                imager.updateMask()

                while (not imager.hasConverged()):

                    #                    maskchanged = imager.updateMask()
                    #                    if maskchanged and imager.hasConverged() :
                    #                        break;

                    t0 = time.time()
                    imager.runMinorCycle()
                    t1 = time.time()
                    casalog.post(
                        "***Time for minor cycle: " + "%.2f" % (t1 - t0) +
                        " sec", "INFO3", "task_tclean")

                    t0 = time.time()
                    imager.runMajorCycle()
                    t1 = time.time()
                    casalog.post(
                        "***Time for major cycle: " + "%.2f" % (t1 - t0) +
                        " sec", "INFO3", "task_tclean")

                    imager.updateMask()

                ## Get summary from iterbot
                if type(interactive) != bool:
                    retrec = imager.getSummary()

            ## Restore images.
            if restoration == True:
                t0 = time.time()
                imager.restoreImages()
                t1 = time.time()
                casalog.post(
                    "***Time for restoring images: " + "%.2f" % (t1 - t0) +
                    " sec", "INFO3", "task_tclean")
                if pbcor == True:
                    t0 = time.time()
                    imager.pbcorImages()
                    t1 = time.time()
                    casalog.post(
                        "***Time for pb-correcting images: " + "%.2f" %
                        (t1 - t0) + " sec", "INFO3", "task_tclean")

        if (pcube):
            print "running concatImages ..."
            casalog.post(
                "Running virtualconcat (type=%s) of sub-cubes" % concattype,
                "INFO2", "task_tclean")
            # fixed to move subcubes
            imager.concatImages(type=concattype)

        ## Close tools.
        imager.deleteTools()

    except Exception as e:
        #print 'Exception : ' + str(e)
        casalog.post('Exception from task_tclean : ' + str(e), "SEVERE",
                     "task_tclean")
        if imager != None:
            imager.deleteTools()

        larg = list(e.args)
        larg[0] = 'Exception from task_tclean : ' + str(larg[0])
        e.args = tuple(larg)
        raise

    return retrec
Пример #6
0
class SDINT_imager:
    """
    Joint imaging of Single Dish and Interferometer Data

    Use Cases :
        Input Data : Joint SD+INT data,  SD-only data,  INT-only data (i.e. tclean)
        Spectral Modes : Spectral Cubes and Continuum (Multi-term) 

    Image Names : 
        Interferometer images : imagename.int.{residual,psf,image}.* 
        Single Dish images : imagename.sd.{residual,psf}
        Joint images : imagename.joint.{residual,psf}

    """

    def __init__(self,

                  ## Data Selection - Interferometer
                 vis='Data/mysky.ms',
                 field='',
                 spw='',
                 timestr='',
                 datacolumn='corrected',
                 start='',
                  
                 
                 
                 ## Single Dish Data - Images
                 sdimage='',
                 sdpsf='',
                 sdgain=1.0,
                 dishdia=100.0,

                 ## Pick what data to use. 'int' or 'sd' or 'sdint'
                 usedata = 'sdint',
                 ## Image Definition
                 imagename='',
                 imsize=512,
                 cell='10.0arcsec',
                 phasecenter='',
                 weighting='natural',
                 specmode='mfs',

                 ## Gridding/Imaging options for Cubes
                 gridder='mosaic',
                 nchan=128,
                 reffreq='6.0GHz',
                 width = '16MHz',
                 pblimit=0.2,
                 interpolation='nearest',
                 wprojplanes=128,

                 ## Imaging/Deconvolution options for Multi-term continuum.
                 deconvolver='mtmfs',
                 scales=[],
                 nterms=2,
                 pbmask=0.2,

                 ## Iteration control
                 niter=10,
                 cycleniter=200,
                 threshold='',
                 mask='' ):

        ## Data Selection - Interferometer
        self.vis=vis
        self.field=field
        self.spw=spw
        self.timestr=timestr
        self.datacolumn=datacolumn

        ## Single Dish Data - Images
        self.sdimage=sdimage
        self.sdpsf=sdpsf
        self.sdgain=sdgain
        self.dishdia=dishdia

        ## What mode to run in
        self.usedata=usedata

        ## Image Definition
        self.imagename=imagename
        self.imsize=imsize
        self.cell=cell
        self.phasecenter=phasecenter
        self.weighting=weighting
        self.specmode=specmode

        ## Gridding/Imaging options for Cubes
        self.gridder=gridder
        self.nchan=nchan
        self.reffreq=reffreq
        self.width = width
        self.pblimit=pblimit
        self.interpolation=interpolation
        self.wprojplanes=wprojplanes
        self.start=start

        ## Imaging/Deconvolution options for Multi-term continuum.
        self.deconvolver=deconvolver
        self.scales=scales
        self.nterms=nterms
        self.pbmask=pbmask
        
        ## Iteration control
        self.niter=niter
        self.cycleniter=cycleniter
        self.threshold=threshold
        self.mask=mask

        ############# Internal variables
        self.imagertool  = None
        self.deconvolvertool = None
        self.lib = SDINT_helper()
        
        self.applypb=False
        if self.gridder=='mosaic' or gridder=='awproject':
            self.applypb=True
 
    def setup_imager(self,imagename=''):
        """
        Cube imaging for major cycles.
        """
        params = ImagerParameters(msname=self.vis, field=self.field,spw=self.spw,timestr=self.timestr,
                                  imagename=imagename,
                                  imsize=self.imsize, cell=self.cell, phasecenter=self.phasecenter, 
                                  weighting=self.weighting,
                                  gridder=self.gridder, pblimit=self.pblimit,wprojplanes=self.wprojplanes,
                                  specmode='cube',nchan=self.nchan,start=self.start, 
                                  reffreq=self.reffreq, width=self.width, interpolation=self.interpolation,
                                  deconvolver='hogbom', niter=0,
                                  wbawp=True,datacolumn=self.datacolumn)
        
        self.imagertool = PySynthesisImager(params=params)
        self.imagertool.initializeImagers()
        self.imagertool.initializeNormalizers()
        self.imagertool.setWeighting()

        self.imagertool.makePSF()
        self.imagertool.makePB()
        self.imagertool.runMajorCycle()
        
        self.lib.copy_restoringbeam(fromthis=imagename+'.psf', tothis=imagename+'.residual')


    def setup_deconvolver(self,imagename=''):
        """
        Cube or MFS minor cycles. 
        """
        params = ImagerParameters(msname=self.vis, field=self.field,spw=self.spw,timestr=self.timestr,
                                  imagename=imagename,
                                  imsize=self.imsize, cell=self.cell, phasecenter=self.phasecenter, 
                                  weighting=self.weighting,
                                  gridder=self.gridder, pblimit=self.pblimit,wprojplanes=self.wprojplanes,
                                  specmode=self.specmode,nchan=self.nchan,start=self.start, 
                                  reffreq=self.reffreq, width=self.width,interpolation=self.interpolation,
                                  deconvolver=self.deconvolver, scales=self.scales,nterms=self.nterms,
                                  niter=self.niter,cycleniter=self.cycleniter, threshold=self.threshold,
                                  mask=self.mask,interactive=False,datacolumn=self.datacolumn)
        
        self.deconvolvertool = PySynthesisImager(params=params)

        ## Why are we initializing these ? 
        self.deconvolvertool.initializeImagers()
        self.deconvolvertool.initializeNormalizers()
        self.deconvolvertool.setWeighting()

        
        ### These three should be unncessary.  Need a 'makeimage' method for csys generation. 
        self.deconvolvertool.makePSF() ## Make this to get a coordinate system
        self.deconvolvertool.makePB()  ## Make this to turn .weight into .pb maps
        self.deconvolvertool.runMajorCycle() ## Make this to make template residual images.

        ## Initialize deconvolvers. ( Order is important. This cleans up a leftover tablecache image.... FIX!)
        self.deconvolvertool.initializeDeconvolvers()
        self.deconvolvertool.initializeIterationControl()

    
    def setup_sdimaging(self,template='',output=''):
        """
        Make the SD cube Image and PSF

        Option 1 : Use/Regrid cubes for the observed image and PSF
        Option 2 : Make the SD image and PSF cubes using 'tsdimager's usage of the SD gridder option.

        Currently, only Option 1 is supported. 

        """
        
        ## Regrid the input SD image and PSF cubes to the target coordinate system. 
        imregrid(imagename=self.sdpsf, template=template+'.psf', 
                 output=output+'.psf',overwrite=True,axes=[0,1],asvelocity=False)
        imregrid(imagename=self.sdimage, template=template+'.residual', 
                 output=output+'.residual',overwrite=True,axes=[0,1],asvelocity=False)
        imregrid(imagename=self.sdimage, template=template+'.residual', 
                 output=output+'.image',overwrite=True,axes=[0,1],asvelocity=False)

        ## Apply the pbmask from the INT image cube, to the SD cubes.
        #TTB: Create *.mask cube  

        self.lib.addmask(inpimage=output+'.residual', pbimage=template+'.pb', pblimit=self.pblimit)
        self.lib.addmask(inpimage=output+'.image', pbimage=template+'.pb', pblimit=self.pblimit)


    def do_reconstruct(self):

        ## Image names
        int_cube = self.imagename+'.int.cube'
        sd_cube = self.imagename+'.sd.cube'
        joint_cube = self.imagename+'.joint.cube'
        joint_multiterm = self.imagename+'.joint.multiterm'

        if self.specmode=='mfs':
            decname = joint_multiterm
        else:
            decname = joint_cube

        ## Initialize INT_imager, JOINT_deconvolver, SD_imager
        self.setup_imager(imagename=int_cube)
        self.setup_deconvolver(imagename=decname)
        self.setup_sdimaging(template=int_cube, output=sd_cube)

        ## Feather INT and SD residual images (feather in flat-sky. output has common PB)
        self.feather_residual(int_cube, sd_cube, joint_cube)
 
        ## Feather INT and SD psfs
        self.lib.feather_int_sd(sdcube=sd_cube+'.psf', 
                                intcube=int_cube+'.psf', 
                                jointcube=joint_cube+'.psf',
                                sdgain=self.sdgain,
                                dishdia=self.dishdia,
                                usedata=self.usedata)

        if self.specmode=='mfs':
            ## Calculate Spectral PSFs and Taylor Residuals
            self.lib.cube_to_taylor_sum(cubename=joint_cube+'.psf', 
                                        mtname=joint_multiterm+'.psf',
                                        nterms=self.nterms, reffreq=self.reffreq, dopsf=True)
            self.lib.cube_to_taylor_sum(cubename=joint_cube+'.residual', 
                                        mtname=joint_multiterm+'.residual',
                                        nterms=self.nterms, reffreq=self.reffreq, dopsf=False)
        
        ## Check for deconvolver convergence criteria
        self.deconvolvertool.hasConverged()
        self.deconvolvertool.updateMask()

        ## Start image reconstruction loops
        while ( not self.deconvolvertool.hasConverged() ):
            ## Run the deconvolver. It produces a joint model of the sky x fixed PB
            self.deconvolvertool.runMinorCycle()
            
            ## Prepare the joint model cube for INT and SD major cycles
            if self.specmode=='mfs':
                ## Convert Taylor model coefficients into a model cube : int_cube.model
                self.lib.taylor_model_to_cube(cubename=int_cube, ## output 
                                              mtname=joint_multiterm,  ## input
                                              nterms=self.nterms, reffreq=self.reffreq)
            else:
                ## Copy the joint_model cube to the int_cube.model
                shutil.rmtree(int_cube+'.model',ignore_errors=True)
                shutil.copytree(joint_cube+'.model', int_cube+'.model')


            if self.applypb==True:
                ## Take the int_cube.model to flat sky. 
                self.lib.modify_with_pb(inpcube=int_cube+'.model', 
                                        pbcube=int_cube+'.pb', 
                                        action='div', pblimit=self.pblimit,freqdep=True)
                                        #action='div', pblimit=self.pblimit,freqdep=False)

            ## copy the int_cube.model to the sd_cube.model
            shutil.rmtree(sd_cube+'.model',ignore_errors=True)
            shutil.copytree(int_cube+'.model', sd_cube+'.model')

            if self.applypb==True:
                ## Multiply flat-sky model with freq-dep PB
                self.lib.modify_with_pb(inpcube=int_cube+'.model', 
                                        pbcube=int_cube+'.pb', 
                                        action='mult', pblimit=self.pblimit, freqdep=True)
            ## Major cycle for interferometer data 
            self.imagertool.runMajorCycle()

            ## Major cycle for Single Dish data (uses the flat sky cube model in sd_cube.model )
            self.lib.calc_sd_residual(origcube=sd_cube+'.image', 
                                      modelcube=sd_cube+'.model', 
                                      residualcube=sd_cube+'.residual',  ## output
                                      psfcube=sd_cube+'.psf')

            ## Feather the residuals
            self.feather_residual(int_cube, sd_cube, joint_cube)

            if self.specmode=='mfs':
                ## Calculate Spectral Taylor Residuals
                self.lib.cube_to_taylor_sum(cubename=joint_cube+'.residual', 
                                            mtname=joint_multiterm+'.residual',
                                            nterms=self.nterms, reffreq=self.reffreq, dopsf=False)
 

            self.deconvolvertool.updateMask()
        
            print 'Finished a major/minor cycle'

        self.deconvolvertool.restoreImages()

        self.closeTools()

        ## PBcor for the intensity image.
        if self.applypb==True:
            if self.specmode=='mfs':
                impbcor(imagename=decname+'.image.tt0' ,  pbimage=decname+'.pb.tt0' , mode='divide', cutoff=self.pblimit,outfile=decname+'.image.tt0.pbcor')
            else:
                self.lib.modify_with_pb(inpcube=joint_cube+'.image', 
                                        pbcube=int_cube+'.pb', 
                                        action='div', pblimit=self.pblimit, freqdep=False)

        return decname

    
    def feather_residual(self,int_cube, sd_cube, joint_cube):
       
        if self.applypb==True:
            ## Take initial INT_dirty image to flat-sky. 
            self.lib.modify_with_pb(inpcube=int_cube+'.residual', 
                                    pbcube=int_cube+'.pb', 
                                    action='div',
                                    pblimit=self.pblimit,
                                    freqdep=True)
            
        ## Feather flat-sky INT dirty image with SD image
        self.lib.feather_int_sd(sdcube=sd_cube+'.residual', 
                                intcube=int_cube+'.residual', 
                                jointcube=joint_cube+'.residual', ## output
                                sdgain=self.sdgain,
                                dishdia=self.dishdia,
                                usedata=self.usedata)

        #stop This stop gives us data in joint cube residual
        #Following chunk of code might be the culprit!

        if self.applypb==True:
            ## Multiply new JOINT dirty image by a common PB to get the effect of conjbeams. 
            self.lib.modify_with_pb(inpcube=joint_cube+'.residual', 
                                pbcube=int_cube+'.pb', 
                                action='mult',
                                pblimit=self.pblimit,
                                freqdep=True)


    def closeTools(self):
        """
        Close the PySynthesisImagers
        """
        self.imagertool.deleteTools()
        self.deconvolvertool.deleteTools()
def init_cube_imager(params):
    """
    Initialize the Imager module and create a dirty map to use as the first
    residual.

    Parameters
    ----------
    params : imagerhelpers.input_parameters.ImagerParameters

    Returns
    -------
    imager : imagerhelpers.imager_base.PySynthesisImager
    """
    log_post(':: Initializing cube imager')
    # FIXME replace with PyParallelCubeSynthesisImager
    imager = PySynthesisImager(params=params)
    imagename = imager.allimpars['0']['imagename']
    remove_all_extensions(imagename)
    # Initialize the major cycle modules
    imager.initializeImagers()
    imager.initializeNormalizers()
    imager.setWeighting()
    imager.initializeDeconvolvers()
    imager.initializeIterationControl()
    # Initialize the minor cycle modules
    imager.makePSF()
    imager.makePB()
    # Create initial dirty image and residual image
    imager.runMajorCycle()
    return imager
Пример #8
0
csys = ia.coordsys().torecord()
ia.close()
ia.open('pybdsf_mask_casa')
mask_shp = ia.shape()
mask_csys = ia.coordsys().torecord()
if np.all(shp == mask_shp):
    ia.setcoordsys(csys)
ia.close()
##########~~~~~~~ END PYBDSF MASKING ~~~~~~~~~~~~~~~~~#########

#RESTART PYSYNTHESIS
paramList = make_paramList(niter=1000, mask='niter0_short.mask')

## (3) Construct the PySynthesisImager object, with all input parameters

imager = PySynthesisImager(params=paramList)

## Init minor cycle modules

imager.initializeDeconvolvers()
imager.initializeIterationControl()

imager.updateMask()

## (7) Run the iteration loops
while (not imager.hasConverged()):
    imager.runMinorCycle()

## (8) Finish up
imager.restoreImages()
Пример #9
0
def tsdimaging(infiles, outfile, overwrite, field, spw, antenna, scan, intent,
               mode, nchan, start, width, veltype, outframe, gridfunction,
               convsupport, truncate, gwidth, jwidth, imsize, cell,
               phasecenter, projection, ephemsrcname, pointingcolumn, restfreq,
               stokes, minweight, brightnessunit, clipminmax):

    origin = 'tsdimaging'
    imager = None

    try:
        # if spw starts with ':', add '*' at the beginning
        if isinstance(spw, str):
            _spw = '*' + spw if spw.startswith(':') else spw
        else:
            _spw = ['*' + v if v.startswith(':') else v for v in spw]

        # if antenna doesn't contain '&&&', append it
        def antenna_to_baseline(s):
            if len(s) == 0:
                return s
            elif len(s) > 3 and s.endswith('&&&'):
                return s
            else:
                return '{0}&&&'.format(s)

        if isinstance(antenna, str):
            baseline = antenna_to_baseline(antenna)
        else:
            baseline = [antenna_to_baseline(a) for a in antenna]

        # handle overwrite parameter
        _outfile = outfile.rstrip('/')
        presumed_imagename = _outfile + image_suffix
        if os.path.exists(presumed_imagename):
            if overwrite == False:
                raise RuntimeError(
                    'Output file \'{0}\' exists.'.format(presumed_imagename))
            else:
                # delete existing images
                casalog.post('Removing \'{0}\''.format(presumed_imagename))
                _remove_image(presumed_imagename)
                assert not os.path.exists(presumed_imagename)
                for _suffix in associate_suffixes:
                    casalog.post('Removing \'{0}\''.format(_outfile + _suffix))
                    _remove_image(_outfile + _suffix)
                    assert not os.path.exists(_outfile + _suffix)

        # parse parameter for spectral axis
        imnchan, imstart, imwidth = _configure_spectral_axis(
            mode, nchan, start, width, restfreq)
        _restfreq = _get_restfreq_if_empty(infiles, _spw, field, restfreq)

        # translate some default values into the ones that are consistent with the current framework
        gtruncate = _handle_grid_defaults(truncate)
        ggwidth = _handle_grid_defaults(gwidth)
        gjwidth = _handle_grid_defaults(jwidth)

        # handle image parameters
        if isinstance(infiles, str) or len(infiles) == 1:
            _imsize, _cell, _phasecenter = _handle_image_params(
                imsize, cell, phasecenter, infiles, field, _spw, antenna, scan,
                intent, _restfreq, pointingcolumn, ephemsrcname)
        else:
            # sort input data using cleanhelper function to get consistent result with older sdimaging
            o = OldImagerBasedTools()
            _sorted = o.sort_vis(infiles, _spw, mode, imwidth, field, antenna,
                                 scan, intent)
            sorted_vis, sorted_field, sorted_spw, sorted_antenna, sorted_scan, sorted_intent = _sorted
            _imsize, _cell, _phasecenter = _handle_image_params(
                imsize, cell, phasecenter, sorted_vis, sorted_field,
                sorted_spw, sorted_antenna, sorted_scan, sorted_intent,
                _restfreq, pointingcolumn, ephemsrcname)

        # calculate pblimit from minweight
        pblimit = _calc_pblimit(minweight)

        ## (2) Set up Input Parameters
        ##       - List all parameters that you need here
        ##       - Defaults will be assumed for unspecified parameters
        ##       - Nearly all parameters are identical to that in the task. Please look at the
        ##         list of parameters under __init__ using  " help ImagerParameters " )
        casalog.post('*** Creating paramList ***', origin=origin)
        paramList = ImagerParameters(
            # input file name
            msname=infiles,  #'sdimaging.ms',
            # data selection
            field=field,  #'',
            spw=_spw,  #'0',
            antenna=baseline,
            scan=scan,
            state=intent,
            # image parameters
            imagename=_outfile,  #'try2',
            nchan=imnchan,  #1024,
            start=imstart,  #'0',
            width=imwidth,  #'1',
            outframe=outframe,
            veltype=veltype,
            restfreq=_restfreq,
            phasecenter=_phasecenter,  #'J2000 17:18:29 +59.31.23',
            imsize=_imsize,  #[75,75], 
            cell=_cell,  #['3arcmin', '3arcmin'], 
            projection=projection,
            stokes=stokes,
            # fix specmode to 'cubedata'
            # output spectral coordinate will be determined based on mode, start, and width
            specmode='cube',
            gridder='singledish',
            # single dish specific parameters
            gridfunction=gridfunction,
            convsupport=convsupport,
            truncate=gtruncate,
            gwidth=ggwidth,
            jwidth=gjwidth,
            pointingcolumntouse=pointingcolumn,
            minweight=minweight,
            clipminmax=clipminmax,
            # normalizer
            normtype='flatsky',
            pblimit=pblimit)

        # TODO: hadnle ephemsrcname
        # handle brightnessunit (CAS-11503)
        image_unit = ''
        if len(brightnessunit) > 0:
            if brightnessunit.lower() == 'k':
                image_unit = 'K'
            elif brightnessunit.lower() == 'jy/beam':
                image_unit = 'Jy/beam'
            else:
                raise ValueError, "Invalid brightness unit, %s" % brightnessunit

        # TODO: handle overwrite
        # TODO: output image name

        ## (3) Construct the PySynthesisImager object, with all input parameters

        casalog.post('*** Creating imager object ***', origin=origin)
        imager = PySynthesisImager(params=paramList)

        ## (4) Initialize various modules.
        ##       - Pick only the modules you will need later on. For example, to only make
        ##         the PSF, there is no need for the deconvolver or iteration control modules.

        ## Initialize modules major cycle modules

        casalog.post('*** Initializing imagers ***', origin=origin)
        imager.initializeImagers()
        casalog.post('*** Initializing normalizers ***', origin=origin)
        imager.initializeNormalizers()
        #imager.setWeighting()

        ## (5) Make the initial images

        #imager.makePSF()
        casalog.post('*** Executing runMajorCycle ***', origin=origin)
        casalog.post('NF = {0}'.format(imager.NF), origin=origin)
        #imager.runMajorCycle()  # Make initial dirty / residual image
        imager.makeSdPSF()
        imager.makeSdImage()

    except Exception as e:
        #print 'Exception : ' + str(e)
        casalog.post('Exception from task_tsdimaging : ' + str(e),
                     "SEVERE",
                     origin=origin)
        #         if imager != None:
        #             imager.deleteTools()

        larg = list(e.args)
        larg[0] = 'Exception from task_tsdimaging : ' + str(larg[0])
        e.args = tuple(larg)
        raise

    finally:
        ## (8) Close tools.

        casalog.post('*** Cleaning up tools ***', origin=origin)
        if imager is not None:
            imager.deleteTools()

        # change image suffix from .residual to .image
        if os.path.exists(outfile + residual_suffix):
            os.rename(outfile + residual_suffix, outfile + image_suffix)

    # set beam size
    # TODO: re-define related functions in the new tool framework (sdms?)
    imagename = outfile + image_suffix
    ms_index = 0
    rep_ms = _get_param(0, infiles)
    rep_field = _get_param(0, field)
    rep_spw = _get_param(0, _spw)
    rep_antenna = _get_param(0, antenna)
    rep_scan = _get_param(0, scan)
    rep_intent = _get_param(0, intent)
    if len(rep_antenna) > 0:
        baseline = '{0}&&&'.format(rep_antenna)
    else:
        baseline = '*&&&'
    with open_ms(rep_ms) as ms:
        ms.msselect({'baseline': baseline})
        ndx = ms.msselectedindices()
        antenna_index = ndx['antenna1'][0]
    with open_table(os.path.join(rep_ms, 'ANTENNA')) as tb:
        antenna_name = tb.getcell('NAME', antenna_index)
        antenna_diameter = tb.getcell('DISH_DIAMETER', antenna_index)
    set_beam_size(rep_ms, imagename, rep_field, rep_spw, baseline, rep_scan,
                  rep_intent, ephemsrcname, pointingcolumn, antenna_name,
                  antenna_diameter, _restfreq, gridfunction, convsupport,
                  truncate, gwidth, jwidth)

    # set brightness unit (CAS-11503)
    if len(image_unit) > 0:
        with open_ia(imagename) as ia:
            casalog.post("Setting brightness unit '%s' to image." % image_unit)
            ia.setbrightnessunit(image_unit)

    # mask low weight pixels
    weightimage = outfile + weight_suffix
    do_weight_mask(imagename, weightimage, minweight)