예제 #1
0
    def test_mulit_obs(self):
        obs1 = "gbt"
        obs2 = "ao"
        obs3 = "barycenter"
        site1 = get_observatory(obs1)
        site2 = get_observatory(obs2)
        site3 = get_observatory(obs3)
        t1 = TOA(self.MJD, freq=self.freq, obs=obs1, error=self.error)
        t2 = TOA(self.MJD + 1.0, freq=self.freq, obs=obs2, error=self.error)
        t3 = TOA(self.MJD + 1.0, freq=self.freq, obs=obs3, error=self.error)

        toas = TOAs(toalist=[t1, t2, t3])

        # Table will be grouped by observatories, and will be sorted by the
        # observatory so the TOA order will be different
        assert toas.table["obs"][0] == site2.name
        assert toas.table["mjd"][0] == t2.mjd
        assert toas.table["obs"][1] == site3.name
        assert toas.table["mjd"][1] == t3.mjd
        assert toas.table["obs"][2] == site1.name
        assert toas.table["mjd"][2] == t1.mjd

        # obs in time object
        assert toas.table["mjd"][0].location == site2.earth_location_itrf()
        assert toas.table["mjd"][1].location == site3.earth_location_itrf()
        assert toas.table["mjd"][2].location == site1.earth_location_itrf()
예제 #2
0
 def test_positions(self):
     for tobs in self.test_obs:
         site = get_observatory(tobs, include_gps=True, include_bipm=True,
                                bipm_version='BIPM2015')
         posvel = site.posvel(self.test_time, ephem='de436')
         assert posvel.pos.shape == (3, len(self.test_time))
         assert posvel.vel.shape == (3, len(self.test_time))
예제 #3
0
 def test_different_bipm(self):
     for tobs in self.test_obs:
         site = get_observatory(tobs,
                                include_gps=False,
                                include_bipm=True,
                                bipm_version="BIPM2019")
         assert site, "BIPM2019 is not a valid BIPM choice"
예제 #4
0
 def test_get_obs(self):
     for tobs in self.test_obs:
         site = get_observatory(tobs,
                                include_gps=False,
                                include_bipm=True,
                                bipm_version="BIPM2015")
         assert site, "Observatory {} did not initialize correctly".format(
             tobs)
예제 #5
0
 def test_positions(self):
     for tobs in self.test_obs:
         site = get_observatory(
             tobs, include_gps=True, include_bipm=True, bipm_version="BIPM2015"
         )
         posvel = site.posvel(self.test_time, ephem="de436")
         assert posvel.pos.shape == (3, len(self.test_time))
         assert posvel.vel.shape == (3, len(self.test_time))
예제 #6
0
 def test_wrong_path(self):
     # observatory clock correction path expections.
     fake_obs = TopoObs('Fake1', tempo_code='?', itoa_code='FK',
                         clock_fmt='tempo2', clock_file='fake2gps.clk',
                         clock_dir='TEMPO2', itrf_xyz=[0.00, 0.0, 0.0])
     site = get_observatory('Fake1', include_gps=True, include_bipm=True,
                            bipm_version='BIPM2015')
     with self.assertRaises(RuntimeError):
         _ = site.clock_corrections(self.test_time)
예제 #7
0
 def test_clock_corr(self):
     for tobs in self.test_obs:
         site = get_observatory(tobs, include_gps=True, include_bipm=True,
                                bipm_version='BIPM2015')
         clock_corr = site.clock_corrections(self.test_time)
         assert len(clock_corr) == len(self.test_time)
         # Test one time
         clock_corr1 = site.clock_corrections(self.test_time[0])
         assert clock_corr1.shape == ()
예제 #8
0
 def test_wrong_TDB_method(self):
     site = get_observatory('ao', include_gps=True, include_bipm=True,
                            bipm_version='BIPM2015')
     with self.assertRaises(ValueError):
         _ = site.get_TDBs(self.test_time, method='ephemeris')
     with self.assertRaises(ValueError):
         _ = site.get_TDBs(self.test_time, method='Unknown_method')
     with self.assertRaises(ValueError):
         _ = site.get_TDBs(self.test_time, method='ephemeris', ephem='de436')
예제 #9
0
 def test_wrong_TDB_method(self):
     site = get_observatory(
         "ao", include_gps=True, include_bipm=True, bipm_version="BIPM2015"
     )
     with self.assertRaises(ValueError):
         site.get_TDBs(self.test_time, method="ephemeris")
     with self.assertRaises(ValueError):
         site.get_TDBs(self.test_time, method="Unknown_method")
     with self.assertRaises(ValueError):
         site.get_TDBs(self.test_time, method="ephemeris", ephem="de436")
예제 #10
0
 def test_clock_corr(self):
     for tobs in self.test_obs:
         site = get_observatory(
             tobs, include_gps=True, include_bipm=True, bipm_version="BIPM2015"
         )
         clock_corr = site.clock_corrections(self.test_time)
         assert len(clock_corr) == len(self.test_time)
         # Test one time
         clock_corr1 = site.clock_corrections(self.test_time[0])
         assert clock_corr1.shape == ()
예제 #11
0
 def test_wrong_TDB_method(self):
     site = get_observatory('ao',
                            include_gps=True,
                            include_bipm=True,
                            bipm_version='BIPM2015')
     with self.assertRaises(ValueError):
         site.get_TDBs(self.test_time, method='ephemeris')
     with self.assertRaises(ValueError):
         site.get_TDBs(self.test_time, method='Unknown_method')
     with self.assertRaises(ValueError):
         site.get_TDBs(self.test_time, method='ephemeris', ephem='de436')
예제 #12
0
 def test_wrong_path(self):
     # observatory clock correction path expections.
     fake_obs = TopoObs('Fake1',
                        tempo_code='?',
                        itoa_code='FK',
                        clock_fmt='tempo2',
                        clock_file='fake2gps.clk',
                        clock_dir='TEMPO2',
                        itrf_xyz=[0.00, 0.0, 0.0])
     site = get_observatory('Fake1',
                            include_gps=True,
                            include_bipm=True,
                            bipm_version='BIPM2015')
     with self.assertRaises(RuntimeError):
         _ = site.clock_corrections(self.test_time)
예제 #13
0
    def test_get_TDBs(self):
        for tobs in self.test_obs:
            site = get_observatory(
                tobs, include_gps=True, include_bipm=True, bipm_version="BIPM2015"
            )
            # Test default TDB calculation
            tdbs = site.get_TDBs(self.test_time)
            assert len(tdbs) == len(self.test_time)
            tdb1 = site.get_TDBs(self.test_time[0])
            assert tdb1.shape == (1,)

            # Test TDB calculation from ephemeris
            tdbs = site.get_TDBs(self.test_time, method="ephemeris", ephem="de430t")
            assert len(tdbs) == len(self.test_time)
            tdb1 = site.get_TDBs(self.test_time[0], method="ephemeris", ephem="de430t")
            assert tdb1.shape == (1,)
예제 #14
0
def test_get_TOAs():
    tt = get_TOAs(StringIO(tim), ephem="DE421")
    # Check the site clock correction by itself
    site = get_observatory("meerkat", include_gps=False, include_bipm=False)
    clock_corr = site.clock_corrections(tt.table["mjd"][0])
    assert np.isclose(clock_corr.value, 0.40802)  # from mk2utc.clk
    # Now check barycentering
    mm = get_model(parfile)
    # The "correct" value is from PRESTO's bary command run like this:
    # > bary MK 17:48:52.75 -20:21:29.0 1400 223.9 DE421
    # 58512.921979
    # .
    # Note that the DM is important!
    bts = mm.get_barycentric_toas(tt)
    # This checks to see if they are the same to within 50us.
    assert np.fabs((bts[0].value - 58512.9185136816) * 86400.0) < 50e-6
예제 #15
0
파일: polar_toas.py 프로젝트: produit/PINT
def load_Polar_TOAs(eventname,weightcolumn="",minunix=0.0, maxunix=2500000000.0):
    '''
    TOAlist = load_Polar_TOAs(eventname)
      Read photon event times out of a POLAR event root file and return
      a list of PINT TOA objects.

      weightcolumn specifies ROOT Branch name to read the photon weights
      from. 

    '''
    import ROOT
    # Load photon times from POLAR event ROOT file
    f = ROOT.TFile.Open(eventname,"read")
    t = f.Get("t_trigger")

    # Read time Branch from ROOT file
    filter = '(tunix > '+str(minunix)+') && (tunix < '+str(maxunix)+')'
    if weightcolumn == "":
        array = tree2array(t,branches=['tunix'],selection=filter,start=0,stop=10000,step=1)
        ut=array['tunix']
    else:
        array = tree2array(t,branches=['tunix',weightcolumn],selection=filter,start=0,stop=10000,step=1)
        ut=array['tunix']
        weights=array[weightcolumn]
    if len(ut) == 0:
        log.error('No unix time read from file!')
        raise

    e=50*u.keV
    mjd1=Time(ut[0],format="unix")
    mjd2=Time(ut[-1],format="unix")
    log.info('Building spacecraft local TOAs, with MJDs in range {0} to {1}'.format(
            mjd1.tt.mjd,mjd2.tt.mjd))
    polarobs = get_observatory('Polar')

    try:
        if weightcolumn == "":
            toalist=[toa.TOA(Time(m,format="unix").tt.mjd, obs='Polar', scale='tt',energy=e)
                            for m in ut]
        else:
            toalist=[toa.TOA(Time(m,format="unix").tt.mjd, obs='Polar', scale='tt',energy=e,weight=w)
                            for m,w in zip(ut,weights)]
    except KeyError:
        log.error('Error processing POLAR TOAs. You may have forgotten to specify an ENG file file with --eng')
        raise

    return toalist
예제 #16
0
    def test_get_TDBs(self):
        for tobs in self.test_obs:
            site = get_observatory(tobs, include_gps=True, include_bipm=True,
                                   bipm_version='BIPM2015')
            # Test default TDB calculation
            tdbs = site.get_TDBs(self.test_time)
            assert len(tdbs) == len(self.test_time)
            tdb1 = site.get_TDBs(self.test_time[0])
            assert tdb1.shape == (1,)

            # Test TDB calculation from ephemeris
            tdbs = site.get_TDBs(self.test_time, method='ephemeris',
                                 ephem='de430t')
            assert len(tdbs) == len(self.test_time)
            tdb1 = site.get_TDBs(self.test_time[0], method='ephemeris',
                                 ephem='de430t')
            assert tdb1.shape == (1,)
예제 #17
0
 def test_no_tempo_but_tempo_clock_requested(self):
     if os.getenv('TEMPO') is not None:
         pytest.skip(
             "TEMPO evnironment variable is set, can't run this test")
     # observatory clock correction path expections.
     fake_obs = TopoObs('Fake1',
                        tempo_code='?',
                        itoa_code='FK',
                        clock_fmt='tempo',
                        clock_file='fake2gps.clk',
                        clock_dir='TEMPO',
                        itrf_xyz=[0.00, 0.0, 0.0])
     site = get_observatory('Fake1',
                            include_gps=True,
                            include_bipm=True,
                            bipm_version='BIPM2015')
     with pytest.raises(RuntimeError):
         site.clock_corrections(self.test_time)
예제 #18
0
 def test_no_tempo2_but_tempo2_clock_requested(self):
     if os.getenv("TEMPO2") is not None:
         pytest.skip("TEMPO2 evnironment variable is set, can't run this test")
     # observatory clock correction path expections.
     fake_obs = TopoObs(
         "Fake1",
         tempo_code="?",
         itoa_code="FK",
         clock_fmt="tempo2",
         clock_file="fake2gps.clk",
         clock_dir="TEMPO2",
         itrf_xyz=[0.00, 0.0, 0.0],
     )
     site = get_observatory(
         "Fake1", include_gps=True, include_bipm=True, bipm_version="BIPM2015"
     )
     with pytest.raises(RuntimeError):
         site.clock_corrections(self.test_time)
예제 #19
0
 def test_clock_correction_file_not_available(self):
     if os.getenv('TEMPO2') is None:
         pytest.skip(
             "TEMPO2 evnironment variable is not set, can't run this test")
     # observatory clock correction path expections.
     fake_obs = TopoObs('Fake1',
                        tempo_code='?',
                        itoa_code='FK',
                        clock_fmt='tempo2',
                        clock_file='fake2gps.clk',
                        clock_dir='TEMPO2',
                        itrf_xyz=[0.00, 0.0, 0.0])
     site = get_observatory('Fake1',
                            include_gps=True,
                            include_bipm=True,
                            bipm_version='BIPM2015')
     try:
         site.clock_corrections(self.test_time)
     except OSError as e:
         assert e.errno == 2
         assert os.path.basename(e.filename) == 'fake2gps.clk'
예제 #20
0
 def test_clock_correction_file_not_available(self):
     if os.getenv("TEMPO2") is None:
         pytest.skip("TEMPO2 evnironment variable is not set, can't run this test")
     # observatory clock correction path expections.
     fake_obs = TopoObs(
         "Fake1",
         tempo_code="?",
         itoa_code="FK",
         clock_fmt="tempo2",
         clock_file="fake2gps.clk",
         clock_dir="TEMPO2",
         itrf_xyz=[0.00, 0.0, 0.0],
     )
     site = get_observatory(
         "Fake1", include_gps=True, include_bipm=True, bipm_version="BIPM2015"
     )
     try:
         site.clock_corrections(self.test_time)
     except (OSError, IOError) as e:
         assert e.errno == 2
         assert os.path.basename(e.filename) == "fake2gps.clk"
예제 #21
0
    def troposphere_delay(self, toas, acc_delay=None):
        """This is the main function for the troposphere delay.
        Pass in the TOAs and it will calculate the delay for each TOA,
        accounting for the observatory locaiton, target coordiantes, and time of observation
        """
        tbl = toas.table
        delay = np.zeros(len(tbl))

        # if not correcting for troposphere, return the default zero delay
        if self.CORRECT_TROPOSPHERE.value:

            radec = self._get_target_skycoord()

            # the only python for loop is to iterate through the unique observatory locations
            # all other math is computed through numpy
            for ii, key in enumerate(tbl.groups.keys):
                grp = tbl.groups[ii]
                loind, hiind = tbl.groups.indices[ii : ii + 2]
                obsobj = get_observatory(tbl.groups.keys[ii]["obs"])

                # exclude non topocentric observations
                if not isinstance(obsobj, TopoObs):
                    log.debug(
                        "Skipping Troposphere delay for non Topocentric TOA: %s"
                        % obsobj.name
                    )
                    continue

                obs = obsobj.earth_location_itrf()

                alt = self._get_target_altitude(obs, grp, radec)

                # now actually calculate the atmospheric delay based on the models

                delay[loind:hiind] = self.delay_model(
                    alt, obs.lat, obs.height, grp["tdbld"]
                )
        return delay * u.s
예제 #22
0
def load_Fermi_TOAs(ft1name,
                    weightcolumn=None,
                    targetcoord=None,
                    logeref=4.1,
                    logesig=0.5,
                    minweight=0.0,
                    minmjd=0.0,
                    maxmjd=np.inf):
    '''
    TOAlist = load_Fermi_TOAs(ft1name)
      Read photon event times out of a Fermi FT1 file and return
      a list of PINT TOA objects.
      Correctly handles raw FT1 files, or ones processed with gtbary
      to have barycentered or geocentered TOAs.

      weightcolumn specifies the FITS column name to read the photon weights
      from.  The special value 'CALC' causes the weights to be computed empirically
      as in Philippe Bruel's SearchPulsation code.
      logeref and logesig are parameters for the weight computation and are only
      used when weightcolumn='CALC'.

      When weights are loaded, or computed, events are filtered by weight >= minweight
    '''
    import astropy.io.fits as pyfits
    # Load photon times from FT1 file
    hdulist = pyfits.open(ft1name)
    ft1hdr = hdulist[1].header
    ft1dat = hdulist[1].data

    # TIMESYS will be 'TT' for unmodified Fermi LAT events (or geocentered), and
    #                 'TDB' for events barycentered with gtbary
    # TIMEREF will be 'GEOCENTER' for geocentered events,
    #                 'SOLARSYSTEM' for barycentered,
    #             and 'LOCAL' for unmodified events

    timesys = ft1hdr['TIMESYS']
    log.info("TIMESYS {0}".format(timesys))
    timeref = ft1hdr['TIMEREF']
    log.info("TIMEREF {0}".format(timeref))

    # Read time column from FITS file
    mjds = read_fits_event_mjds_tuples(hdulist[1])
    if len(mjds) == 0:
        log.error('No MJDs read from file!')
        raise

    energies = ft1dat.field('ENERGY') * u.MeV
    if weightcolumn is not None:
        if weightcolumn == 'CALC':
            photoncoords = SkyCoord(ft1dat.field('RA') * u.degree,
                                    ft1dat.field('DEC') * u.degree,
                                    frame='icrs')
            weights = calc_lat_weights(ft1dat.field('ENERGY'),
                                       photoncoords.separation(targetcoord),
                                       logeref=logeref,
                                       logesig=logesig)
        else:
            weights = ft1dat.field(weightcolumn)
        if minweight > 0.0:
            idx = np.where(weights > minweight)[0]
            mjds = mjds[idx]
            energies = energies[idx]
            weights = weights[idx]

    # limit the TOAs to ones in selected MJD range
    mjds_float = np.array([r[0] + r[1] for r in mjds])
    idx = np.logical_and((mjds_float > minmjd), (mjds_float < maxmjd))
    mjds = mjds[idx]
    energies = energies[idx]
    if weightcolumn is not None:
        weights = weights[idx]

    if timesys == 'TDB':
        log.info("Building barycentered TOAs")
        if weightcolumn is None:
            toalist = [
                toa.TOA(m,
                        obs='Barycenter',
                        scale='tdb',
                        energy=e,
                        error=1.0 * u.us) for m, e in zip(mjds, energies)
            ]
        else:
            toalist = [
                toa.TOA(m,
                        obs='Barycenter',
                        scale='tdb',
                        energy=e,
                        weight=w,
                        error=1.0 * u.us)
                for m, e, w in zip(mjds, energies, weights)
            ]
    else:
        if timeref == 'LOCAL':
            log.info(
                'Building spacecraft local TOAs, with MJDs in range {0} to {1}'
                .format(mjds[0], mjds[-1]))
            assert timesys == 'TT'
            try:
                fermiobs = get_observatory('Fermi')
            except KeyError:
                log.error(
                    'Fermi observatory not defined. Make sure you have specified an FT2 file!'
                )
                raise

            try:
                if weightcolumn is None:
                    toalist = [
                        toa.TOA(m,
                                obs='Fermi',
                                scale='tt',
                                energy=e,
                                error=1.0 * u.us)
                        for m, e in zip(mjds, energies)
                    ]
                else:
                    toalist = [
                        toa.TOA(m,
                                obs='Fermi',
                                scale='tt',
                                energy=e,
                                weight=w,
                                error=1.0 * u.us)
                        for m, e, w in zip(mjds, energies, weights)
                    ]
            except KeyError:
                log.error(
                    'Error processing Fermi TOAs. You may have forgotten to specify an FT2 file with --ft2'
                )
                raise
        else:
            log.info("Building geocentered TOAs")
            if weightcolumn is None:
                toalist = [
                    toa.TOA(m, obs='Geocenter', scale='tt', energy=e)
                    for m, e in zip(mjds, energies)
                ]
            else:
                toalist = [
                    toa.TOA(m, obs='Geocenter', scale='tt', energy=e, weight=w)
                    for m, e, w in zip(mjds, energies, weights)
                ]

    return toalist
예제 #23
0
def make_fake_toas_fromMJDs(
    MJDs,
    model,
    freq=1400 * u.MHz,
    obs="GBT",
    error=1 * u.us,
    add_noise=False,
    dm=None,
    dm_error=1e-4 * pint.dmu,
    name="fake",
    include_bipm=False,
    include_gps=True,
):
    """Make evenly spaced toas

    Can include alternating frequencies if fed an array of frequencies,
    only works with one observatory at a time

    Parameters
    ----------
    MJDs : astropy.units.Quantity
        array of MJDs for fake toas
    model : pint.models.timing_model.TimingModel
        current model
    freq : astropy.units.Quantity, optional
        frequency of the fake toas, default 1400 MHz
    obs : str, optional
        observatory for fake toas, default GBT
    error : astropy.units.Quantity
        uncertainty to attach to each TOA
    add_noise : bool, optional
        Add noise to the TOAs (otherwise `error` just populates the column)
    dm : astropy.units.Quantity, optional
        DM value to include with each TOA; default is
        not to include any DM information
    dm_error : astropy.units.Quantity
        uncertainty to attach to each DM measurement
    name : str, optional
        Name for the TOAs (goes into the flags)
    include_bipm : bool, optional
        Whether or not to disable UTC-> TT BIPM clock
        correction (see :class:`pint.observatory.topo_obs.TopoObs`)
    include_gps : bool, optional
        Whether or not to disable UTC(GPS)->UTC clock correction
        (see :class:`pint.observatory.topo_obs.TopoObs`)

    Returns
    -------
    TOAs : pint.toa.TOAs
        object with toas matched to input array with optional errors

    Notes
    -----
    `add_noise` respects any ``EFAC`` or ``EQUAD`` present in the `model`

    See Also
    --------
    :func:`make_fake_toas`
    """

    times = MJDs

    if freq is None or np.isinf(freq).all():
        freq = np.inf * u.MHz
    freq_array = _get_freq_array(np.atleast_1d(freq), len(times))
    t1 = [
        pint.toa.TOA(t.value, obs=obs, freq=f, scale=get_observatory(obs).timescale)
        for t, f in zip(times, freq_array)
    ]
    ts = pint.toa.TOAs(toalist=t1)
    ts.planets = model["PLANET_SHAPIRO"].value
    ts.ephem = model["EPHEM"].value
    update_fake_toa_clock(ts, model, include_bipm=include_bipm, include_gps=include_gps)
    ts.table["error"] = error
    if dm is not None:
        for f in ts.table["flags"]:
            f["pp_dm"] = str(dm.to_value(pint.dmu))
            f["pp_dme"] = str(dm_error.to_value(pint.dmu))
    ts.compute_TDBs()
    ts.compute_posvels()
    return make_fake_toas(ts, model=model, add_noise=add_noise, name=name)
예제 #24
0
def make_fake_toas_uniform(
    startMJD,
    endMJD,
    ntoas,
    model,
    fuzz=0,
    freq=1400 * u.MHz,
    obs="GBT",
    error=1 * u.us,
    add_noise=False,
    dm=None,
    dm_error=1e-4 * pint.dmu,
    name="fake",
    include_bipm=False,
    include_gps=True,
):
    """Make evenly spaced toas

    Can include alternating frequencies if fed an array of frequencies,
    only works with one observatory at a time

    Parameters
    ----------
    startMJD : float
        starting MJD for fake toas
    endMJD : float
        ending MJD for fake toas
    ntoas : int
        number of fake toas to create between startMJD and endMJD
    model : pint.models.timing_model.TimingModel
        current model
    fuzz : astropy.units.Quantity, optional
        Standard deviation of 'fuzz' distribution to be applied to TOAs
    freq : astropy.units.Quantity, optional
        frequency of the fake toas, default 1400 MHz
    obs : str, optional
        observatory for fake toas, default GBT
    error : astropy.units.Quantity
        uncertainty to attach to each TOA
    add_noise : bool, optional
        Add noise to the TOAs (otherwise `error` just populates the column)
    dm : astropy.units.Quantity, optional
        DM value to include with each TOA; default is
        not to include any DM information
    dm_error : astropy.units.Quantity
        uncertainty to attach to each DM measurement
    name : str, optional
        Name for the TOAs (goes into the flags)
    include_bipm : bool, optional
        Whether or not to disable UTC-> TT BIPM clock
        correction (see :class:`pint.observatory.topo_obs.TopoObs`)
    include_gps : bool, optional
        Whether or not to disable UTC(GPS)->UTC clock correction
        (see :class:`pint.observatory.topo_obs.TopoObs`)
    Returns
    -------
    TOAs : pint.toa.TOAs
        object with evenly spaced toas spanning given start and end MJD with
        ntoas toas, with optional errors

    Notes
    -----
    `add_noise` respects any ``EFAC`` or ``EQUAD`` present in the `model`

    See Also
    --------
    :func:`make_fake_toas`
    """

    times = np.linspace(startMJD, endMJD, ntoas, dtype=np.longdouble) * u.d
    if fuzz > 0:
        # apply some fuzz to the dates
        fuzz = np.random.normal(scale=fuzz.to_value(u.d), size=len(times)) * u.d
        times += fuzz

    if freq is None or np.isinf(freq).all():
        freq = np.inf * u.MHz
    freq_array = _get_freq_array(np.atleast_1d(freq), len(times))
    t1 = [
        pint.toa.TOA(t.value, obs=obs, freq=f, scale=get_observatory(obs).timescale)
        for t, f in zip(times, freq_array)
    ]
    ts = pint.toa.TOAs(toalist=t1)
    ts.planets = model["PLANET_SHAPIRO"].value
    ts.ephem = model["EPHEM"].value
    update_fake_toa_clock(ts, model, include_bipm=include_bipm, include_gps=include_gps)
    ts.table["error"] = error
    if dm is not None:
        for f in ts.table["flags"]:
            f["pp_dm"] = str(dm.to_value(pint.dmu))
            f["pp_dme"] = str(dm_error.to_value(pint.dmu))
    ts.compute_TDBs()
    ts.compute_posvels()
    return make_fake_toas(ts, model=model, add_noise=add_noise, name=name)
예제 #25
0
 def test_get_obs(self):
     for tobs in self.test_obs:
         site = get_observatory(tobs, include_gps=False, include_bipm=True,
                                bipm_version='BIPM2015')
         assert site, \
             "Observatory {} did not initilized correctly".format(tobs)
예제 #26
0
 def test_wrong_name(self):
     _ = get_observatory('Wrong_name')
예제 #27
0
def main(argv=None):
    import argparse
    parser = argparse.ArgumentParser(description="PINT tool for simulating TOAs")
    parser.add_argument("parfile",help="par file to read model from")
    parser.add_argument("timfile",help="Output TOA file name")
    parser.add_argument("--startMJD",help="MJD of first fake TOA (default=56000.0)",
                    type=float, default=56000.0)
    parser.add_argument("--ntoa",help="Number of fake TOAs to generate",type=int,default=100)
    parser.add_argument("--duration",help="Span of TOAs to generate (days)",type=int,default=400)
    parser.add_argument("--obs",help="Observatory code (default: GBT)",default="GBT")
    parser.add_argument("--freq",help="Frequency for TOAs (MHz) (default: 1400)",nargs='+',
                    type=float, default=1400.0)
    parser.add_argument("--error",help="Random error to apply to each TOA (us, default=1.0)",
                    type=float, default=1.0)
    parser.add_argument("--plot",help="Plot residuals",action="store_true",default=False)
    parser.add_argument("--ephem",help="Ephemeris to use",default="DE421")
    parser.add_argument("--planets",help="Use planetary Shapiro delay",action="store_true",
                        default=False)
    parser.add_argument("--format",help="The format of out put .tim file.", default='TEMPO')
    args = parser.parse_args(argv)

    log.info("Reading model from {0}".format(args.parfile))
    m = pint.models.get_model(args.parfile)

    duration = args.duration*u.day
    #start = Time(args.startMJD,scale='utc',format='pulsar_mjd',precision=9)
    start = np.longdouble(args.startMJD) * u.day
    error = args.error*u.microsecond
    freq = np.atleast_1d(args.freq) * u.MHz
    site = get_observatory(args.obs)
    scale = site.timescale
    out_format = args.format

    times = np.linspace(0,duration.to(u.day).value,args.ntoa)*u.day + start
    # Add mulitple frequency
    freq_array = get_freq_array(freq, len(times))

    tl = [toa.TOA(t.value,error=error, obs=args.obs, freq=f,
                 scale=scale) for t, f in zip(times, freq_array)]
    ts = toa.TOAs(toalist=tl)

    # WARNING! I'm not sure how clock corrections should be handled here!
    # Do we apply them, or not?
    if not any(['clkcorr' in f for f in ts.table['flags']]):
        log.info("Applying clock corrections.")
        ts.apply_clock_corrections()
    if 'tdb' not in ts.table.colnames:
        log.info("Getting IERS params and computing TDBs.")
        ts.compute_TDBs()
    if 'ssb_obs_pos' not in ts.table.colnames:
        log.info("Computing observatory positions and velocities.")
        ts.compute_posvels(args.ephem, args.planets)

    # F_local has units of Hz; discard cycles unit in phase to get a unit
    # that TimeDelta understands
    log.info("Creating TOAs")
    F_local = m.d_phase_d_toa(ts)
    rs = m.phase(ts.table).frac.value/F_local

    # Adjust the TOA times to put them where their residuals will be 0.0
    ts.adjust_TOAs(TimeDelta(-1.0*rs))
    rspost = m.phase(ts.table).frac.value/F_local
    log.info("Second iteration")
    # Do a second iteration
    ts.adjust_TOAs(TimeDelta(-1.0*rspost))

     # Write TOAs to a file
    ts.write_TOA_file(args.timfile,name='fake',format=out_format)

    if args.plot:
        # This should be a very boring plot with all residuals flat at 0.0!
        import matplotlib.pyplot as plt
        rspost2 = m.phase(ts.table).frac/F_local
        plt.errorbar(ts.get_mjds().value,rspost2.to(u.us).value,yerr=ts.get_errors().to(u.us).value)
        newts = pint.toa.get_TOAs(args.timfile, ephem = args.ephem, planets=args.planets)
        rsnew = m.phase(newts.table).frac/F_local
        plt.errorbar(newts.get_mjds().value,rsnew.to(u.us).value,yerr=newts.get_errors().to(u.us).value)
        #plt.plot(ts.get_mjds(),rspost.to(u.us),'x')
        plt.xlabel('MJD')
        plt.ylabel('Residual (us)')
        plt.show()
예제 #28
0
def load_Fermi_TOAs(
    ft1name,
    weightcolumn=None,
    targetcoord=None,
    logeref=4.1,
    logesig=0.5,
    minweight=0.0,
    minmjd=-np.inf,
    maxmjd=np.inf,
    fermiobs="Fermi",
):
    """
    toalist = load_Fermi_TOAs(ft1name)
      Read photon event times out of a Fermi FT1 file and return
      a list of PINT TOA objects.
      Correctly handles raw FT1 files, or ones processed with gtbary
      to have barycentered or geocentered TOAs.


    Parameters
    ----------
    weightcolumn : str
        Specifies the FITS column name to read the photon weights from.
        The special value 'CALC' causes the weights to be computed
        empirically as in Philippe Bruel's SearchPulsation code.
    targetcoord : astropy.SkyCoord
        Source coordinate for weight computation if weightcolumn=='CALC'
    logeref : float
        Parameter for the weight computation if weightcolumn=='CALC'
    logesig : float
        Parameter for the weight computation if weightcolumn=='CALC'
    minweight : float
        If weights are loaded or computed, exclude events with smaller weights.
    minmjd : float
        Events with earlier MJDs are excluded.
    maxmjd : float
        Events with later MJDs are excluded.
    fermiobs: str
      The default observatory name is Fermi, and must have already been
      registered.  The user can specify another name

    Returns
    -------
    toalist : list
        A list of TOA objects corresponding to the Fermi events.
    """

    # Load photon times from FT1 file
    hdulist = fits.open(ft1name)
    ft1hdr = hdulist[1].header
    ft1dat = hdulist[1].data

    # TIMESYS will be 'TT' for unmodified Fermi LAT events (or geocentered), and
    #                 'TDB' for events barycentered with gtbary
    # TIMEREF will be 'GEOCENTER' for geocentered events,
    #                 'SOLARSYSTEM' for barycentered,
    #             and 'LOCAL' for unmodified events

    timesys = ft1hdr["TIMESYS"]
    log.info("TIMESYS {0}".format(timesys))
    timeref = ft1hdr["TIMEREF"]
    log.info("TIMEREF {0}".format(timeref))

    # Read time column from FITS file
    mjds = read_fits_event_mjds_tuples(hdulist[1])
    if len(mjds) == 0:
        log.error("No MJDs read from file!")
        raise

    energies = ft1dat.field("ENERGY") * u.MeV
    if weightcolumn is not None:
        if weightcolumn == "CALC":
            photoncoords = SkyCoord(
                ft1dat.field("RA") * u.degree,
                ft1dat.field("DEC") * u.degree,
                frame="icrs",
            )
            weights = calc_lat_weights(
                ft1dat.field("ENERGY"),
                photoncoords.separation(targetcoord),
                logeref=logeref,
                logesig=logesig,
            )
        else:
            weights = ft1dat.field(weightcolumn)
        if minweight > 0.0:
            idx = np.where(weights > minweight)[0]
            mjds = mjds[idx]
            energies = energies[idx]
            weights = weights[idx]

    # limit the TOAs to ones in selected MJD range
    mjds_float = np.asarray([r[0] + r[1] for r in mjds])
    idx = (minmjd < mjds_float) & (mjds_float < maxmjd)
    mjds = mjds[idx]
    energies = energies[idx]
    if weightcolumn is not None:
        weights = weights[idx]

    if timesys == "TDB":
        log.info("Building barycentered TOAs")
        obs = "Barycenter"
        scale = "tdb"
        msg = "barycentric"
    elif (timesys == "TT") and (timeref == "LOCAL"):
        assert timesys == "TT"
        try:
            get_observatory(fermiobs)
        except KeyError:
            log.error(
                "%s observatory not defined. Make sure you have specified an FT2 file!"
                % fermiobs)
            raise
        obs = fermiobs
        scale = "tt"
        msg = "spacecraft local"
    elif (timesys == "TT") and (timeref == "GEOCENTRIC"):
        obs = "Geocenter"
        scale = "tt"
        msg = "geocentric"
    else:
        raise ValueError("Unrecognized TIMEREF/TIMESYS.")

    log.info("Building {0} TOAs, with MJDs in range {1} to {2}".format(
        msg, mjds[0, 0] + mjds[0, 1], mjds[-1, 0] + mjds[-1, 1]))
    if weightcolumn is None:
        toalist = [
            toa.TOA(m,
                    obs=obs,
                    scale=scale,
                    energy=str(e.to_value(u.MeV)),
                    error=1.0 * u.us) for m, e in zip(mjds, energies)
        ]
    else:
        toalist = [
            toa.TOA(
                m,
                obs=obs,
                scale=scale,
                energy=str(e.to_value(u.MeV)),
                weight=str(w),
                error=1.0 * u.us,
            ) for m, e, w in zip(mjds, energies, weights)
        ]

    return toalist
예제 #29
0
 def test_wrong_name(self):
     with pytest.raises(KeyError):
         get_observatory("Wrong_name")
예제 #30
0
파일: zima.py 프로젝트: demorest/PINT
def main(argv=None):
    import argparse
    parser = argparse.ArgumentParser(description="PINT tool for simulating TOAs")
    parser.add_argument("parfile",help="par file to read model from")
    parser.add_argument("timfile",help="Output TOA file name")
    parser.add_argument("--inputtim",help="Input tim file for fake TOA sampling",type=str,default=None)
    parser.add_argument("--startMJD",help="MJD of first fake TOA (default=56000.0)",
                    type=float, default=56000.0)
    parser.add_argument("--ntoa",help="Number of fake TOAs to generate",type=int,default=100)
    parser.add_argument("--duration",help="Span of TOAs to generate (days)",type=float,default=400.0)
    parser.add_argument("--obs",help="Observatory code (default: GBT)",default="GBT")
    parser.add_argument("--freq",help="Frequency for TOAs (MHz) (default: 1400)",nargs='+',
                    type=float, default=1400.0)
    parser.add_argument("--error",help="Random error to apply to each TOA (us, default=1.0)",
                    type=float, default=1.0)
    parser.add_argument("--fuzzdays",help="Standard deviation of 'fuzz' distribution (jd) (default: 0.0)",type=float, default=0.0)
    parser.add_argument("--plot",help="Plot residuals",action="store_true",default=False)
    parser.add_argument("--ephem",help="Ephemeris to use",default="DE421")
    parser.add_argument("--planets",help="Use planetary Shapiro delay",action="store_true",
                        default=False)
    parser.add_argument("--format",help="The format of out put .tim file.", default='TEMPO2')
    args = parser.parse_args(argv)

    log.info("Reading model from {0}".format(args.parfile))
    m = pint.models.get_model(args.parfile)

    out_format = args.format
    error = args.error*u.microsecond

    if args.inputtim is None:
        log.info('Generating uniformly spaced TOAs')
        duration = args.duration*u.day
        #start = Time(args.startMJD,scale='utc',format='pulsar_mjd',precision=9)
        start = np.longdouble(args.startMJD) * u.day
        freq = np.atleast_1d(args.freq) * u.MHz
        site = get_observatory(args.obs)
        scale = site.timescale

        times = np.linspace(0,duration.to(u.day).value,args.ntoa)*u.day + start

        # 'Fuzz' out times
        if args.fuzzdays > 0.0:
            fuzz = np.random.normal(scale=args.fuzzdays,size=len(times))*u.day
            times += fuzz

        # Add mulitple frequency
        freq_array = get_freq_array(freq, len(times))
        tl = [toa.TOA(t.value,error=error, obs=args.obs, freq=f,
                     scale=scale) for t, f in zip(times, freq_array)]
        ts = toa.TOAs(toalist=tl)
    else:
        log.info('Reading initial TOAs from {0}'.format(args.inputtim))
        ts = toa.TOAs(toafile=args.inputtim)
        ts.table['error'][:] = error

    # WARNING! I'm not sure how clock corrections should be handled here!
    # Do we apply them, or not?
    if not any(['clkcorr' in f for f in ts.table['flags']]):
        log.info("Applying clock corrections.")
        ts.apply_clock_corrections()
    if 'tdb' not in ts.table.colnames:
        log.info("Getting IERS params and computing TDBs.")
        ts.compute_TDBs(ephem=args.ephem)
    if 'ssb_obs_pos' not in ts.table.colnames:
        log.info("Computing observatory positions and velocities.")
        ts.compute_posvels(args.ephem, args.planets)

    # F_local has units of Hz; discard cycles unit in phase to get a unit
    # that TimeDelta understands
    log.info("Creating TOAs")
    F_local = m.d_phase_d_toa(ts)
    rs = m.phase(ts).frac.value/F_local

    # Adjust the TOA times to put them where their residuals will be 0.0

    ts.adjust_TOAs(TimeDelta(-1.0*rs))
    rspost = m.phase(ts).frac.value/F_local

    log.info("Second iteration")
    # Do a second iteration
    ts.adjust_TOAs(TimeDelta(-1.0*rspost))

    err = np.random.randn(len(ts.table)) * error
    #Add the actual error fuzzing
    ts.adjust_TOAs(TimeDelta(err))

     # Write TOAs to a file
    ts.write_TOA_file(args.timfile,name='fake',format=out_format)

    if args.plot:
        # This should be a very boring plot with all residuals flat at 0.0!
        import matplotlib.pyplot as plt
        rspost2 = m.phase(ts).frac/F_local
        plt.errorbar(ts.get_mjds().value,rspost2.to(u.us).value,yerr=ts.get_errors().to(u.us).value)
        newts = pint.toa.get_TOAs(args.timfile, ephem = args.ephem, planets=args.planets)
        rsnew = m.phase(newts).frac/F_local
        plt.errorbar(newts.get_mjds().value,rsnew.to(u.us).value,yerr=newts.get_errors().to(u.us).value)
        #plt.plot(ts.get_mjds(),rspost.to(u.us),'x')
        plt.xlabel('MJD')
        plt.ylabel('Residual (us)')
        plt.show()
예제 #31
0
def main(argv=None):
    import argparse

    parser = argparse.ArgumentParser(description="PINT tool for simulating TOAs")
    parser.add_argument("parfile", help="par file to read model from")
    parser.add_argument("timfile", help="Output TOA file name")
    parser.add_argument(
        "--inputtim",
        help="Input tim file for fake TOA sampling",
        type=str,
        default=None,
    )
    parser.add_argument(
        "--startMJD",
        help="MJD of first fake TOA (default=56000.0)",
        type=float,
        default=56000.0,
    )
    parser.add_argument(
        "--ntoa", help="Number of fake TOAs to generate", type=int, default=100
    )
    parser.add_argument(
        "--duration", help="Span of TOAs to generate (days)", type=float, default=400.0
    )
    parser.add_argument("--obs", help="Observatory code (default: GBT)", default="GBT")
    parser.add_argument(
        "--freq",
        help="Frequency for TOAs (MHz) (default: 1400)",
        nargs="+",
        type=float,
        default=1400.0,
    )
    parser.add_argument(
        "--error",
        help="Random error to apply to each TOA (us, default=1.0)",
        type=float,
        default=1.0,
    )
    parser.add_argument(
        "--fuzzdays",
        help="Standard deviation of 'fuzz' distribution (jd) (default: 0.0)",
        type=float,
        default=0.0,
    )
    parser.add_argument(
        "--plot", help="Plot residuals", action="store_true", default=False
    )
    parser.add_argument("--ephem", help="Ephemeris to use", default="DE421")
    parser.add_argument(
        "--planets",
        help="Use planetary Shapiro delay",
        action="store_true",
        default=False,
    )
    parser.add_argument(
        "--format", help="The format of out put .tim file.", default="TEMPO2"
    )
    args = parser.parse_args(argv)

    log.info("Reading model from {0}".format(args.parfile))
    m = pint.models.get_model(args.parfile)

    out_format = args.format
    error = args.error * u.microsecond

    if args.inputtim is None:
        log.info("Generating uniformly spaced TOAs")
        duration = args.duration * u.day
        # start = Time(args.startMJD,scale='utc',format='pulsar_mjd',precision=9)
        start = np.longdouble(args.startMJD) * u.day
        freq = np.atleast_1d(args.freq) * u.MHz
        site = get_observatory(args.obs)
        scale = site.timescale

        times = np.linspace(0, duration.to(u.day).value, args.ntoa) * u.day + start

        # 'Fuzz' out times
        if args.fuzzdays > 0.0:
            fuzz = np.random.normal(scale=args.fuzzdays, size=len(times)) * u.day
            times += fuzz

        # Add mulitple frequency
        freq_array = get_freq_array(freq, len(times))
        tl = [
            toa.TOA(t.value, error=error, obs=args.obs, freq=f, scale=scale)
            for t, f in zip(times, freq_array)
        ]
        ts = toa.TOAs(toalist=tl)
    else:
        log.info("Reading initial TOAs from {0}".format(args.inputtim))
        ts = toa.TOAs(toafile=args.inputtim)
        ts.table["error"][:] = error

    # WARNING! I'm not sure how clock corrections should be handled here!
    # Do we apply them, or not?
    if not any(["clkcorr" in f for f in ts.table["flags"]]):
        log.info("Applying clock corrections.")
        ts.apply_clock_corrections()
    if "tdb" not in ts.table.colnames:
        log.info("Getting IERS params and computing TDBs.")
        ts.compute_TDBs(ephem=args.ephem)
    if "ssb_obs_pos" not in ts.table.colnames:
        log.info("Computing observatory positions and velocities.")
        ts.compute_posvels(args.ephem, args.planets)

    log.info("Creating TOAs")
    F_local = m.d_phase_d_toa(ts)
    rs = m.phase(ts).frac / F_local

    # Adjust the TOA times to put them where their residuals will be 0.0
    ts.adjust_TOAs(TimeDelta(-1.0 * rs))
    rspost = m.phase(ts).frac / F_local

    log.info("Second iteration")
    # Do a second iteration
    ts.adjust_TOAs(TimeDelta(-1.0 * rspost))

    err = np.random.randn(len(ts.table)) * error
    # Add the actual error fuzzing
    ts.adjust_TOAs(TimeDelta(err))

    # Write TOAs to a file
    ts.write_TOA_file(args.timfile, name="fake", format=out_format)

    if args.plot:
        # This should be a very boring plot with all residuals flat at 0.0!
        import matplotlib.pyplot as plt
        from astropy.visualization import quantity_support

        quantity_support()

        rspost2 = m.phase(ts).frac / F_local
        plt.errorbar(
            ts.get_mjds(), rspost2.to(u.us), yerr=ts.get_errors().to(u.us), fmt="."
        )
        newts = pint.toa.get_TOAs(args.timfile, ephem=args.ephem, planets=args.planets)
        rsnew = m.phase(newts).frac / F_local
        plt.errorbar(
            newts.get_mjds(), rsnew.to(u.us), yerr=newts.get_errors().to(u.us), fmt="."
        )
        # plt.plot(ts.get_mjds(),rspost.to(u.us),'x')
        plt.xlabel("MJD")
        plt.ylabel("Residual (us)")
        plt.grid(True)
        plt.show()
예제 #32
0
def load_Fermi_TOAs(ft1name,weightcolumn=None,targetcoord=None,logeref=4.1,
                    logesig=0.5,minweight=0.0, minmjd=0.0, maxmjd=np.inf):
    '''
    TOAlist = load_Fermi_TOAs(ft1name)
      Read photon event times out of a Fermi FT1 file and return
      a list of PINT TOA objects.
      Correctly handles raw FT1 files, or ones processed with gtbary
      to have barycentered or geocentered TOAs.

      weightcolumn specifies the FITS column name to read the photon weights
      from.  The special value 'CALC' causes the weights to be computed empirically
      as in Philippe Bruel's SearchPulsation code.
      logeref and logesig are parameters for the weight computation and are only
      used when weightcolumn='CALC'.

      When weights are loaded, or computed, events are filtered by weight >= minweight
    '''
    import astropy.io.fits as pyfits
    # Load photon times from FT1 file
    hdulist = pyfits.open(ft1name)
    ft1hdr=hdulist[1].header
    ft1dat=hdulist[1].data

    # TIMESYS will be 'TT' for unmodified Fermi LAT events (or geocentered), and
    #                 'TDB' for events barycentered with gtbary
    # TIMEREF will be 'GEOCENTER' for geocentered events,
    #                 'SOLARSYSTEM' for barycentered,
    #             and 'LOCAL' for unmodified events

    timesys = ft1hdr['TIMESYS']
    log.info("TIMESYS {0}".format(timesys))
    timeref = ft1hdr['TIMEREF']
    log.info("TIMEREF {0}".format(timeref))

    # Read time column from FITS file
    mjds = read_fits_event_mjds_tuples(hdulist[1])
    if len(mjds) == 0:
        log.error('No MJDs read from file!')
        raise

    energies = ft1dat.field('ENERGY')*u.MeV
    if weightcolumn is not None:
        if weightcolumn == 'CALC':
            photoncoords = SkyCoord(ft1dat.field('RA')*u.degree,ft1dat.field('DEC')*u.degree,frame='icrs')
            weights = calc_lat_weights(ft1dat.field('ENERGY'),
                photoncoords.separation(targetcoord), logeref=logeref,
                logesig=logesig)
        else:
            weights = ft1dat.field(weightcolumn)
        if minweight > 0.0:
            idx = np.where(weights>minweight)[0]
            mjds = mjds[idx]
            energies = energies[idx]
            weights = weights[idx]

    # limit the TOAs to ones in selected MJD range
    mjds_float = np.array([r[0] + r[1] for r in mjds])
    idx = np.logical_and((mjds_float > minmjd),(mjds_float < maxmjd))
    mjds = mjds[idx]
    energies = energies[idx]
    if weightcolumn is not None:
        weights = weights[idx]

    if timesys == 'TDB':
        log.info("Building barycentered TOAs")
        if weightcolumn is None:
            toalist=[toa.TOA(m,obs='Barycenter',scale='tdb',energy=e) for m,e in zip(mjds,energies)]
        else:
            toalist=[toa.TOA(m,obs='Barycenter',scale='tdb',energy=e,weight=w) for m,e,w in zip(mjds,energies,weights)]
    else:
        if timeref == 'LOCAL':
            log.info('Building spacecraft local TOAs, with MJDs in range {0} to {1}'.format(mjds[0],mjds[-1]))
            assert timesys == 'TT'
            fermiobs = get_observatory('Fermi')

            try:
                if weightcolumn is None:
                    toalist=[toa.TOA(m, obs='Fermi', scale='tt',energy=e)
                            for m,e in zip(mjds,energies)]
                else:
                    toalist=[toa.TOA(m, obs='Fermi', scale='tt',energy=e,weight=w)
                            for m,e,w in zip(mjds,energies,weights)]
            except KeyError:
                log.error('Error processing Fermi TOAs. You may have forgotten to specify an FT2 file with --ft2')
                raise
        else:
            log.info("Building geocentered TOAs")
            if weightcolumn is None:
                toalist=[toa.TOA(m, obs='Geocenter', scale='tt',energy=e) for m,e in zip(mjds,energies)]
            else:
                toalist=[toa.TOA(m, obs='Geocenter', scale='tt',energy=e,weight=w) for m,e,w in zip(mjds,energies,weights)]

    return toalist
예제 #33
0
 def test_wrong_name(self):
     _ = get_observatory('Wrong_name')