Exemplo n.º 1
0
def get_phase_from_ephemeris_file(mjdstart,
                                  mjdstop,
                                  parfile,
                                  ntimes=1000,
                                  ephem="DE405"):
    """Get a correction for orbital motion from pulsar parameter file.

    Parameters
    ----------
    mjdstart, mjdstop : float
        Start and end of the time interval where we want the orbital solution
    parfile : str
        Any parameter file understood by PINT (Tempo or Tempo2 format)

    Other parameters
    ----------------
    ntimes : int
        Number of time intervals to use for interpolation. Default 1000

    Returns
    -------
    correction_mjd : function
        Function that accepts times in MJDs and returns the deorbited times.
    """

    mjds = np.linspace(mjdstart, mjdstop, ntimes)

    toalist = prepare_TOAs(mjds, ephem)
    m = get_model(parfile)
    phase_int, phase_frac = np.array(m.phase(toalist, abs_phase=True))
    phases = phase_int + phase_frac

    correction_mjd_rough = interp1d(mjds, phases, fill_value="extrapolate")
    return correction_mjd_rough
Exemplo n.º 2
0
 def run(self):
     infofile = GetInfo(self.fname, self.config_file, self.worker_timeout).output().path
     info = load_yaml_file(infofile)
     events = get_events_from_fits(self.fname)
     mjdstart, mjdstop = info["mjdstart"], info["mjdstop"]
     parfile = GetParfile(self.fname, self.config_file, self.worker_timeout).output().path
     correction_fun = get_phase_from_ephemeris_file(
         mjdstart, mjdstop, parfile, ephem=info["ephem"]
     )
     mjds = events.time / 86400 + events.mjdref
     phase = correction_fun(mjds)
     phase -= np.floor(phase)
     table = calculate_profile(phase)
     table.meta.update(info)
     model = get_model(parfile)
     for attr in ["F0", "F1", "F2"]:
         table.meta[attr] = getattr(model, attr).value
     table.meta["epoch"] = model.PEPOCH.value
     # table.meta['mjd'] = (local_events[0] + local_events[-1]) / 2
     table.write(self.output().path)
def get_exposure_per_bin(event_times,
                         event_priors,
                         parfile,
                         nbin=16,
                         cache_file=None,
                         mjdref=0):
    """
    Examples
    --------
    # >>> event_times = np.arange(1, 10, 0.01)
    # >>> event_priors = np.zeros_like(event_times)
    # >>> event_priors[0] = 1
    # >>> prof = get_exposure_per_bin(event_times, event_priors, 1/10, nbin=10)
    # >>> prof[0]
    # 0
    # >>> np.allclose(prof[1:], 1)
    # True
    """
    log.info("Calculating exposure")

    if cache_file is not None and os.path.exists(cache_file):
        log.info("Using cached exposure information")
        return pickle.load(open(cache_file, 'rb'))

    start_live_time = event_times - event_priors

    m = get_model(parfile)

    sampling_time = 1 / m.F0.value / nbin / 7.1234514351515132414
    chunk_size = np.min((sampling_time * 50000000, 1000))
    mjdstart = (event_times[0] - 10) / 86400 + mjdref
    mjdstop = (event_times[-1] + 10) / 86400 + mjdref

    phase_correction_fun = get_phase_from_ephemeris_file(
        mjdstart, mjdstop, parfile)

    prof_all = 0
    prof_dead = 0
    for start_time in tqdm.tqdm(
            np.arange(start_live_time[0], event_times[-1], chunk_size)):
        sample_times = np.arange(start_time, start_time + chunk_size,
                                 sampling_time)

        idxs_live = np.searchsorted(start_live_time,
                                    sample_times,
                                    side='right')
        idxs_dead = np.searchsorted(event_times, sample_times, side='right')

        dead = idxs_live == idxs_dead

        sample_times = sample_times / 86400 + mjdref
        # phases_all = _fast_phase_fddot(sample_times, freq, fdot, fddot)
        # phases_dead = _fast_phase_fddot(sample_times[dead], freq, fdot, fddot)
        phases_all = phase_correction_fun(sample_times)
        # phases_dead = phase_correction_fun(sample_times[dead])
        phases_all = phases_all - np.floor(phases_all)
        phases_dead = phases_all[dead]

        prof_all += histogram(phases_all.astype(np.double),
                              bins=nbin,
                              ranges=[0, 1])
        prof_dead += histogram(phases_dead.astype(np.double),
                               bins=nbin,
                               ranges=[0, 1])

    expo = (prof_all - prof_dead) / prof_all

    if cache_file is not None:
        pickle.dump(expo, open(cache_file, 'wb'))

    return expo