Beispiel #1
0
def test_get_obstime_real():
    # times in seconds, rates in s^-1
    t_obs = 600
    dead_time_per_event = 7e-6
    cosmics_rate = 1e4
    # interleaved event rates:
    pedestal_rate = 100
    flatfield_rate = 100
    # starting times for interleaved events (arbitrary):
    t0_pedestal = 0
    t0_flatfield = 0.002
    n_cosmics = np.random.poisson(cosmics_rate * t_obs)

    timestamps = np.random.uniform(0, t_obs, n_cosmics)
    timestamps = np.append(timestamps,
                           np.arange(t0_pedestal, t_obs, 1 / pedestal_rate))
    timestamps = np.append(timestamps,
                           np.arange(t0_flatfield, t_obs, 1 / flatfield_rate))
    # sort events by timestamp:
    timestamps.sort()

    # time to previous event:
    delta_t = np.insert(np.diff(timestamps), 0, 0)

    # now remove events which are closer than dead_time_per_event
    recorded_events = delta_t > dead_time_per_event

    # true effective time:
    true_t_eff = t_obs - dead_time_per_event * recorded_events.sum()
    true_t_eff *= u.s

    # we'll write only 80% of the remaining events - this simulates triggered
    # events which are no longer present in the DL2 event list
    cut = np.random.uniform(0.0, 1.0, recorded_events.sum()) > 0.2

    events = pd.DataFrame({
        "delta_t": delta_t[recorded_events][cut],
        "dragon_time": timestamps[recorded_events][cut],
    })
    t_eff, t_elapsed = utils.get_effective_time(events)
    print(t_obs, t_elapsed, true_t_eff, t_eff)
    # test accuracy to 0.05%:
    assert np.isclose(t_eff, true_t_eff, rtol=5e-4)

    # now test with a QTable:
    a = delta_t[recorded_events][cut] * u.s
    b = timestamps[recorded_events][cut] * u.s
    events = QTable([a, b], names=("delta_t", "dragon_time"))
    t_eff, t_elapsed = utils.get_effective_time(events)
    print(t_obs, t_elapsed, true_t_eff, t_eff)
    # test accuracy to 0.05%:
    assert np.isclose(t_eff, true_t_eff, rtol=5e-4)
    def start(self):

        if not self.source_dep:
            self.data = read_data_dl2_to_QTable(self.input_dl2)
        else:
            self.data = read_data_dl2_to_QTable(self.input_dl2, 'on')
        self.effective_time, self.elapsed_time = get_effective_time(self.data)
        self.run_number = run_info_from_filename(self.input_dl2)[1]

        if not self.source_dep:
            self.apply_srcindep_gh_cut()
        else:
            self.apply_srcdep_gh_alpha_cut()

        self.data = add_icrs_position_params(self.data, self.source_pos)

        self.log.info("Generating event list")
        self.events, self.gti, self.pointing = create_event_list(
            data=self.data,
            run_number=self.run_number,
            source_name=self.source_name,
            source_pos=self.source_pos,
            effective_time=self.effective_time.value,
            elapsed_time=self.elapsed_time.value,
        )

        self.hdulist = fits.HDUList(
            [fits.PrimaryHDU(), self.events, self.gti, self.pointing])

        irf = fits.open(self.input_irf)
        self.log.info("Adding IRF HDUs")

        for irf_hdu in irf[1:]:
            self.hdulist.append(irf_hdu)
Beispiel #3
0
def dl3_file(tmp_dl3_path, observed_dl2_file, simulated_irf_file):
    from lstchain.high_level.hdu_table import create_event_list, add_icrs_position_params
    from lstchain.io.io import read_data_dl2_to_QTable
    from lstchain.reco.utils import get_effective_time
    from astropy.coordinates import SkyCoord
    from astropy.io import fits

    events = read_data_dl2_to_QTable(observed_dl2_file)
    t_eff, t_tot = get_effective_time(events)
    events = events[events["intensity"] > 200]
    source_pos = SkyCoord(ra=83.633, dec=22.01, unit="deg")
    events = add_icrs_position_params(events, source_pos)

    evts, gti, pnt = create_event_list(
        events,
        run_number=2008,
        source_name="Crab",
        source_pos=source_pos,
        effective_time=t_eff.value,
        elapsed_time=t_tot.value,
    )

    name = observed_dl2_file.name
    observed_dl3_file = tmp_dl3_path / name.replace('dl2', 'dl3')
    observed_dl3_file = observed_dl3_file.with_suffix(".fits")

    # create a temp dl3 file to test indexing function

    temp_hdulist = fits.HDUList([fits.PrimaryHDU(), evts, gti, pnt])
    for f in fits.open(simulated_irf_file)[1:]:
        temp_hdulist.append(f)

    temp_hdulist.writeto(observed_dl3_file, overwrite=True)

    return observed_dl3_file
Beispiel #4
0
def read_lst_dl2_runs(paths):
    observations = {}
    obstime = 0 * u.s
    for p in paths:
        data = read_data_dl2_to_QTable(path)
        data["time"] = Time(data["dragon_time"], format="mjd", scale="tai")
        log.info(f"Loading of {p} finished")
        log.info(f"{len(data)} events")
        observations[data[0]["obs_id"]] = data
        t_eff, t_elapsed = get_effective_time(data)
        log.info(f"Effective Observation time: {t_eff}")
        obstime += t_eff
    log.info(f"Combined observation time: {obstime.to(u.min):.2f}")
    return observations
Beispiel #5
0
def process_real(dl2_file):
    events = pd.read_hdf(dl2_file, key=dl2_params_lstcam_key)
    obstime_real = get_effective_time(events)[0]

    filter_good_events = ((events.leakage_intensity_width_2 < 0.2)
                          & (events.intensity > 100))
    events = events[filter_good_events]

    e_reco = events.reco_energy.to_numpy() * u.TeV
    gammaness = events.gammaness
    # If the particle is a gamma ray, it returns the squared angular distance
    # from the reconstructed gamma-ray position and the simulated incoming position
    alt2 = events.alt_tel
    az2 = events.az_tel

    alt1 = events.reco_alt
    az1 = events.reco_az

    angdist2 = (angular_separation(az1, alt1, az2, alt2).to_numpy() * u.rad)**2
    events['theta2'] = angdist2.to_value(u.deg**2)

    return gammaness, angdist2.to(u.deg**2), e_reco, events, obstime_real
Beispiel #6
0
def test_create_event_list(observed_dl2_file, simulated_irf_file):
    from lstchain.irf.hdu_table import create_event_list
    from lstchain.io.io import read_data_dl2_to_QTable
    from lstchain.reco.utils import get_effective_time
    from astropy.coordinates import SkyCoord
    from astropy.io import fits

    events = read_data_dl2_to_QTable(observed_dl2_file)
    t_eff, t_tot = get_effective_time(events)
    events = events[events["intensity"] > 200]

    evts, gti, pnt = create_event_list(
        events,
        run_number=2008,
        source_name="Crab",
        source_pos=SkyCoord(ra=83.633, dec=22.01, unit="deg"),
        effective_time=t_eff.value,
        elapsed_time=t_tot.value,
    )

    assert "TIME" in Table.read(evts).columns
    assert "START" in Table.read(gti).columns
    assert "RA_PNT" in Table.read(pnt).columns

    observed_dl3_file = observed_dl2_file.name.replace("dl2", "dl3")
    observed_dl3_file = (observed_dl2_file.parent /
                         observed_dl3_file.replace(".h5", ".fits"))
    # create a temp dl3 file to test indexing function

    temp_hdulist = fits.HDUList([fits.PrimaryHDU(), evts, gti, pnt])
    for f in fits.open(simulated_irf_file)[1:]:
        temp_hdulist.append(f)

    temp_hdulist.writeto(observed_dl3_file, overwrite=True)

    assert observed_dl3_file.is_file()