def solveCrossHandDelays(self, minsnr=3.0, solint='inf', combine='scan,spw', spw_interval="", channels="", refantmode="strict"):
        self.logger.info("Solving Cross-hand Delays")
        self.logger.info("Vis: "+ self.vis)
        self.logger.info("Field: "+ self.polanglefield)
        self.logger.info("Refant: "+ self.refant)

        self.casalog.post("Solving Cross-hand Delays", "INFO")
        self.casalog.post("Vis: "+ self.vis, "INFO")
        self.casalog.post("Field: "+ self.polanglefield, "INFO")
        self.casalog.post("Refant: "+ self.refant, "INFO")
        caltable = self.vis[:-3]+".Kcross"
        if os.path.exists(caltable): rmtables(caltable)
        firstspw=self.spw_ids[0]
        lastspw=self.spw_ids[-1]

        if spw_interval == "":
            if channels == "":
                spw = str(firstspw)+'~'+str(lastspw)
            else:
                spw = str(firstspw)+'~'+str(lastspw)+':'+channels
        else:
            if channels != "":
                spw = spw_interval+':'+channels
            else:
                spw = spw_interval

        self.logger.info("Spw: " + spw)
        self.casalog.post("Spw: " + spw, "INFO")
        gaincal(vis=self.vis, caltable=caltable, field=self.polanglefield, spw=spw, refant=self.kcross_refant, refantmode=refantmode, antenna=self.antennas, minsnr=minsnr, gaintype="KCROSS", solint=solint, combine=combine, calmode="ap", append=False, gaintable=[''], gainfield=[''], interp=[''], spwmap=[[]], parang=True)
        if not os.path.exists(caltable): sys.exit("Caltable was not created and cannot continue. Exiting...")
        plotms(vis=caltable, xaxis='frequency', yaxis='delay', antenna=self.kcross_refant, coloraxis='corr', showgui=False, plotfile=self.vis[:-3]+'.freqvsdelayKcross.png', overwrite=True)
        self.kcrosstable = caltable
        return caltable
    def calibratePolAngle(self, solint='inf', minsnr=3.0, poltype="Xf", spwmap=[], gaintable=[], gainfield=[], interpmode='linear', spw="", field=""):
        if(gaintable == []):
            if(self.kcrosstable == ""):
                gaintable=[self.leakagetable]
            else:
                gaintable=[self.kcrosstable, self.leakagetable]
        self.logger.info("Polarization angle calibration")
        self.logger.info("Vis: "+ self.vis)
        self.logger.info("Field: "+ self.polanglefield)

        self.casalog.post("Polarization angle calibration", "INFO")
        self.casalog.post("Vis: "+ self.vis, "INFO")
        self.casalog.post("Field: "+ self.polanglefield, "INFO")

        print("Gain tables: ", gaintable)

        self.logger.info("Refant: "+ self.refant)
        self.casalog.post("Refant: "+ self.refant, "INFO")

        caltable = self.vis[:-3]+".X0"
        if(gainfield == []): gainfield=[''] * len(gaintable)
        if os.path.exists(caltable): rmtables(caltable)
        firstspw=self.spw_ids[0]
        lastspw=self.spw_ids[-1]

        if spw == "":
            spw = str(firstspw)+'~'+str(lastspw)

        spwmap0 = [self.mapped_spw] * self.nspw

        if(spwmap == []):
            if(len(gaintable)-1 > 0):
                spwmap_empty = [[]] * (len(gaintable)-1) #subtract kcrosstable
                spwmap_empty.insert(0, spwmap0)
                spwmap = spwmap_empty
            else:
                spwmap=[spwmap0]

        interp = [interpmode] * len(gaintable)
        if(self.old_VLA):
            spw = ''
            spwmap = []
            interp = 'nearest'

        self.logger.info("Spw: " + spw)
        self.casalog.post("Spw: "+ spw, "INFO")
        print("Spwmap: ", spwmap)

        if field == "":
            polcal(vis=self.vis, caltable=caltable, field=self.polanglefield, spw=spw, refant=self.refant, antenna=self.antennas, poltype=poltype, solint=solint, combine='scan', spwmap=spwmap, interp=interp, minsnr=minsnr, gaintable=gaintable, gainfield=gainfield)
        else:
            polcal(vis=self.vis, caltable=caltable, field=field, spw=spw, refant=self.refant, antenna=self.antennas, poltype=poltype, solint=solint, combine='scan', spwmap=spwmap, interp=interp, minsnr=minsnr, gaintable=gaintable, gainfield=gainfield)

        if not os.path.exists(caltable): sys.exit("Caltable was not created and cannot continue. Exiting...")
        plotms(vis=caltable,xaxis='frequency',yaxis='phase',coloraxis='spw', showgui=False, plotfile=self.vis[:-3]+'.X0.phasevsfreq.png', overwrite=True)

        self.polangletable = caltable
        return caltable
    def setKnownModel(self, pol_source_object = None, standard="Perley-Butler 2017", field="", epoch="2017", nterms_angle=3, nterms_frac=3, usescratch=False):

        # get spectral idx coeffs from VLA tables
        intensity, spec_idx, spec_idx_err = pol_source_object.getKnownSourceInformation(nu_0=self.nu_0, standard=standard, epoch=epoch)
        pol_angle_coeffs, pol_angle_coeff_errs, pol_frac_coeffs, pol_frac_coeff_errs = pol_source_object.getSourcePolInformation(nterms_angle=nterms_angle, nterms_frac=nterms_frac, nu_min=self.nu_min, nu_max=self.nu_max)
        # get intensity in reference frequency
        self.logger.info("Setting model of: "+pol_source_object.getName())
        self.logger.info("Field: "+ field)
        self.logger.info("Reference freq (GHz): "+ str(self.nu_0/1e9))
        self.logger.info("I = "+ str(intensity))

        self.casalog.post("Setting model of: "+pol_source_object.getName(), "INFO")
        self.casalog.post("Field: "+ field, "INFO")
        self.casalog.post("Reference freq (GHz): "+ str(self.nu_0/1e9), "INFO")
        self.casalog.post("I = "+ str(intensity), "INFO")

        print("Alpha & Beta: ", spec_idx)
        print("Error: ", spec_idx_err)
        print("Pol fraction coeffs: ", pol_frac_coeffs)
        print("Error: ", pol_frac_coeff_errs)
        print("Pol angle coeffs: ", pol_angle_coeffs)
        print("Error: ", pol_angle_coeff_errs)
        source_dict = setjy(vis=self.vis, field=field, standard='manual', spw='', fluxdensity=[intensity,0,0,0], spix=spec_idx, reffreq=str(self.nu_0/1e9)+"GHz", polindex=pol_frac_coeffs, polangle=pol_angle_coeffs, interpolation="nearest", scalebychan=True, usescratch=usescratch)
        print(source_dict)
        plotms(vis=self.vis, field=field, correlation='RR', timerange='', antenna=self.refant, xaxis='frequency', yaxis='amp', ydatacolumn='model', showgui=False, plotfile=field+'_RRamp_model.png', overwrite=True)
        plotms(vis=self.vis, field=field, correlation='RL', timerange='', antenna=self.refant, xaxis='frequency', yaxis='amp', ydatacolumn='model', showgui=False, plotfile=field+'_RLamp_model.png', overwrite=True)
        plotms(vis=self.vis, field=field, correlation='RR', timerange='', antenna=self.refant, xaxis='frequency', yaxis='phase', ydatacolumn='model', showgui=False, plotfile=field+'_RRphase_model.png', overwrite=True)
        plotms(vis=self.vis, field=field, correlation='RL', timerange='', antenna=self.refant, xaxis='frequency', yaxis='phase', ydatacolumn='model', showgui=False, plotfile=field+'_RLphase_model.png', overwrite=True)
    def setUnknownModel(self, pol_source_object=None, field="", gaintable="", referencefield="", transferfield="", fitorder=1, usescratch=False):

        field_table = queryTable(table=self.vis, query="SELECT NAME FROM "+self.vis+"/FIELD")
        field_ids = field_table.rownumbers()
        fields = field_table.getcol("NAME")
        field_id_query = np.where(fields == field)[0][0]
        field_id = field_ids[field_id_query]
        print("Field "+field+" - ID: " + str(field_id))
        field_table.close()
        fluxtable = self.vis[:-3]+".F."+field
        if os.path.exists(fluxtable): rmtables(fluxtable)
        # From fluxscale documentation we know that the coefficients are return from the natural log nu/nu_0 Taylor expansion
        fluxdict = fluxscale(vis=self.vis, fluxtable=fluxtable, caltable=gaintable, reference=referencefield, transfer=transferfield, fitorder=fitorder)
        print(fluxdict)
        coeffs = fluxdict[str(field_id)]['spidx'].tolist()

        print("Coeffs: ", coeffs) # a0 log10(S at nu_0), a1 spectral idx, a2 spectral curvature
        pol_source_object.setCoeffs(coeffs)
        intensity = 10.0**(coeffs.pop(0)) # Extract a0 and make coeffs to have only spectral index and spectral curvature coefficients
        spec_idx = coeffs

        self.logger.info("Setting model of: "+pol_source_object.getName())
        self.logger.info("Field: "+ field)
        self.logger.info("Reference freq (GHz): "+ str(self.nu_0/1e9))
        self.logger.info("I(nu_0) = "+str(intensity))

        self.casalog.post("Setting model of: "+pol_source_object.getName(), "INFO")
        self.casalog.post("Field: "+ field, "INFO")
        self.casalog.post("Reference freq (GHz): "+ str(self.nu_0/1e9), "INFO")
        self.casalog.post("I(nu_0) = "+ str(intensity), "INFO")

        print("Alpha & Beta: ", spec_idx)
        source_dict = setjy(vis=self.vis, field=field, standard='manual', spw='', fluxdensity=[intensity,0,0,0], spix=spec_idx, reffreq=str(self.nu_0/1e9)+"GHz", interpolation="nearest", scalebychan=True, usescratch=usescratch)
        print(source_dict)
        plotms(vis=self.vis, field=field, correlation='RR', timerange='', antenna=self.refant, xaxis='frequency', yaxis='amp', ydatacolumn='model', showgui=False, plotfile=field+'_RRamp_model.png', overwrite=True)
        plotms(vis=self.vis, field=field, correlation='RL', timerange='', antenna=self.refant, xaxis='frequency', yaxis='amp', ydatacolumn='model', showgui=False, plotfile=field+'_RLamp_model.png', overwrite=True)
        plotms(vis=self.vis, field=field, correlation='RR', timerange='', antenna=self.refant, xaxis='frequency', yaxis='phase', ydatacolumn='model', showgui=False, plotfile=field+'_RRphase_model.png', overwrite=True)
        plotms(vis=self.vis, field=field, correlation='RL', timerange='', antenna=self.refant, xaxis='frequency', yaxis='phase', ydatacolumn='model', showgui=False, plotfile=field+'_RLphase_model.png', overwrite=True)
        return fluxtable
    def finalPlots(self):
        plotms(vis=self.vis, field=self.polanglefield, correlation='',
               timerange='',antenna='',avgtime='60',
               xaxis='frequency',yaxis='amp',ydatacolumn='corrected',
               coloraxis='corr',
               plotfile=self.polanglefield+'.corrected-amp.png', showgui=False, overwrite=True)

        plotms(vis=self.vis, field=self.polanglefield, correlation='',
               timerange='',antenna='',avgtime='60',
               xaxis='frequency',yaxis='phase',ydatacolumn='corrected',
               plotrange=[-1,-1,-180,180],coloraxis='corr',
               plotfile=self.polanglefield+'.corrected-phase.png', showgui=False, overwrite=True)

        plotms(vis=self.vis, field=self.leakagefield, correlation='',
               timerange='',antenna='',avgtime='60',
               xaxis='frequency',yaxis='amp',ydatacolumn='corrected', coloraxis='corr',
               plotfile=self.leakagefield+'.corrected-amp.png', showgui=False, overwrite=True)

        plotms(vis=self.vis, field=self.leakagefield, correlation='RR,LL',
               timerange='',antenna='',avgtime='60',
               xaxis='frequency',yaxis='phase',ydatacolumn='corrected',
               plotrange=[-1,-1,-180,180],coloraxis='corr',
               plotfile=self.leakagefield+'.corrected-phase.png', showgui=False, overwrite=True)
 def plotLeakage(self, plotdir="", field="", caltable=""):
     if field == "" and caltable=="":
         plotms(vis=self.leakagetable, xaxis='antenna', yaxis='amp', plotfile=plotdir+self.vis[:-3]+'.D0.amp.png', showgui=False, overwrite=True)
         plotms(vis=self.leakagetable, xaxis='antenna', yaxis='phase', iteraxis='antenna', plotfile=plotdir+self.vis[:-3]+'.D0.phs.png', showgui=False, overwrite=True)
         plotms(vis=self.leakagetable, xaxis='antenna', yaxis='snr', showgui=False, plotfile=plotdir+self.vis[:-3]+'.D0.snr.png', overwrite=True)
         plotms(vis=self.leakagetable, xaxis='real', yaxis='imag', showgui=False, plotfile=plotdir+self.vis[:-3]+'.D0.cmplx.png', overwrite=True)
     else:
         plotms(vis=caltable, xaxis='antenna', yaxis='amp', plotfile=plotdir+self.vis[:-3]+'.D.'+field+'amp.png', showgui=False, overwrite=True)
         plotms(vis=caltable, xaxis='antenna', yaxis='phase', iteraxis='antenna', plotfile=plotdir+self.vis[:-3]+'.D.'+field+'phs.png', showgui=False, overwrite=True)
         plotms(vis=caltable, xaxis='antenna', yaxis='snr', showgui=False, plotfile=plotdir+self.vis[:-3]+'.D.'+field+'snr.png', overwrite=True)
         plotms(vis=caltable, xaxis='real', yaxis='imag', showgui=False, plotfile=plotdir+self.vis[:-3]+'.D.'+field+'cmplx.png', overwrite=True)
    def calibrateLeakage(self, solint='inf', minsnr=3.0, poltype="Df", spwmap=[], gaintable=[], gainfield=[], clipmin=0.0, clipmax=0.25, flagclip=True, interpmode='linear', spw="", field=""):
        if(gaintable == []):
            if(self.kcrosstable == ""):
                gaintable=[]
            else:
                gaintable=[self.kcrosstable]
        self.logger.info("Leakage calibration")
        self.logger.info("Vis: "+ self.vis)


        self.casalog.post("Leakage calibration", "INFO")
        self.casalog.post("Vis: "+ self.vis, "INFO")


        print("Gain tables: ", gaintable)
        self.logger.info("Refant: "+ self.refant)
        self.casalog.post("Refant: "+ self.refant, "INFO")

        if field == "":
            caltable = self.vis[:-3]+".D0"
            self.logger.info("Field: "+ self.leakagefield)
            self.casalog.post("Field: "+ self.leakagefield, "INFO")
            print("Field "+self.leakagefield)
        else:
            caltable = self.vis[:-3]+".D."+field
            self.logger.info("Field: "+ field)
            self.casalog.post("Field: "+ field, "INFO")
            print("Field "+field)

        if(gainfield == []): gainfield=[''] * len(gaintable)
        if os.path.exists(caltable): rmtables(caltable)
        firstspw=self.spw_ids[0]
        lastspw=self.spw_ids[-1]

        if spw == "":
            spw = str(firstspw)+'~'+str(lastspw)

        spwmap0 = [self.mapped_spw] * self.nspw

        if(spwmap == []):
            if(len(gaintable)-1 > 0):
                spwmap_empty = [[]] * (len(gaintable)-1) #subtract kcrosstable
                spwmap_empty.insert(0, spwmap0)
                spwmap = spwmap_empty
            else:
                spwmap=[spwmap0]

        interp = [interpmode] * len(gaintable)

        if(self.old_VLA):
            spw = ''
            spwmap = []
            interp='nearest'

        self.logger.info("Spw: " + spw)
        self.casalog.post("Spw: " + spw, "INFO")
        print("Spwmap: ", spwmap)

        if field == "":
            polcal(vis=self.vis, caltable=caltable, field=self.leakagefield, spw=spw, refant=self.refant, antenna=self.antennas, poltype=poltype, solint=solint, spwmap=spwmap, combine='scan', interp=interp, minsnr=minsnr, gaintable=gaintable, gainfield=gainfield)
        else:
            polcal(vis=self.vis, caltable=caltable, field=field, spw=spw, refant=self.refant, antenna=self.antennas, poltype=poltype, solint=solint, spwmap=spwmap, combine='scan', interp=interp, minsnr=minsnr, gaintable=gaintable, gainfield=gainfield)

        if not os.path.exists(caltable): sys.exit("Caltable was not created and cannot continue. Exiting...")

        if(flagclip):
            flagdata(vis=caltable, mode='clip', correlation='ABS_ALL', clipminmax=[clipmin, clipmax], datacolumn='CPARAM', clipoutside=True, action='apply', flagbackup=False, savepars=False)
            flagmanager(vis=caltable, mode="save", versionname="clip_flagging", comment="Clip flagging outside ["+str(clipmin)+","+str(clipmax)+"]")

        plotms(vis=caltable,xaxis='freq',yaxis='amp',
       iteraxis='antenna',coloraxis='corr', showgui=False, plotfile=self.vis[:-3]+'.D0.ampvsfreq.png', overwrite=True)

        plotms(vis=caltable,xaxis='chan',yaxis='phase',
               iteraxis='antenna',coloraxis='corr',plotrange=[-1,-1,-180,180], showgui=False, plotfile=self.vis[:-3]+'.D0.phasevschan.png', overwrite=True)

        plotms(vis=caltable,xaxis='chan',yaxis='phase',
               iteraxis='antenna',coloraxis='corr',plotrange=[-1,-1,-180,180], showgui=False, plotfile=self.vis[:-3]+'.D0.ampvsantenna.png', overwrite=True)

        plotms(vis=caltable,xaxis='antenna1',yaxis='amp',coloraxis='corr', showgui=False, plotfile=self.vis[:-3]+'.D0.ampvsantenna1.png', overwrite=True)

        self.leakagetable = caltable
        return caltable
def split_and_cal(rootvis, rootprefix, spwn, fluxcal, phasecal, target,
        anttable=[], fieldpre=[], fresh_clean=False):
    print "Beginning calibration and mapping of spw ", spwn
    prefix = rootprefix+"_spw"+spwn

    # hopefully this decreases read/write times...
    vis = outvis = prefix+".ms"
    split(vis=rootvis, outputvis=outvis, spw=spwn, datacolumn='all')
    listobs(outvis)

    plotants(vis=vis,figfile=prefix+'plotants_'+vis+".png")
    #plotms(vis=vis, xaxis='', yaxis='', averagedata=False, transform=False, extendflag=False,
    #        plotfile='FirstPlot_AmpVsTime.png',selectdata=True,field='')
    plotms(vis=vis, spw='0', averagedata=True, avgchannel='64', avgtime='5', xaxis='uvdist', yaxis='amp', field=phasecal, plotfile=prefix+"phasecal_%s_AmpVsUVdist_spw%s.png" % (phasecal,spwn),overwrite=True)
    plotms(vis=vis, spw='0', averagedata=True, avgchannel='64', avgtime='5', xaxis='uvdist', yaxis='amp', field=fluxcal, plotfile=prefix+"fluxcal_%s_AmpVsUVdist_spw%s.png" % (fluxcal,spwn),overwrite=True)
    plotms(vis=vis, spw='0', averagedata=True, avgchannel='64', avgtime='5', xaxis='uvdist', yaxis='amp', field=target, plotfile=prefix+"target_%s_AmpVsUVdist_spw%s.png" % (target,spwn),overwrite=True)
    plotms(vis=vis, field='',correlation='RR,LL',timerange='',antenna='ea01',spw='0',
            xaxis='time',yaxis='antenna2',coloraxis='field',plotfile=prefix+'antenna2vsantenna1vstime_spw%s.png' % spwn,overwrite=True)

    #gencal(vis=vis,caltable=prefix+".antpos",caltype="antpos")

    # this apparently doubles the data size... clearcal(vis=vis,field='',spw='')
    #setjy(vis=vis, listmodels=T)
    gaincal(vis=vis, caltable=prefix+'.G0all', 
            field='0,1', refant='ea21', spw='0:32~96',
            gaintype='G',calmode='p', solint='int', 
            minsnr=5, gaintable=anttable)

    #didn't work
    plotcal(caltable=prefix+'.G0all',xaxis='time',yaxis='phase',
            spw='0',
            poln='R',plotrange=[-1,-1,-180,180],
            figfile=prefix+'.G0all.png')

    gaincal(vis=vis, caltable=prefix+'.G0', 
            field=fluxcal, refant='ea21', spw='0:20~100', calmode='p', solint='int', 
            minsnr=5, gaintable=anttable)

    plotcal(caltable=prefix+'.G0',xaxis='time',yaxis='phase',
            spw='0',
            poln='R',plotrange=[-1,-1,-180,180],
            figfile=prefix+'.G0.png')


    gaincal(vis=vis,caltable=prefix+'.K0', 
            field=fluxcal,refant='ea21',spw='0:5~123',gaintype='K', 
            solint='inf',combine='scan',minsnr=5,
            gaintable=anttable+[
                       prefix+'.G0'])

    plotcal(caltable=prefix+'.K0',xaxis='antenna',yaxis='delay',
            spw='0',
            figfile=prefix+'.K0_delayvsant_spw'+spwn+'.png')

    bandpass(vis=vis,caltable=prefix+'.B0',
             field=fluxcal,spw='0',refant='ea21',solnorm=True,combine='scan', 
             solint='inf',bandtype='B',
             gaintable=anttable+[
                        prefix+'.G0',
                        prefix+'.K0'])

    # In CASA
    plotcal(caltable= prefix+'.B0',poln='R', 
            spw='0',
            xaxis='chan',yaxis='amp',field=fluxcal,subplot=221, 
            figfile=prefix+'plotcal_fluxcal-B0-R-amp_spw'+spwn+'.png')
    #
    plotcal(caltable= prefix+'.B0',poln='L', 
            spw='0',
            xaxis='chan',yaxis='amp',field=fluxcal,subplot=221, 
            figfile=prefix+'plotcal_fluxcal-B0-L-amp_spw'+spwn+'.png')
    #
    plotcal(caltable= prefix+'.B0',poln='R', 
            spw='0',
            xaxis='chan',yaxis='phase',field=fluxcal,subplot=221, 
            plotrange=[-1,-1,-180,180],
            figfile=prefix+'plotcal_fluxcal-B0-R-phase_spw'+spwn+'.png')
    #
    plotcal(caltable= prefix+'.B0',poln='L', 
            spw='0',
            xaxis='chan',yaxis='phase',field=fluxcal,subplot=221, 
            plotrange=[-1,-1,-180,180],
            figfile=prefix+'plotcal_fluxcal-B0-L-phase_spw'+spwn+'.png')
             

    gaincal(vis=vis,caltable=prefix+'.G1',
            field=fluxcal,spw='0:5~123',
            solint='inf',refant='ea21',gaintype='G',calmode='ap',solnorm=F,
            gaintable=anttable+[
                       prefix+'.K0',
                       prefix+'.B0'])
            
    gaincal(vis=vis,caltable=prefix+'.G1',
            field=phasecal,
            spw='0:5~123',solint='inf',refant='ea21',gaintype='G',calmode='ap',
            gaintable=anttable+[
                       prefix+'.K0',
                       prefix+'.B0'],
            append=True)

    plotcal(caltable=prefix+'.G1',xaxis='time',yaxis='phase',
            spw='0',
            poln='R',plotrange=[-1,-1,-180,180],figfile=prefix+'plotcal_fluxcal-G1-phase-R_spw'+spwn+'.png')
    plotcal(caltable=prefix+'.G1',xaxis='time',yaxis='phase',
            spw='0',
            poln='L',plotrange=[-1,-1,-180,180],figfile=prefix+'plotcal_fluxcal-G1-phase-L_spw'+spwn+'.png')
    plotcal(caltable=prefix+'.G1',xaxis='time',yaxis='amp',
            spw='0',
            poln='R',figfile=prefix+'plotcal_fluxcal-G1-amp-R_spw'+spwn+'.png')
    plotcal(caltable=prefix+'.G1',xaxis='time',yaxis='amp',
            spw='0',
            poln='L',figfile=prefix+'plotcal_fluxcal-G1-amp-L_spw'+spwn+'.png')


    myscale = fluxscale(vis=vis,
                        caltable=prefix+'.G1', 
                        fluxtable=prefix+'.fluxscale1', 
                        reference=[fluxcal],
                        transfer=[phasecal])


    applycal(vis=vis,
             field=fluxcal,
             spw='0',
             gaintable=anttable+[ 
                        prefix+'.fluxscale1',
                        prefix+'.K0',
                        prefix+'.B0',
                        ],
             gainfield=fieldpre+[fluxcal,'',''], 
             interp=fieldpre+['nearest','',''],
             calwt=F)
     
    applycal(vis=vis,
             field=phasecal,
             spw='0',
             gaintable=anttable+[ 
                        prefix+'.fluxscale1',
                        prefix+'.K0',
                        prefix+'.B0',
                        ],
             gainfield=fieldpre+[phasecal,'',''], 
             interp=fieldpre+['nearest','',''],
             calwt=F)

    applycal(vis=vis,
             field=target,
             spw='0',
             gaintable=anttable+[ 
                        prefix+'.fluxscale1',
                        prefix+'.K0',
                        prefix+'.B0',
                        ],
             gainfield=fieldpre+[phasecal,'',''], 
             interp=fieldpre+['linear','',''],
             calwt=F)

    plotms(vis=prefix+'.ms',field=fluxcal,correlation='',
            spw='0',
           antenna='',avgtime='60s',
           xaxis='channel',yaxis='amp',ydatacolumn='corrected',
           plotfile=prefix+'_fluxcal-corrected-amp_spw'+spwn+'.png',overwrite=True)
    #
    plotms(vis=prefix+'.ms',field=fluxcal,correlation='',
            spw='0',
           antenna='',avgtime='60s',
           xaxis='channel',yaxis='phase',ydatacolumn='corrected',
           plotrange=[-1,-1,-180,180],coloraxis='corr',
           plotfile=prefix+'_fluxcal-corrected-phase_spw'+spwn+'.png',overwrite=True)
    #
    plotms(vis=prefix+'.ms',field=phasecal,correlation='RR,LL',
            spw='0',
           timerange='',antenna='',avgtime='60s',
           xaxis='channel',yaxis='amp',ydatacolumn='corrected',
           plotfile=prefix+'_phasecal-corrected-amp_spw'+spwn+'.png',overwrite=True)
    #
    plotms(vis=prefix+'.ms',field=phasecal,correlation='RR,LL',
            spw='0',
           timerange='',antenna='',avgtime='60s',
           xaxis='channel',yaxis='phase',ydatacolumn='corrected',
           plotrange=[-1,-1,-180,180],coloraxis='corr',
           plotfile=prefix+'_phasecal-corrected-phase_spw'+spwn+'.png',overwrite=True)

    plotms(vis=prefix+'.ms',field=phasecal,correlation='RR,LL',
            spw='0',
           timerange='',antenna='',avgtime='60s',
           xaxis='phase',xdatacolumn='corrected',yaxis='amp',ydatacolumn='corrected',
           plotrange=[-180,180,0,3],coloraxis='corr',
           plotfile=prefix+'_phasecal-corrected-ampvsphase_spw'+spwn+'.png',overwrite=True)

    plotms(vis=vis,xaxis='uvwave',yaxis='amp',
            spw='0',
           field=fluxcal,avgtime='30s',correlation='RR',
           plotfile=prefix+'_fluxcal-mosaic0-uvwave_spw'+spwn+'.png',overwrite=True)

    plotms(vis=vis,xaxis='uvwave',yaxis='amp',
            spw='0',
           field=target,avgtime='30s',correlation='RR',
           plotfile=prefix+'_target-mosaic0-uvwave_spw'+spwn+'.png',overwrite=True)

    for antenna in xrange(28):
        plotms(vis=vis, xaxis='time', yaxis='amp', spw='0',
                field='', avgchannel='64', coloraxis='corr',
                avgtime='5s',
                antenna='%i' % antenna,
                plotfile=prefix+"_antenna%02i_ampVStime.png" % antenna,
                overwrite=True)

    imagename = prefix+"_mfs"

    if fresh_clean:
        shutil.rmtree(imagename+".model")
        shutil.rmtree(imagename+".image")
        shutil.rmtree(imagename+".psf")
        shutil.rmtree(imagename+".flux")
        shutil.rmtree(imagename+".residual")

    clean(vis=outvis,
          imagename=imagename,
          field=target,spw='',
          mode='mfs', # use channel to get cubes
          nterms=1, # no linear polynomial
          niter=5000,
          gain=0.1, threshold='1.0mJy',
          psfmode='clark',
          multiscale=[0], 
          interactive=False,
          imsize=[2560,2560], 
          cell=['0.1arcsec','0.1arcsec'],
          stokes='I',
          weighting='uniform',
          allowchunk=True,
          usescratch=True)
    exportfits(imagename=imagename+".image", fitsimage=imagename+".fits", overwrite=True)
    print "Finished spw ",spwn
Esempio n. 9
0
 def ampuv(self, saveDir):
     plotms(self.msName,
            xaxis="uvdist",
            avgchannel="all",
            coloraxis="baseline",
            plotfile=saveDir)
Esempio n. 10
0
 def ampuv(self, saveDir):
     plotms(self.msName, xaxis="uvdist", avgchannel="all", coloraxis="baseline", plotfile=saveDir)