def copyModelToData(msname='sim_data_ALMA.ms'):
    """
    ### Copy visibilities from the MODEL column to the data columns
    ### This is required when predicting using tclean or ft as they will only write to the MODEL column
    Also, initialize the WEIGHT_SPECTRUM column so that the "visstat" task may be used later.....
    """
    tb.open(msname,nomodify=False);
    moddata = tb.getcol(columnname='MODEL_DATA');
    tb.putcol(columnname='DATA',value=moddata);
    #tb.putcol(columnname='CORRECTED_DATA',value=moddata);
    moddata.fill(0.0);
    tb.putcol(columnname='MODEL_DATA',value=moddata);
    tb.close();
    
    ## Add the WEIGHT_SPECTRUM column. This is needed only if you intend to use task_visstat on this dataset. 
    os.system('rm -rf tmp_addedcol.ms')
    mstransform(vis=msname,outputvis='tmp_addedcol.ms',datacolumn='DATA',usewtspectrum=True)
    os.system('rm -rf '+msname)
    os.system('cp -r tmp_addedcol.ms '+msname)
def split_ms(src_dir, img_dir, visname, msname, ATCA_band, pri, sec, tar, n_spw):
    os.system(f"rm -r {msname}")
    os.system(f"rm -r {msname}.flagversions")
    os.system("rm -r *.last")
    # have removed n_spw for mstransform and included it in the split just before imaging
    mstransform(
        vis=visname,
        outputvis=msname,
        datacolumn="data",
        field=f"{pri},{sec},{tar}",
        # nspw=n_spw,
        regridms=True,
        # field=f"{sec},{tar}",
        # scan="3,>90"
    )
    listobs(
        vis=msname,
        listfile=f"{src_dir}/listobs_{ATCA_band}_{tar}.dat",
        overwrite=True,
    )
    flagmanager(vis=msname, mode="save", versionname="after_transform")
    return
def split_imgms(data_dir, tar, epoch, ATCA_band, n_spw):
    if epoch in ["01", "03", "04", "05"]:
        timerange = f"2020/{epoch}/01/00:00:00~2020/{epoch}/30/23:59:59"
    else:
        timerange = ""
    visname = f"{data_dir}data/2020_{tar}_{ATCA_band}.ms"
    outputvis = f"{data_dir}data/2020-{epoch}_{tar}_{ATCA_band}.ms"
    if os.path.exists(outputvis):
        os.system(f"rm -r {outputvis}*")
    mstransform(
        vis=visname,
        outputvis=outputvis,
        datacolumn="corrected",
        # regridms=True,
        field=tar,
        timerange=timerange,
    )
    listobs(
        vis=outputvis,
        listfile=f"{data_dir}{tar}/listobs_2020-{epoch}_{tar}_{ATCA_band}_preimage.dat",
        overwrite=True,
    )
    return
示例#4
0
def flagcal(msfile, params, niters=1, flagger='default', interactive=False):
    '''
    This function takes an initially flagged msfile and goes through the flagging and calibration loops. It does delaycal, bandpass and a amplitude and phase cals. It then flags the data and then continues to the calibration cycle. The loops continue till niters end or in the interactive mode when the user is satisfied. The calibration in then applied to the source and it is mildly flagged using Rflag and splitted in a seperate msfile. 
    '''
    
    fcals = ','.join(fluxcal) # joining the fluxcals in a casa readable format
     
    
    # Main flagcal loops begin
    
    flagcal_fin = 'n' # a flag to denote finished flagging
    loop = 1 # Keeping a track of number of iterations 
    while flagcal_fin == 'n':
        # Beginning with an empty gaintable list, which will be succesively appended after every calibration.  
        gaintables = []
        params['calibration'].update({'gaintables': gaintables})
        prms.write(params, 'parameters.yaml') # writes the gaintables column in the parameters file. 
        cts.setjy(msfile, field=fcals, scalebychan=True, standard='Perley-Butler 2017', listmodels=False, usescratch=False)
    
        clb.delaycal(msfile, params, field=fcals)
        #append the caltable and save the params file
        params['calibration']['gaintables'].append(params['calibration']['delaytable'])
        prms.write(params, 'parameters.yaml')
        if interactive:
            caltable = params['calibration']['delaytable']
            subprocess.run('casaplotms vis={} field={}'.format(caltable, fcals), shell=True, check=True) 
            antflag = input("Do you want to flag any antenna(s) ? [y/n] \n If yes, append the bad antennas in the params file now.")
            if antflag == 'y':
                flg.badantflag(msfile, params)
            else:
                print('No flagging after delaycal.') 

        else:
            None

        clb.bpasscal(msfile, params, field=fcals) # Using fluxcals for bandpass calibration
        params['calibration']['gaintables'].append(params['calibration']['bpasstable'])
        prms.write(params, 'parameters.yaml')
             
        # Joining all the calibrators and doing amplitutde and phase calibration
        phasecal = params['general']['phasecal']
        print('Phasecal is ', phasecal)
        allcals = fluxcal + [phasecal]

        #for i in allcals:
        #    clb.apcal(msfile, params, field=i)
        
        allcals2 = ','.join(allcals)  
        clb.apcal(msfile, params, field=allcals2)

        # Adding the amplitude and phase calibration tables in gaintables
        params['calibration']['gaintables'].append(params['calibration']['apgaintable'])
        prms.write(params, 'parameters.yaml')
        
        
        uvran = params['general']['uvran']
        
        # GETJY
        print('Setting the absolute fluxscale for ', phasecal)
        fluxtbl = params['calibration']['fluxscaletable']
        
        phasecal_flux = cts.fluxscale(msfile, caltable=params['calibration']['apgaintable'], fluxtable=fluxtbl, reference=fcals, transfer=phasecal, incremental=False, append=False)
        print('The flux density of {}'.format(phasecal)+'is',phasecal_flux['1']['0']['fluxd'][0], '+/-', phasecal_flux['1']['0']['fluxdErr'][0] )
        #Replace the apgaintable with fluxscale table
        params['calibration']['gaintables'].remove(params['calibration']['apgaintable'])
        params['calibration']['gaintables'].append(fluxtbl)
        prms.write(params, 'parameters.yaml')
        subprocess.run('rm -r {}'.format(params['calibration']['apgaintable']), shell=True, check=True)
        
        # Applying the calibrations on flux cal
        for i in fluxcal:
            print('Applying the calibration table to ', i)
            
            cts.applycal(msfile, field=i, spw=spw, gaintable=gaintables, gainfield=['','',i], uvrange=uvran, interp=['','','linear'], calwt=[False], parang=False)
       
        # Apply the calibration on phase cal
        cts.applycal(msfile, field=phasecal, uvrange=uvran, gaintable=gaintables, gainfield=['','',phasecal], interp=['','','linear'], calwt=[False], parang=False)


        # Flagging after calibration
        def clip_est(fluxscale, nsigma, loop, beta):
            return [fluxscale - nsigma/loop**beta, fluxscale + nsigma/loop**beta]
        
        fluxscale_list = [15, 22.5, 3.3]
        uplims_list = [20, 20, 20] 
        beta=[0.5, 0.6, 0.6]
        tcut_list = [6, 6, 10]
        fcut_list = [6, 6, 10]
        if flagger == 'default':
            for j,i in enumerate(allcals):
                flg.clipper(msfile, params, field=i, cliplevel=clip_est(fluxscale_list[j], uplims_list[j], loop, beta[j]), instance='postcal')
                flg.tfcropper(msfile, params, field=i, tcut=tcut_list[j], fcut=fcut_list[j], instance='postcal')
                flg.extend(msfile, params, field=i, grow=80, instance='postcal')
                print('Flagged', i, 'postcalibration.')
            # Flag the phasecal using rflag
            flg.rflagger(msfile, params, field=phasecal, tcut=10, fcut=10, instance='postcal')
            flg.extend(msfile, params, field=phasecal, grow=80, instance='postcal')

        else:
            print('No flagging.')

        if interactive:
            subprocess.run('casaplotms vis={} field={}'.format(msfile, fluxcal[0]), shell=True, check=True)
            flagcal_fin = input('Are you satisfied with the calibration?[y/n] ')
            # Remove the old cal tables before continuing 
            for i in gaintables:
                subprocess.run('rm -r {}'.format(i), shell=True, check=True)
            
            continue
        else:
            print('Flagcal running in automode.')
            niters = niters - 1
            loop = loop + 1

            if niters < 1:
                print('Flagcal loops finished.')
                break
            else:
                # Remove all the old cal tables before continuing. 
                print('Clearing the corrected column and fluxcal model...')
                cts.clearcal(msfile) # Removes the corrected datacolumn
                cts.delmod(msfile) # Removes the setjy model
                
                print('Clearing all the gaintables...')
                for i in gaintables:
                    subprocess.run('rm -r {}'.format(i), shell=True, check=True)
                
                continue

    else:
        print('Calibration of flux and phase calibrators done.')

    # apply the calibrations on the target
    target = params['general']['target']
    print('Applying the calibration tables to the target...') 
    cts.applycal(msfile, field=target, uvrange=uvran, gaintable=gaintables, gainfield=['','',phasecal], interp=['','','linear'], calwt=[False], parang=False)

    
    # rflag the data
    print('Mildly Rflagging the target at cutoff of 10 sigma...')
    flg.rflagger(msfile, params, field=target, tcut=10, fcut=10, instance='postcal')
    flg.extend(msfile, params, field=target, grow=80, instance='postcal')

    # split the calibrated target data
    out_cal_file = params['general']['targetcalfile']
    
    print('Splitting the target...')
    cts.mstransform(msfile, field=target, spw=spw, chanaverage=False, datacolumn='corrected', outputvis=out_cal_file)
    
    return print('Flagcal script done.')
示例#5
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 
def split_ms_final(ms_name,
                   spw_dict,
                   data_column='CORRECTED',
                   target_name_prefix="",
                   line_intents='*TARGET*',
                   continuum_intents='*TARGET*',
                   time_bin='0s',
                   keep_flags=False,
                   keep_lines_only=True,
                   overwrite=False,
                   output_suffix=""):
    '''
    Split a calibrated MS into a final version with target or required
    calibrators (if continuum).


    Parameters
    ----------
    ms_name : str
        Name of MS.

    data_column : str, optional
        Column to keep in the split data. Default is "CORRECTED".

    target_name_prefix : str, optional
        Give string to match to target fields to split out.
        Not normally needed if the split intent is TARGET.

    line_intents : str, optional
        Intents to keep in split for the spectral line data. Default is "*TARGET*".

    continuum intents : str, optional
        Intents to keep in split for the continuum data. Default is "*" (all).
        NOTE: Eventually this may be changed to just the target fields, but we want
        to keep the calibrators for now to test polarization calibration.

    time_bin : str, optional
        Time average the data to reduce the volume. Default is "0s" (no time averaging).

    keep_flags : bool, optional
        Keep or removed flag data in the split. Default is False.

    keep_lines_only : bool, optional
        For the spectral lines, split out only the spectral line SPWs. In 20A-346
        tracks, this will remove all backup continuum SPWs. Default is True.

    overwrite : bool, optional
        Overwrite an existing MS with the same output name. Default is False.

    '''

    from casatasks import mstransform
    from casatools import logsink

    casalog = logsink()

    folder_base, ms_name_base = os.path.split(ms_name)

    if len(folder_base) == 0:
        folder_base = '.'

    if len(output_suffix) == 0:
        output_ms_name = f"{ms_name_base}.split"
    else:
        output_ms_name = f"{ms_name_base}.split_{output_suffix}"

    if overwrite and os.path.exists(output_ms_name):
        os.system(f"rm -r {output_ms_name}")

    if os.path.exists(output_ms_name):
        casalog.post(
            f"Found existing MS and overwrite=False. Skipping. Name: {output_ms_name}"
        )
        return

    # We're classifying based on "continuum" or "speclines" in the name.
    if 'speclines' in ms_name_base:

        # Remove the continuum SPWs that are backups for calibration
        if keep_lines_only:
            line_spws = []
            for thisspw in spw_dict:
                if "continuum" not in spw_dict[thisspw]['label']:
                    line_spws.append(str(thisspw))

            spw_select_str = ",".join(list(set(line_spws)))

        else:
            spw_select_str = ""

        mstransform(vis=ms_name,
                    outputvis="{0}/{1}".format(folder_base, output_ms_name),
                    spw=spw_select_str,
                    datacolumn=data_column,
                    intent=line_intents,
                    timebin=time_bin,
                    field=f"{target_name_prefix}*",
                    keepflags=keep_flags,
                    reindex=False)

    elif 'continuum' in ms_name_base:
        # do split
        # For now, we're keeping the whole MS intact in case data issues/additional
        # flagging is needed.

        mstransform(vis=ms_name,
                    outputvis="{0}/{1}".format(folder_base, output_ms_name),
                    spw="",
                    datacolumn=data_column,
                    intent=continuum_intents,
                    timebin=time_bin,
                    field=f"{target_name_prefix}*",
                    keepflags=keep_flags,
                    reindex=False)

    else:
        raise ValueError(
            f"Cannot find 'continuum' or 'speclines' in name {ms_name_base}")
def split_ms(ms_name,
             outfolder_prefix=None,
             split_type='all',
             continuum_kwargs={"baseband": 'both'},
             line_kwargs={
                 "include_rrls": False,
                 "keep_backup_continuum": True
             },
             overwrite=False,
             reindex=False,
             hanningsmooth_continuum=False):
    '''
    Split an MS into continuum and line SPWs.


    Parameters
    ----------
    ms_name : str
        Name of MS.

    spw_dict : dict
        Dictionary with SPW mapping. See 20A-346_spw_setup.py.

    outfolder_prefix : str, optional
        Basename of folder where the split MSs will be located.
        If None, this defaults to the ms_name + "continuum" or "speclines".
        When given, the folders will be outfolder_prefix + "continuum" or "speclines".

    split_type : str, optional
        Which SPW type to split out. Default is 'all' to split the continuum and lines.
        Otherwise use "continuum" or "line" to only split out one type.

    continuum_kwargs : dict, optional

    reindex : bool. optional
        Disable re-indexing the SPW numbers in mstransform. Defaults is False.

    hanningsmooth_continuum : bool, optional
        Apply Hanning smoothing to the continuum. Default is False.
        If enabled, do NOT use `hifv_hanning` in the pipeline!

    '''

    from casatasks import mstransform

    folder_base, ms_name_base = os.path.split(ms_name)

    ms_name_base = ms_name_base.rstrip(".ms")

    if outfolder_prefix is None:
        outfolder_prefix = ms_name_base

    do_split_continuum = False
    do_split_lines = False

    if split_type == "all":
        do_split_continuum = True
        do_split_lines = True
    elif split_type == 'continuum':
        do_split_continuum = True
    elif split_type == 'speclines':
        do_split_lines = True
    else:
        raise ValueError(
            "Unexpected input {} for split_type. ".format(split_type) +
            "Accepted inputs are 'all', 'continuum', 'speclines'.")

    # Define the spw mapping dictionary
    spw_dict = create_spw_dict(ms_name)

    if do_split_continuum:

        continuum_folder = os.path.join(
            folder_base, "{}_continuum".format(outfolder_prefix))

        if not os.path.exists(continuum_folder):
            os.mkdir(continuum_folder)
        else:
            # Delete existing version when overwrite is enabled
            if overwrite:
                os.system("rm -r {}/*".format(continuum_folder))

        continuum_spw_str = get_continuum_spws(spw_dict,
                                               return_string=True,
                                               **continuum_kwargs)

        mstransform(vis=ms_name,
                    outputvis="{0}/{1}.continuum.ms".format(
                        continuum_folder, ms_name_base),
                    spw=continuum_spw_str,
                    datacolumn='DATA',
                    hanning=hanningsmooth_continuum,
                    field="",
                    reindex=reindex)

    if do_split_lines:

        lines_folder = os.path.join(folder_base,
                                    "{}_speclines".format(outfolder_prefix))

        if not os.path.exists(lines_folder):
            os.mkdir(lines_folder)
        else:
            # Delete existing version when overwrite is enabled
            if overwrite:
                os.system("rm -r {}/*".format(lines_folder))

        line_spw_str = get_line_spws(spw_dict,
                                     return_string=True,
                                     **line_kwargs)

        mstransform(vis=ms_name,
                    outputvis="{0}/{1}.speclines.ms".format(
                        lines_folder, ms_name_base),
                    spw=line_spw_str,
                    datacolumn='DATA',
                    field="",
                    reindex=reindex)
示例#8
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.')