def test_write_tim(self):
        """
        Test writing out the .tim file and then reading it back in.
        """

        self.fakepsr.savetim(str(TMP_DIR / "fakepsr.tim"))
        self.fakepsrtp.savetim(str(TMP_DIR / "fakepsrtp.tim"))

        self.assertTrue((TMP_DIR / "fakepsr.tim").exists())
        self.assertTrue((TMP_DIR / "fakepsrtp.tim").exists())

        t2.purgetim(str(TMP_DIR / "fakepsr.tim"))
        t2.purgetim(str(TMP_DIR / "fakepsrtp.tim"))

        newfakepsr = t2.tempopulsar(parfile=self.parfile, timfile=str(TMP_DIR / "fakepsr.tim"), dofit=False)
        newfakepsrtp = t2.tempopulsar(parfile=self.parfile, timfile=str(TMP_DIR / "fakepsrtp.tim"), dofit=False)

        self.assertEqual(newfakepsrtp.nobs, len(self.obstimes))
        self.assertEqual(newfakepsrtp.nobs, newfakepsr.nobs)
        self.assertEqual(newfakepsrtp.name, "1909-3744")
        self.assertEqual(newfakepsr.name, "1909-3744")

        self.assertTrue(np.all(newfakepsrtp.stoas == self.obstimes))
        self.assertTrue(np.all(newfakepsrtp.stoas == self.fakepsrtp.stoas))
        self.assertTrue(np.all(newfakepsrtp.toas() == self.fakepsrtp.toas()))
        self.assertTrue(np.all(newfakepsr.stoas == self.fakepsrtp.stoas))
        self.assertTrue(np.all(newfakepsr.toas() == newfakepsrtp.toas()))

        # check residuals are the same
        self.assertTrue(np.all(newfakepsrtp.residuals() == self.fakepsrtp.residuals()))
        self.assertTrue(np.all(newfakepsrtp.phaseresiduals() == self.fakepsrtp.phaseresiduals()))
        self.assertTrue(np.all(newfakepsrtp.residuals() == newfakepsr.residuals()))
Exemple #2
0
    def __init__(self, parfile=None, timfile=None, testpulsar=False):
        """
        Initialize the pulsar object

        @param parfile:     Filename of par file
        @param timfile:     Filename of tim file
        @param testpulsar:  If true, load J1744 test pulsar
        """

        super(LTPulsar, self).__init__()

        self._interface = "libstempo"
        
        if testpulsar:
            # Write a test-pulsar, and open that for testing
            parfilename = tempfile.mktemp()
            timfilename = tempfile.mktemp()
            parfile = open(parfilename, 'w')
            timfile = open(timfilename, 'w')
            parfile.write(J1744_parfile)
            timfile.write(J1744_timfile)
            parfile.close()
            timfile.close()

            self._psr = lt.tempopulsar(parfilename, timfilename, dofit=False)

            os.remove(parfilename)
            os.remove(timfilename)
        elif parfile is not None and timfile is not None:
            self._psr = lt.tempopulsar(parfile, timfile, dofit=False)
        else:
            raise ValueError("No valid pulsar to load")
Exemple #3
0
def readArray(partimdir, mindist=0.5, maxdist=2.0):
    """
    Read in a list of ptaPulsar objects from a set of par/tim files. Pulsar
    distances are randomly drawn between two values

    @param partimdir:   Directory of par/tim files
    @param mindist:     Minimum distance of pulsar
    @param maxdist:     Maximum distance of pulsar

    @return: list of ptapsrs
    """
    ptapsrs = []
    curdir = os.getcwd()
    os.chdir(partimdir)
    for ii, infile in enumerate(glob.glob(os.path.join('./', 'J*.par') )):
        filename = os.path.splitext(infile)
        basename = os.path.basename(filename[0])
        parfile = './' + basename +'.par'
        timfile = './' + basename +'.tim'
        
        psr = lt.tempopulsar(parfile, timfile, dofit=False)

        dist = mindist + np.random.rand(1) * (maxdist - mindist)

        ptapsrs.append(ptaPulsar(psr['RAJ'].val, psr['DECJ'].val, psr.name, \
                                 psr.toas(), psr.residuals(), \
                                 psr.toaerrs*1.0e-6, 1000*dist))
    
    os.chdir(curdir)

    return ptapsrs
Exemple #4
0
    def setUpClass(cls):
        cls.obstimes = np.arange(53000, 54800, 10, dtype=np.float128)
        cls.toaerr = 1e-3
        cls.freq = 1440.0
        cls.observatory = "ao"
        cls.parfile = DATA_PATH + "/J1909-3744_NANOGrav_dfg+12.par"

        # create a fake pulsar using fakepulsar
        cls.fakepsr = fakepulsar(
            parfile=cls.parfile,
            obstimes=cls.obstimes,
            toaerr=cls.toaerr,
            freq=cls.freq,
            observatory=cls.observatory,
            iters=0,
        )

        # create a fake pulsar using tempopulsar
        cls.fakepsrtp = t2.tempopulsar(
            parfile=cls.parfile,
            toas=cls.obstimes,
            toaerrs=cls.toaerr,
            observatory=cls.observatory,
            obsfreq=cls.freq,
            dofit=False,
        )
Exemple #5
0
 def setUpClass(self):
     self.parfileB1855 = 'B1855+09_NANOGrav_dfg+12_modified.par'
     self.timB1855 = 'B1855+09_NANOGrav_dfg+12.tim'
     self.toasB1855 = toa.get_TOAs(self.timB1855, ephem="DE405", planets=False)
     self.modelB1855 = mb.get_model(self.parfileB1855)
     self.psrB1855 = lt.tempopulsar(self.parfileB1855, self.timB1855)
     self.ltres = self.psrB1855.residuals()
Exemple #6
0
 def add_gwb(self, gwb, dist, InjectionFile="None", verbose=-1):
     """ Add GW background on simulated TOAs using a GWB object 
     from libstempo and the pulsar distance in kpc."""
     if verbose==-1 : verbose = self.verbose
     
     if verbose!=0 : print "Add GWB ..."
     #### Making libstempo.tempopulsar save in parfile and timfile
     localverbose=0
     if verbose>1 : localverbose=1
     self.savepar("TmpIdeal", verbose=localverbose)
     self.savetim("TmpIdeal",IdealTOAs=True, verbose=localverbose)
     psr = libstempo.tempopulsar(parfile="TmpIdeal.par",timfile="TmpIdeal.tim",dofit=False)
     #### Creating GWB data
     GWBval = gwb.add_gwb(psr,dist)
     #### Adding data
     fOut = None
     if InjectionFile!="None":
         fOut = open(InjectionFile,'w')
         fOut.write("#TOAIdeal GWB TOARelBeforeInjection TOARelAfterInjection DiffTOAAft-Bef\n")
     for itoa in xrange(len(self.timTOAs)) :
         TOABefInj = self.timTOAs[itoa][0]
         self.timTOAs[itoa][0] += mp.mpf(np.float64(GWBval[itoa]),n=self.prec)
         if fOut!=None :
             fOut.write(mp.nstr(self.timTOAsIdeal[itoa][0],n=self.prec)+" "+repr(GWBval[itoa])+" "\
                        +mp.nstr(TOABefInj-self.timTOAsIdeal[0][0],n=self.prec)+" "\
                        +mp.nstr(self.timTOAs[itoa][0]-self.timTOAsIdeal[0][0],n=self.prec)+" "\
                        +mp.nstr(self.timTOAs[itoa][0]-TOABefInj,n=self.prec)+"\n")
     if fOut!=None :
         fOut.close()
Exemple #7
0
 def test_DMX(self):
     print "Testing DMX module."
     rs = residuals.resids(self.toas, self.DMXm).time_resids.to(u.us).value
     psr = lt.tempopulsar(self.parf, self.timf)
     resDiff = rs-psr.residuals()
     assert np.all(resDiff) < 1e-7, \
         "PINT and tempo Residual difference is too big. "
 def fit(self, pfile, tfile):  #,pfile='test.par',tfile='test.tim'):
     psr = T.tempopulsar(parfile=pfile, timfile=tfile)
     # print psr.toas(), psr.residuals()
     i = N.argsort(psr.toas())
     self.toas = psr.toas()[i]
     self.res = psr.residuals()[i]
     self.toaerr = psr.toaerrs[i]
     del (psr)
def produce_libstempo_delays():
    """Use simulated data of J1955 with TEMPO2 and test if reproducible"""

    parfile = 'tests/J1955.par'
    timfile = 'tests/J1955.tim'

    toas = toa.get_TOAs(timfile, planets=False, usepickle=False)

    SECS_PER_DAY = 86400

    toasld = toas.table['tdbld']
    t0 = toasld[0]
    toaslds = (toasld - t0) * SECS_PER_DAY

    newmodel = bt.BT()

    newmodel.read_parfile(parfile)

    phases = newmodel.phase(toas.table)

    #t0 = time.time()
    delays = newmodel.delay(toas.table)

    psr = lt.tempopulsar(parfile, timfile)
    t2resids = psr.residuals()
    btdelay = psr.binarydelay()
    bttoasld = psr.toas()
    btt0 = bttoasld[0]
    bttoaslds = (bttoasld - btt0) * SECS_PER_DAY

    # HACK (paulr): Flip sign of btdelay to match what PINT uses.  Not sure why this is necessary. Should be figured out.
    btdelay *= -1.0

    with open('tests/J1955_ltdelays.dat', 'w') as fobj:
        for i in range(len(delays)):
            print("{:.20} {:.20}".format(toaslds[i], btdelay[i]), file=fobj)

    plt.figure("Delays")
    plt.plot(toaslds, delays, label='PINT')
    plt.scatter(toaslds, delays)

    plt.plot(bttoaslds, btdelay, label='T2')
    plt.scatter(bttoaslds, btdelay)

    plt.legend()
    plt.xlabel('Time (d)')
    plt.ylabel('Delay (s)')

    plt.figure("Diff")

    plt.plot(toaslds, delays - btdelay)
    plt.scatter(toaslds, delays - btdelay)

    plt.legend()
    plt.xlabel('Time (d)')
    plt.ylabel('Diff (s)')
    plt.show()
Exemple #10
0
def gettempo2(pulsarfile,pulsardir='.',timsuffix='',summary=None,debug=False):
    """Load a pickled archive, or use libstempo.pyx to load pulsar data from tempo2 par and tim files."""

    pfile = 'pickles/{0}.pickle'.format(pulsarfile)

    if os.path.isfile(pfile):
        if debug:
            print "Using pickled file %s." % pfile
        return pulsarfile, getpickle(pfile)

    pulsarfile, parfile, timfile = findtempo2(pulsarfile,pulsardir=pulsardir,timsuffix=timsuffix,debug=debug)

    import libstempo as T

    pulsar = T.tempopulsar(parfile,timfile)

    meta = N.zeros((1,),dtype=[('name','a32'),('ra','f8'),('dec','f8'),('designpars','i4'),('pars','i4')])

    meta['name']       = pulsarfile
    meta['ra']         = pulsar['RAJ'].val
    meta['dec']        = pulsar['DECJ'].val
    meta['designpars'] = 0                  # really an offset
    meta['pars']       = len(pulsar.pars)

    # get data (will make copies)

    times_f = N.array(pulsar.toas() - pulsar['PEPOCH'].val,'d')   # days
    resid_f = N.array(pulsar.residuals(),'d')                     # seconds
    error_f = N.array(1e-6 * pulsar.toaerrs,'d')                  # seconds
    freqs_f = N.array(pulsar.freqs,'d')                           # MHz

    # get design matrix

    desi = pulsar.designmatrix()

    if desi.shape[1] != meta['pars'] + 1:
        msg = 'The number of fitting parameters ({0}) and the size of design matrix ({1}x{2}) do not match!'.format(
            meta['pars'],desi.shape[0],desi.shape[1])
        raise ValueError, msg

    # outlier-removal logic
    # argout = lambda data,m=2: N.abs(data - N.mean(data)) < m * N.std(data)
    # kx = argout(resid_f,3)
    # print "Removing {0} TOAs".format(desi.shape[0] - N.sum(kx))
    # desi, times_f, resid_f, error_f = desi[kx,:], times_f[kx], resid_f[kx], error_f[kx]

    # rescale residuals and errors to units of 100 ns (yes, hardcoded)

    resid_f = resid_f / 1e-7
    error_f = error_f / 1e-7

    if summary:
        summary['parfile'] = parfile
        summary['timfile'] = timfile

    return pulsarfile,(meta,desi,times_f,resid_f,error_f,freqs_f)
Exemple #11
0
def fakepulsar(parfile,
               obstimes,
               toaerr,
               freq=1440.0,
               observatory='AXIS',
               flags='',
               iters=3):
    """Returns a libstempo tempopulsar object corresponding to a noiseless set
    of observations for the pulsar specified in 'parfile', with observations
    happening at times (MJD) given in the array (or list) 'obstimes', with
    measurement errors given by toaerr (us).

    A new timfile can then be saved with pulsar.savetim(). Re the other parameters:
    - 'toaerr' needs to be either a common error, or a list of errors
       of the same length of 'obstimes';
    - 'freq' can be either a common observation frequency in MHz, or a list;
       it defaults to 1440;
    - 'observatory' can be either a common observatory name, or a list;
       it defaults to the IPTA MDC 'AXIS';
    - 'flags' can be a string (such as '-sys EFF.EBPP.1360') or a list of strings;
       it defaults to an empty string;
    - 'iters' is the number of iterative removals of computed residuals from TOAs
      (which is how the fake pulsar is made...)"""

    import tempfile
    outfile = tempfile.NamedTemporaryFile(delete=False)

    outfile.write(b'FORMAT 1\n')
    outfile.write(b'MODE 1\n')

    obsname = 'fake_' + os.path.basename(parfile)
    if obsname[-4:] == '.par':
        obsname = obsname[:-4]

    for i, t in enumerate(obstimes):
        outfile.write('{0} {1} {2} {3} {4} {5}\n'.format(
            obsname, _geti(freq, i), t, _geti(toaerr, i),
            _geti(observatory, i), _geti(flags, i)).encode('ascii'))

    timfile = outfile.name
    outfile.close()

    pulsar = libstempo.tempopulsar(parfile, timfile, dofit=False)

    for i in range(iters):
        pulsar.stoas[:] -= pulsar.residuals() / 86400.0
        pulsar.formbats()

    os.remove(timfile)

    return pulsar
def make_fakes(amp, psrcut):
    datadir = './data/partim/'
    ## Get parameter noise dictionary
    noisedir = os.getcwd() + '/data/noisefiles/'

    noise_params = {}
    for filepath in glob.iglob(noisedir + '/*.json'):
        with open(filepath, 'r') as f:
            noise_params.update(json.load(f))
    # print(noise_params)

    # import par and tim files into parfiles and timfiles
    tempopsr = []
    print('Importing to libstempo....')
    for psr in psrcut:
        parfiles = sorted(glob.glob(datadir + psr.name + '*.par'))
        timfiles = sorted(glob.glob(datadir + psr.name + '*.tim'))
        psr = libs.tempopulsar(parfile=parfiles[0],
                               timfile=timfiles[0],
                               maxobs=50000)
        tempopsr.append(psr)
    print('adding efac')
    for psr in tempopsr:
        LT.make_ideal(psr)
        LT.add_efac(psr, efac=1.0)
    print('adding red noise')
    for psr in tempopsr:
        # print(psr.name)
        rednoise_toadd = noise_params[str(psr.name) + '_log10_A']
        # print(psr.name)
        gamma = noise_params[str(psr.name) + '_gamma']
        LT.add_rednoise(psr, 10**rednoise_toadd, gamma, components=30)
        psr.fit()
    print('adding GWB')
    LT.createGWB(tempopsr, Amp=amp, gam=13. / 3.)
    for psr in tempopsr:
        psr.fit()
    # save the pulsars
    pardir = './fakes_gwb/par/'
    timdir = './fakes_gwb/tim/'
    if not os.path.exists(pardir):
        os.makedirs(pardir)
        os.makedirs(timdir)

    print('fixing tim files')
    for psr in tempopsr:
        psr.savepar(pardir + psr.name + '-sim.par')
        psr.savetim(timdir + psr.name + '-sim.tim')
        libs.purgetim(timdir + psr.name + '-sim.tim')  # fix the tim files
    def test_astropy_array(self):
        """
        Test passing TOAs as an astropy Time array.
        """

        times = Time(self.obstimes, format="mjd", scale="utc")

        psr = t2.tempopulsar(
            parfile=self.parfile, toas=times, toaerrs=self.toaerr, observatory=self.observatory, obsfreq=self.freq,
        )

        self.assertEqual(len(self.obstimes), psr.nobs)
        self.assertTrue(np.all(self.obstimes == self.fakepsr.stoas))
        self.assertTrue(np.all(psr.stoas == self.fakepsr.stoas))
        self.assertEqual(psr.stoas[0].dtype, np.float128)
Exemple #14
0
    def test_single_values(self):
        """
        Test passing single value TOAs.
        """

        psr = t2.tempopulsar(
            parfile=self.parfile,
            toas=self.obstimes[0],
            toaerrs=self.toaerr,
            observatory=self.observatory,
            obsfreq=self.freq,
        )

        self.assertEqual(psr.nobs, 1)
        self.assertEqual(len(psr.stoas), 1)
        self.assertTrue(np.all(self.fakepsr.stoas[0] == psr.stoas[0]))
        self.assertEqual(psr.stoas[0].dtype, np.float128)
 def __init__(self, parfile, timfile):
     
     # initialize libstempo object
     t2psr = t2.tempopulsar(parfile, timfile, maxobs=30000, )
     
     # get attributes
     self.name = t2psr.name
     self.residuals = np.double(t2psr.residuals())
     self.toas = np.double(t2psr.toas()*86400)
     self.toaerrs = np.double(t2psr.toaerrs*1e-6)
     self.flags = t2psr.flagvals('f')
     Mmat = np.double(t2psr.designmatrix())
     
     # sort
     self.isort, self.iisort = utils.argsortTOAs(self.toas, self.flags, 
                                                    which='jitterext', dt=1.0)
     
     self.toas = self.toas[self.isort]
     self.toaerrs = self.toaerrs[self.isort]
     self.residuals = self.residuals[self.isort]
     self.flags = self.flags[self.isort]
     Mmat = Mmat[self.isort, :]
     
     u, s, v = np.linalg.svd(Mmat, full_matrices=False)
     self.Musvd = u
     self.Mssvd = s
     
     # get sky location
     pars = t2psr.pars()
     if 'RAJ' not in pars and 'DECJ' not in pars:
         elong, elat = t2psr['ELONG'].val, t2psr['ELAT'].val
         ec = ephem.Ecliptic(elong, elat)
             
         # check for B name
         if 'B' in t2psr.name:
             epoch = '1950'
         else:
             epoch = '2000'
         eq = ephem.Equatorial(ec, epoch=epoch)
         self.phi = np.double([eq.ra])
         self.theta = np.pi/2 - np.double([eq.dec])
     
     else:
         self.phi = np.double(t2psr['RAJ'].val)
         self.theta = np.pi/2 - np.double(t2psr['DECJ'].val)
Exemple #16
0
def readRealisations(path):
    """ Uses the libstempo package to make tempo2 read a set of par/tim files
        in a specific directory. The package libstempo can be found at:
        https://github.com/vallis/mc3pta/tree/master/stempo
        credit: Michele Vallisneri

        The directory 'path' will be scanned for .par files and similarly named
        .tim files.
        
        @param path: path to the directory with par/tim files
        
    """

    psrs = []
    for infile in glob.glob(os.path.join(path, '*.par')):
        filename = os.path.splitext(infile)
        psrs.append(T.tempopulsar(parfile=filename[0]+'.par',timfile=filename[0]+'.tim'))
    
    return psrs
Exemple #17
0
def get_tempo2_result(parfile, timfile, general2=None):
    """This function is to get the results from tempo and write them to a file.
    Parameter
    ---------
    parfile : str
        The file to read parameters.
    timfile : str
        The file to read toas.
    general2 : list/ None
        The values required from tempo2 general2 plugin.
    Return
    ----------
    A file named as parfile name ends with '.tempo2_test' stored residuals in the
    first column, binary delay in the second column and general2 results if
    general2 are provided.
    """
    psr = lt.tempopulsar(parfile, timfile)
    residuals = psr.residuals()
    binary_delay = psr.binarydelay()
    outfile = parfile + '.tempo2_test'
    f = open(outfile, 'w')
    outstr = 'residuals  BinaryDelay '

    if general2 is not None and general2 != []:
        if not has_tempo2_utils:
            raise ImportError("To get tempo2 general2 results, tempo2_utils are"
                              " required. See page"
                              " https://github.com/demorest/tempo_utils")
        else:
            tempo2_vals = t2u.general2(parfile, timfile, general2)
            for keys in general2:
                outstr += keys+' '

    outstr += '\n'
    f.write(outstr)
    for ii in range(len(residuals)):
        outstr = longdouble2string(residuals[ii]) + ' ' +longdouble2string(binary_delay[ii]) +' '
        if general2 is not None:
            for keys in general2:
                outstr += longdouble2string(tempo2_vals[keys][ii])+' '
        outstr += '\n'
        f.write(outstr)
    f.close()
Exemple #18
0
    def __init__(self, parfiles, timfiles, ephem='DE436',verbose=True):

        #### Make a list of libstempo.tempopulsar objects.
        libs_psrs = []
        for p, t in zip(parfiles, timfiles):

            psr = T2.tempopulsar(p, t, ephem=ephem, maxobs=30000)
            libs_psrs.append(psr)
            if verbose:
                print('\rPSR {0} loaded.'.format(psr_name(p)),
                      flush=True,end='')

        print('')
        self.libs_psrs = libs_psrs
        self.psrs = None
        self.ephem = ephem
        self.first_toa = np.amin([p.toas().min() for p in libs_psrs])
        self.last_toa = np.amax([p.toas().max() for p in libs_psrs])
        self.toa_cuts = [self.last_toa]
        self.gwb_added = False
Exemple #19
0
def readRealisations(path):
    """ Uses the libstempo package to make tempo2 read a set of par/tim files
        in a specific directory. The package libstempo can be found at:
        https://github.com/vallis/mc3pta/tree/master/stempo
        credit: Michele Vallisneri

        The directory 'path' will be scanned for .par files and similarly named
        .tim files.
        
        @param path: path to the directory with par/tim files
        
    """

    psrs = []
    for infile in glob.glob(os.path.join(path, '*.par')):
        filename = os.path.splitext(infile)
        psrs.append(
            T.tempopulsar(parfile=filename[0] + '.par',
                          timfile=filename[0] + '.tim'))

    return psrs
Exemple #20
0
def fakepulsar(parfile,obstimes,toaerr,freq=1440.0,observatory='AXIS',flags=''):
    """Returns a libstempo tempopulsar object corresponding to a noiseless set
    of observations for the pulsar specified in 'parfile', with observations
    happening at times (MJD) given in the array (or list) 'obstimes', with
    measurement errors given by toaerr (us).

    A new timfile can then be saved with pulsar.savetim(). Re the other parameters:
    - 'toaerr' needs to be either a common error, or a list of errors
       of the same length of 'obstimes';
    - 'freq' can be either a common observation frequency in MHz, or a list;
       it defaults to 1440;
    - 'observatory' can be either a common observatory name, or a list;
       it defaults to the IPTA MDC 'AXIS';
    - 'flags' can be a string (such as '-sys EFF.EBPP.1360') or a list of strings;
       it defaults to an empty string."""

    import tempfile
    outfile = tempfile.NamedTemporaryFile(delete=False)

    outfile.write('FORMAT 1\n')
    outfile.write('MODE 1\n')

    obsname = 'fake_' + os.path.basename(parfile)
    if obsname[-4:] == '.par':
        obsname = obsname[:-4]

    for i,t in enumerate(obstimes):
        outfile.write('{0} {1} {2} {3} {4} {5}\n'.format(
            obsname,_geti(freq,i),t,_geti(toaerr,i),_geti(observatory,i),_geti(flags,i)
        ))

    timfile = outfile.name
    outfile.close()

    pulsar = libstempo.tempopulsar(parfile,timfile,dofit=False)
    pulsar.stoas[:] -= pulsar.residuals(updatebats=False) / 86400.0

    os.remove(timfile)

    return pulsar
Exemple #21
0
    def getBaryCorrs(self):
        newpar = open('bary.par', 'w')

        newpar.write("PSRJ           J0000+0000\n")
        newpar.write("RAJ            " + str(self.RA) + "      0\n")
        newpar.write("DECJ           " + str(self.Dec) + "        0\n")
        newpar.write("F0             1.0 0 \n")
        newpar.write("PEPOCH         " + str(self.RefMJD) + "\n")
        newpar.write("POSEPOCH       " + str(self.RefMJD) + "\n")
        newpar.write("DMEPOCH        " + str(self.RefMJD) + "\n")
        newpar.write("DM		" + str(self.DM) + " 0\n")
        newpar.write("EPHVER         5\n")
        newpar.write("CLK            UNCORR\n")
        newpar.write("MODE 1\n")
        newpar.write("EPHEM          DE421\n")

        newpar.close()

        newtim = open('bary.tim', 'w')
        tstep = 10.0
        start = self.RefMJD
        stop = self.RefMJD + (self.NSamps * self.TSamp + tstep) / 24 / 60 / 60
        N = np.floor((stop - start) * 24 * 60 * 60 / tstep).astype(int)
        x = np.linspace(start, stop, N)

        newtim.write("FORMAT 1\n")
        for i in range(len(x)):
            newtim.write("barytim " + str(self.LowChan) + " " + str(x[i]) +
                         " 0.1 pks\n")

        newtim.close()

        psr = T.tempopulsar(parfile='bary.par', timfile='bary.tim')
        psr.fit()
        BCorrs = psr.batCorrs()
        self.BCorrs = np.zeros([2, len(BCorrs)])
        self.BCorrs[0] = (psr.stoas).copy()
        self.BCorrs[1] = BCorrs.copy() * 24 * 60 * 60
        os.remove('bary.par')
        os.remove('bary.tim')
# module import
import pystan
import numpy as np
import pylab as py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from libstempo.libstempo import *
import libstempo as T


#First load pulsar.  We need the sats (separate day/second), and the file names of the archives (FNames)
psr = T.tempopulsar(parfile="../TempoNest/Examples/Example2/J0030+0451.par", timfile = "../TempoNest/Examples/Example2/J0030+0451.tim")
psr.fit()
NToAs = psr.nobs

toas=psr.toas()
residuals = np.float64(psr.residuals())
errs=psr.toaerrs*(10.0**-6)


#Check how many timing model parameters we are fitting for (in addition to phase)
numTime=len(psr.pars())+1
redChisq = psr.chisq()/(psr.nobs-len(psr.pars())-1)
TempoPriors=np.zeros([numTime,2]).astype(np.float64)

TempoPriors[0][0] = 0
TempoPriors[0][1] = 1.0/np.sqrt(np.sum(1.0/(psr.toaerrs*10.0**-6)**2)) 

for i in range(1,numTime):
Exemple #23
0
    def __init__(self,parfile,timfile=None,refpsr=None,verbose=0):
        """
        used to simulate a pulsar from a reference pulsar defined by its parfile OR by a libstempo.tempopulsar .
        The TOAs are created from existing TOAs (timfile or tempopulsar).
        """
        
        self.niter = 5
        self.prec = 25
        mp.mp.dps = self.prec
        self.verbose = verbose
        
        #### Checking parfile
        if not os.path.isfile(parfile):
            print "ERROR : No such file",parfile
            sys.exit(1)

        #### Checking tempopulsar and timfile. If it's a libstempo.tempopulsar create a local tim file 
        if refpsr!=None :
            timfile = "Tmp.tim"
            refpsr.savetim(timfile)
        else:
            if not os.path.isfile(timfile):
                print "ERROR : No such file",timfile
                sys.exit(1)
            refpsr = libstempo.tempopulsar(parfile=parfile,timfile=timfile,dofit=False)

        #### Extracting list of backend
        self.syslist = refpsr.listsys()
        self.psrname = refpsr.name
        if verbose!=0 : print "Pulsar :", self.psrname
        if verbose!=0 : print "Backend :",self.syslist

        #### Extracting EFAC, EQUAD, RN and DM from TN parameters in the parfile and remove them from the parfile
        if verbose!=0 : print "EFAC, EQUAD, RN and DM read in parfile",parfile
        self.EFAC  = np.ones(len(self.syslist))
        self.EQUAD = np.zeros(len(self.syslist)) 
        self.RNAmp = 0.
        self.RNGam = 1.1
        self.DMAmp = 0.
        self.DMGam = 1.1
        fIn = open(parfile,'r')
        lines = fIn.readlines()
        fIn.close()
        self.parlines = []
        self.parlinesTNEFEEQ = []
        self.parlinesTNRNDM = []
        for line in lines:
            w = re.split("\s+",line)
            if w[0]=="TNEF" or w[0]=="TNEQ" :
                self.isTNEFEQ = True
                iSys = -1
                for i in xrange(len(self.syslist)):
                    if self.syslist[i]==w[2]:
                        iSys = i
                if iSys == -1 :
                    print "ERROR : Load TNEF and TNEQ : the sys",w[2],"from",w,"has not been found in",self.syslist
                    sys.exit(1)
                if w[0]=="TNEF":
                    self.EFAC[iSys] = float(w[3])
                if w[0]=="TNEQ":
                    self.EQUAD[iSys] = 10**float(w[3])
            
            if w[0]=="TNRedAmp" :
                self.RNAmp = 10**float(w[1])
            if w[0]=="TNRedGam" :
                self.RNGam = float(w[1]) 
            if w[0]=="TNDMAmp" :
                self.DMAmp = 10**float(w[1])
            if w[0]=="TNDMGam" :
                self.DMGam = float(w[1]) 
                    
            if w[0]=="TNEF" or w[0]=="TNEQ":
                self.parlinesTNEFEEQ.append(line)
            elif w[0]=="TNRedAmp" or w[0]=="TNRedGam" or w[0]=="TNDMAmp" or w[0]=="TNDMGam" :
                self.parlinesTNRNDM.append(line)
            else:
                self.parlines.append(line)
            
        if verbose!=0 :
            for i in xrange(len(self.syslist)):
                print "\t -",self.syslist[i],": EFAC =",self.EFAC[i]," EQUAD =",self.EQUAD[i]
            print "\t - Red noise : amplitude =",self.RNAmp," gamma =",self.RNGam
            print "\t - DM : amplitude =",self.DMAmp," gamma =",self.DMGam
                
                
        #### Extracting list of toas and timlines :
        # The data are store in timTOAs, each element containing : 
        # toas in mpmath format, errs in mpmath format, sys, words of original line, observation frequency
        self.timHead = []
        self.timTOAs = []   
        self.loadtim(timfile)
                
        #### Idealizing each backend
        for xSys in self.syslist :
            if verbose!=0 : print "Idealizing backend",xSys,"..."
            localverbose = 0
            if verbose>1 : localverbose = 1
            for ite in xrange(self.niter):
                ### Using libstempo.tempopulsar to get residuals
                self.savepar("TmpSys", verbose=localverbose)
                self.savetim("TmpSys", xSys, verbose=localverbose)
                psrS =  libstempo.tempopulsar(parfile="TmpSys.par",timfile="TmpSys.tim",dofit=False)
                res = psrS.residuals()
                ### Substract residuals
                k=0
                for itoa in xrange(len(self.timTOAs)) :
                    if self.timTOAs[itoa][2]==xSys :
                        self.timTOAs[itoa][0] -= mp.mpf(repr(res[k])) / mp.mpf('86400.')
                        k += 1
                ### Substract residuals to libstempo.tempopulsar just to compute RMS
                if verbose>1 : print "\t - iteration %d : before : rms = %e" % ( ite, psrS.rms())
                psrS.stoas[:] -= res / 86400.
                if verbose>1 : print "\t - iteration %d : after : rms = %e , residuals mean (std) = %e (%e) s" % ( ite, psrS.rms(), np.mean(res), np.std(res) )
                
        #### Keep ideal TOAs in memory to be the basis of 
        self.timTOAsIdeal = copy.deepcopy(self.timTOAs)
Exemple #24
0
pulsars.sort()

print pulsars
################################################################################################################################
# PASSING THROUGH TEMPO2 VIA libstempo
################################################################################################################################
if args.mean_or_max == 'mean':
    par_ext = 'Mean'
elif args.mean_or_max == 'max':
    par_ext = 'ML'

t2psr=[]
for ii in range(len(pulsars)):
    os.chdir(path+'/'+pulsars[ii])
    if os.path.isfile('{0}_NoAFB.par'.format(pulsars[ii])):
        t2psr.append(T2.tempopulsar(parfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_TD.{0}.par'.format(par_ext),\
                                    timfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_NoAFB.tim'))
    else:
        t2psr.append(T2.tempopulsar(parfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_TD.{0}.par'.format(par_ext),\
                                    timfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_all.tim'))
    os.chdir(path)
    t2psr[ii].fit(iters=10)
    if np.any(np.isfinite(t2psr[ii].residuals())==False)==True:
        os.chdir(path+'/'+pulsars[ii])
        if os.path.isfile('{0}_NoAFB.par'.format(pulsars[ii])):
            t2psr[ii] = T2.tempopulsar(parfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_TD.{0}.par'.format(par_ext),\
                                       timfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_NoAFB.tim')
        else:
            t2psr[ii] = T2.tempopulsar(parfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_TD.{0}.par'.format(par_ext),\
                                       timfile=path+'/'+pulsars[ii]+'/'+pulsars[ii]+'_all.tim')
        os.chdir(path)
def main():
	save_path = sys.argv[2]

	try:
		completeName = os.path.join( save_path, sys.argv[3] )
	except IndexError:
		completeName = os.path.join( save_path, "CorrelationDATA.txt" )

	outFile = open( completeName, 'w' )

	pList = []
	FILES = os.listdir( sys.argv[1] )
	FILESpar = [ x for x in FILES if x.endswith(".par") ]
	FILEStim = [ x for x in FILES if x.endswith(".tim") ]

	for par in FILESpar:
		for tim in FILEStim:
			if par.split('.')[0] == tim.split('.')[0]:
				pList.append( ( par, tim ) )

	T.data = sys.argv[1]

	DATA = [] # DATA[i] contains two lists - first one has all TOAs, second one has all residuals
	COOR = [] # COOR[i] contains celestial coordinates (tuple) of pulsar i
	for i in range( len( pList ) ):
		PSR = T.tempopulsar( parfile = T.data+pList[i][0], timfile = T.data+pList[i][1], maxobs = 100000 )

		try: # different data sets use different coordinate systems
			PSR['RAJ'].val
			COOR.append( (PSR['RAJ'].val, PSR['DECJ'].val) )
		except KeyError:
			COOR.append( copy.copy( Ecl2Cel( PSR['ELONG'].val, PSR['ELAT'].val, PSR['POSEPOCH'].val ) ) )

		ok = False
		FILES = os.listdir( save_path )
		for X in FILES:
			if X[:len( X )-23] == PSR.name:
				currFile = open( os.path.join( save_path, X ), "r" )
				ok = True
		if( not ok ):
			os.system( "python Average_Epochs.py "+T.data+pList[i][0]+" "+T.data+pList[i][1]+" "+save_path )
			currFile = open( os.path.join( save_path, PSR.name+"Output_for_plotting.tim" ), "r" )

		toas = []
		resid = []
		lines = currFile.readlines()[7:]
		for line in lines:
			toas.append( float( line.split('\t')[0] ) )
			resid.append( float( line.split('\t')[1] ) )
		currFile.close()

		DATA.append( [ copy.copy( toas ), copy.copy( resid ) ] ) # send a copy because DATA[i] points to objects
		del PSR
		gc.collect() # free up some memory

		DATA[i] = [ [ DATA[i][0][x], DATA[i][1][x] ] for x in range( len( DATA[i][0] ) ) ]
		DATA[i].sort( key = lambda row: row[0] ) # sort by TOAs

	nPSR = len( pList ) # number of all pulsars

	for i in range( nPSR-1 ):
		for j in range( i+1, nPSR ):
			C = find_C( DATA[i], DATA[j] ) # find correlation between pulsar i and pulsar j
			ANG_SEP = angsep( COOR[i], COOR[j] ) # find angular separation between pulsar i and pulsar j
			if C != -500: # C becomes -500 when there is not enough residuals to calculate the correlation
				outFile.write( str( ANG_SEP )+" "+str( C )+"\n" )

	outFile.close()

	try:
		where = sys.argv[3]
		if where[-4] != '.':
			where += ".txt"
		os.system( "python HD_Plot.py "+sys.argv[1]+" "+sys.argv[2]+" "+where )
	except IndexError:
		os.system( "python HD_Plot.py "+sys.argv[1]+" "+sys.argv[2]+" "+"CorrelationDATA.txt" )
        else:
            fnoi.write(p+" EQUAD global "+str(EQg)+" -1\n")
        if not options.UseTNEFEQ :
            for x in EFEQs :
                fnoi.write(p+" EFAC "+x[0]+" "+str(x[1])+" -1\n")
                fnoi.write(p+" EQUAD "+x[0]+" "+str(x[2])+" -1\n")
    fnoi.close()


    ##### Checking plots

    if options.Plots :
        Npsrfits = 10

        ### Loading the original pulsar
        psr_o = T.tempopulsar(parfile=DirInSub+"/"+parfile,timfile=DirInSub+"/"+timfile)
        for i in xrange(Npsrfits):
            psr_o.fit()
        toas_d_o = psr_o.toas() # days
        res_o  = 1.0e6*psr_o.residuals() # microseconds
        errs_o = psr_o.toaerrs # microseconds
        toas_yr_o = 2014+(toas_d_o-RefMJD_d)/365.25
        rms_o = psr_o.rms()*1e6

        """
        ### Loading the simulated pulsar
        psr_n = T.tempopulsar(parfile=DirSimu+"/"+p+".par",timfile=DirSimu+"/"+p+".tim",dofit=False)
        toas_d_n = psr_n.toas() # days
        res_n  = 1.0e6*psr_n.residuals() # microseconds
        errs_n = psr_n.toaerrs # microseconds
        toas_yr_n = 2014+(toas_d_n-RefMJD_d)/365.25
Exemple #27
0
    def addDistance(self, parfile, timfile, distfile):
        """
        
        Add puslar distance and uncertainty from file.

        """

        # Check whether the two files exist
        if not os.path.isfile(parfile) or not os.path.isfile(timfile):
            raise IOError, "Cannot find parfile (%s) or timfile (%s)!" % (parfile, timfile)
        assert(self.filename != None), "ERROR: HDF5 file not set!"

        # 'a' means: read/write if exists, create otherwise
        self.h5file = h5.File(self.filename, 'a')

        # Create the data subgroup if it does not exist
        if "Data" in self.h5file:
            datagroup = self.h5file["Data"]
        else:
            raise IOError, "Cannot add noise parameters if Data group does not exist!"

        # Load pulsar data from the JPL Cython tempo2 library
        t2pulsar = t2.tempopulsar(parfile, timfile)

        # Create the pulsar subgroup if it does not exist
        if "Pulsars" in datagroup:
            pulsarsgroup = datagroup["Pulsars"]
        else:
            raise IOError, "Cannot add noise parameters if pulsar group does not exist!"

        # Look up the name of the pulsar, and see if it exist
        if t2pulsar.name in pulsarsgroup:
            pass
        else:
            raise IOError, "%s must already exists in %s to add noise parameters!"\
                    % (t2pulsar.name, self.filename)

        pulsarsgroup = pulsarsgroup[t2pulsar.name]

        # find distance and uncertainty from file
        dfile = open(distfile,'r')

        dist = None
        distErr = None
        for line in dfile.readlines():

            if t2pulsar.name in line or 'J' + t2pulsar.name in line or 'B' + t2pulsar.name in line:
                dist = float(line.split()[1])
                distErr = float(line.split()[2])

        # add distance to file if found
        if dist is not None and distErr is not None:
            pulsarsgroup.create_dataset('dist', data=dist)
            pulsarsgroup.create_dataset('distErr', data=distErr)
        else:
            print "Cannot find PSR {0} in distance file {1}. Using dist = 1kpc with 10% uncertainty".format(t2pulsar.name, distfile)
            
            pulsarsgroup.create_dataset('dist', data=1.0)
            pulsarsgroup.create_dataset('distErr', data=0.1)
            
        self.h5file.close()
        dfile.close()
		float( B )
		return True
	except ValueError:
		return False

def remove_empty( A ):
	ret = []
	for i in range( len( A ) ):
		if A[i] != "":
			ret.append( A[i] )
	return ret

inFile = open( sys.argv[2], "r" ) # the .tim file
All_Lines = inFile.readlines()

psr = T.tempopulsar( parfile = sys.argv[1], timfile = sys.argv[2], maxobs = 100000 )

start = float( sys.argv[3] ) # start and end frequency
end = float( sys.argv[4] ) 

save_path = sys.argv[5]
if not save_path.endswith('.tim'):
    nameFile = os.path.join( save_path, "FreqRange_"+sys.argv[2].split("/")[-1] )
else:
    nameFile = save_path
outFile = open( nameFile, "w" ) # outfile that will contain the filtered frequencies
outFile.write('FORMAT 1\n')

cnt = 0
for i in range( len( All_Lines ) ):
	X = All_Lines[i].split(' ')
Exemple #29
0
import pint.toa as toa
import libstempo as lt
import matplotlib.pyplot as plt
import tempo2_utils
import astropy.units as u
from pint.residuals import resids
import numpy as np
import os, unittest

datapath = os.path.join(os.environ['PINT'],'tests','datafile')
# Using Nanograv data B1855
parfile = os.path.join(datapath, 'B1855+09_NANOGrav_dfg+12_modified.par')
timfile = os.path.join(datapath, 'B1855+09_NANOGrav_dfg+12.tim')
# libstempo calculation
print "libstempo calculation"
psr = lt.tempopulsar(parfile, timfile)
# Build PINT model
print "PINT calculation"
mdd = mb.get_model(parfile)
# Get toas to pint
toas = toa.get_TOAs(timfile, planets=True)
tt = toas.table
# Run tempo2 general2 pluging
tempo2_vals = tempo2_utils.general2(parfile, timfile,
                                    ['tt2tb', 'roemer', 'post_phase',
                                     'shapiro', 'shapiroJ','bat','clock0',
                                     'clock1','clock2','clock3','clock4','sat',
                                     'tropo'])
# compute residules
t2_resids = tempo2_vals['post_phase'] / float(mdd.F0.value) * 1e6 * u.us
presids_us = resids(toas, mdd).time_resids.to(u.us)
Exemple #30
0
parFile = [pulsargroup[key]['parFile'].value for key in pulsargroup]
timFile = [pulsargroup[key]['timFile'].value for key in pulsargroup]

# close hdf5 file
pfile.close()

# check to make sure same number of tim and par files
if len(parFile) != len(timFile):
    raise IOError, "Need same number of par and tim files!"

# check to make sure same number of tim/par files as was in hdf5 file
if len(parFile) != npsr:
    raise IOError, "Different number of pulsars in par directory and hdf5 file!"

# run tempo2
pp = [t2.tempopulsar(parFile[ii],timFile[ii]) for ii in range(npsr)]

# finally check to make sure that they are the same pulsars
for ct,p in enumerate(psr):
    if p.name not in  [ps.name for ps in pp]:
        raise IOError, "PSR {0} not found in hd5f file!".format(p.name)

# make sure pulsar names are in correct order
# TODO: is this a very round about way to do this?
index = []
for ct,p in enumerate(pp):
    
    if p.name == psr[ct].name:
        index.append(ct)
    else:
        for ii in range(npsr):
Exemple #31
0
noisefiles = [
    x for x in noisefiles if x.split('/')[-1].split('_')[0] in psrlist
]

print(len(parfiles), len(timfiles), len(noisefiles))

#######################################
# PASSING THROUGH TEMPO2 VIA libstempo
#######################################

t2psr = []
for ii in range(len(parfiles)):

    t2psr.append(
        T2.tempopulsar(parfile=parfiles[ii],
                       timfile=timfiles[ii],
                       maxobs=30000,
                       ephem='DE436'))

    if np.any(np.isfinite(t2psr[ii].residuals()) == False) == True:
        t2psr[ii] = T2.tempopulsar(parfile=parfiles[ii], timfile=timfiles[ii])

    print('\r{0} of {1}'.format(ii + 1, len(parfiles)), flush=True, end='')

noise_params = {}
for nf in noisefiles:
    noise_params.update(get_noise_from_pal2(nf))

#Now parse this large dictionary so that we can call noise parameters as noise_dict[pulsar name][noise type]
#Returns either floats or 2 column arrays of flags and values.

noise_dict = {}
Exemple #32
0
from triplot import getsigmalevels, makesubplot2d
from pylab import *
import matplotlib.pyplot as plt
import numpy as np
import os, sys
import libstempo 

#data = np.load('1950.before.npz')
data = np.load('1950.after.npz')

fitpars = list(data['fitpars'])
res = data['res']

#psr = libstempo.tempopulsar(parfile='before_DDGR.par', timfile='J1950+2414_T2.tim') 
psr = libstempo.tempopulsar(parfile='after_DDGR.par', timfile='1950.all.tim')
psr.fit()
pars = psr.pars() 
vals = psr.vals()
errs = psr.errs()
fitidx = [i for i,p in enumerate(pars) if p in fitpars ]
#print fitidx
vals0 = vals[fitidx]
errs0 = errs[fitidx]
parsize = vals0.size
#print parsize
#print 'fit for: ', fitpars

plist = fitpars
MarkovChain = res[:,1:]

MCMCSize = len(MarkovChain)
Exemple #33
0
    def addInverseCovFromNoiseFile(self, parfile, timfile, noisefile, DMOFF=None, DMXOFF=None, dailyAverage=False):
        """
        
        Add noise covariance matrix after timing model subtraction.

        """

        # Check whether the two files exist
        if not os.path.isfile(parfile) or not os.path.isfile(timfile):
            raise IOError, "Cannot find parfile (%s) or timfile (%s)!" % (parfile, timfile)
        assert(self.filename != None), "ERROR: HDF5 file not set!"

        # 'a' means: read/write if exists, create otherwise
        self.h5file = h5.File(self.filename, 'a')

        # Create the data subgroup if it does not exist
        if "Data" in self.h5file:
            datagroup = self.h5file["Data"]
        else:
            raise IOError, "Cannot add noise parameters if Data group does not exist!"

        # Load pulsar data from the JPL Cython tempo2 library
        t2pulsar = t2.tempopulsar(parfile, timfile)
        
        # turn off DMMODEL fitting
        if DMOFF is not None:
            t2pulsar['DMMODEL'].fit = False

        # turn off DMX fitting
        if DMXOFF is not None:
            DMXFlag = False
            print 'Turning off DMX fitting and turning DM fitting on'
            for par in t2pulsar.pars:
                if 'DMX' in par:
                    t2pulsar[par].fit = False
                    t2pulsar['DM'].fit = True
                    DMXFlag = True
            if DMXFlag== False: 
                print 'NO DMX for pulsar {0}'.format(t2pulsar.name)

        # refit 5 times to make sure we are converged
        t2pulsar.fit(iters=5)

        # Create the pulsar subgroup if it does not exist
        if "Pulsars" in datagroup:
            pulsarsgroup = datagroup["Pulsars"]
        else:
            raise IOError, "Cannot add noise parameters if pulsar group does not exist!"

        # Look up the name of the pulsar, and see if it exist
        if t2pulsar.name in pulsarsgroup:
            pass
        else:
            raise IOError, "%s must already exists in %s to add noise parameters!"\
                    % (t2pulsar.name, self.filename)

        pulsarsgroup = pulsarsgroup[t2pulsar.name]

        # first create G matrix from design matrix and toas
        designmatrix = np.double(t2pulsar.designmatrix())
        toas = np.double(t2pulsar.toas()*86400)
        errs = np.double(t2pulsar.toaerrs*1e-6)

        # if doing daily averaging
        if dailyAverage:

            # get average quantities
            toas, qmatrix, errs, dmatrix, freqs, bands = PALutils.dailyAverage(t2pulsar)

            # construct new daily averaged residuals and designmatrix
            toas *= 86400
            designmatrix = np.dot(qmatrix, dmatrix)
        
        G = PALutils.createGmatrix(designmatrix)

        # create matrix of time lags
        tm = PALutils.createTimeLags(toas, toas, round=True)

        # now read noise file to get model and parameters
        file = open(noisefile,'r')

        fH = None
        tau = None
        DMAmp = None
        DMgam = None
 
        for line in file.readlines():
            
            # default parameters for different models other than pure PL
            key = line.split()[0]

            # get amplitude
            if "Amp" == key:
                Amp = float(line.split()[-1])

            # get spectral index
            elif "gam" == key:
                gam = float(line.split()[-1])
            
            # get efac
            elif "efac" == key:
                efac = float(line.split()[-1])
            
            # get quad
            elif "equad" == key:
                equad = float(line.split()[-1])
            
            # get high frequency cutoff if available
            elif "fH" == key:
                fH = float(line.split()[-1])
            
            # get correlation time scale if available
            elif "tau" == key:
                tau = float(line.split()[-1])

            # get DM Amplitude if available
            elif "DMAmp" == key:
                DMAmp = float(line.split()[-1])

            # get DM Spectral Index if available
            elif "DMgam" == key:
                DMgam = float(line.split()[-1])

        # cosstruct red and white noise covariance matrices
        red = PALutils.createRedNoiseCovarianceMatrix(tm, Amp, gam, fH=fH)
        white = PALutils.createWhiteNoiseCovarianceMatrix(errs, efac, equad, tau=tau)

        # construct post timing model marginalization covariance matrix
        cov = red + white
        pcov = np.dot(G.T, np.dot(cov, G))

        # finally construct "inverse"
        invCov = np.dot(G, np.dot(np.linalg.inv(pcov), G.T))

        # create dataset for inverse covariance matrix
        pulsarsgroup.create_dataset('invCov', data = invCov) 

        # create dataset for G matrix
        pulsarsgroup.create_dataset('Gmatrix', data = G) 

        # record noise parameter values
        pulsarsgroup.create_dataset('Amp', data = Amp)
        pulsarsgroup.create_dataset('gam', data = gam)
        pulsarsgroup.create_dataset('efac', data = efac)
        pulsarsgroup.create_dataset('equad', data = equad)
        if fH is not None:
            pulsarsgroup.create_dataset('fH', data = fH)
        if tau is not None:
            pulsarsgroup.create_dataset('tau', data = tau)
        if DMAmp is not None:
            pulsarsgroup.create_dataset('DMAmp', data = DMAmp)
        if DMgam is not None:
            pulsarsgroup.create_dataset('DMgam', data = DMgam)


        # Close the hdf5 file
        self.h5file.close()
Exemple #34
0
parfiles = []
timfiles = []
for file in sorted(glob.glob(args.datapath+"/*.par")):
    parfiles.append(file)
    
for file in sorted(glob.glob(args.datapath+"/*.tim")):
    timfiles.append(file)

print parfiles, timfiles
################################################################################################################################
# PASSING THROUGH TEMPO2 VIA libstempo
################################################################################################################################

t2psr=[]
for ii in range(len(parfiles)):
    t2psr.append(T2.tempopulsar(parfile=parfiles[ii], timfile=timfiles[ii]))
    #t2psr[ii].fit(iters=10)
    #if np.any(np.isfinite(t2psr[ii].residuals())==False)==True:
    #    t2psr[ii] = T2.tempopulsar(parfile=parfiles[ii], timfile=timfiles[ii])


# storing red-noise amplitude (nano style),
# gamma (nano style), white-noise level (microseconds)
# [from Arzoumanian et al. (2015a)]
active_psrs = OrderedDict.fromkeys([p.name for p in t2psr])

active_psrs['B1855+09'] = [0.017,-4.9,0.505]
active_psrs['J0030+0451'] = [0.014,-4.8,0.212]
active_psrs['J0613-0200'] = [0.093,-2.9,0.165]
active_psrs['J1012+5307'] = [0.669,-1.0,0.355]
active_psrs['J1024-0719'] = [0.0,0.0,0.280]
Exemple #35
0
    def addpulsar(self, parfile, timfile, DMOFF=None, DMXOFF=None, dailyAverage=False):

        """
        Add another pulsar to the HDF5 file, given a tempo2 par and tim file.

        @param parfile: tempo2 par file
        @param timfile: tempo2 tim file
        @param DMOFF: Option to turn off DMMODEL fitting
        @param DMOFF: Option to turn off DMX fitting
        @param dailyAverage: Option to perform daily averaging to reduce the number
                             of points by consructing daily averaged TOAs that have
                             one residual per day per frequency band. (This has only
                             been tested on NANOGrav data thus far.)

        """

        # Check whether the two files exist
        if not os.path.isfile(parfile) or not os.path.isfile(timfile):
            raise IOError, "Cannot find parfile (%s) or timfile (%s)!" % (parfile, timfile)
        assert(self.filename != None), "ERROR: HDF5 file not set!"

        # 'a' means: read/write if exists, create otherwise
        self.h5file = h5.File(self.filename, 'a')

        if "Model" in self.h5file:
            self.h5file.close()
            self.h5file = None
            raise IOError, "model already available in '%s'. Refusing to add data" % (self.filename)

        # Create the data subgroup if it does not exist
        if "Data" in self.h5file:
            datagroup = self.h5file["Data"]
        else:
            datagroup = self.h5file.create_group("Data")

        # Load pulsar data from the JPL Cython tempo2 library
        t2pulsar = t2.tempopulsar(parfile, timfile)

        # do multiple fits
        #t2pulsar.fit(iters=10)

        # turn off DMMODEL fitting
        if DMOFF is not None:
            t2pulsar['DMMODEL'].fit = False
        
        # turn off DMX fitting
        if DMXOFF is not None:
            DMXFlag = False
            print 'Turning off DMX fitting and turning DM fitting on'
            for par in t2pulsar.pars:
                if 'DMX' in par:
                    print par
                    t2pulsar[par].fit = False
                    t2pulsar['DM'].fit = True
                    DMXFlag = True
            if DMXFlag== False: 
                print 'NO DMX for pulsar {0}'.format(t2pulsar.name)

        # refit 5 times to make sure we are converged
        t2pulsar.fit(iters=5)

        # Create the pulsar subgroup if it does not exist
        if "Pulsars" in datagroup:
            pulsarsgroup = datagroup["Pulsars"]
        else:
            pulsarsgroup = datagroup.create_group("Pulsars")

        # Look up the name of the pulsar, and see if it exist
        if t2pulsar.name in pulsarsgroup:
            self.h5file.close()
            raise IOError, "%s already exists in %s!" % (t2pulsar.name, self.filename)

        pulsarsgroup = pulsarsgroup.create_group(t2pulsar.name)

        # Read the data from the tempo2 structure.
        designmatrix = np.double(t2pulsar.designmatrix())
        residuals = np.double(t2pulsar.residuals())    
        toas = np.double(t2pulsar.toas())
        errs = np.double(t2pulsar.toaerrs*1e-6)
        pname = t2pulsar.name

        try:    # if tim file has frequencies
            freqs = np.double(t2pulsar.freqs)
        except AttributeError: 
            freqs = 0

        try:    # if tim file has frequency band flags
            bands = t2pulsar.flags['B']
        except KeyError:
            bands = 0

        # if doing daily averaging
        if dailyAverage:

            # get average quantities
            toas, qmatrix, errs, dmatrix, freqs, bands = PALutils.dailyAverage(t2pulsar)

            # construct new daily averaged residuals and designmatrix
            residuals = np.dot(qmatrix, residuals)
            designmatrix = np.dot(qmatrix, dmatrix)

        
        # Write the TOAs, residuals, and uncertainties.
        spd = 24.0*3600     # seconds per day
        pulsarsgroup.create_dataset('TOAs', data = toas*spd)           # days (MJD) * sec per day
        pulsarsgroup.create_dataset('residuals', data = residuals)     # seconds
        pulsarsgroup.create_dataset('toaErr', data = errs)             # seconds
        pulsarsgroup.create_dataset('freqs', data = freqs*1e6)             # Hz
        pulsarsgroup.create_dataset('bands', data = bands)             # Hz

        # add tim and par file paths
        pulsarsgroup.create_dataset('parFile', data = parfile)             # string
        pulsarsgroup.create_dataset('timFile', data = timfile)             # string


        # Write the full design matrix
        pulsarsgroup.create_dataset('designmatrix', data = designmatrix)

        # Obtain the timing model parameters
        tmpname = np.array(t2pulsar.pars)
        tmpvalpre = np.double([t2pulsar.prefit[parname].val for parname in t2pulsar.pars])
        tmpvalpost = np.double([t2pulsar[parname].val for parname in t2pulsar.pars])
        tmperrpre = np.double([t2pulsar.prefit[parname].err for parname in t2pulsar.pars])
        tmperrpost = np.double([t2pulsar[parname].err for parname in t2pulsar.pars])


        # Write the timing model parameter (TMP) descriptions
        pulsarsgroup.create_dataset('pname', data=pname)            # pulsar name
        pulsarsgroup.create_dataset('tmp_name', data=tmpname)       # TMP name
        pulsarsgroup.create_dataset('tmp_valpre', data=tmpvalpre)   # TMP pre-fit value
        pulsarsgroup.create_dataset('tmp_valpost', data=tmpvalpost) # TMP post-fit value
        pulsarsgroup.create_dataset('tmp_errpre', data=tmperrpre)   # TMP pre-fit error
        pulsarsgroup.create_dataset('tmp_errpost', data=tmperrpost) # TMP post-fit error

        # Close the hdf5 file
        self.h5file.close()
Exemple #36
0
def readPrepRealisations(path):
    psrs = []
    ntotobs, ntotpars = 0, 0
    relparpath = '/par/'
    reltimpath = '/tim/'
    reldespath = '/design/'
    relrespath = '/res/'
    relcovpath = '/cov/'

    # Read in all the pulsars through libstempo
    for infile in glob.glob(os.path.join(path+relparpath, '*.par')):
        filename = os.path.splitext(infile)
        basename = os.path.basename(filename[0])

        # Locate the par-file and the timfile
        parfile = path+relparpath+basename+'.par'
        timfile = path+reltimpath+basename+'.tim'
        desfile = path+reldespath+basename+'designMatrix.txt'

        # Make the libstempo object
        timfiletup = os.path.split(timfile)
        dirname = timfiletup[0]
        reltimfile = timfiletup[-1]
        relparfile = os.path.relpath(parfile, dirname)
        savedir = os.getcwd()
        os.chdir(dirname)
        psrs.append(T.tempopulsar(relparfile, reltimfile))
        os.chdir(savedir)

        # Read the designmatrix from the file
        #desmat = psrs[-1].designmatrix().copy()
        desmat = np.loadtxt(desfile)

        # Register how many paramters and observations we have
        #ntotobs += psr.nobs
        #ntotpars += psr.ndim+1
        ntotobs += desmat.shape[0]
        ntotpars += desmat.shape[1]
        if desmat.shape[0] != psrs[-1].nobs:
            print "For " + basename + " obs != obs ", desmat.shape[0], psrs[-1].nobs

        if desmat.shape[1] != psrs[-1].ndim+1:
            print "For " + basename + " pars != pars ", desmat.shape[1], psrs[-1].ndim+1
        
    hdmat = hdcormat(psrs)

    # Allocate memory for the model description
    toas = np.zeros(ntotobs)
    residuals = np.zeros(ntotobs)
    toaerrs = np.zeros(ntotobs)
    designmatrix = np.zeros((ntotobs, ntotpars))
    Gmatrix = np.zeros((ntotobs, ntotobs-ntotpars))
    GNGinv = []
    ptheta = []
    pphi = []
    psrobs = []
    psrpars = []
    psrg = []


    infiles = glob.glob(os.path.join(path+relparpath, '*.par'))
    indo, indp, indg = 0, 0, 0
    for i in range(len(infiles)):
        # This automatically also loops over the psrs of course
        filename = os.path.splitext(infiles[i])
        basename = os.path.basename(filename[0])

        # Locate the par-file and the timfile
        parfile = path+relparpath+basename+'.par'
        timfile = path+reltimpath+basename+'.tim'
        desfile = path+reldespath+basename+'designMatrix.txt'
        covfile = path+relcovpath+basename+'covMatrix.txt'
        resfile = path+relrespath+basename+'res.dat'

        # Read in the design matrix (again), covariance matrix, and the residuals
        desmat = np.loadtxt(desfile)
        covmat = np.loadtxt(covfile)
        resvec = np.loadtxt(resfile)
        # These comments are for when reading in from the psr object
        #desmat = psrs[i].designmatrix().copy()
        #covmat = np.diag((psrs[i].toaerrs*1e-6)**2)
        #resvec = np.array([psrs[i].toas(), psrs[i].residuals(), psrs[i].toaerrs*1e-6]).T
        
        # Determine the dimensions
        pobs = desmat.shape[0]
        ppars = desmat.shape[1]
        pgs = desmat.shape[0] - desmat.shape[1]
        psrobs.append(pobs)
        psrpars.append(ppars)
        psrg.append(pgs)

        # Load the basic quantities
        toas[indo:indo+pobs] = resvec[:,0]
        residuals[indo:indo+pobs] = resvec[:,1]
        toaerrs[indo:indo+pobs] = resvec[:,2]

        designmatrix[indo:indo+pobs, indp:indp+ppars] = desmat
        ptheta.append(0.5*np.pi - psrs[i]['DECJ'].val)
        pphi.append(psrs[i]['RAJ'].val)

        # Create the G-matrix
        U, s, Vh = sl.svd(desmat)
        Gmatrix[indo:indo+pobs, indg:indg+pgs] = U[:,ppars:].copy()

        # Create the noise matrix
        pNoise = covmat
        GNG = np.dot(U[:,ppars:].copy().T, np.dot(pNoise, U[:,ppars:].copy()))
        cf = sl.cho_factor(GNG)
        GNGinv.append(sl.cho_solve(cf, np.identity(GNG.shape[0])))

        indo += pobs
        indp += ppars
        indg += pgs
    
    model = (toas, residuals, toaerrs, designmatrix, Gmatrix, hdmat, psrobs, psrpars, psrg, GNGinv, ptheta, pphi)

    return (psrs, model)
import numpy as N
import matplotlib.pyplot as P
import libstempo as T
import os.path

def freq_idx( F ):
	freq=[ [100, 360], [361, 600], [601, 999], [1000, 2100], [2101, 3000] ]
	for i in range( 5 ):
		if ( F >= freq[ i ][ 0 ] ) and ( F <= freq[ i ][ 1 ] ):
			return i
	return 0
	
F = [ 350, 450, 800, 1400, 2300 ]

output_dir = str(sys.argv[ 3 ])
psr = T.tempopulsar( parfile = str( sys.argv[ 1 ] ), timfile = str( sys.argv[ 2 ] ) )
sort_cmp = N.argsort( psr.stoas )
bin = [] # format: residuals, error, TOA, frequency

# sorting residuals, errors, frequencies, and TOAs by TOAs
Residuals = psr.residuals()[ sort_cmp ]
Errors = psr.toaerrs[ sort_cmp ]
TOA = psr.stoas[ sort_cmp ]
FREQ = psr.freqs[ sort_cmp ]

curr = [ Residuals[ 0 ], Errors[ 0 ], TOA[ 0 ], FREQ[ 0 ] ]
for i in range( 1, len( TOA ) ):
	if( TOA[ i ]-TOA[ i-1 ] > (3.0/144.0) ):
		bin.append( curr )
		curr = []
	curr.append( Residuals[ i ] )
parfiles = []
timfiles = []
for file in sorted(glob.glob(args.datapath+"/*.par")):
    parfiles.append(file)
    
for file in sorted(glob.glob(args.datapath+"/*.tim")):
    timfiles.append(file)

print parfiles, timfiles
################################################################################################################################
# PASSING THROUGH TEMPO2 VIA libstempo
################################################################################################################################

t2psr=[]
for ii in range(len(parfiles)):
    t2psr.append(T2.tempopulsar(parfile=parfiles[ii], timfile=timfiles[ii]))
    t2psr[ii].fit(iters=10)
    #if np.any(np.isfinite(t2psr[ii].residuals())==False)==True:
    #    t2psr[ii] = T2.tempopulsar(parfile=parfiles[ii], timfile=timfiles[ii])


# storing red-noise amplitude (nano style),
# gamma (nano style), white-noise level (microseconds)
# [from Arzoumanian et al. (2015a)]
active_psrs = OrderedDict.fromkeys([p.name for p in t2psr])

active_psrs['B1855+09'] = [0.017,-4.9,0.505]
active_psrs['J0030+0451'] = [0.014,-4.8,0.212]
active_psrs['J0613-0200'] = [0.093,-2.9,0.165]
active_psrs['J1012+5307'] = [0.669,-1.0,0.355]
active_psrs['J1024-0719'] = [0.0,0.0,0.280]
parser.add_option('--timfile', dest='timfile', action='store', type=str,
                   help='Full path to timfile')
parser.add_option('--sample-or-maximize', dest='sample_or_maximize', action='store', type=str, default='maximize',
                   help='Do you want sample from the posteror distribution or just find the maximum likelihood noise values? (default=\'maximize\')?')

(args, x) = parser.parse_args()

if args.nmodes:
    print "\n You've given me the number of frequencies to include in the low-rank time-frequency approximation, got it?\n"
else:
    print "\n You've given me the sampling cadence for the observations, which determines the upper frequency limit and the number of modes, got it?\n"

################################################################################################################################
# PASSING THROUGH TEMPO2 VIA libstempo
################################################################################################################################
t2psr = T2.tempopulsar(parfile=args.parfile, timfile=args.timfile)
t2psr.fit(iters=10)
if np.any(np.isfinite(t2psr.residuals())==False)==True:
    t2psr = T2.tempopulsar(parfile=args.parfile,timfile=args.timfile)

psr = NX01_psr.PsrObj(t2psr)
psr.grab_all_vars()

if args.sample_or_maximize == 'maximize':
    from pyswarm import pso
else:    
    if not os.path.exists('chains_singlePsr_{0}'.format(psr.name)):
        os.makedirs('chains_singlePsr_{0}'.format(psr.name))

################################################################################################################################
# GETTING MAXIMUM TIME, COMPUTING FOURIER DESIGN MATRICES, AND GETTING MODES 
Exemple #40
0
parser.add_argument(                    dest='pulsar',
                    help='pulsar name (no suffix)')

cfg = parser.parse_args()

# by default, we search over all parameters that are fit by tempo2
if not cfg.searchpars:
    cfg.searchpars = psr.fitpars
else:
    cfg.searchpars = libstempo.like.expandranges(cfg.searchpars.split(','))

# the output files will be of the form {outputdir}/{pulsar}[-{suffix}]-...
basename = '{0}/{1}{2}'.format(cfg.outputdir,cfg.pulsar,'-' + cfg.suffix if cfg.suffix else '')

parfile, timfile = libstempo.findpartim(pulsar=cfg.pulsar,dirname=cfg.inputdir,partimfiles=cfg.files)
pulsar = libstempo.tempopulsar(parfile=parfile,timfile=timfile)

for par in cfg.searchpars:
    if par in pulsar.fitpars:
        pulsar[par].fit = False

ll = libstempo.like.Loglike(pulsar,cfg.searchpars)
p0 = libstempo.like.Prior(pulsar,cfg.searchpars)

iamroot = MPI.COMM_WORLD.Get_rank() == 0

if cfg.config is None: cfg.config = cfg.pulsar + '.ini'
if os.path.isfile(cfg.config):
    if iamroot: print "Loading configuration file", cfg.config
    execfile(cfg.config)
Exemple #41
0
def Pulsar(*args, **kwargs):

    ephem = kwargs.get('ephem', None)
    planets = kwargs.get('planets', True)
    sort = kwargs.get('sort', True)
    drop_t2pulsar = kwargs.get('drop_t2pulsar', True)
    timing_package = kwargs.get('timing_package', 'tempo2')

    if pint:
        toas = list(filter(lambda x: isinstance(x, toa.TOAs), args))
        model = list(filter(lambda x: isinstance(x, TimingModel), args))

    t2pulsar = list(filter(lambda x: isinstance(x, t2.tempopulsar), args))

    parfile = list(filter(lambda x: isinstance(x, str) and
                          x.split('.')[-1] == 'par', args))
    timfile = list(filter(lambda x: isinstance(x, str) and
                          x.split('.')[-1] in ['tim', 'toa'], args))

    if pint and toas and model:
        return PintPulsar(toas[0], model[0], sort=sort, planets=planets)
    elif t2pulsar:
        return Tempo2Pulsar(t2pulsar, sort=sort, drop_t2pulsar=drop_t2pulsar,
                            planets=planets)
    elif parfile and timfile:
        # Check whether the two files exist
        if not os.path.isfile(parfile[0]) or not os.path.isfile(timfile[0]):
            msg = 'Cannot find parfile {0} or timfile {1}!'.format(
                parfile[0], timfile[0])
            raise IOError(msg)

        # Obtain the directory name of the timfile, and change to it
        timfiletup = os.path.split(timfile[0])
        dirname = timfiletup[0] or './'
        reltimfile = timfiletup[-1]
        relparfile = os.path.relpath(parfile[0], dirname)

        # get current directory
        cwd = os.getcwd()

        # Change directory to the base directory of the tim-file to deal with
        # INCLUDE statements in the tim-file
        os.chdir(dirname)

        if timing_package.lower() == 'pint':
            if ephem is None:
                ephem = 'DE421'
            toas = toa.get_TOAs(reltimfile, ephem=ephem, planets=planets)
            model = mb.get_model(relparfile)
            os.chdir(cwd)
            return PintPulsar(toas, model, sort=sort, planets=planets)

        elif timing_package.lower() == 'tempo2':

            # hack to set maxobs
            maxobs = get_maxobs(reltimfile) + 100
            t2pulsar = t2.tempopulsar(relparfile, reltimfile,
                                      maxobs=maxobs, ephem=ephem)
            os.chdir(cwd)
            return Tempo2Pulsar(t2pulsar, sort=sort,
                                drop_t2pulsar=drop_t2pulsar,
                                planets=planets)
    else:
        print('Unknown arguments {}'.format(args))
Exemple #42
0
def readPrepRealisations(path):
    psrs = []
    ntotobs, ntotpars = 0, 0
    relparpath = '/par/'
    reltimpath = '/tim/'
    reldespath = '/design/'
    relrespath = '/res/'
    relcovpath = '/cov/'

    # Read in all the pulsars through libstempo
    for infile in glob.glob(os.path.join(path + relparpath, '*.par')):
        filename = os.path.splitext(infile)
        basename = os.path.basename(filename[0])

        # Locate the par-file and the timfile
        parfile = path + relparpath + basename + '.par'
        timfile = path + reltimpath + basename + '.tim'
        desfile = path + reldespath + basename + 'designMatrix.txt'

        # Make the libstempo object
        timfiletup = os.path.split(timfile)
        dirname = timfiletup[0]
        reltimfile = timfiletup[-1]
        relparfile = os.path.relpath(parfile, dirname)
        savedir = os.getcwd()
        os.chdir(dirname)
        psrs.append(T.tempopulsar(relparfile, reltimfile))
        os.chdir(savedir)

        # Read the designmatrix from the file
        #desmat = psrs[-1].designmatrix().copy()
        desmat = np.loadtxt(desfile)

        # Register how many paramters and observations we have
        #ntotobs += psr.nobs
        #ntotpars += psr.ndim+1
        ntotobs += desmat.shape[0]
        ntotpars += desmat.shape[1]
        if desmat.shape[0] != psrs[-1].nobs:
            print "For " + basename + " obs != obs ", desmat.shape[0], psrs[
                -1].nobs

        if desmat.shape[1] != psrs[-1].ndim + 1:
            print "For " + basename + " pars != pars ", desmat.shape[
                1], psrs[-1].ndim + 1

    hdmat = hdcormat(psrs)

    # Allocate memory for the model description
    toas = np.zeros(ntotobs)
    residuals = np.zeros(ntotobs)
    toaerrs = np.zeros(ntotobs)
    designmatrix = np.zeros((ntotobs, ntotpars))
    Gmatrix = np.zeros((ntotobs, ntotobs - ntotpars))
    GNGinv = []
    ptheta = []
    pphi = []
    psrobs = []
    psrpars = []
    psrg = []

    infiles = glob.glob(os.path.join(path + relparpath, '*.par'))
    indo, indp, indg = 0, 0, 0
    for i in range(len(infiles)):
        # This automatically also loops over the psrs of course
        filename = os.path.splitext(infiles[i])
        basename = os.path.basename(filename[0])

        # Locate the par-file and the timfile
        parfile = path + relparpath + basename + '.par'
        timfile = path + reltimpath + basename + '.tim'
        desfile = path + reldespath + basename + 'designMatrix.txt'
        covfile = path + relcovpath + basename + 'covMatrix.txt'
        resfile = path + relrespath + basename + 'res.dat'

        # Read in the design matrix (again), covariance matrix, and the residuals
        desmat = np.loadtxt(desfile)
        covmat = np.loadtxt(covfile)
        resvec = np.loadtxt(resfile)
        # These comments are for when reading in from the psr object
        #desmat = psrs[i].designmatrix().copy()
        #covmat = np.diag((psrs[i].toaerrs*1e-6)**2)
        #resvec = np.array([psrs[i].toas(), psrs[i].residuals(), psrs[i].toaerrs*1e-6]).T

        # Determine the dimensions
        pobs = desmat.shape[0]
        ppars = desmat.shape[1]
        pgs = desmat.shape[0] - desmat.shape[1]
        psrobs.append(pobs)
        psrpars.append(ppars)
        psrg.append(pgs)

        # Load the basic quantities
        toas[indo:indo + pobs] = resvec[:, 0]
        residuals[indo:indo + pobs] = resvec[:, 1]
        toaerrs[indo:indo + pobs] = resvec[:, 2]

        designmatrix[indo:indo + pobs, indp:indp + ppars] = desmat
        ptheta.append(0.5 * np.pi - psrs[i]['DECJ'].val)
        pphi.append(psrs[i]['RAJ'].val)

        # Create the G-matrix
        U, s, Vh = sl.svd(desmat)
        Gmatrix[indo:indo + pobs, indg:indg + pgs] = U[:, ppars:].copy()

        # Create the noise matrix
        pNoise = covmat
        GNG = np.dot(U[:, ppars:].copy().T, np.dot(pNoise, U[:,
                                                             ppars:].copy()))
        cf = sl.cho_factor(GNG)
        GNGinv.append(sl.cho_solve(cf, np.identity(GNG.shape[0])))

        indo += pobs
        indp += ppars
        indg += pgs

    model = (toas, residuals, toaerrs, designmatrix, Gmatrix, hdmat, psrobs,
             psrpars, psrg, GNGinv, ptheta, pphi)

    return (psrs, model)
	Nbins = len(profamps)
	Step=100
	noiselist=[]
	for i in range(Nbins-Step):
		noise=np.std(profamps[i:i+Step])
		noiselist.append(noise)
	noiselist=np.array(noiselist)
	minnoise=np.min(noiselist)
	threesiglist=noiselist[noiselist<3*minnoise]
	mediannoise=np.median(threesiglist)
	return mediannoise

SECDAY = 24*60*60

#First load pulsar.  We need the sats (separate day/second), and the file names of the archives (FNames)
psr = T.tempopulsar(parfile="OneProf.par", timfile = "OneChan.tim")
psr.fit()
SatSecs = psr.satSec()
SatDays = psr.satDay()
FNames = psr.fnames()
NToAs = psr.nobs


#Check how many timing model parameters we are fitting for (in addition to phase)
numTime=len(psr.pars())
redChisq = psr.chisq()/(psr.nobs-len(psr.pars())-1)
TempoPriors=np.zeros([numTime,2]).astype(np.float128)
for i in range(numTime):
        TempoPriors[i][0]=psr[psr.pars()[i]].val
        TempoPriors[i][1]=psr[psr.pars()[i]].err/np.sqrt(redChisq)
	print "fitting for: ", psr.pars()[i], TempoPriors[i][0], TempoPriors[i][1]
Exemple #44
0
    def __init__(self, parfile=None, timfile=None, testpulsar=False):
        """
        Initialize the pulsar object

        @param parfile:     Filename of par file
        @param timfile:     Filename of tim file
        @param testpulsar:  If true, load J1744 test pulsar
        """

        self._interface = "libstempo"

        if testpulsar:
            # Write a test-pulsar, and open that for testing
            parfilename = tempfile.mktemp()
            timfilename = tempfile.mktemp()
            parfile = open(parfilename, 'w')
            timfile = open(timfilename, 'w')
            parfile.write(J1744_parfile)
            timfile.write(J1744_timfile)
            parfile.close()
            timfile.close()

            self._psr = lt.tempopulsar(parfilename, timfilename, dofit=False)

            os.remove(parfilename)
            os.remove(timfilename)
        elif parfile is not None and timfile is not None:
            self._psr = lt.tempopulsar(parfile, timfile, dofit=False)
        else:
            raise ValueError("No valid pulsar to load")

        # Some parameters we do not want to add a fitting checkbox for:
        self.nofitboxpars = ['START', 'FINISH', 'POSEPOCH', 'PEPOCH', 'DMEPOCH', \
            'EPHVER', 'TZRMJD', 'TZRFRQ', 'TRES']

        # The possible binary pulsar parameters
        self.binarypars = [
            'T0', 'T0_1', 'PB', 'PBDOT', 'PB_1', 'ECC', 'ECC_1', 'OM', 'OM_1',
            'A1', 'A1_1', 'OM', 'OM_1', 'E2DOT', 'EDOT', 'KOM', 'KIN',
            'SHAPMAX', 'M2', 'MTOT', 'DR', 'DTH', 'A0', 'B0', 'BP', 'BPP',
            'DTHETA', 'SINI', 'H3', 'STIG', 'H4', 'NHARM', 'GAMMA', 'PBDOT',
            'XPBDOT', 'XDOT', 'X2DOT', 'XOMDOT', 'AFAC', 'OMDOT', 'OM2DOT',
            'ORBPX', 'TASC', 'EPS1', 'EPS1DOT', 'EPS2', 'EPS2DOT', 'TZRMJD',
            'TZRFRQ', 'TSPAN', 'BPJEP_0', 'BPJEP_1', 'BPJPH_0', 'BPJPH_1',
            'BPJA1_0', 'BPJA1_1', 'BPJEC_0', 'BPJEC_1', 'BPJOM_0', 'BPJOM_1',
            'BPJPB_0', 'BPJPB_1'
        ]

        self.binmodel_ids = [
            'BT', 'BTJ', 'BTX', 'ELL1', 'DD', 'DDK', 'DDS', 'MSS', 'DDGR',
            'T2', 'T2-PTA', 'DDH', 'ELL1H'
        ]

        # BTmodel
        self.binmodel = OrderedDict()
        self.binmodel['BT'] = OrderedDict({
            'T0': [50000.0, 60000.0],
            'PB': [0.01, 3000],
            'ECC': [0.0, 1.0],
            'PBDOT': [0.0, 1.0e-8],
            'A1': [0.0, 1.0e3],
            'XDOT': [-1.0e-12, 1.0e-12],
            'OMDOT': [0.0, 5.0],
            'OM': [0.0, 360.0],
            'GAMMA': [0.0, 1.0]  # What is the scale of this parameter??
        })
        self.binmodel['BTJ'] = OrderedDict({
            'T0': [50000.0, 60000.0],
            'PB': [0.01, 3000],
            'ECC': [0.0, 1.0],
            'PBDOT': [0.0, 1.0e-8],
            'XDOT': [-1.0e-12, 1.0e-12],
            'A1': [0.0, 1.0e3],
            'OMDOT': [0.0, 5.0],
            'OM': [0.0, 360.0],
            'GAMMA': [0.0, 1.0],  # What is the scale of this parameter??
            'BPJEP_0': [0.0, 1.0],  # ??
            'BPJEP_1': [0.0, 1.0],
            'BPJPH_0': [0.0, 1.0],
            'BPJPH_1': [0.0, 1.0],
            'BPJA1_0': [0.0, 1.0],
            'BPJA1_1': [0.0, 1.0],
            'BPJEC_0': [0.0, 1.0],
            'BPJEC_1': [0.0, 1.0],
            'BPJOM_0': [0.0, 1.0],
            'BPJOM_1': [0.0, 1.0],
            'BPJPB_0': [0.0, 1.0],
            'BPJPB_1': [0.0, 1.0]
        })
        self.binmodel['BTX'] = OrderedDict({
            'T0': [50000.0, 60000.0],
            'ECC': [0.0, 1.0],
            'XDOT': [-1.0e-12, 1.0e-12],
            'A1': [0.0, 1.0e3],
            'OMDOT': [0.0, 5.0],
            'OM': [0.0, 360.0],
            'GAMMA': [0.0, 1.0],  # What is the scale of this parameter??
            'FB0': [0.0, 1.0],  # ??
            'FB1': [0.0, 1.0],  # ??
            'FB2': [0.0, 1.0],  # ??
            'FB3': [0.0, 1.0],  # ??
            'FB4': [0.0, 1.0],  # ??
            'FB5': [0.0, 1.0],  # ??
            'FB6': [0.0, 1.0],  # ??
            'FB7': [0.0, 1.0],  # ??
            'FB8': [0.0, 1.0],  # ??
            'FB9': [0.0, 1.0]  # ??
        })
        self.binmodel['DD'] = OrderedDict({
            'SINI': [0.0, 1.0],
            'M2': [0.0, 10.0],
            'PB': [0.01, 3000],
            'OMDOT': [0.0, 5.0],
            'T0': [50000.0, 60000.0],
            'GAMMA': [0.0, 1.0],  # What is the scale of this parameter??
            'OM': [0.0, 360.0],
            'A1': [0.0, 1.0e3],
            'XDOT': [-1.0e-12, 1.0e-12],
            'PBDOT': [0.0, 1.0e-8],
            'ECC': [0.0, 1.0],
            'XPBDOT': [0.0, 1.0],  # Units???  Scale???
            'EDOT': [0.0, 1.0]  # Units???  Scale???
        })
        self.binmodel['DDS'] = OrderedDict({
            'SHAPMAX': [0.0, 1.0],  # Scale?
            'SINI': [0.0, 1.0],
            'M2': [0.0, 10.0],
            'PB': [0.01, 3000],
            'OMDOT': [0.0, 5.0],
            'T0': [50000.0, 60000.0],
            'GAMMA': [0.0, 1.0],  # What is the scale of this parameter??
            'OM': [0.0, 360.0],
            'A1': [0.0, 1.0e3],
            'XDOT': [-1.0e-12, 1.0e-12],
            'PBDOT': [0.0, 1.0e-8],
            'ECC': [0.0, 1.0],
            'XPBDOT': [0.0, 1.0],  # Units???  Scale???
            'EDOT': [0.0, 1.0]  # Units???  Scale???
        })
        self.binmodel['DDGR'] = OrderedDict({
            'T0': [50000.0, 60000.0],
            'SINI': [0.0, 1.0],
            'M2': [0.0, 10.0],
            'PB': [0.01, 3000],
            'MTOT': [0.0, 10.0],
            'OM': [0.0, 360.0],
            'XDOT': [-1.0e-12, 1.0e-12],
            'PBDOT': [0.0, 1.0e-8],
            'XPBDOT': [0.0, 1.0],  # Units???  Scale???
            'A1': [0.0, 1.0e3],
            'ECC': [0.0, 1.0],
            'EDOT': [0.0, 1.0]  # Units???  Scale???
        })