예제 #1
0
파일: NX01_psr.py 프로젝트: stasbabak/NX01
    def grab_all_vars(self,
                      jitterbin=10.,
                      makeGmat=False,
                      fastDesign=True):  # jitterbin is in seconds

        print "--> Processing {0}".format(self.T2psr.name)

        # basic quantities
        self.name = self.T2psr.name
        self.toas = np.double(self.T2psr.toas())
        self.res = np.double(self.T2psr.residuals())
        self.toaerrs = np.double(self.T2psr.toaerrs) * 1e-6
        self.obs_freqs = np.double(self.T2psr.ssbfreqs())
        self.Mmat = np.double(self.T2psr.designmatrix())

        isort, iisort = None, None
        if 'pta' in self.T2psr.flags():
            if 'NANOGrav' in list(set(self.T2psr.flagvals('pta'))):
                # now order everything
                try:
                    isort, iisort = utils.argsortTOAs(
                        self.toas,
                        self.T2psr.flagvals('group'),
                        which='jitterext',
                        dt=jitterbin / 86400.)
                except KeyError:
                    isort, iisort = utils.argsortTOAs(self.toas,
                                                      self.T2psr.flagvals('f'),
                                                      which='jitterext',
                                                      dt=jitterbin / 86400.)

                # sort data
                self.toas = self.toas[isort]
                self.toaerrs = self.toaerrs[isort]
                self.res = self.res[isort]
                self.obs_freqs = self.obs_freqs[isort]
                self.Mmat = self.Mmat[isort, :]

                print "--> Initial sorting of data."

        # get the sky position
        if 'RAJ' and 'DECJ' in self.T2psr.pars():
            self.psr_locs = [
                np.double(self.T2psr['RAJ'].val),
                np.double(self.T2psr['DECJ'].val)
            ]
        elif 'ELONG' and 'ELAT' in self.T2psr.pars():
            fac = 180. / np.pi
            # check for B name
            if 'B' in self.name:
                epoch = '1950'
            else:
                epoch = '2000'
            coords = Equatorial(Ecliptic(str(self.T2psr['ELONG'].val * fac),
                                         str(self.T2psr['ELAT'].val * fac)),
                                epoch=epoch)
            self.psr_locs = [float(repr(coords.ra)), float(repr(coords.dec))]

        print "--> Grabbed the pulsar position."
        ################################################################################################

        # These are all the relevant system flags used by the PTAs.
        system_flags = ['group', 'sys', 'i', 'f']
        self.sysflagdict = OrderedDict.fromkeys(system_flags)

        # Put the systems into a dictionary which
        # has the locations of their toa placements.
        for systm in self.sysflagdict:
            try:
                if systm in self.T2psr.flags():
                    sys_uflagvals = list(set(self.T2psr.flagvals(systm)))
                    self.sysflagdict[systm] = OrderedDict.fromkeys(
                        sys_uflagvals)
                    for kk, subsys in enumerate(sys_uflagvals):
                        if isort is not None:
                            self.sysflagdict[systm][subsys] = \
                              np.where(self.T2psr.flagvals(systm)[isort] == sys_uflagvals[kk])
                        elif isort is None:
                            self.sysflagdict[systm][subsys] = \
                              np.where(self.T2psr.flagvals(systm) == sys_uflagvals[kk])
            except KeyError:
                pass

        # If we have some NANOGrav data, then separate
        # this off for later ECORR assignment.
        if 'pta' in self.T2psr.flags():
            pta_names = list(set(self.T2psr.flagvals('pta')))
            pta_mask = [
                self.T2psr.flagvals('pta')[isort] == ptaname
                for ptaname in pta_names
            ]
            pta_maskdict = OrderedDict.fromkeys(pta_names)
            for ii, item in enumerate(pta_maskdict):
                pta_maskdict[item] = pta_mask[ii]
            if len(pta_names) != 0 and ('NANOGrav' in pta_names):
                try:
                    nanoflagdict = OrderedDict.fromkeys(['nano-f'])
                    nano_flags = list(
                        set(
                            self.T2psr.flagvals('group')[
                                pta_maskdict['NANOGrav']]))
                    nanoflagdict['nano-f'] = OrderedDict.fromkeys(nano_flags)
                    for kk, subsys in enumerate(nano_flags):
                        nanoflagdict['nano-f'][subsys] = \
                          np.where(self.T2psr.flagvals('group')[isort] == nano_flags[kk])
                    self.sysflagdict.update(nanoflagdict)
                except KeyError:
                    nanoflagdict = OrderedDict.fromkeys(['nano-f'])
                    nano_flags = list(
                        set(
                            self.T2psr.flagvals('f')[
                                pta_maskdict['NANOGrav']]))
                    nanoflagdict['nano-f'] = OrderedDict.fromkeys(nano_flags)
                    for kk, subsys in enumerate(nano_flags):
                        nanoflagdict['nano-f'][subsys] = \
                          np.where(self.T2psr.flagvals('f')[isort] == nano_flags[kk])
                    self.sysflagdict.update(nanoflagdict)

        # If there are really no relevant flags,
        # then just make a full list of the toa indices.
        if np.all([self.sysflagdict[sys] is None for sys in self.sysflagdict]):
            print "No relevant flags found"
            print "Assuming one overall system for {0}\n".format(
                self.T2psr.name)
            self.sysflagdict[self.T2psr.name] = OrderedDict.fromkeys(
                [self.T2psr.name])
            self.sysflagdict[self.T2psr.name][self.T2psr.name] = np.arange(
                len(self.toas))

        print "--> Processed all relevant flags plus associated locations."
        ##################################################################################################

        if 'pta' in self.T2psr.flags():
            if 'NANOGrav' in pta_names:
                # now order everything
                try:
                    #isort_b, iisort_b = utils.argsortTOAs(self.toas, self.T2psr.flagvals('group')[isort],
                    #which='jitterext', dt=jitterbin/86400.)
                    flags = self.T2psr.flagvals('group')[isort]
                except KeyError:
                    #isort_b, iisort_b = utils.argsortTOAs(self.toas, self.T2psr.flagvals('f')[isort],
                    #which='jitterext', dt=jitterbin/86400.)
                    flags = self.T2psr.flagvals('f')[isort]

                # sort data
                #self.toas = self.toas[isort_b]
                #self.toaerrs = self.toaerrs[isort_b]
                #self.res = self.res[isort_b]
                #self.obs_freqs = self.obs_freqs[isort_b]
                #self.Mmat = self.Mmat[isort_b, :]
                #flags = flags[isort_b]

                print "--> Sorted data."

                # get quantization matrix
                avetoas, self.Umat, Ui = utils.quantize_split(self.toas,
                                                              flags,
                                                              dt=jitterbin /
                                                              86400.,
                                                              calci=True)
                print "--> Computed quantization matrix."

                self.detsig_avetoas = avetoas.copy()
                self.detsig_Uinds = utils.quant2ind(self.Umat)

                # get only epochs that need jitter/ecorr
                self.Umat, avetoas, aveflags = utils.quantreduce(
                    self.Umat, avetoas, flags)
                print "--> Excized epochs without jitter."

                # get quantization indices
                self.Uinds = utils.quant2ind(self.Umat)
                self.epflags = flags[self.Uinds[:, 0]]

                print "--> Checking TOA sorting and quantization..."
                print utils.checkTOAsort(self.toas,
                                         flags,
                                         which='jitterext',
                                         dt=jitterbin / 86400.)
                print utils.checkquant(self.Umat, flags)
                print "...Finished checks."

        # perform SVD of design matrix to stabilise
        if fastDesign:
            print "--> Stabilizing the design matrix the fast way..."

            Mm = self.Mmat.copy()
            norm = np.sqrt(np.sum(Mm**2, axis=0))
            Mm /= norm

            self.Gc = Mm
        else:
            print "--> Performing SVD of design matrix for stabilization..."

            u, s, v = np.linalg.svd(self.Mmat)

            if makeGmat:
                self.G = u[:, len(s):len(u)]
                self.Gres = np.dot(self.G.T, self.res)

            self.Gc = u[:, :len(s)]

        print "--> Done reading in pulsar :-) \n"
예제 #2
0
    def grab_all_vars(self, jitterbin=10., makeGmat=False, fastDesign=True,
                      planetssb=False, allEphem=False,
                      startMJD=None, endMJD=None):
        # jitterbin is in seconds

        print "--> Processing {0}".format(self.T2psr.name)

        # basic quantities
        self.name = self.T2psr.name
        self.psrPos = self.T2psr.psrPos
        if 'ELONG' and 'ELAT' in self.T2psr.pars():
            # converting to equatorial
            print "--> Converting pulsar position time-series to equatorial"
            self.psrPos = utils.ecl2eq_vec(self.psrPos)
        self.toas = np.double(self.T2psr.toas())
        self.res = np.double(self.T2psr.residuals())
        self.toaerrs = np.double(self.T2psr.toaerrs) * 1e-6
        self.obs_freqs = np.double(self.T2psr.ssbfreqs())
        self.Mmat = np.double(self.T2psr.designmatrix())
        self.flags = self.T2psr.flags()
        self.flagvals = OrderedDict.fromkeys(self.flags)
        for flag in self.flags:
            self.flagvals[flag] = self.T2psr.flagvals(flag)

        # getting ephemeris properties
        self.ephemeris = self.T2psr.ephemeris
        if '436' in self.T2psr.ephemeris:
            self.ephemname = 'DE436'
        elif '435' in self.T2psr.ephemeris:
            self.ephemname = 'DE435'
        elif '430' in self.T2psr.ephemeris:
            self.ephemname = 'DE430'
        elif '421' in self.T2psr.ephemeris:
            self.ephemname = 'DE421'
        elif '418' in self.T2psr.ephemeris:
            self.ephemname = 'DE418'

        # populating roemer-delay dictionary
        self.roemer = OrderedDict()
        self.roemer[self.ephemname] = np.double(self.T2psr.roemer)

        # time filtering
        if startMJD is not None and endMJD is not None:
            self.tmask = np.logical_and(self.T2psr.toas() >= startMJD,
                                        self.T2psr.toas() <= endMJD)

            self.psrPos = self.psrPos[self.tmask]
            self.toas = self.toas[self.tmask]
            self.toaerrs = self.toaerrs[self.tmask]
            self.res = self.res[self.tmask]
            self.obs_freqs = self.obs_freqs[self.tmask]

            self.Mmat = self.Mmat[self.tmask,:]
            dmx_mask = np.sum(self.Mmat, axis=0) != 0.0
            self.Mmat = self.Mmat[:,dmx_mask]

            for flag in self.flags:
                self.flagvals[flag] = self.T2psr.flagvals(flag)[self.tmask]

            for eph in self.roemer:
                self.roemer[eph] = self.roemer[eph][self.tmask]

        # get the position vectors of the planets
        if planetssb:
            if allEphem:
                # JPL planet position vectors will
                # always be in equatorial coordinates
                from jplephem.spk import SPK
                from scipy import constants as sc

                ephemchoices = sorted(glob.glob(os.environ['TEMPO2']+'/ephemeris/*'))
                matchers = ['421.bsp', '430t.bsp', '435t.bsp', '436t.bsp']
                ephemfiles = [s for s in ephemchoices if any(xs in s for xs in matchers)]

                self.planet_ssb = OrderedDict()
                for eph in ephemfiles:

                    if '436' in eph:
                        ephemname = 'DE436'
                    elif '435' in eph:
                        ephemname = 'DE435'
                    elif '430' in eph:
                        ephemname = 'DE430'
                    elif '421' in eph:
                        ephemname = 'DE421'

                    kernel = SPK.open(eph)
                    jd = self.toas + mjd2jd
                    self.planet_ssb[ephemname] = np.zeros((self.toas.shape[0],9,6))
                    for ii in range(9):
                        position, velocity = kernel[0,ii+1].compute_and_differentiate(jd)
                        position = np.hstack([position.T * 1e3 / sc.c,
                                              velocity.T * 1e3 / sc.c / 86400.])
                        self.planet_ssb[ephemname][:,ii,:] = position
            else:
                # Planet position vectors will initially
                # be in coordinate system of .par file
                for ii in range(1,10):
                    tag = 'DMASSPLANET'+str(ii)
                    self.T2psr[tag].val = 0.0
                self.T2psr.formbats()
                self.planet_ssb = OrderedDict.fromkeys([self.ephemname])
                self.planet_ssb[self.ephemname] = np.zeros((len(self.T2psr.toas()),9,6))
                self.planet_ssb[self.ephemname][:,0,:] = self.T2psr.mercury_ssb
                self.planet_ssb[self.ephemname][:,1,:] = self.T2psr.venus_ssb
                self.planet_ssb[self.ephemname][:,2,:] = self.T2psr.earth_ssb
                self.planet_ssb[self.ephemname][:,3,:] = self.T2psr.mars_ssb
                self.planet_ssb[self.ephemname][:,4,:] = self.T2psr.jupiter_ssb
                self.planet_ssb[self.ephemname][:,5,:] = self.T2psr.saturn_ssb
                self.planet_ssb[self.ephemname][:,6,:] = self.T2psr.uranus_ssb
                self.planet_ssb[self.ephemname][:,7,:] = self.T2psr.neptune_ssb
                self.planet_ssb[self.ephemname][:,8,:] = self.T2psr.pluto_ssb

                if 'ELONG' and 'ELAT' in self.T2psr.pars():
                    # Converting to equatorial if necessary
                    print "--> Converting planet position time-series to equatorial"
                    for ii in range(9):
                        # position
                        self.planet_ssb[self.ephemname][:,ii,:3] = \
                            utils.ecl2eq_vec(self.planet_ssb[self.ephemname][:,ii,:3])
                        # velocity
                        self.planet_ssb[self.ephemname][:,ii,3:] = \
                            utils.ecl2eq_vec(self.planet_ssb[self.ephemname][:,ii,3:])



            if startMJD is not None and endMJD is not None:
                for eph in self.planet_ssb:
                    self.planet_ssb[eph] = \
                      self.planet_ssb[eph][self.tmask,:,:]

            print "--> Grabbed the planet position-vectors at the pulsar timestamps."

        self.isort, self.iisort = None, None
        if 'pta' in self.flags:
            if 'NANOGrav' in list(set(self.flagvals['pta'])):
                # now order everything
                try:
                    self.isort, self.iisort = utils.argsortTOAs(self.toas, self.flagvals['group'],
                                                      which='jitterext', dt=jitterbin/86400.)
                except KeyError:
                    self.isort, self.iisort = utils.argsortTOAs(self.toas, self.flagvals['f'],
                                                      which='jitterext', dt=jitterbin/86400.)

                # sort data
                self.psrPos = self.psrPos[self.isort]
                self.toas = self.toas[self.isort]
                self.toaerrs = self.toaerrs[self.isort]
                self.res = self.res[self.isort]
                self.obs_freqs = self.obs_freqs[self.isort]
                self.Mmat = self.Mmat[self.isort, :]
                for eph in self.roemer:
                    self.roemer[eph] = self.roemer[eph][self.isort]
                if planetssb:
                    for eph in self.planet_ssb:
                        self.planet_ssb[eph] = \
                          self.planet_ssb[eph][self.isort, :, :]

                print "--> Initial sorting of data."

        # get the sky position
        # check for B name
        if 'B' in self.T2psr.name:
            epoch = '1950'
        else:
            epoch = '2000'
        if 'RAJ' and 'DECJ' in self.T2psr.pars(which='set'):
            self.raj = np.double(self.T2psr['RAJ'].val)
            self.decj = np.double(self.T2psr['DECJ'].val)

            self.psr_locs = [self.raj, self.decj]

            eq = ephem.Equatorial(self.T2psr['RAJ'].val,
                                  self.T2psr['DECJ'].val)
            ec = ephem.Ecliptic(eq, epoch=epoch)
            self.elong = np.double(ec.lon)
            self.elat = np.double(ec.lat)

        elif 'ELONG' and 'ELAT' in self.T2psr.pars(which='set'):
            self.elong = np.double(self.T2psr['ELONG'].val)
            self.elat = np.double(self.T2psr['ELAT'].val)

            ec = ephem.Ecliptic(self.elong, self.elat)
            eq = ephem.Equatorial(ec, epoch=epoch)
            self.raj = np.double(eq.ra)
            self.decj = np.double(eq.dec)

            self.psr_locs = [self.raj, self.decj]

        print "--> Grabbed the pulsar position."

        ################################################################################################

        # These are all the relevant system flags used by the PTAs.
        system_flags = ['group','f','sys','g','h']
        self.sysflagdict = OrderedDict.fromkeys(system_flags)

        # Put the systems into a dictionary which
        # has the locations of their toa placements.
        for systm in self.sysflagdict:
            try:
                if systm in self.flags:
                    sys_uflagvals = list(set(self.flagvals[systm]))
                    self.sysflagdict[systm] = OrderedDict.fromkeys(sys_uflagvals)
                    for kk,subsys in enumerate(sys_uflagvals):
                        if self.isort is not None:
                            self.sysflagdict[systm][subsys] = \
                              np.where(self.flagvals[systm][self.isort] == sys_uflagvals[kk])
                        elif self.isort is None:
                            self.sysflagdict[systm][subsys] = \
                              np.where(self.flagvals[systm] == sys_uflagvals[kk])
            except KeyError:
                pass

        # If we have some NANOGrav data, then separate
        # this off for later ECORR assignment.
        if 'pta' in self.flags:
            pta_names = list(set(self.flagvals['pta']))
            pta_mask = [self.flagvals['pta'][self.isort]==ptaname for ptaname in pta_names]
            pta_maskdict = OrderedDict.fromkeys(pta_names)
            for ii,item in enumerate(pta_maskdict):
                pta_maskdict[item] = pta_mask[ii]
            if len(pta_names)!=0 and ('NANOGrav' in pta_names):
                try:
                    nanoflagdict = OrderedDict.fromkeys(['nano-f'])
                    nano_flags = list(set(self.flagvals['group'][self.isort][pta_maskdict['NANOGrav']]))
                    nanoflagdict['nano-f'] = OrderedDict.fromkeys(nano_flags)
                    for kk,subsys in enumerate(nano_flags):
                        nanoflagdict['nano-f'][subsys] = \
                          np.where(self.flagvals['group'][self.isort] == nano_flags[kk])
                    self.sysflagdict.update(nanoflagdict)
                except KeyError:
                    nanoflagdict = OrderedDict.fromkeys(['nano-f'])
                    nano_flags = list(set(self.flagvals['f'][self.isort][pta_maskdict['NANOGrav']]))
                    nanoflagdict['nano-f'] = OrderedDict.fromkeys(nano_flags)
                    for kk,subsys in enumerate(nano_flags):
                        nanoflagdict['nano-f'][subsys] = \
                          np.where(self.flagvals['f'][self.isort] == nano_flags[kk])
                    self.sysflagdict.update(nanoflagdict)



        # If there are really no relevant flags,
        # then just make a full list of the toa indices.
        if np.all([self.sysflagdict[sys] is None for sys in self.sysflagdict]):
            print "No relevant flags found"
            print "Assuming one overall system for {0}\n".format(self.T2psr.name)
            self.sysflagdict[self.T2psr.name] = OrderedDict.fromkeys([self.T2psr.name])
            self.sysflagdict[self.T2psr.name][self.T2psr.name] = np.arange(len(self.toas))

        print "--> Processed all relevant flags plus associated locations."
        ##################################################################################################

        if 'pta' in self.flags:
            if 'NANOGrav' in pta_names:
                # now order everything
                try:
                    dummy_flags = self.flagvals['group'][self.isort]
                except KeyError:
                    dummy_flags = self.flagvals['f'][self.isort]

                print "--> Sorted data."

                # get quantization matrix
                avetoas, self.Umat, Ui = utils.quantize_split(self.toas, dummy_flags, dt=jitterbin/86400., calci=True)
                print "--> Computed quantization matrix."

                self.detsig_avetoas = avetoas.copy()
                self.detsig_Uinds = utils.quant2ind(self.Umat)

                # get only epochs that need jitter/ecorr
                self.Umat, avetoas, aveflags = utils.quantreduce(self.Umat, avetoas, dummy_flags)
                print "--> Excized epochs without jitter."

                # get quantization indices
                self.Uinds = utils.quant2ind(self.Umat)
                self.epflags = dummy_flags[self.Uinds[:, 0]]

                print "--> Checking TOA sorting and quantization..."
                print utils.checkTOAsort(self.toas, dummy_flags, which='jitterext', dt=jitterbin/86400.)
                print utils.checkquant(self.Umat, dummy_flags)
                print "...Finished checks."

        # perform SVD of design matrix to stabilise
        if fastDesign:
            print "--> Stabilizing the design matrix the fast way..."

            Mm = self.Mmat.copy()
            norm = np.sqrt(np.sum(Mm ** 2, axis=0))
            Mm /= norm

            self.Gc = Mm
        else:
            print "--> Performing SVD of design matrix for stabilization..."

            if makeGmat:
                u,s,v = np.linalg.svd(self.Mmat)

                self.G = u[:,len(s):len(u)]
                self.Gres = np.dot(self.G.T, self.res)

                self.Gc =  u[:,:len(s)]

            elif not makeGmat:
                u,s,v = np.linalg.svd(self.Mmat, full_matrices=0)

                self.Gc =  u

        print "--> Done reading in pulsar :-) \n"
예제 #3
0
    def grab_all_vars(self, jitterbin=10., makeGmat=False, fastDesign=True, planetssb=False): # jitterbin is in seconds

        print "--> Processing {0}".format(self.T2psr.name)
        
        # basic quantities
        self.name = self.T2psr.name
        self.toas = np.double(self.T2psr.toas())
        self.res = np.double(self.T2psr.residuals())
        self.toaerrs = np.double(self.T2psr.toaerrs) * 1e-6
        self.obs_freqs = np.double(self.T2psr.ssbfreqs())
        self.Mmat = np.double(self.T2psr.designmatrix())

        # get the position vectors of the planets
        if planetssb:
            for ii in range(1,10):
                tag = 'DMASSPLANET'+str(ii)
                self.T2psr[tag].val = 0.0
            self.T2psr.formbats()
            self.planet_ssb = np.zeros((len(self.toas),9,6))
            self.planet_ssb[:,0,:] = self.T2psr.mercury_ssb
            self.planet_ssb[:,1,:] = self.T2psr.venus_ssb
            self.planet_ssb[:,2,:] = self.T2psr.earth_ssb
            self.planet_ssb[:,3,:] = self.T2psr.mars_ssb
            self.planet_ssb[:,4,:] = self.T2psr.jupiter_ssb
            self.planet_ssb[:,5,:] = self.T2psr.saturn_ssb
            self.planet_ssb[:,6,:] = self.T2psr.uranus_ssb
            self.planet_ssb[:,7,:] = self.T2psr.neptune_ssb
            self.planet_ssb[:,8,:] = self.T2psr.pluto_ssb

            print "--> Grabbed the planet position-vectors at the pulsar timestamps."

        isort, iisort = None, None
        if 'pta' in self.T2psr.flags():
            if 'NANOGrav' in list(set(self.T2psr.flagvals('pta'))):
                # now order everything
                try:
                    isort, iisort = utils.argsortTOAs(self.toas, self.T2psr.flagvals('group'),
                                                      which='jitterext', dt=jitterbin/86400.)
                except KeyError:
                    isort, iisort = utils.argsortTOAs(self.toas, self.T2psr.flagvals('f'),
                                                      which='jitterext', dt=jitterbin/86400.)
        
                # sort data
                self.toas = self.toas[isort]
                self.toaerrs = self.toaerrs[isort]
                self.res = self.res[isort]
                self.obs_freqs = self.obs_freqs[isort]
                self.Mmat = self.Mmat[isort, :]
                if planetssb:
                    self.planet_ssb = self.planet_ssb[isort, :, :]

                print "--> Initial sorting of data."
              
        # get the sky position
        if 'RAJ' and 'DECJ' in self.T2psr.pars():
            self.psr_locs = [np.double(self.T2psr['RAJ'].val),np.double(self.T2psr['DECJ'].val)]
        elif 'ELONG' and 'ELAT' in self.T2psr.pars():
            fac = 180./np.pi
            # check for B name
            if 'B' in self.name:
                epoch = '1950'
            else:
                epoch = '2000'
            coords = Equatorial(Ecliptic(str(self.T2psr['ELONG'].val*fac),
                                         str(self.T2psr['ELAT'].val*fac)),
                                         epoch=epoch)
            self.psr_locs = [float(repr(coords.ra)),float(repr(coords.dec))]

        print "--> Grabbed the pulsar position."
        
        ################################################################################################
            
        # These are all the relevant system flags used by the PTAs.
        system_flags = ['group','f','sys','g','h']
        self.sysflagdict = OrderedDict.fromkeys(system_flags)

        # Put the systems into a dictionary which 
        # has the locations of their toa placements.
        for systm in self.sysflagdict:
            try:
                if systm in self.T2psr.flags():
                    sys_uflagvals = list(set(self.T2psr.flagvals(systm)))
                    self.sysflagdict[systm] = OrderedDict.fromkeys(sys_uflagvals)
                    for kk,subsys in enumerate(sys_uflagvals):
                        if isort is not None:
                            self.sysflagdict[systm][subsys] = \
                              np.where(self.T2psr.flagvals(systm)[isort] == sys_uflagvals[kk])
                        elif isort is None:
                            self.sysflagdict[systm][subsys] = \
                              np.where(self.T2psr.flagvals(systm) == sys_uflagvals[kk])
            except KeyError:
                pass

        # If we have some NANOGrav data, then separate
        # this off for later ECORR assignment.
        if 'pta' in self.T2psr.flags():
            pta_names = list(set(self.T2psr.flagvals('pta')))
            pta_mask = [self.T2psr.flagvals('pta')[isort]==ptaname for ptaname in pta_names]
            pta_maskdict = OrderedDict.fromkeys(pta_names)
            for ii,item in enumerate(pta_maskdict):
                pta_maskdict[item] = pta_mask[ii]
            if len(pta_names)!=0 and ('NANOGrav' in pta_names):
                try:
                    nanoflagdict = OrderedDict.fromkeys(['nano-f'])
                    nano_flags = list(set(self.T2psr.flagvals('group')[isort][pta_maskdict['NANOGrav']]))
                    nanoflagdict['nano-f'] = OrderedDict.fromkeys(nano_flags)
                    for kk,subsys in enumerate(nano_flags):
                        nanoflagdict['nano-f'][subsys] = \
                          np.where(self.T2psr.flagvals('group')[isort] == nano_flags[kk])
                    self.sysflagdict.update(nanoflagdict)
                except KeyError:
                    nanoflagdict = OrderedDict.fromkeys(['nano-f'])
                    nano_flags = list(set(self.T2psr.flagvals('f')[isort][pta_maskdict['NANOGrav']]))
                    nanoflagdict['nano-f'] = OrderedDict.fromkeys(nano_flags)
                    for kk,subsys in enumerate(nano_flags):
                        nanoflagdict['nano-f'][subsys] = \
                          np.where(self.T2psr.flagvals('f')[isort] == nano_flags[kk])
                    self.sysflagdict.update(nanoflagdict)
                    
                    
        
        # If there are really no relevant flags,
        # then just make a full list of the toa indices.
        if np.all([self.sysflagdict[sys] is None for sys in self.sysflagdict]):
            print "No relevant flags found"
            print "Assuming one overall system for {0}\n".format(self.T2psr.name)
            self.sysflagdict[self.T2psr.name] = OrderedDict.fromkeys([self.T2psr.name])
            self.sysflagdict[self.T2psr.name][self.T2psr.name] = np.arange(len(self.toas))

        print "--> Processed all relevant flags plus associated locations."
        ##################################################################################################

        if 'pta' in self.T2psr.flags():
            if 'NANOGrav' in pta_names:
                # now order everything
                try:
                    #isort_b, iisort_b = utils.argsortTOAs(self.toas, self.T2psr.flagvals('group')[isort],
                    #which='jitterext', dt=jitterbin/86400.)
                    flags = self.T2psr.flagvals('group')[isort]
                except KeyError:
                    #isort_b, iisort_b = utils.argsortTOAs(self.toas, self.T2psr.flagvals('f')[isort],
                    #which='jitterext', dt=jitterbin/86400.)
                    flags = self.T2psr.flagvals('f')[isort]
        
                # sort data
                #self.toas = self.toas[isort_b]
                #self.toaerrs = self.toaerrs[isort_b]
                #self.res = self.res[isort_b]
                #self.obs_freqs = self.obs_freqs[isort_b]
                #self.Mmat = self.Mmat[isort_b, :]
                #flags = flags[isort_b]
                
                print "--> Sorted data."
    
                # get quantization matrix
                avetoas, self.Umat, Ui = utils.quantize_split(self.toas, flags, dt=jitterbin/86400., calci=True)
                print "--> Computed quantization matrix."

                self.detsig_avetoas = avetoas.copy()
                self.detsig_Uinds = utils.quant2ind(self.Umat)

                # get only epochs that need jitter/ecorr
                self.Umat, avetoas, aveflags = utils.quantreduce(self.Umat, avetoas, flags)
                print "--> Excized epochs without jitter."

                # get quantization indices
                self.Uinds = utils.quant2ind(self.Umat)
                self.epflags = flags[self.Uinds[:, 0]]

                print "--> Checking TOA sorting and quantization..."
                print utils.checkTOAsort(self.toas, flags, which='jitterext', dt=jitterbin/86400.)
                print utils.checkquant(self.Umat, flags)
                print "...Finished checks."

        # perform SVD of design matrix to stabilise
        if fastDesign:
            print "--> Stabilizing the design matrix the fast way..."

            Mm = self.Mmat.copy()
            norm = np.sqrt(np.sum(Mm ** 2, axis=0))
            Mm /= norm

            self.Gc = Mm
        else:
            print "--> Performing SVD of design matrix for stabilization..."   

            u,s,v = np.linalg.svd(self.Mmat)

            if makeGmat:
                self.G = u[:,len(s):len(u)]
                self.Gres = np.dot(self.G.T, self.res)

            self.Gc =  u[:,:len(s)]

        print "--> Done reading in pulsar :-) \n"