Beispiel #1
0
def join_eventlists(event_file1, event_file2, new_event_file=None):
    """Join two event files.

    Parameters
    ----------
    event_file1 : str
        First event file
    event_file2 : str
        Second event file

    Other parameters
    ----------------
    new_event_file : str, default None
        Output event file. If not specified uses `hendrics.utils.common_name`
        to figure out a good name to use mixing up the two input names.

    Returns
    -------
    new_event_file : str
        Output event file
    """
    if new_event_file is None:
        new_event_file = \
            common_name(event_file1, event_file2) + '_ev' + HEN_FILE_EXTENSION

    events1 = load_events(event_file1)
    events2 = load_events(event_file2)
    events = events1.join(events2)
    events.header = events1.header
    save_events(events, new_event_file)
    return new_event_file
Beispiel #2
0
    def setup_class(cls):
        cls.pulse_frequency = 1 / 0.101
        cls.tstart = 0
        cls.tend = 25.25
        cls.tseg = cls.tend - cls.tstart
        cls.dt = 0.00606
        cls.times = np.arange(cls.tstart, cls.tend, cls.dt) + cls.dt / 2
        cls.counts = \
            100 + 20 * np.cos(2 * np.pi * cls.times * cls.pulse_frequency)
        lc = Lightcurve(cls.times, cls.counts, gti=[[cls.tstart, cls.tend]])
        events = EventList()
        events.simulate_times(lc)
        events.mjdref = 57000.
        cls.event_times = events.time
        cls.dum = 'events' + HEN_FILE_EXTENSION
        save_events(events, cls.dum)

        curdir = os.path.abspath(os.path.dirname(__file__))
        cls.datadir = os.path.join(curdir, 'data')
        fits_file = os.path.join(cls.datadir, 'monol_testA.evt')
        command = 'HENreadevents {0}'.format(fits_file)
        sp.check_call(command.split())

        cls.real_event_file = os.path.join(
            cls.datadir, 'monol_testA_nustar_fpma_ev' + HEN_FILE_EXTENSION)
Beispiel #3
0
 def test_load_and_save_events(self):
     events = EventList([0, 2, 3.], pi=[1, 2, 3], mjdref=54385.3254923845,
                        gti = np.longdouble([[-0.5, 3.5]]))
     events.energy = np.array([3., 4., 5.])
     save_events(events, self.dum)
     events2 = load_events(self.dum)
     assert np.allclose(events.time, events2.time)
     assert np.allclose(events.pi, events2.pi)
     assert np.allclose(events.mjdref, events2.mjdref)
     assert np.allclose(events.gti, events2.gti)
     assert np.allclose(events.energy, events2.energy)
Beispiel #4
0
 def setup_class(cls):
     cls.pulse_frequency = 1 / 0.101
     cls.tstart = 0
     cls.tend = 25.25
     cls.tseg = cls.tend - cls.tstart
     cls.dt = 0.00606
     cls.times = np.arange(cls.tstart, cls.tend, cls.dt) + cls.dt / 2
     cls.counts = \
         100 + 20 * np.cos(2 * np.pi * cls.times * cls.pulse_frequency)
     lc = Lightcurve(cls.times, cls.counts, gti=[[cls.tstart, cls.tend]])
     events = EventList()
     events.simulate_times(lc)
     cls.event_times = events.time
     cls.dum = 'events' + HEN_FILE_EXTENSION
     save_events(events, cls.dum)
Beispiel #5
0
def save_to_intermediate_file(stingray_object, fname):
    """Save Stingray object to intermediate file."""
    from stingray.lightcurve import Lightcurve
    from stingray.events import EventList
    from stingray.crossspectrum import Crossspectrum
    from hendrics.io import save_lcurve, save_events, save_pds
    if isinstance(stingray_object, Lightcurve):
        save_lcurve(stingray_object, fname)
    elif isinstance(stingray_object, EventList):
        save_events(stingray_object, fname)
    # This also work for Powerspectrum and AveragedCrosspowerspectrum, clearly
    elif isinstance(stingray_object, Crossspectrum):
        save_pds(stingray_object, fname)
    else:
        logging.error("save_to_intermediate_file: Unknown object type: %s" % type(stingray_object).__name__)
        return False

    return True
Beispiel #6
0
def save_to_intermediate_file(stingray_object, fname):
    """Save Stingray object to intermediate file."""
    from stingray.lightcurve import Lightcurve
    from stingray.events import EventList
    from stingray.crossspectrum import Crossspectrum
    from hendrics.io import save_lcurve, save_events, save_pds
    if isinstance(stingray_object, Lightcurve):
        save_lcurve(stingray_object, fname)
    elif isinstance(stingray_object, EventList):
        save_events(stingray_object, fname)
    # This also work for Powerspectrum and AveragedCrosspowerspectrum, clearly
    elif isinstance(stingray_object, Crossspectrum):
        save_pds(stingray_object, fname)
    else:
        logging.error("save_to_intermediate_file: Unknown object type: %s" %
                      type(stingray_object).__name__)
        return False

    return True
Beispiel #7
0
def treat_event_file(filename, noclobber=False, gti_split=False,
                     min_length=4, gtistring=None, length_split=None):
    """Read data from an event file, with no external GTI information.

    Parameters
    ----------
    filename : str

    Other Parameters
    ----------------
    noclobber: bool
        if a file is present, do not overwrite it
    gtistring: str
        comma-separated set of GTI strings to consider
    gti_split: bool
        split the file in multiple chunks, containing one GTI each
    length_split: float, default None
        split the file in multiple chunks, with approximately this length
    min_length: float
        minimum length of GTIs accepted (only if gti_split is True or
        length_split is not None)
    """
    gtistring = assign_value_if_none(gtistring, 'GTI,STDGTI')
    logging.info('Opening %s' % filename)

    instr = read_header_key(filename, 'INSTRUME')
    mission = read_header_key(filename, 'TELESCOP')

    data = load_events_and_gtis(filename,
                                gtistring=gtistring)

    events = data.ev_list
    gtis = events.gti
    detector_id = data.detector_id

    if detector_id is not None:
        detectors = np.array(list(set(detector_id)))
    else:
        detectors = [None]
    outfile_root = \
        hen_root(filename) + '_' + mission.lower() + '_' + instr.lower()

    for d in detectors:
        if d is not None:
            good_det = d == data.detector_id
            outroot_local = \
                '{0}_det{1:02d}'.format(outfile_root, d)

        else:
            good_det = np.ones_like(events.time, dtype=bool)
            outroot_local = outfile_root

        outfile = outroot_local + '_ev' + HEN_FILE_EXTENSION
        if noclobber and os.path.exists(outfile) and (not (gti_split or length_split)):
            warnings.warn(
                '{0} exists and using noclobber. Skipping'.format(outfile))
            return

        if gti_split or (length_split is not None):
            lengths = np.array([g1 - g0 for (g0, g1) in gtis])
            gtis = gtis[lengths >= min_length]

            if length_split:
                gti0 = np.arange(gtis[0, 0], gtis[-1, 1], length_split)
                gti1 = gti0 + length_split
                gti_chunks = np.array([[g0, g1] for (g0, g1) in zip(gti0, gti1)])
                label='chunk'
            else:
                gti_chunks = gtis
                label='gti'

            for ig, g in enumerate(gti_chunks):
                outfile_local = \
                    '{0}_{1}{2:03d}_ev'.format(outroot_local, label,
                                           ig) + HEN_FILE_EXTENSION

                good_gtis = cross_two_gtis([g], gtis)
                if noclobber and os.path.exists(outfile_local):
                    warnings.warn('{0} exists, '.format(outfile_local) +
                                  'and noclobber option used. Skipping')
                    return
                good = np.logical_and(events.time >= g[0],
                                      events.time < g[1])
                all_good = good_det & good
                if len(events.time[all_good]) < 1:
                    continue
                events_filt = EventList(events.time[all_good],
                                        pi=events.pi[all_good],
                                        gti=good_gtis,
                                        mjdref=events.mjdref)
                events_filt.instr = events.instr
                events_filt.header = events.header
                save_events(events_filt, outfile_local)
            pass
        else:
            events_filt = EventList(events.time[good_det],
                                    pi=events.pi[good_det],
                                    gti=events.gti, mjdref=events.mjdref)
            events_filt.instr = events.instr
            events_filt.header = events.header

            save_events(events_filt, outfile)