def run(self):
        caltable = ""
        for i in range(0, self.loops):
            caltable = 'apcal_' + str(i)
            self.caltables.append(caltable)
            rmtables(caltable)
            gaincal(vis=self.visfile, field=self.Imager.getField(), caltable=caltable, spw=self.Imager.getSpw(), gaintype=self.gaintype, refant=self.refant, calmode=self.calmode,
                    combine=self.combine, solint=self.solint[
                        i], minsnr=self.minsnr, minblperant=self.minblperant, gaintable=self.input_caltable, spwmap=self.spwmap,
                    solnorm=True)

            self.plot_selfcal(caltable, xaxis="time", yaxis="amp", iteration="antenna",
                              subplot=421, plotrange=[0, 0, 0.2, 1.8], want_plot=self.want_plot)

            versionname = 'before_apcal_' + str(i)
            flagmanager(vis=self.visfile, mode='save',
                        versionname=versionname)
            self.caltables_versions.append(versionname)

            applycal(vis=self.visfile, spwmap=[self.spwmap, self.spwmap], field=self.Imager.getField(), gaintable=[
                     self.input_caltable, caltable], gainfield='', calwt=False, flagbackup=False, interp=self.interp, applymode=self.applymode)

            imagename = self.imagename + '_ap' + str(i)

            self.Imager.run(imagename)

            if(self.flag_dataset_bool):
                flag_dataset(mode=self.flag_mode)

            self.psnr_history.append(self.Imager.getPSNR())

            print("Solint: " + str(self.solint[i]) +
                  " - PSNR: " + str(self.psnr_history[i]))
            print("Noise: " + str(self.Imager.getSTDV() * 1000.0) + " mJy/beam")
            if(self.restore_PSNR):
                if(i > 0):
                    if(self.psnr_history[i] < self.psnr_history[i - 1]):
                        self.restore_selfcal(
                            caltable_version=self.caltables_versions[i - 1])
                        self.psnr_history.pop()
                        self.caltables_versions.pop()
                        self.caltables.pop()
                        print(
                            "PSNR decreasing in this solution interval - restoring to last MS and exiting loop")
                        break
                else:
                    if(self.selfcal_object):
                        if(self.psnr_history[i] < self.selfcal_object.getPSNRHistory()[-1]):
                            self.restore_selfcal(
                                caltable_version=self.selfcal_object.getCaltablesVersions()[-1])
                            self.psnr_history.pop()
                            self.caltables_versions.pop()
                            self.caltables.pop()
                            self.caltables = self.selfcal_object.getCaltables()
                            self.psnr_history = self.selfcal_object.getPSNRHistory()
                            self.caltables_versions = self.selfcal_object.getCaltablesVersions()
                            print(
                                "PSNR decreasing in this solution interval - restoring to last MS and exiting loop")
                            break
    def run(self):
        caltable = "before_selfcal"
        flagmanager(vis=self.visfile, mode='save',
                    versionname=caltable)
        self.caltables_versions.append(caltable)
        imagename = self.imagename + '_original'
        self.Imager.run(imagename)
        print("Original: - PSNR: " + str(self.Imager.getPSNR()))
        print("Noise: " + str(self.Imager.getSTDV() * 1000.0) + " mJy/beam")
        self.psnr_history.append(self.Imager.getPSNR())

        for i in range(0, self.loops):
            caltable = 'pcal' + str(i)
            self.caltables.append(caltable)
            rmtables(caltable)

            gaincal(vis=self.visfile, caltable=caltable, field=self.Imager.getField(), spw=self.Imager.getSpw(), gaintype=self.gaintype, refant=self.refant,
                    calmode=self.calmode, combine=self.combine, solint=self.solint[i], minsnr=self.minsnr, minblperant=self.minblperant)

            self.plot_selfcal(caltable, xaxis="time", yaxis="phase", iteration="antenna",
                              subplot=421, plotrange=[0, 0, -180, 180], want_plot=self.want_plot)

            versionname = 'before_phasecal_' + str(i)
            flagmanager(vis=self.visfile, mode='save',
                        versionname=versionname)
            self.caltables_versions.append(versionname)

            applycal(vis=self.visfile, field=self.Imager.getField(), spwmap=self.spwmap, gaintable=[
                     caltable], gainfield='', calwt=False, flagbackup=False, interp=self.interp, applymode=self.applymode)

            imagename = self.imagename + '_ph' + str(i)

            self.Imager.run(imagename)

            if(self.flag_dataset_bool):
                flag_dataset(mode=self.flag_mode)

            self.psnr_history.append(self.Imager.getPSNR())

            print("Solint: " + str(self.solint[i]) +
                  " - PSNR: " + str(self.psnr_history[-1]))
            print("Noise: " + str(self.Imager.getSTDV() * 1000.0) + " mJy/beam")
            if(self.restore_PSNR):
                print(self.psnr_history)
                print(self.caltables_versions)
                print(self.caltables)
                if(self.psnr_history[-1] < self.psnr_history[-2]):
                    self.restore_selfcal(
                        caltable_version=self.caltables_versions[i])
                    self.psnr_history.pop()
                    self.caltables_versions.pop()
                    self.caltables.pop()
                    print(
                        "PSNR decreasing in this solution interval - restoring to last MS and exiting loop")
                    break
 def applySolutions3(self, gainfield=[], applymode="calflagstrict"):
     gaintable=[self.kcrosstable, self.leakagetable]
     firstspw=self.spw_ids[0]
     lastspw=self.spw_ids[-1]
     spw = str(firstspw)+'~'+str(lastspw)
     self.logger.info("Spw: "+ spw)
     self.casalog.post("Spw: "+ spw, "INFO")
     spwmap0 = [self.mapped_spw] * self.nspw
     interp = [''] * len(gaintable)
     calwt = [False] * len(gaintable)
     if(gainfield == []): gainfield = ['']
     applycal(vis=self.vis, field='', spw=spw, gaintable=gaintable, spwmap=[spwmap0], calwt=calwt, applymode=applymode, interp=interp, gainfield=gainfield, antenna='*&*', parang=True, flagbackup=True)
Example #4
0
def _fillweight(vis):
    if not os.path.exists(vis):
        return

    casalog.post('fill WEIGHT and SIGMA columns for %s ...' % (vis))

    # work with private cb tool
    with sdutil.cbmanager(vis, compress=False, addcorr=False,
                          addmodel=False) as cb:
        status = cb.initweights()

    if status:
        # cb.initweights() succeeded so try to apply Tsys to
        # weight column
        # procedure:
        # 1. generate temporary Tsys caltable
        # 2. apply temporary Tsys caltable to vis
        # 3. remove temporary Tsys caltable
        import time
        from gencal import gencal
        from applycal import applycal
        caltable = 'temporary_caltable%s.tsys' % (str(time.time()).replace(
            '.', ''))
        casalog.post('tempolary caltable name: %s' % (caltable))
        try:
            gencal(vis=vis, caltable=caltable, caltype='tsys')
            # add 0.5*INTERVAL to the TIME values in caltable to make them time of
            # integration midpoint, because gencal currently sets (TIME_VIS -
            # INTERVAL_VIS/2), namely start time, into TIME in its output based on
            # ALMA's conventions...(2014/6/17 WK)
            with sdutil.tbmanager(caltable, nomodify=False) as tbcal:
                with sdutil.tbmanager(vis) as tbvis:
                    interval = tbvis.getcol('INTERVAL')[0]
                tbcal.putcol('TIME', tbcal.getcol('TIME') + 0.5 * interval)
            applycal(vis=vis,
                     docallib=False,
                     gaintable=[caltable],
                     applymode='calonly')
        except Exception, e:
            # Tsys application failed so that reset WEIGHT and SIGMA to 1.0
            _resetweight(vis)
            raise e
        finally:
    def applySolutions(self, spwmap=[], gaintable=[], gainfield=[], applymode="calflagstrict", antenna='*&*', flagbackup=True):
        #leakagegain.append(fluxtable)
        if(gaintable == []):
            if(self.kcrosstable == ""):
                gaintable=[self.leakagetable, self.polangletable]
            else:
                gaintable=[self.kcrosstable, self.leakagetable, self.polangletable]
        self.logger.info("Applying solutions")
        self.casalog.post("Applying solutions", "INFO")
        print("Gain tables: ", gaintable)
        firstspw=self.spw_ids[0]
        lastspw=self.spw_ids[-1]

        interp = [''] * len(gaintable)
        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]

        calwt = [False] * len(gaintable)
        selectdata=True
        if(self.old_VLA):
            interp = 'nearest'
            spwmap = []
            spw = ''
            calwt = [False]
            selectdata=False
            antenna=''

        self.logger.info("Spw: "+ spw)
        self.casalog.post("Spw: "+ spw, "INFO")
        print("Spwmap: ", spwmap)
        if(gainfield == []): gainfield = [''] * len(gaintable)
        applycal(vis=self.vis, field='', spw=spw, gaintable=gaintable, selectdata=selectdata, spwmap=spwmap, calwt=calwt, applymode=applymode, interp=interp, gainfield=gainfield, antenna=antenna, parang=True, flagbackup=flagbackup)
Example #6
0
def _fillweight(vis):
    if not os.path.exists(vis):
        return

    casalog.post('fill WEIGHT and SIGMA columns for %s ...'%(vis))

    # work with private cb tool
    with sdutil.cbmanager(vis, compress=False, addcorr=False, addmodel=False) as cb:
        status = cb.initweights()

    if status:
        # cb.initweights() succeeded so try to apply Tsys to
        # weight column
        # procedure:
        # 1. generate temporary Tsys caltable
        # 2. apply temporary Tsys caltable to vis
        # 3. remove temporary Tsys caltable
        import time
        from gencal import gencal
        from applycal import applycal
        caltable = 'temporary_caltable%s.tsys'%(str(time.time()).replace('.',''))
        casalog.post('tempolary caltable name: %s'%(caltable))
        try:
            gencal(vis=vis, caltable=caltable, caltype='tsys')
            # add 0.5*INTERVAL to the TIME values in caltable to make them time of
            # integration midpoint, because gencal currently sets (TIME_VIS -
            # INTERVAL_VIS/2), namely start time, into TIME in its output based on
            # ALMA's conventions...(2014/6/17 WK)
            with sdutil.tbmanager(caltable, nomodify=False) as tbcal:
                with sdutil.tbmanager(vis) as tbvis:
                    interval = tbvis.getcol('INTERVAL')[0]
                tbcal.putcol('TIME', tbcal.getcol('TIME') + 0.5*interval)
            applycal(vis=vis, docallib=False, gaintable=[caltable], applymode='calonly')
        except Exception, e:
            # Tsys application failed so that reset WEIGHT and SIGMA to 1.0
            _resetweight(vis)
            raise e
        finally:
Example #7
0
                         "IncrementalSelfcalScript")
            casalog.post(
                "caltables set to {0}, calinfo set to {1}".format(
                    caltables, calinfo), "INFO", "IncrementalSelfcalScript")
            print("Skipping {0}".format(imagename))
            continue

        if len(caltables) > 0:
            print("Applying caltables {0}".format(caltables))
            casalog.post("Applying caltables {0}".format(caltables), "INFO",
                         "IncrementalSelfcalScript")
            applycal(
                vis=selfcal_vis,
                field=field,
                gaintable=caltables,
                spwmap=[[0] * nspws if calinfo[ii]['combine'] == 'spw' else []
                        for ii in range(len(caltables))],
                interp=
                'linear,linear',  #['linearperobs,linear' if combine=='spw' else 'linearperobs,linear']*len(caltables),
                applymode='calonly',
                calwt=True)

        for ttsuffix in ('.tt0', '.tt1', '.tt2'):
            for suffix in (
                    'pb{tt}',
                    'weight',
                    'sumwt{tt}',
                    'psf{tt}',
                    'model{tt}',
                    'mask',
                    'image{tt}',
                    'residual{tt}',
 def applySingleSolution(self, field='', spw='', gaintable=[], gainfield=[], selectdata=True, spwmap=[], calwt=[False], applymode="calflagstrict", interp='linear', antenna='', flagbackup=True):
     applycal(vis=self.vis, field='', spw=spw, gaintable=gaintable, gainfield=gainfield, selectdata=selectdata, spwmap=spwmap, calwt=calwt, applymode=applymode, interp=interp, antenna=antenna, parang=True, flagbackup=flagbackup)
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
Example #10
0
def apply_calibration(vis, caltables, **kwargs):
    print 'Applying Calibrations to the Visibility'
    applycal(vis, gaintable=caltables)