Example #1
0
def load_events(fname):
    """Load events from a file."""
    if get_file_format(fname) == 'pickle':
        out = _load_data_pickle(fname)
    elif get_file_format(fname) == 'nc':
        out = _load_data_nc(fname)

    eventlist = EventList()

    eventlist.time = out['time']
    eventlist.gti = out['gti']
    eventlist.pi = out['pi']
    eventlist.mjdref = out['mjdref']
    if 'instr' in list(out.keys()):
        eventlist.instr = out["instr"]
    if 'energy' in list(out.keys()):
        eventlist.energy = out["energy"]
    if 'header' in list(out.keys()):
        eventlist.header = out["header"]
    return eventlist
Example #2
0
def test_get_eventlist_dataset_from_stingray_Eventlist(capsys):
    from stingray.events import EventList
    from astropy.io.fits import Header
    ev = EventList(time=[0, 1],
                   pi=[2, 2],
                   energy=[3., 4.],
                   gti=np.array([[-0.5, 1.5]]))

    ds = get_eventlist_dataset_from_stingray_Eventlist(ev)
    out, err = capsys.readouterr()

    print("Out:", out)
    print("Err:", err)
    assert "Event list has no header" in err

    header = Header()
    header["Bu"] = "Bu"
    ev.header = header.tostring()

    ds = get_eventlist_dataset_from_stingray_Eventlist(ev)

    assert np.allclose(ds.tables["EVENTS"].columns["TIME"].values, ev.time)
    assert np.allclose(ds.tables["EVENTS"].columns["ENERGY"].values, ev.energy)
    assert np.allclose(ds.tables["EVENTS"].columns["PI"].values, ev.pi)
Example #3
0
def test_get_eventlist_dataset_from_stingray_Eventlist(capsys):
    from stingray.events import EventList
    from astropy.io.fits import Header
    ev = EventList(time=[0, 1], pi=[2, 2], energy=[3., 4.],
                   gti=np.array([[-0.5, 1.5]]))

    ds = get_eventlist_dataset_from_stingray_Eventlist(ev)
    out, err = capsys.readouterr()

    print("Out:", out)
    print("Err:", err)
    if err:
        assert "Event list has no header" in err

    header = Header()
    header["Bu"] = "Bu"
    ev.header = header.tostring()

    ds = get_eventlist_dataset_from_stingray_Eventlist(ev)

    assert np.allclose(ds.tables["EVENTS"].columns["TIME"].values, ev.time)
    if "ENERGY" in ds.tables["EVENTS"].columns:
        assert np.allclose(ds.tables["EVENTS"].columns["ENERGY"].values, ev.energy)
    assert np.allclose(ds.tables["EVENTS"].columns["PI"].values, ev.pi)
Example #4
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)
Example #5
0
def treat_event_file(filename,
                     noclobber=False,
                     gti_split=False,
                     min_length=4,
                     gtistring=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
    min_length: float
        minimum length of GTIs accepted (only if gti_split is True)
    """
    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 = 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 = 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):
            warnings.warn(
                '{0} exists, and noclobber option used. Skipping'.format(
                    outfile))
            return

        if gti_split:
            for ig, g in enumerate(gtis):
                length = g[1] - g[0]
                if length < min_length:
                    print("GTI shorter than {} s; skipping".format(min_length))
                    continue

                outfile_local = \
                    '{0}_gti{1}_ev'.format(outroot_local,
                                           ig) + HEN_FILE_EXTENSION

                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
                events_filt = EventList(events.time[all_good],
                                        pi=events.pi[all_good],
                                        gti=np.array([g], dtype=np.longdouble),
                                        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)