Run calibration pipeline. Assumes L-band.
        refant is list of antenna name strings (e.g., ['ea10']). default is to calculate based on distance from array center.
        antsel is list of ants to use (or reject) (e.g., ['!ea08'])
        uvrange is string giving uvrange (e.g., '<5klambda')
        fluxname, fluxname_full, and band are used to find flux calibrator info (e.g., '3C48', '0137+331=3C48', 'L').
        spw0 is spw selection for gain cal before bp cal (e.g., '0~1:60~75')
        spw1 is spw selection for gain cal after bp cal (e.g., '0~1:6~122')
        flaglist is the list of flag commands (e.g., ["mode='unflag'", "mode='shadow'", "mode='manual' antenna='ea11'"])

def run(self, refant=[], antsel=[], uvrange='', fluxname='', fluxname_full='', band='', spw0='', spw1='', flaglist=[]):
        """ Run calibration pipeline. Assumes L-band.
        refant is list of antenna name strings (e.g., ['ea10']). default is to calculate based on distance from array center.
        antsel is list of ants to use (or reject) (e.g., ['!ea08'])
        uvrange is string giving uvrange (e.g., '<5klambda')
        fluxname, fluxname_full, and band are used to find flux calibrator info (e.g., '3C48', '0137+331=3C48', 'L').
        spw0 is spw selection for gain cal before bp cal (e.g., '0~1:60~75')
        spw1 is spw selection for gain cal after bp cal (e.g., '0~1:6~122')
        flaglist is the list of flag commands (e.g., ["mode='unflag'", "mode='shadow'", "mode='manual' antenna='ea11'"])
        """

        os.chdir(self.workdir)

        if not len(refant):
            refant = self.find_refants()

        antposname = self.fileroot + '.antpos'   # antpos
        delayname = self.fileroot + '.delay'   # delay cal
        g0name = self.fileroot + '.g0'   # initial gain correction before bp
        b1name = self.fileroot + '.b1'   # bandpass file
        g1name = self.fileroot + '.g1'   # gain cal per scan
        g2name = self.fileroot + '.g2'   # flux scale applied

        # overload auto detected flux cal info, if desired
        if fluxname:
            self.fluxname = fluxname
        if band:
            self.band = band
        if fluxname_full:
            self.fluxname_full = fluxname

        # if flux calibrator available, use its model
        if self.fluxname and self.band:
            if self.band == 'P':
                calband = 'L'
            else:
                calband = self.band
            fluxmodel = '/home/casa/packages/RHEL5/release/casapy-41.0.24668-001-64b/data/nrao/VLA/CalModels/' + self.fluxname + '_' + calband + '.im'
        else:
            fluxmodel = ''

        # set up MS file
        msfile = self.genms()

        # flag data
        if flaglist:
            self.flagdata(msfile, flaglist=flaglist)
        elif os.path.exists(os.path.join(self.workdir, 'flags.txt')):
            self.flagdata(msfile, flagfile=os.path.join(self.workdir, 'flags.txt'))
        else:
            print 'No flagging.'

        # Calibrate!
        if fluxmodel:
            if not os.path.exists(g0name):
                print 'Applying flux model for BP calibrator...'
                cfg = tl.SetjyConfig()
                cfg.vis = msfile
                cfg.scan = self.bpstr
                cfg.modimage = fluxmodel
                cfg.standard = 'Perley-Butler 2010'    # for some reason 2013 standard can't find 3C48
                tl.setjy(cfg)

                print 'Starting initial gain cal...'
                cfg = tl.GaincalConfig()
                cfg.vis = msfile
                cfg.caltable = g0name
                cfg.gaintable = []
                cfg.scan = self.bpstr
                cfg.gaintype = 'G'
                cfg.solint = 'inf'
                cfg.spw = spw0
                cfg.refant = refant
                cfg.minsnr = 5.
                cfg.calmode = 'p'
                cfg.antenna = antsel
                cfg.uvrange = uvrange
                tl.gaincal(cfg)
            else:
                print '%s exists' % g0name

            if not os.path.exists(b1name):
                print 'Starting bp cal...'
                cfg = tl.GaincalConfig()
                cfg.vis = msfile
                cfg.caltable = b1name
                cfg.gaintable = [g0name]
                cfg.scan = self.bpstr
                cfg.spw = spw1
                cfg.gaintype = 'BPOLY'
                cfg.degamp = 5
                cfg.degphase = 2
                cfg.maskedge = 6
                cfg.solint = 'inf'
                cfg.combine = ['scan']
                cfg.solnorm = True
                cfg.refant = refant
                cfg.antenna = antsel
                cfg.uvrange = uvrange
                tl.gaincal(cfg)
            else:
                print '%s exists' % b1name

            if not os.path.exists(g1name) or not os.path.exists(g2name):
                print 'Starting gain cal...'
                cfg = tl.GaincalConfig()
                cfg.vis = msfile
                cfg.caltable = g1name
                cfg.gaintable = [b1name]
                cfg.scan = self.allstr
                cfg.gaintype = 'G'
                cfg.solint = 'inf'
                cfg.spw = spw1
                cfg.refant = refant
                cfg.minsnr = 5.
                cfg.calmode='ap'
                cfg.antenna = antsel
                cfg.uvrange = uvrange
                tl.gaincal(cfg)

                print 'Transferring flux scale...'
                cfg = tl.FluxscaleConfig()
                cfg.vis = msfile
                cfg.caltable = g1name
                cfg.fluxtable = g2name
                cfg.reference = self.fluxname_full
                tl.fluxscale(cfg)
            else:
                print 'either %s or %s exist' % (g1name, g2name)

        else:    # without fluxscale
            if not os.path.exists(g0name):
                print 'Starting initial gain cal...'
                cfg = tl.GaincalConfig()
                cfg.vis = msfile
                cfg.caltable = g0name
                cfg.gaintable = []
                cfg.scan = self.bpstr
                cfg.gaintype = 'G'
                cfg.solint = 'inf'
                cfg.spw = spw0
                cfg.refant = refant
                cfg.minsnr = 5.
                cfg.calmode = 'p'
                cfg.antenna = antsel
                cfg.uvrange = uvrange
                tl.gaincal(cfg)
            else:
                print '%s exists' % g0name

            if not os.path.exists(b1name):
                print 'Starting bp cal...'
                cfg = tl.GaincalConfig()
                cfg.vis = msfile
                cfg.caltable = b1name
                cfg.gaintable = [g0name]
                cfg.scan = self.bpstr
                cfg.spw = spw1
                cfg.gaintype = 'BPOLY'
                cfg.degamp = 5
                cfg.degphase = 2
                cfg.maskedge = 6
                cfg.solint = 'inf'
                cfg.combine = ['scan']
                cfg.solnorm = True
                cfg.refant = refant
                cfg.antenna = antsel
                cfg.uvrange = uvrange
                tl.gaincal(cfg)
            else:
                print '%s exists' % b1name

            if not os.path.exists(g1name):
                print 'Starting gain cal...'
                cfg = tl.GaincalConfig()
                cfg.vis = msfile
                cfg.caltable = g1name
                cfg.gaintable = [b1name]
                cfg.scan = self.allstr
                cfg.gaintype = 'G'
                cfg.solint = 'inf'
                cfg.spw = spw1
                cfg.refant = refant
                cfg.minsnr = 5.
                cfg.calmode='ap'
                cfg.antenna = antsel
                cfg.uvrange = uvrange
                tl.gaincal(cfg)
            else:
                print '%s exists' % g1name

        return 0