예제 #1
0
def cleanfinal(msin, cal_source):
    """Second CLEANing round and imaging

    :param msin: Visibility dataset in measurement set format path
    :type msin: str
    :param cal_source: Calibration source for masking
    :type cal_source: str
    """
    imgname = os.path.basename(msin) + '.fin.img'
    if cal_source in cal_source_dct.keys():
        clean_mask = cal_source_dct[cal_source]['mask']
    else:
        cal_source = ''  # TODO create mask for arbitrary point source given
        clean_mask = None
    tclean(vis=msin,
           imagename=imgname,
           spw='0:60~745',
           niter=3000,
           weighting='briggs',
           robust=-1,
           imsize=[512, 512],
           cell=['250arcsec'],
           specmode='mfs',
           nterms=1,
           mask=clean_mask)
    imggal = cal_source + 'combined.galcord'
    imregrid(imagename=imgname + '.image', output=imggal, template='GALACTIC')
예제 #2
0
def cleaninit(msin, cal_source):
    """First CLEANing round

    :param msin: Visibility dataset in measurement set format path
    :type msin: str
    :param cal_source: Calibration source for masking
    :type cal_source: str

    TODO: Argument to specify mask
    """
    imgname = os.path.basename(msin) + '.init.img'
    if cal_source in cal_source_dct.keys():
        clean_mask = cal_source_dct[cal_source]['mask']
    else:
        clean_mask = None
    tclean(vis=msin,
           imagename=imgname,
           niter=500,
           weighting='briggs',
           robust=0,
           imsize=[512, 512],
           cell=['250arcsec'],
           specmode='mfs',
           nterms=1,
           spw='0:150~900',
           mask=clean_mask)
def predictImager(msname='sim_data.ms',
                  imname_true='sim_onepoint_true.im',
                  gridder='standard'):

    os.system('rm -rf sim_predict.*')

    # Run tclean in predictModel mode.
    tclean(
        vis=msname,
        startmodel=imname_true,
        imagename='sim_predict',
        savemodel='modelcolumn',
        imsize=256,
        cell='8.0arcsec',
        specmode='cube',
        interpolation='nearest',
        start='1.0GHz',
        width='0.1GHz',
        nchan=10,
        reffreq='1.5Hz',
        gridder=gridder,
        normtype='flatsky',  # sky model is flat-sky
        cfcache='sim_predict.cfcache',
        wbawp=
        True,  # ensure that gridders='mosaic' and 'awproject' do freq-dep PBs
        pblimit=0.05,
        conjbeams=False,
        calcres=False,
        calcpsf=True,
        niter=0,
        wprojplanes=1)
예제 #4
0
def tclean_wrapper(vis,
                   imagename,
                   imsize=None,
                   cellsize=None,
                   interactive=False,
                   robust=0.5,
                   niter=50000,
                   pbcor=True,
                   **kwargs):

    for ext in [
            ".image",
            ".mask",
            ".model",
            ".pb",
            ".psf",
            ".residual",
            ".sumwt",
            ".image.pbcor",
    ]:
        fname = imagename + ext
        if os.path.exists(fname):
            shutil.rmtree(fname)

    tclean(
        vis=vis,
        imagename=imagename,
        weighting="briggs",
        robust=robust,
        imsize=imsize,
        cell=cellsize,
        niter=niter,  # we want to end on the threshold
        interactive=interactive,
        pbcor=pbcor,
        **kwargs)
예제 #5
0
def tcleaner(targetcalfile,
             params,
             threshold='1.0mJy',
             niter=1000,
             outimage=None,
             interactive=False):
    '''
    This tcleaner is specifically written for imaging during the self-calibration round. For a general purpose imaging, use the casa task tclean outside of yarrp. 
    '''

    imaging_params = params['imagecal']
    #outimage = imaging_params['outimage']
    target = params['general']['target']
    imsize = imaging_params['imsize']
    cell = imaging_params['cell']
    robust = imaging_params['robust']
    weighting = imaging_params['weighting']
    uvran = imaging_params['uvran']
    uvtaper = imaging_params['uvtaper']
    nterms = imaging_params['nterms']
    #niter = imaging_params['niter']
    #threshold = imaging_params['threshold']
    wprojplanes = imaging_params['wprojplanes']
    scales = imaging_params['scales']

    cts.tclean(targetcalfile,
               imagename=outimage,
               field=target,
               spw='0',
               imsize=imsize,
               cell=cell,
               robust=robust,
               weighting=weighting,
               uvrange=uvran,
               uvtaper=uvtaper,
               specmode='mfs',
               nterms=nterms,
               niter=niter,
               usemask='auto-multithresh',
               minbeamfrac=0.1,
               sidelobethreshold=1.5,
               smallscalebias=0.6,
               threshold=threshold,
               aterm=True,
               pblimit=-1,
               deconvolver='mtmfs',
               gridder='wproject',
               wprojplanes=wprojplanes,
               scales=scales,
               wbawp=False,
               restoration=True,
               savemodel='modelcolumn',
               cyclefactor=0.5,
               parallel=False,
               interactive=interactive)

    return print('tcleaned the {}'.format(targetcalfile), 'file.')
def imageAnts(vis='sim_data_ALMA.ms',
              imname='try_ALMA',
              field='0',
              antsel='',
              tel='ALMA',
              vptable=''):
    """
    Run imager..... 
    TODO  set pblimit=-1 ... or set the image stats method to look inside the PB region only. 
    """
    antsels = getBaselineTypes(tel=tel)

    if antsel not in antsels.keys():
        print('Pick an antenna selection from ' + str(antsels))
        return

    if tel == 'ALMA':
        cell = '0.4arcsec'
        imsize = 1024
        phasecenter = 'J2000 +19h59m28.5s -40d44m21.5s'
    if (tel == 'NGVLA'):
        cell = '1.0arcsec'
        imsize = 1024
        phasecenter = 'J2000 +19h59m28.5s +40d44m21.5s'

    if field == '0' or field == '1':
        ftype = 'single'
    else:
        ftype = 'mosaic'

    imname1 = imname + '_' + antsel + '_' + ftype

    os.system('rm -rf ' + imname1 + '.*')

    # Run tclean
    tclean(vis=vis,
           antenna=antsels[antsel],
           field=field,
           imagename=imname1,
           imsize=imsize,
           cell=cell,
           phasecenter=phasecenter,
           specmode='cube',
           interpolation='nearest',
           nchan=-1,
           gridder='mosaic',
           vptable=vptable,
           normtype='flatnoise',
           wbawp=True,
           pblimit=0.05,
           conjbeams=False,
           niter=1000,
           nsigma=3.0,
           datacolumn='data',
           weighting='natural')
예제 #7
0
def makeimage(msfile,
              field,
              outname='tmp.',
              niter=50,
              cell=0.5,
              npix=4096,
              spw='',
              antenna='*',
              uvrange='>0lambda',
              weighting='natural',
              gridder='standard',
              robust=0.5,
              uvtaper=[],
              deconvolver='hogbom',
              conjbeams=False,
              wprojplanes=-1):
    """

    Tclean on the data to generate the image
    
    Example Usage:
    makeimage(msfile1, fieldnames[0], outname='IMNAME', spw='5~8', niter=100, cell=0.5, npix=4096)
    
    spw range is end inclusive
    
    """
    # To remove earlier products?
    #     %sx rm -rf $msfile?*

    wprojplanes = -1 if 'project' in gridder else 1
    tasks.tclean(vis=msfile,
                 imagename=outname,
                 field=field,
                 niter=niter,
                 stokes='I',
                 antenna=antenna,
                 uvrange=uvrange,
                 weighting=weighting,
                 cell=cell,
                 imsize=[npix, npix],
                 spw=spw,
                 gridder=gridder,
                 robust=robust,
                 uvtaper=uvtaper,
                 deconvolver=deconvolver,
                 conjbeams=conjbeams,
                 wprojplanes=wprojplanes)
def predictImager(msname='sim_data_ALMA.ms',
                  imname_true='sim_onepoint_true.im',
                  gridder='mosaic',
                  tel='ALMA',
                  vptable=''):

    if tel=='ALMA':
        cell='0.2arcsec'
        imsize=2048
    if tel=='VLA':
        cell='12.0arcsec'
        imsize=512
    if (tel=='NGVLA'):
        cell='0.5arcsec'
        imsize=2048

    
    os.system('rm -rf sim_predict.*')
    
    # Run tclean in predictModel mode. 
    tclean(vis=msname,
       startmodel=imname_true,
       vptable = vptable,
       imagename='sim_predict',
       savemodel='modelcolumn',
       imsize=imsize,
       cell=cell,
       specmode='cube',
       interpolation='nearest',
       nchan=-1,
       #start='90.0GHz',
       #width='1.0GHz',
       #nchan=10,
       #reffreq='95.0GHz',
       gridder=gridder,
       normtype='flatsky',  # sky model is flat-sky
       #cfcache='sim_predict.cfcache',
       wbawp=True,      # ensure that gridders='mosaic' and 'awproject' do freq-dep PBs
       pblimit=0.05,    
       conjbeams=False, 
       calcres=False, 
       calcpsf=True, 
       niter=0, 
       wprojplanes=1)
예제 #9
0
import os, warnings
warnings.simplefilter(
    "ignore", category=RuntimeWarning)  # suppress warnings about nan-slices

from casatasks import tclean
from casatasks import immoments
from cngi.conversion import convert_image
from cngi.image import implot
from cngi.image import moments
import os
import numpy as np

#tclean returns error for some reason 2020 1013
'''
tclean(vis='sis14_twhya_calibrated_flagged.ms', imagename='sis14_twhya_calibrated_flagged', field='5', spw='',
       specmode='cube', deconvolver='hogbom', nterms=1, imsize=[250,250], gridder='standard', cell=['0.1arcsec'],
       nchan=10, weighting='natural', threshold='0mJy', niter=5000, interactive=False, savemodel='modelcolumn',
       usemask='auto-multithresh')
'''
#load image instead of running tclean. Change the file path when test it in your local machine
Casa6ImageFileName = '/Users/wxiong/NRAO/dev/cngi_prototype/data/sis14_twhya_calibrated_flagged/sis14_twhya_calibrated_flagged.image'

# casa6
os.system('rm -rf casa6.immoments.image*')
immoments(Casa6ImageFileName,
          axis='spectral',
          moments=[-1, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11],
          outfile='casa6.immoments.image')
casa_xds = convert_image('casa6.immoments.image.average',
                         artifacts=[
                             'average', 'median', 'integrated', 'maximum',
예제 #10
0
def imagecal(targetcalfile, params, nloops=1, ploops=5, aploops=2, flagger='default', interactive=False):
    
    '''
    This function takes in a calibrated target file (usually the channel averaged file) and goes through "niters" of imagecal loops. A imagecal loop is defined as first making a image using imgr.tcleaner. Then "ploops" of phase-only self-cal and "aploops" of a&p selfcal. Then the continuum image is subtracted from the UV data and the residuals are flagged. After having gone through all the "niters" loop it spits out a final continuum image.
    '''

    imaging_params = params['imagecal']
    outimage = imaging_params['outimage']
    target = params['general']['target']
    threshold_range = imaging_params['threshold_range']
    threshold_final = imaging_params['threshold_final']
    solints = imaging_params['solints']
    niter_range = imaging_params['niter_range']  
    #temp_dir = params['general']['temp']
    
    # Preparing the ranges of different parameters for the loops
    solint_range = np.linspace(solints[0], solints[1], ploops)
    solint_range = [str(i)+'min' for i in solint_range]
    threshold_range = np.linspace(threshold_range[0], threshold_range[1], ploops)
    threshold_range = [str(i)+'mJy' for i in threshold_range]
    
    niter_range = np.linspace(niter_range[0], niter_range[1], ploops)
    niter_range = [int(i) for i in niter_range]

    while nloops >= 1:
        #subprocess.run('rm -r {}'.format(temp_dir), shell=True, check=True) # Clearing the temp directory
        #subprocess.run('mkdir {}'.format(temp_dir), shell=True, check=True) 
        ploop_index = 1 + np.arange(ploops) #This gives the index to the cont image and cal files created later
        sc_p_msfile = targetcalfile # Initially begin with the avspc file, and then change this file name to the running self-cal file
        for pindex in ploop_index: # Run all the ploops
            print('Self-cal phase-only loop', pindex)
            imgr.tcleaner(sc_p_msfile, params, threshold=threshold_range[pindex - 1], niter=niter_range[pindex - 1], outimage='../RGG_5_sc_p.'+str(pindex), interactive=interactive) 
            clb.selfcal(sc_p_msfile, params, mode='p', in_gaintable=[], out_gaintable='sc_p.gcal.'+str(pindex), solint=solint_range[pindex - 1], solnorm = False)
            gaintable = ['sc_p.gcal.'+str(pindex)] # Change the gaintable to the latest
            cts.applycal(sc_p_msfile, gaintable=gaintable, field='', gainfield='', applymode='calonly', interp=['linear'], calwt=False, parang=False)
            cts.mstransform(sc_p_msfile, field='0', spw='0', datacolumn='corrected', outputvis='sc_p_'+str(pindex)+'.ms')
            sc_p_msfile = 'sc_p_'+str(pindex)+'.ms' 
        
        aploop_index = 1 + np.arange(aploops) #This gives the index to the cont image and cal files created later
        sc_ap_msfile = sc_p_msfile # Initially begin with the avspc file, and then change this file name to the running self-cal file
        niter_ap = niter_range[-1] # The last object in pcal niters
        threshold_ap = threshold_range[-1]
        solint_ap = solint_range[-1]
        for apindex in aploop_index: # Run all the aploops
            print('Self-cal a&p loop', apindex)
            imgr.tcleaner(sc_ap_msfile, params, threshold=threshold_ap, niter=niter_ap, outimage='../RGG5_sc_ap.'+str(apindex), interactive=interactive)
            clb.selfcal(sc_ap_msfile, params, mode='a&p', in_gaintable=[], out_gaintable='sc_ap.gcal.'+str(apindex), solint=solint_ap, solnorm=True)
            gaintable = ['sc_ap.gcal.'+str(apindex)] # Change the gaintable to the latest
            cts.applycal(sc_ap_msfile, gaintable=gaintable, field='', gainfield='', applymode='calonly', interp=['linear'], calwt=False, parang=False)
            cts.mstransform(sc_ap_msfile, field='0', spw='0', datacolumn='corrected', outputvis='sc_ap_'+str(apindex)+'.ms')
            sc_ap_msfile = 'sc_ap_'+str(apindex)+'.ms'

        
        # Add some r-flagging on the residuals and then continue the loops.
        
        nloops = nloops - 1 
    else:
        print('Self-cal loops over.')

    print('Making a final continuum  image...') 
    
    imaging_params = params['imagecal']
    outimage = imaging_params['outimage']
    target = params['general']['target']
    imsize = imaging_params['imsize']
    cell = imaging_params['cell']
    robust = imaging_params['robust'] 
    weighting = imaging_params['weighting']
    uvran = imaging_params['uvran']
    uvtaper = imaging_params['uvtaper']
    nterms = imaging_params['nterms']
    niter = 5000 #niter_ap
    threshold = threshold_final    
    wprojplanes = imaging_params['wprojplanes']
    scales = imaging_params['scales']
    
    cts.tclean(sc_ap_msfile, imagename=outimage+'_final', field=target, spw='0', imsize=imsize, cell=cell, robust=robust, weighting=weighting, uvrange=uvran, uvtaper=uvtaper, 
            specmode='mfs',	nterms=nterms, niter=niter, usemask='auto-multithresh', minbeamfrac=0.1, sidelobethreshold = 1.5,
	    smallscalebias=0.6, threshold= threshold, aterm =True, pblimit=-1,
	    deconvolver='mtmfs', gridder='wproject', wprojplanes=wprojplanes, scales=scales,wbawp=False,
	    restoration = True, savemodel='modelcolumn', cyclefactor = 0.5, parallel=False,
       	    interactive=False)


    print('Imaging and self-calibration done.')

    return gaintable 
예제 #11
0
def slefcal_ms(src_dir, imagems, imagename, ATCA_band, n_spw):
    print(
        "+ + + + + + + + + + + + + + + + +\n+  Self Cal Round 1  +\n+ + + + + + + + + + + + + + + + +"
    )
    mode = "mfs"
    nterms = 2
    niter = 3000
    antenna = "0~6,0~6"
    n_spw = n_spw
    if ATCA_band == "L":
        imsize = 2250
        solint = "60s"
        minsnr = 3.0
        cell = "1arcsec"
        minblperant = 3
    if ATCA_band == "C":
        imsize = 2250
        solint = "60s"
        minsnr = 3.0
        minblperant = 3
        cell = "0.5arcsec"
    if ATCA_band == "X":
        imsize = 1152
        solint = "60s"
        minsnr = 3.0
        minblperant = 3
        cell = "0.2arcsec"
    uvrange = ""
    stokes = "I"
    weighting = "briggs"
    robust = 0.5
    interactive = False
    gain = 0.01
    threshold = "5e-4Jy"
    # if os.path.exists(f"{src_dir}/cal_tables/pcal1_{imagename}"):
    rmtables(f"{src_dir}/cal_tables/pcal1_{imagename}")
    gaincal(
        vis=imagems,
        caltable=f"{src_dir}/cal_tables/pcal1_{imagename}",
        combine="scan,spw",
        spwmap=[0] * n_spw,
        gaintype="G",
        calmode="p",
        solint=solint,
        minsnr=minsnr,
        minblperant=minblperant,
    )
    applycal(
        vis=imagems,
        gaintable=f"{src_dir}/cal_tables/pcal1_{imagename}",
        spwmap=[0] * n_spw,
        parang=True,
        applymode="calonly",
        flagbackup=False,
    )
    flagmanager(vis=imagems, mode="save", versionname="post self1")

    for i in range(0, n_spw):
        spw = str(i)
        # if os.path.exists(f"{src_dir}/casa_files/{imagename}_{spw}_self1*"):
        os.system(
            f"rm -r {src_dir}/casa_files/{imagename}_{spw}_self1*"
        )
        print("Cleaning on band: " + str(spw))
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_self1",
            selectdata=True,
            mask=f"{src_dir}/casa_files/{imagename}_mfs.mask",
            spw=spw,
            startmodel=f"{src_dir}/casa_files/{imagename}_{spw}_preself.model",
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=niter,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            savemodel="modelcolumn",
            pbcor=False,
            uvrange=uvrange,
        )
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_self1",
            selectdata=True,
            mask="",
            spw=spw,
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=0,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            pbcor=False,
            savemodel="modelcolumn",
            calcres=False,
            calcpsf=False,
            uvrange=uvrange,
        )

    threshold = "5e-5Jy"
    # if os.path.exists(f"{src_dir}/cal_tables/pcal2_{imagename}"):
    rmtables(f"{src_dir}/cal_tables/pcal2_{imagename}")
    print(
        "+ + + + + + + + + + + + + + + + +\n+  Self Cal Round 2  +\n+ + + + + + + + + + + + + + + + +"
    )

    gaincal(
        vis=imagems,
        caltable=f"{src_dir}/cal_tables/pcal2_{imagename}",
        gaintable=f"{src_dir}/cal_tables/pcal1_{imagename}",
        combine="scan,spw",
        spwmap=[0] * n_spw,
        gaintype="G",
        calmode="p",
        solint=solint,
        minsnr=minsnr,
        minblperant=minblperant,
    )
    applycal(
        vis=imagems,
        gaintable=[
            f"{src_dir}/cal_tables/pcal1_{imagename}",
            f"{src_dir}/cal_tables/pcal2_{imagename}",
        ],
        spwmap=[[0] * n_spw, [0] * n_spw],
        parang=True,
        applymode="calonly",
        flagbackup=False,
    )
    flagmanager(vis=imagems, mode="save", versionname="post self2")
    for i in range(0, n_spw):
        spw = str(i)
        # if os.path.exists(f"{src_dir}/casa_files/{imagename}_{spw}_self2*"):
        os.system(f"rm -r {src_dir}/casa_files/{imagename}_{spw}_self2*")
        print("Cleaning on band: " + str(spw))
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_self2",
            selectdata=True,
            mask=f"{src_dir}/casa_files/{imagename}_mfs.mask",
            spw=spw,
            startmodel=f"{src_dir}/casa_files/{imagename}_{spw}_self1.model",
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=niter,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            savemodel="modelcolumn",
            pbcor=False,
            uvrange=uvrange,
        )
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_self2",
            selectdata=True,
            mask="",
            spw=spw,
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=0,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            pbcor=False,
            savemodel="modelcolumn",
            calcres=False,
            calcpsf=False,
            uvrange=uvrange,
        )

    threshold = "5e-6Jy"
    print(
        "+ + + + + + + + + + + + + + + + +\n+  Self Cal Round 3  +\n+ + + + + + + + + + + + + + + + +"
    )

    rmtables(f"{src_dir}/cal_tables/pcal3_{imagename}")
    gaincal(
        vis=imagems,
        caltable=f"{src_dir}/cal_tables/pcal3_{imagename}",
        gaintable=[
            f"{src_dir}/cal_tables/pcal1_{imagename}",
            f"{src_dir}/cal_tables/pcal2_{imagename}",
        ],
        combine="scan,spw",
        spwmap=[[0] * n_spw, [0] * n_spw],
        gaintype="G",
        calmode="p",
        solint=solint,
        minsnr=minsnr,
        minblperant=minblperant,
    )
    applycal(
        vis=imagems,
        gaintable=[
            f"{src_dir}/cal_tables/pcal1_{imagename}",
            f"{src_dir}/cal_tables/pcal2_{imagename}",
            f"{src_dir}/cal_tables/pcal3_{imagename}",
        ],
        spwmap=[[0] * n_spw, [0] * n_spw, [0] * n_spw],
        parang=True,
        applymode="calonly",
        flagbackup=False,
    )
    flagmanager(vis=imagems, mode="save", versionname="post self3")
    for i in range(0, n_spw):
        spw = str(i)
        # if os.path.exists(f"{src_dir}/casa_files/{imagename}_{spw}_self3*"):
        os.system(f"rm -r {src_dir}/casa_files/{imagename}_{spw}_self3*")
        print("Cleaning on band: " + str(spw))
        # imagename = f"{src_dir}/casa_files/{imagename}_{spw}"
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_self3",
            selectdata=True,
            mask=f"{src_dir}/casa_files/{imagename}_mfs.mask",
            startmodel=f"{src_dir}/casa_files/{imagename}_{spw}_self2.model",
            spw=spw,
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=niter,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            savemodel="modelcolumn",
            pbcor=False,
            uvrange=uvrange,
        )
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_self3",
            selectdata=True,
            mask="",
            spw=spw,
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=0,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            pbcor=False,
            savemodel="modelcolumn",
            calcres=False,
            calcpsf=False,
            uvrange=uvrange,
        )
    return
예제 #12
0
def img_ms(src_dir, imagems, imagename, ATCA_band, n_spw):
    print(
        "+ + + + + + + + + + + + + + + + +\n+  Preself Imaging  +\n+ + + + + + + + + + + + + + + + +"
    )
    antenna = "0~6,0~6"
    mode = "mfs"
    nterms = 1
    niter = 3000
    n_spw = n_spw
    if ATCA_band == "L":
        imsize = 2250
        cell = "1arcsec"
    if ATCA_band == "C":
        imsize = 2250
        cell = "0.5arcsec"
    if ATCA_band == "X":
        imsize = 1152
        cell = "0.2arcsec"
    stokes = "I"
    weighting = "briggs"
    robust = 0.5
    interactive = False
    gain = 0.01
    threshold = "5e-3Jy"
    uvrange = ""
    # flagmanager(vis=imagems, mode="save", versionname="preself")
    for i in range(0, n_spw):
        spw = str(i)
        print("Cleaning on band: " + str(spw))
        # if os.path.exists(f"{src_dir}/casa_files/{imagename}_{spw}_preself*"):
        os.system(
            f"rm -r {src_dir}/casa_files/{imagename}_{spw}_preself*"
        )
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_preself",
            selectdata=True,
            mask=f"{src_dir}/casa_files/{imagename}_mfs.mask",
            spw=spw,
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=niter,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            savemodel="modelcolumn",
            pbcor=False,
            uvrange=uvrange,
        )
        tclean(
            vis=imagems,
            imagename=f"{src_dir}/casa_files/{imagename}_{spw}_preself",
            selectdata=True,
            mask="",
            spw=spw,
            gain=gain,
            specmode=mode,
            nterms=nterms,
            niter=0,
            threshold=threshold,
            imsize=imsize,
            cell=cell,
            stokes=stokes,
            weighting=weighting,
            robust=robust,
            antenna=antenna,
            interactive=interactive,
            pbcor=False,
            savemodel="modelcolumn",
            calcres=False,
            calcpsf=False,
            uvrange=uvrange,
        )
    return
예제 #13
0
def imgmfs_ms(src_dir, imagems, imagename, ATCA_band, n_spw):
    mode = "mfs"
    nterms = 1
    niter = 3000
    antenna = "0~6,0~6"
    uvrange = ""
    n_spw = n_spw
    if ATCA_band == "L":
        imsize = 2250
        cell = "1arcsec"
        threshold = "2e-2Jy"
    if ATCA_band == "C":
        imsize = 2250
        cell = "0.5arcsec"
        threshold = "2e-2Jy"
    if ATCA_band == "X":
        imsize = 2250
        cell = "0.2arcsec"
        threshold = "2e-2Jy"
    stokes = "I"
    weighting = "briggs"
    robust = 0.5
    interactive = True
    gain = 0.01
    # if os.path.exists(f"{src_dir}/casa_files/{imagename}_mfs*"):
    os.system(f"rm -r {src_dir}/casa_files/{imagename}_mfs*")
    flagmanager(vis=imagems, mode="save", versionname="before_selfcal")
    print("Initiating interactive cleaning on {0}".format(imagename))

    tclean(
        vis=imagems,
        imagename=f"{src_dir}/casa_files/{imagename}_mfs",
        gain=gain,
        specmode=mode,
        nterms=nterms,
        niter=niter,
        threshold=threshold,
        imsize=imsize,
        cell=cell,
        stokes=stokes,
        weighting=weighting,
        robust=robust,
        antenna=antenna,
        interactive=interactive,
        savemodel="modelcolumn",
        pbcor=False,
        uvrange=uvrange,
    )
    tclean(
        vis=imagems,
        imagename=f"{src_dir}/casa_files/{imagename}_mfs",
        gain=gain,
        specmode=mode,
        nterms=nterms,
        niter=0,
        threshold=threshold,
        imsize=imsize,
        cell=cell,
        stokes=stokes,
        weighting=weighting,
        robust=robust,
        antenna=antenna,
        interactive=interactive,
        savemodel="modelcolumn",
        pbcor=False,
        calcres=False,
        calcpsf=False,
        uvrange=uvrange,
    )
    return
예제 #14
0
def test_casa_masking():
    with tempfile.TemporaryDirectory() as tmpdir:
        # SIMULATE SOME DATA SET

        # Define antennas
        diam = [25, 25, 25, 25, 25]
        xx = [50, 100, 150, 200, 250]
        yy = [2, -5, -20, -50, -100]
        zz = [-0.5, -1.0, -1.5, -2.0, -2.5]

        sm = simulator()
        sm.open(tmpdir + '/SIM.ms')
        # do configuration
        posvla = me.observatory('VLA')
        sm.setconfig(telescopename='VLA',
                     x=xx,
                     y=yy,
                     z=zz,
                     dishdiameter=diam,
                     mount='alt-az',
                     antname='VLA',
                     coordsystem='local',
                     referencelocation=posvla)

        # Initialize the spectral windows
        sm.setspwindow(spwname='CBand',
                       freq='5GHz',
                       deltafreq='50MHz',
                       freqresolution='50MHz',
                       nchannels=1,
                       stokes='RR RL LR LL')

        # Initialize the source and calibrater
        sm.setfield(sourcename='My cal',
                    sourcedirection=['J2000', '00h0m0.0', '+45.0.0.000'],
                    calcode='A')
        sm.setfield(sourcename='My source',
                    sourcedirection=['J2000', '01h0m0.0', '+47.0.0.000'])

        sm.setlimits(shadowlimit=0.001, elevationlimit='8.0deg')
        sm.setauto(autocorrwt=0.0)

        sm.settimes(integrationtime='10s',
                    usehourangle=False,
                    referencetime=me.epoch('utc', 'today'))

        sm.observe('My cal', 'CBand', starttime='720s', stoptime='1020s')
        sm.observe('My source', 'CBand', starttime='1030s', stoptime='1500s')

        sm.close()

        # Create mask to use during clean
        reg = EllipseSkyRegion(center=coordinates.SkyCoord(0.0 * u.deg,
                                                           45.0 * u.deg,
                                                           frame='fk5'),
                               width=1.0 * u.arcmin,
                               height=2.0 * u.arcmin,
                               angle=45 * u.deg)
        write_crtf([reg], tmpdir + '/SIM.crtf', 'fk5', '.6f', 'deg')

        # Image the dataset
        tclean(vis=tmpdir + '/SIM.ms',
               imagename=tmpdir + '/SIM',
               imsize=100,
               cell='5arcsec',
               niter=1,
               mask=tmpdir + '/SIM.crtf',
               interactive=False)

        ia = image()
        ia.open(tmpdir + '/SIM.mask')
        mask_array = ia.getregion()
        ia.close()

        with open('data/binary_mask.pkl', 'rb') as f:
            ref_mask = pickle.load(f)

        assert all(mask_array == ref_mask)
예제 #15
0
def pipeline(msfile, params, doinitial_flagging=True, doflagcal=True, doimagecal=True, douvsub=True, docubeimage=False):
    '''
    This function takes combines several recipes to construct a pipeline. In particular here it follows a simple procedure. 

    initial flagging --> setjy+delay calibration --> amplitude calibration + flagging loops on calibrators --> bandpass calibration + flagging loops --> imaging+selfcal loops --> final image --> UVSUB+CVEL --> Cube image with source finder. 
    '''
    with open('yarp_art.txt', 'r') as f:
        for line in f:
            print(line.rstrip())
    print('Running the yarp pipeline on {}'.format(msfile))
    if doinitial_flagging:
        flag_spw = params['flagging']['spw']
        bad_ants = params['flagging']['badants']
        flagcmd = params['flagging']['flagcmd']
        if len(bad_ants) != 0:
            flg.badantflag(msfile, params)
        else:
            print('No bad antennas found.')
        if flagcmd != False:
            print('Flagging based on user defined commands ...')
            cts.flagdata(msfile, mode='list', inpfile=flagcmd)
        else:
            None
        cts.flagdata(msfile, mode='quack', field='', spw='0', antenna='', correlation='', timerange='',
                     quackinterval=10, quackmode='beg', action='apply', savepars=True, cmdreason='quackbeg')
        cts.flagdata(msfile, mode='quack', field='', spw='0', antenna='', correlation='', timerange='',
                     quackinterval=10, quackmode='endb', action='apply', savepars=True, cmdreason='quackendb')
        if flag_spw != False:
            cts.flagdata(msfile, mode='manual', field='', spw=flag_spw, antenna='', correlation='',
                         timerange='', action='apply', savepars=True, cmdreason='badchannels')
        else:
            print('Not flagging edge channels')
        flg.tfcropper(msfile, params, field='', tcut=6,
                      fcut=6, instance='initial')
        # Flag very long baselines as that can affect the calibration
        cts.flagdata(msfile, mode='manual', uvrange='>100klambda')
        flg.extend(msfile, params, instance='initial')
        #flg.aoflagger(msfile, params)
    else:
        print('No initial flagging this time.')

    # Flagcal begins

    if doflagcal:
        print('Running the flagcal script...')
        nloops = params['calibration']['nloops']
        flagcal2(msfile, params, niters=nloops,
                 flagger='default', interactive=False)
    else:
        print('No flagcal this time.')

    if doimagecal:
        print('Doing image and self-calibration...')
        target = params['general']['target']
        targetcalfile = params['general']['targetcalfile']
        avspcfile = targetcalfile[:-3]+'_avspc.ms'

        print('Flagging all data above uvran 100klambda ...')
        cts.flagdata(targetcalfile, mode='manual', uvrange='>100klambda')
        print('Averaging the channels to 1 MHz...')
        chanbin = params['imagecal']['chanbin']
        cts.mstransform(targetcalfile, field=target, spw='0', chanaverage=True,
                        chanbin=chanbin, datacolumn='data', outputvis=avspcfile)
        print('Rflagging the channel averaged file...')
        flg.rflagger(avspcfile, params, field=target,
                     tcut=10, fcut=10, instance='initial')
        print('Flagging the line spws...')
        flag_spw = params['imagecal']['spec_line_spw']
        cts.flagdata(avspcfile, mode='manual', spw=flag_spw)
        print('Doing imaging and self-calibration...')

        nloops = params['imagecal']['nloops']
        ploops = params['imagecal']['ploops']
        aploops = params['imagecal']['aploops']
        print('Running {} cycles of self-cal with {} ploops and {} aploops in each cycle...'.format(
            str(nloops), str(ploops), str(aploops)))
        final_image, selfcaltable = imagecal_dev(
            avspcfile, params, nloops=nloops, ploops=ploops, aploops=aploops, flagger='default', interactive=False)
        print('Final self-cal table is', selfcaltable)

    else:
        print('No imaging and self-calibration this time.')

    if douvsub:
        print('Doing uvsub...')
        target = params['general']['target']
        targetcalfile = params['general']['targetcalfile']
        avspcfile = targetcalfile[:-3]+'_avspc.ms'
        nloops = params['imagecal']['nloops']
        ploops = params['imagecal']['ploops']
        aploops = params['imagecal']['aploops']
        outdir = params['general']['outdir']
        # apply self-cal table to the full chan resolution file
        # selfcaltable = [outdir+'sc_p.gcal.' +
        #                str(nloops)+str(ploops), outdir+'sc_ap.gcal.'+str(nloops)+str(aploops)]
        '''
	selfcaltable = [outdir+'sc_p.gcal.' +
                        str(1)+str(5)]
        print(selfcaltable)
        cts.applycal(targetcalfile, gaintable=selfcaltable, field='', gainfield='',
                     applymode='calonly', interp=['linearperobs'], calwt=False, parang=False)

        print('Rflagging the self-calibrated data')
        '''
	linefreespw = params['uvsub']['linefreespw']
        linespw = params['uvsub']['linespw']
        '''
	flg.rflagger(targetcalfile, params, spw=linefreespw, field=target, tcut=6,
                     fcut=6, instance='postcal')  # deep flag the line free channels
        flg.rflagger(targetcalfile, params, spw=linespw, field=target, tcut=10,
                     fcut=10, instance='postcal')  # Be  more conservative on the line channels
        flg.extend(targetcalfile, params, field=target,
                   grow=80, instance='postcal')
        '''
	# UVLIN the full chan resolution file
        print('Doing uvcontsub...')

        fitorder = params['uvsub']['fitorder']
        # print(linefreespw)
        cts.uvcontsub(targetcalfile, fitspw=linefreespw, fitorder=fitorder, solint='int', combine='scan')
        #tempdir = params['general']['temp']
        #uvsubber(targetcalfile, params, fitspw=linefreespw, fitorder=fitorder, nterms=1, 
	#    model_image=[tempdir+'AGC203001_cont_sc_p.15.model'])
        targetcalfile = params['general']['targetcalfile']
        cont_sub_file = targetcalfile+'.uvsub.contsub'
        # subprocess.run('mv {} {}'.format(cont_sub_file, outdir),
        #               shell=True, check=True)
    else:
        print('No uvsub this time.')

    if docubeimage:
        print('Doing cube image...')
        # image the cube
        outdir = params['general']['outdir']
        target = params['general']['target']
        targetcalfile = params['general']['targetcalfile']
        restfreq = '1.420405752GHz'
        #cont_sub_file = targetcalfile+'.contsub'
        #cont_sub_file = cont_sub_file
        cont_sub_file = targetcalfile+'.contsub'
        weighting = params['cube']['weighting']
        robust = params['cube']['robust']
        deconvolver = params['cube']['deconvolver']

        # uvran_list = ['0.5~5klambda', '0.5~10klambda', '0.5~20klambda','0.5~40klambda'] # list of uvranges to image the cube
        #uvtaper_list = ['4.5klambda', '6klambda', '12klambda', '30klambda']
        #imsize_list = [256, 512, 540, 1024]
        #cellsize_list = ['8arcsec', '4arcsec', '3arcsec', '1arcsec']
        #threshold_list = ['0.44mJy', '1.0mJy', '1.0mJy', '1.0mJy']

        uvran_list = ['0.5~6klambda']
        uvtaper_list = ['5.5klambda']
        imsize_list = [128]
        cellsize_list = ['8arcsec']
        threshold_list = ['1mJy']

        vel_res1 = params['cube']['vel_res']
        vel_res = str(vel_res1)+'km/s'
        #vel_res = '-14km/s'
        file_name = str(vel_res1)+'kmps'
        for i in range(len(uvran_list)):
            cts.tclean(cont_sub_file,
                       imagename=outdir+target+'_cube_' +
                       str(file_name)+'/'+'uvran_'+str(uvran_list[i]),
                       field='0',
                       spw='0',
                       specmode='cube',
                       nchan=-1,
                       width=vel_res,
                       outframe='bary',
                       veltype='optical',
                       restfreq=restfreq,
                       deconvolver='hogbom',
                       gridder='standard',
                       uvrange=uvran_list[i],
                       uvtaper=uvtaper_list[i],
                       imsize=imsize_list[i],
                       cell=cellsize_list[i],
                       threshold=threshold_list[i],
                       weighting=weighting,
                       robust=robust,
                       restoringbeam='common',
                       interactive=False,
                       usemask='pb',
                       pbmask=0.2,
                       # usemask='auto-multithresh',
                       # minbeamfrac=0.1,
                       #sidelobethreshold = 1.5,
                       # smallscalebias=0.6,
                       niter=100000
                       )
    else:
        print('No cube image this time.')

    outdir = general_params['outdir']
    subprocess.run('cp {} {}'.format(
        'parameters.yaml', outdir), shell=True, check=True)

    return print('yarp pipeline ended.')
예제 #16
0
import mpoldatasets.image

import casatasks

ms_path = "logo_cube.noise.ms"

imname = "logo_cube.dirty"
mpoldatasets.image.clear_extensions(imname)

imsize = 512

# produce a dirty image
casatasks.tclean(
    vis=ms_path,
    imagename=imname,
    specmode="cube",
    weighting="briggs",
    robust=0.5,
    imsize=800,
    cell="0.007arcsec",
    niter=0,
    interactive=False,
    perchanweightdensity=False,
    restoringbeam="common",
)

# produce a cleaned image
mpoldatasets.image.exportfits(imname + ".image", imname + ".fits")
# # A few Imaging and Calibration examples
#
# ## Image one channel

# In[481]:

# Call tclean
os.system('rm -rf try0.*')

tclean(
    vis='sim_data.ms',
    imagename='try0',
    datacolumn='data',
    spw='0:5',  # pick channel 5 and image it
    imsize=300,
    cell='8.0arcsec',
    specmode='mfs',
    gridder='standard',
    niter=200,
    gain=0.3)

# In[482]:

# Display the output restored image
dispImage('try0.image')

# In[483]:

# Display the point spread function
dispImage('try0.psf')
예제 #18
0
def quicklook_line_imaging(
        myvis,
        thisgal,
        linespw_dict,
        nchan_vel=5,
        # channel_width_kms=20.,
        niter=0,
        nsigma=5.,
        imsize_max=800,
        overwrite_imaging=False,
        export_fits=True):

    if not os.path.exists("quicklook_imaging"):
        os.mkdir("quicklook_imaging")

    this_vsys = target_vsys_kms[thisgal]

    # Pick our line range based on the HI for all lines.
    this_velrange = target_line_range_kms[thisgal]['HI']
    # We have a MW foreground window on some targets. Skip this for the galaxy range.
    if isinstance(this_velrange[0], list):
        for this_range in this_velrange:
            if min(this_range) < this_vsys < max(this_range):
                this_velrange = this_range
                break

    # Check that the search for the right velocity range didn't fail
    if isinstance(this_velrange[0], list):
        raise ValueError(
            f"Unable to find range with target vsys ({this_vsys}) from {this_velrange}."
            f" Check the velocity ranges defined in target_setup.py for {thisgal}"
        )

    # width_vel = channel_width_kms
    # width_vel_str = f"{width_vel}km/s"

    start_vel = f"{int(min(this_velrange))}km/s"

    # nchan_vel = int(abs(this_velrange[0] - this_velrange[1]) / width_vel)

    width_vel = int(
        round(abs(this_velrange[0] - this_velrange[1]) / float(nchan_vel)))
    width_vel_str = f"{width_vel}km/s"

    # Select only the non-continuum SPWs
    line_spws = []
    for thisspw in linespw_dict:
        if "continuum" not in linespw_dict[thisspw]['label']:
            # Our 20A-346 tracks have a combined OH1665/1667 SPW. Split into separate cubes in this case
            line_labels = linespw_dict[thisspw]['label'].split("-")

            for line_label in line_labels:
                line_spws.append([str(thisspw), line_label])

    # Select our target fields. We will loop through
    # to avoid the time + memory needed for mosaics.

    synthutil = synthesisutils()

    myms = ms()

    # if no fields are provided use observe_target intent
    # I saw once a calibrator also has this intent so check carefully
    # mymsmd.open(vis)
    myms.open(myvis)

    mymsmd = myms.metadata()

    target_fields = mymsmd.fieldsforintent("*TARGET*", True)

    mymsmd.close()
    myms.close()

    t0 = datetime.datetime.now()

    # Loop through targets and line SPWs
    for target_field in target_fields:

        casalog.post(f"Quick look imaging of field {target_field}")

        # Loop through the SPWs to identify the biggest image size needed.
        # For ease downstream, we will use the same imsize for all SPWs.
        # NOTE: for L-band, that's a factor of ~2 difference. It may be more pronounced in other
        # bands

        cell_size = {}
        imsizes = []

        for thisspw_info in line_spws:

            thisspw, line_name = thisspw_info

            # Ask for cellsize
            this_im = imager()
            this_im.selectvis(vis=myvis, field=target_field, spw=str(thisspw))

            image_settings = this_im.advise()
            this_im.close()

            # When all data is flagged, uvmax = 0 so cellsize = 0.
            # Check for that case to avoid tclean failures
            # if image_settings[2]['value'] == 0.:
            #     casalog.post(f"All data flagged for {this_imagename}. Skipping")
            #     continue

            # NOTE: Rounding will only be reasonable for arcsec units with our L-band setup.
            # Could easily fail on ~<0.1 arcsec cell sizes.
            cell_size[thisspw] = [
                image_settings[2]['value'], image_settings[2]['unit']
            ]

            # No point in estimating image size for an empty SPW.
            if image_settings[2]['value'] == 0.:
                continue

            # For the image size, we will do an approx scaling was
            # theta_PB = 45 / nu (arcmin)
            this_msmd = msmetadata()
            this_msmd.open(myvis)
            mean_freq = this_msmd.chanfreqs(
                int(thisspw)).mean() / 1.e9  # Hz to GHz
            this_msmd.close()

            approx_pbsize = 1.2 * (45. / mean_freq) * 60  # arcsec
            approx_imsize = synthutil.getOptimumSize(
                int(approx_pbsize / image_settings[2]['value']))
            imsizes.append(approx_imsize)

        if len(imsizes) == 0:
            casalog.post(f"{target_field} is fully flagged. Skipping.")
            continue

        this_imsize = min(imsize_max, max(imsizes))

        for thisspw_info in line_spws:

            thisspw, line_name = thisspw_info

            casalog.post(
                f"Quick look imaging of field {target_field} SPW {thisspw}")

            target_field_label = target_field.replace('-', '_')

            this_imagename = f"quicklook_imaging/quicklook-{target_field_label}-spw{thisspw}-{line_name}-{myvis}"

            if export_fits:
                check_exists = os.path.exists(f"{this_imagename}.image.fits")
            else:
                check_exists = os.path.exists(f"{this_imagename}.image")

            if check_exists:
                if overwrite_imaging:
                    rmtables(f"{this_imagename}*")
                    os.remove(f"{this_imagename}.image.fits")
                else:
                    casalog.post(f"Found {this_imagename}. Skipping imaging.")
                    continue

            if cell_size[thisspw][0] == 0:
                casalog.post(
                    f"All data flagged for {this_imagename}. Skipping")
                continue

            this_cellsize = f"{round(cell_size[thisspw][0] * 0.8, 1)}{cell_size[thisspw][1]}"

            this_pblim = 0.5

            this_nsigma = nsigma
            this_niter = niter

            # Clean up any possible imaging remnants first
            rmtables(f"{this_imagename}*")

            tclean(vis=myvis,
                   field=target_field,
                   spw=str(thisspw),
                   cell=this_cellsize,
                   imsize=this_imsize,
                   specmode='cube',
                   weighting='briggs',
                   robust=0.0,
                   start=start_vel,
                   width=width_vel_str,
                   nchan=nchan_vel,
                   niter=this_niter,
                   nsigma=this_nsigma,
                   imagename=this_imagename,
                   restfreq=f"{linerest_dict_GHz[line_name]}GHz",
                   pblimit=this_pblim)

            if export_fits:
                exportfits(imagename=f"{this_imagename}.image",
                           fitsimage=f"{this_imagename}.image.fits",
                           history=False,
                           overwrite=True)

            # Clean-up extra imaging products if they are not needed.
            cleanup_misc_quicklook(this_imagename,
                                   remove_psf=True,
                                   remove_residual=this_niter == 0,
                                   remove_image=True if export_fits else False)

    t1 = datetime.datetime.now()

    casalog.post(f"Quicklook line imaging took {t1 - t0}")
예제 #19
0
def quicklook_continuum_imaging(myvis,
                                contspw_dict,
                                niter=0,
                                nsigma=5.,
                                imsize_max=800,
                                overwrite_imaging=False,
                                export_fits=True):
    '''
    Per-SPW MFS, nterm=1, dirty images of the targets
    '''

    if not os.path.exists("quicklook_imaging"):
        os.mkdir("quicklook_imaging")

    # Select only the continuum SPWs (in case there are any line SPWs).
    continuum_spws = []
    for thisspw in contspw_dict:
        if "continuum" in contspw_dict[thisspw]['label']:
            continuum_spws.append(str(thisspw))

    # Select our target fields. We will loop through
    # to avoid the time + memory needed for mosaics.

    synthutil = synthesisutils()

    myms = ms()

    # if no fields are provided use observe_target intent
    # I saw once a calibrator also has this intent so check carefully
    # mymsmd.open(vis)
    myms.open(myvis)

    mymsmd = myms.metadata()

    target_fields = mymsmd.fieldsforintent("*TARGET*", True)

    mymsmd.close()
    myms.close()

    t0 = datetime.datetime.now()

    # Loop through targets and line SPWs
    for target_field in target_fields:

        casalog.post(f"Quick look imaging of field {target_field}")

        cell_size = {}
        imsizes = []

        for thisspw in continuum_spws:

            # Ask for cellsize
            this_im = imager()
            this_im.selectvis(vis=myvis, field=target_field, spw=str(thisspw))

            image_settings = this_im.advise()
            this_im.close()

            # When all data is flagged, uvmax = 0 so cellsize = 0.
            # Check for that case to avoid tclean failures
            # if image_settings[2]['value'] == 0.:
            #     casalog.post(f"All data flagged for {this_imagename}. Skipping")
            #     continue

            # NOTE: Rounding will only be reasonable for arcsec units with our L-band setup.
            # Could easily fail on ~<0.1 arcsec cell sizes.
            cell_size[thisspw] = [
                image_settings[2]['value'], image_settings[2]['unit']
            ]

            # No point in estimating image size for an empty SPW.
            if image_settings[2]['value'] == 0.:
                continue

            # For the image size, we will do an approx scaling was
            # theta_PB = 45 / nu (arcmin)
            this_msmd = msmetadata()
            this_msmd.open(myvis)
            mean_freq = this_msmd.chanfreqs(
                int(thisspw)).mean() / 1.e9  # Hz to GHz
            this_msmd.close()

            approx_pbsize = 1.2 * (45. / mean_freq) * 60  # arcsec
            approx_imsize = synthutil.getOptimumSize(
                int(approx_pbsize / image_settings[2]['value']))
            imsizes.append(approx_imsize)

        if len(imsizes) == 0:
            casalog.post(f"{target_field} is fully flagged. Skipping.")
            continue

        this_imsize = min(imsize_max, max(imsizes))

        for thisspw in continuum_spws:

            casalog.post(
                f"Quick look imaging of field {target_field} SPW {thisspw}")

            target_field_label = target_field.replace('-', '_')

            this_imagename = f"quicklook_imaging/quicklook-{target_field_label}-spw{thisspw}-continuum-{myvis}"

            if export_fits:
                check_exists = os.path.exists(f"{this_imagename}.image.fits")
            else:
                check_exists = os.path.exists(f"{this_imagename}.image")

            if check_exists:
                if overwrite_imaging:
                    rmtables(f"{this_imagename}*")
                    os.remove(f"{this_imagename}.image.fits")
                else:
                    casalog.post(f"Found {this_imagename}. Skipping imaging.")
                    continue

            if cell_size[thisspw][0] == 0:
                casalog.post(
                    f"All data flagged for {this_imagename}. Skipping")
                continue

            this_cellsize = f"{round(cell_size[thisspw][0] * 0.8, 1)}{cell_size[thisspw][1]}"

            this_pblim = 0.5

            this_nsigma = nsigma
            this_niter = niter

            # Clean up any possible imaging remnants first
            rmtables(f"{this_imagename}*")

            tclean(vis=myvis,
                   field=target_field,
                   spw=str(thisspw),
                   cell=this_cellsize,
                   imsize=this_imsize,
                   specmode='mfs',
                   nterms=1,
                   weighting='briggs',
                   robust=0.0,
                   niter=this_niter,
                   nsigma=this_nsigma,
                   fastnoise=True,
                   imagename=this_imagename,
                   pblimit=this_pblim)

            if export_fits:
                exportfits(imagename=f"{this_imagename}.image",
                           fitsimage=f"{this_imagename}.image.fits",
                           history=False,
                           overwrite=True)

            # Clean-up extra imaging products if they are not needed.
            cleanup_misc_quicklook(this_imagename,
                                   remove_psf=True,
                                   remove_residual=this_niter == 0,
                                   remove_image=True if export_fits else False)

    t1 = datetime.datetime.now()

    casalog.post(f"Quicklook continuum imaging took {t1 - t0}")
예제 #20
0
파일: datacomb.py 프로젝트: LydiaMF/dc2019
def runtclean(vis,
              imname,
              startmodel='',
              spw='',
              field='',
              specmode='mfs',
              imsize=[],
              cell='',
              phasecenter='',
              start=0,
              width=1,
              nchan=-1,
              restfreq=None,
              threshold='',
              niter=0,
              usemask='auto-multithresh',
              sidelobethreshold=2.0,
              noisethreshold=4.25,
              lownoisethreshold=1.5,
              minbeamfrac=0.3,
              growiterations=75,
              negativethreshold=0.0,
              mask='',
              pbmask=0.4,
              interactive=True):
    """
    runtclean (A. Plunkett, NRAO)
    a wrapper around the CASA task "TCLEAN,"

    vis - the MS containing the interferometric data
    imname - the root name of the output images
    startmodel - start model for cleaning
          default: '' i.e. no start model, example: 'TP.scaled.image'
    spw - the standard selection parameter spw of tclean
           default: '' i.e. all SPWs
    field - the standard selection parameter field of tclean
           default: '' i.e. all fields
    specmode - the standard tclean specmode parameter: supported are msf or cube
           default: msf 
    imsize - (optional) the standard tclean imsize parameter 
            should correspond to the imagesize for the most extended
            interferometer config.
           default: determine from the input MS with aU.pickCellSize
    cell - (optional) the standard tclean cell parameter
            should correspond to the cell size for the most extended
            interferometer config, i.e. smallest beam / 5.
           default: determine from the input MS with aU.pickCellSize
    phasecenter - the standard tclean phasecenter parameter
           e.g. 'J2000 12:00:00 -35.00.00.0000'
           default: '' - determine from the input MS with aU.pickCellSize
    start - the standard tclean start parameter
             default: 0
    width - the standard tclean width parameter
             default: 1
    nchan - the standard tclean nchan parameter
             default: -1
    restfreq - the restfrequency to write to the image for velocity calculations
             default: None, example: '115.271GHz'
    threshold - the threshold for cleaning
             default: None, example: '12mJy'
    niter - the standard tclean niter parameter
             default: 0, example: niter=1000000
    usemask - the standard tclean mask parameter.  If usemask='auto-multithresh', can specify:
             sidelobethreshold, noisethreshold, lownoisethreshold, minbeamfrac, growiterations - 
             if usemask='user', must specify mask='maskname.mask' 
             if usemask='pb', can specify pbmask=0.4, or some level.
             default: 'auto-multithresh'
    interactive - the standard tclean interactive option
             default: True
    
    Example: runtclean('gmc_120L.alma.all_int-weighted.ms', 
                'gmc_120L', phasecenter='J2000 12:00:00 -35.00.00.0000', 
                spw='0', field='0~68', imsize=[1120,1120], cell='0.21arcsec',
                threshold='12mJy',niter=100000,
                usemask='auto-multithresh')

    """

    if os.path.exists(vis):
        myvis = vis
    else:
        print(vis + ' does not exist')
        return False

    mymaskname = ''
    if usemask == 'auto-multithresh':
        mymask = usemask
        print('Run with {0} mask'.format(mymask))
    elif usemask == 'pb':
        mymask = usemask
        pbmask = pbmask
        print('Run with {0} mask {1}'.format(mymask, pbmask))
    elif usemask == 'user':
        if os.path.exists(maskname):
            mymask = usemask
            mymaskname = mask
            print('Run with {0} mask {1} '.format(mymask, maskname))
        else:
            print('mask ' + maskname + ' does not exist, or not specified')
            return False
    else:
        print('check the mask options')
        return False

    os.system('rm -rf ' + imname + '.TCLEAN.*')
    tclean(
        vis=myvis,
        imagename=imname + '.TCLEAN',
        startmodel=startmodel,
        field=field,
        intent='OBSERVE_TARGET#ON_SOURCE',
        phasecenter=phasecenter,
        stokes='I',
        spw=spw,
        outframe='LSRK',
        specmode=specmode,
        nterms=1,
        imsize=imsize,
        cell=cell,
        deconvolver='hogbom',
        niter=niter,
        cycleniter=niter,
        cyclefactor=2.0,
        weighting='briggs',
        robust=0.5,
        gridder='mosaic',
        pbcor=True,
        threshold=threshold,
        interactive=interactive,
        # Masking Parameters below this line
        # --> Should be updated depending on dataset
        usemask=mymask,
        sidelobethreshold=sidelobethreshold,
        noisethreshold=noisethreshold,
        lownoisethreshold=lownoisethreshold,
        minbeamfrac=minbeamfrac,
        growiterations=growiterations,
        negativethreshold=negativethreshold,
        mask=mask,
        pbmask=pbmask,
        verbose=True)

    print('Exporting final pbcor image to FITS ...')
    exportfits(imname + '.TCLEAN.image.pbcor', imname + '.TCLEAN.pbcor.fits')

    return True
예제 #21
0
 cts.tclean(
     cont_sub_file,
     #imagename = outdir+target+'_cube_multiscale_'+str(file_name)+'/'+'uvran_'+str(uvran_list[i]),
     imagename=outdir + target + '_cube_' + str(file_name) + '/' +
     'uvran_' + str(uvran_list[i]),
     field='0',
     spw='0',
     specmode='cube',
     nchan=-1,
     width=vel_res,
     outframe='bary',
     veltype='optical',
     restfreq=restfreq,
     deconvolver='hogbom',  # 'multiscale',
     #scales = [0, 5, 15],
     gridder='standard',
     uvrange=uvran_list[i],
     uvtaper=uvtaper_list[i],
     imsize=imsize_list[i],
     cell=cellsize_list[i],
     threshold=threshold_list[i],
     weighting='briggs',
     robust=0,
     restoringbeam='common',
     interactive=False,
     usemask='pb',
     pbmask=0.05,
     #usemask='auto-multithresh',
     #minbeamfrac=0.1,
     #sidelobethreshold = 1.5,
     #smallscalebias=0.6,
     niter=100000)
예제 #22
0
mpoldatasets.image.clear_extensions(imagename)

casatasks.delmod(vis=vis)

tic = time.perf_counter()

casatasks.tclean(
    vis=vis,
    imagename=imagename,
    specmode="mfs",
    deconvolver="multiscale",
    scales=[0, 5, 30, 75],
    weighting="briggs",
    robust=0.0,
    gain=0.3,
    imsize=3000,
    cell=".003arcsec",
    niter=50000,
    threshold="0.05mJy",
    cycleniter=300,
    cyclefactor=1,
    uvtaper=[".042arcsec", ".020arcsec", "172.1deg"],
    mask=common_mask,
    nterms=1,
    savemodel="modelcolumn",
)

toc = time.perf_counter()
print("tclean elapsed time {:}s".format(toc - tic))

mpoldatasets.image.exportfits(imagename + ".image", imagename + ".fits")
예제 #23
0
imname = "logo_cube.tclean"
mpoldatasets.image.clear_extensions(imname)

imsize = 512

tic = time.perf_counter()

# produce a dirty image
casatasks.tclean(
    vis=ms_path,
    imagename=imname,
    specmode="cube",
    weighting="briggs",
    deconvolver="multiscale",
    scales=[0, 5, 30, 75],
    robust=1.5,
    imsize=800,
    cell="0.007arcsec",
    niter=10000,
    threshold="0.5mJy",
    interactive=False,
    perchanweightdensity=False,
    restoringbeam="common",
    savemodel="modelcolumn",
)

toc = time.perf_counter()
print("tclean elapsed time {:}s".format(toc - tic))

# produce a cleaned image
mpoldatasets.image.exportfits(imname + ".image", imname + ".fits")