Esempio n. 1
0
    def run(self, detector):
        """
        Reads in all observers in the CoREAS files and returns a new simulated
        event for each.

        Parameters
        ----------
        detector: Detector object
            Detector description of the detector that shall be simulated

        """
        for input_file in self.__input_files:
            self.__current_event = 0
            with h5py.File(input_file, "r") as corsika:
                if "highlevel" not in corsika.keys() or list(corsika["highlevel"].values()) == []:
                    logger.warning(" No highlevel quantities in simulated hdf5 files, weights will be taken from station position")
                    positions = []
                    for observer in corsika['CoREAS']['observers'].values():
                        position = observer.attrs['position']
                        positions.append(np.array([-position[1], position[0], 0]) * units.cm)
                    positions = np.array(positions)
                    zenith, azimuth, magnetic_field_vector = coreas.get_angles(corsika)
                    weights = coreas.calculate_simulation_weights(positions, zenith, azimuth, debug=self.__debug)

                    if self.__debug:
                        import matplotlib.pyplot as plt
                        fig, ax = plt.subplots()
                        im = ax.scatter(positions[:, 0], positions[:, 1], c=weights)
                        fig.colorbar(im, ax=ax).set_label(label=r'Area $[m^2]$')
                        plt.xlabel('East [m]')
                        plt.ylabel('West [m]')
                        plt.title('Final weighting')
                        plt.gca().set_aspect('equal')
                        plt.show()

                else:
                    positions = list(corsika["highlevel"].values())[0]["antenna_position"]
                    zenith, azimuth, magnetic_field_vector = coreas.get_angles(corsika)
                    weights = coreas.calculate_simulation_weights(positions, zenith, azimuth)

                for i, (name, observer) in enumerate(corsika['CoREAS']['observers'].items()):
                    evt = NuRadioReco.framework.event.Event(self.__current_input_file, self.__current_event)  # create empty event
                    station = NuRadioReco.framework.station.Station(self.__station_id)
                    sim_station = coreas.make_sim_station(
                        self.__station_id,
                        corsika,
                        observer,
                        detector.get_channel_ids(self.__station_id),
                        weights[i]
                    )
                    station.set_sim_station(sim_station)
                    sim_shower = coreas.make_sim_shower(corsika, observer, detector, self.__station_id)
                    evt.add_sim_shower(sim_shower)
                    evt.set_station(station)
                    self.__current_event += 1
                    yield evt
            self.__current_input_file += 1
    def run(self, detector, output_mode=0):
        """
        Read in a random sample of stations from a CoREAS file.
        For each position the closest observer is selected and a simulated
        event is created for that observer.

        Parameters
        ----------
        detector: Detector object
            Detector description of the detector that shall be simulated
        output_mode: integer (default 0)
            0: only the event object is returned
            1: the function reuturns the event object, the current inputfilename, the distance between the choosen station and the requested core position,
               and the area in which the core positions are randomly distributed


        """
        while (self.__current_input_file < len(self.__input_files)):
            t = time.time()
            t_per_event = time.time()
            filesize = os.path.getsize(
                self.__input_files[self.__current_input_file])
            if (
                    filesize < 18456 * 2
            ):  # based on the observation that a file with such a small filesize is corrupt
                self.logger.warning(
                    "file {} seems to be corrupt, skipping to next file".
                    format(self.__input_files[self.__current_input_file]))
                self.__current_input_file += 1
                continue
            corsika = h5py.File(self.__input_files[self.__current_input_file],
                                "r")
            self.logger.info(
                "using coreas simulation {} with E={:2g} theta = {:.0f}".
                format(self.__input_files[self.__current_input_file],
                       corsika['inputs'].attrs["ERANGE"][0] * units.GeV,
                       corsika['inputs'].attrs["THETAP"][0]))
            positions = []
            for i, observer in enumerate(
                    corsika['CoREAS']['observers'].values()):
                position = observer.attrs['position']
                positions.append(
                    np.array([-position[1], position[0], 0]) * units.cm)
#                 self.logger.debug("({:.0f}, {:.0f})".format(positions[i][0], positions[i][1]))
            positions = np.array(positions)

            zenith, azimuth, magnetic_field_vector = coreas.get_angles(corsika)
            cs = cstrafo.cstrafo(zenith, azimuth, magnetic_field_vector)
            positions_vBvvB = cs.transform_from_magnetic_to_geographic(
                positions.T)
            positions_vBvvB = cs.transform_to_vxB_vxvxB(positions_vBvvB).T
            #             for i, pos in enumerate(positions_vBvvB):
            #                 self.logger.debug("star shape")
            #                 self.logger.debug("({:.0f}, {:.0f}); ({:.0f}, {:.0f})".format(positions[i, 0], positions[i, 1], pos[0], pos[1]))

            dd = (positions_vBvvB[:, 0]**2 + positions_vBvvB[:, 1]**2)**0.5
            ddmax = dd.max()
            self.logger.info("star shape from: {} - {}".format(
                -dd.max(), dd.max()))

            # generate core positions randomly within a rectangle
            cores = np.array([
                self.__random_generator.uniform(self.__area[0], self.__area[1],
                                                self.__n_cores),
                self.__random_generator.uniform(self.__area[2], self.__area[3],
                                                self.__n_cores),
                np.zeros(self.__n_cores)
            ]).T

            self.__t_per_event += time.time() - t_per_event
            self.__t += time.time() - t

            station_ids = detector.get_station_ids()
            for iCore, core in enumerate(cores):
                t = time.time()
                evt = NuRadioReco.framework.event.Event(
                    self.__current_input_file, iCore)  # create empty event
                sim_shower = coreas.make_sim_shower(corsika)
                evt.add_sim_shower(sim_shower)
                rd_shower = NuRadioReco.framework.radio_shower.RadioShower(
                    station_ids=station_ids)
                evt.add_shower(rd_shower)

                for station_id in station_ids:
                    # convert into vxvxB frame to calculate closests simulated station to detecor station
                    det_station_position = detector.get_absolute_position(
                        station_id)
                    det_station_position[2] = 0
                    core_rel_to_station = core - det_station_position
                    #             core_rel_to_station_vBvvB = cs.transform_from_magnetic_to_geographic(core_rel_to_station)
                    core_rel_to_station_vBvvB = cs.transform_to_vxB_vxvxB(
                        core_rel_to_station)
                    dcore = (core_rel_to_station_vBvvB[0]**2 +
                             core_rel_to_station_vBvvB[1]**2)**0.5
                    #                     print(f"{core_rel_to_station}, {core_rel_to_station_vBvvB} -> {dcore}")
                    if (dcore > ddmax):
                        # station is outside of the star shape pattern, create empty station
                        station = NuRadioReco.framework.station.Station(
                            station_id)
                        channel_ids = detector.get_channel_ids(station_id)
                        sim_station = coreas.make_sim_station(
                            station_id, corsika, None, channel_ids)
                        station.set_sim_station(sim_station)
                        evt.set_station(station)
                        self.logger.debug(
                            f"station {station_id} is outside of star shape, channel_ids {channel_ids}"
                        )
                    else:
                        distances = np.linalg.norm(
                            core_rel_to_station_vBvvB[:2] -
                            positions_vBvvB[:, :2],
                            axis=1)
                        index = np.argmin(distances)
                        distance = distances[index]
                        key = list(
                            corsika['CoREAS']['observers'].keys())[index]
                        self.logger.debug(
                            "generating core at ground ({:.0f}, {:.0f}), rel to station ({:.0f}, {:.0f}) vBvvB({:.0f}, {:.0f}), nearest simulated station is {:.0f}m away at ground ({:.0f}, {:.0f}), vBvvB({:.0f}, {:.0f})"
                            .format(cores[iCore][0], cores[iCore][1],
                                    core_rel_to_station[0],
                                    core_rel_to_station[1],
                                    core_rel_to_station_vBvvB[0],
                                    core_rel_to_station_vBvvB[1],
                                    distance / units.m, positions[index][0],
                                    positions[index][1],
                                    positions_vBvvB[index][0],
                                    positions_vBvvB[index][1]))
                        t_event_structure = time.time()
                        observer = corsika['CoREAS']['observers'].get(key)

                        station = NuRadioReco.framework.station.Station(
                            station_id)
                        channel_ids = detector.get_channel_ids(station_id)
                        sim_station = coreas.make_sim_station(
                            station_id, corsika, observer, channel_ids)
                        station.set_sim_station(sim_station)
                        evt.set_station(station)
                if (output_mode == 0):
                    self.__t += time.time() - t
                    yield evt
                elif (output_mode == 1):
                    self.__t += time.time() - t
                    self.__t_event_structure += time.time() - t_event_structure
                    yield evt, self.__current_input_file
                else:
                    self.logger.debug("output mode > 1 not implemented")
                    raise NotImplementedError

            self.__current_input_file += 1
Esempio n. 3
0
    def run(self):
        """
        Reads in a CoREAS file and returns an event containing all simulated stations

        """
        while (self.__current_input_file < len(self.__input_files)):
            t = time.time()
            t_per_event = time.time()

            filesize = os.path.getsize(
                self.__input_files[self.__current_input_file])
            if (
                    filesize < 18456 * 2
            ):  # based on the observation that a file with such a small filesize is corrupt
                logger.warning(
                    "file {} seems to be corrupt, skipping to next file".
                    format(self.__input_files[self.__current_input_file]))
                self.__current_input_file += 1
                continue

            logger.info('Reading %s ...' %
                        self.__input_files[self.__current_input_file])

            corsika = h5py.File(self.__input_files[self.__current_input_file],
                                "r")
            logger.info(
                "using coreas simulation {} with E={:2g} theta = {:.0f}".
                format(self.__input_files[self.__current_input_file],
                       corsika['inputs'].attrs["ERANGE"][0] * units.GeV,
                       corsika['inputs'].attrs["THETAP"][0]))

            f_coreas = corsika["CoREAS"]

            if self.__ascending_run_and_event_number:
                evt = NuRadioReco.framework.event.Event(
                    self.__ascending_run_and_event_number,
                    self.__ascending_run_and_event_number)
                self.__ascending_run_and_event_number += 1
            else:
                evt = NuRadioReco.framework.event.Event(
                    corsika['inputs'].attrs['RUNNR'],
                    corsika['inputs'].attrs['EVTNR'])

            evt.__event_time = f_coreas.attrs["GPSSecs"]

            # create sim shower, no core is set since no external detector description is given
            sim_shower = coreas.make_sim_shower(corsika)
            sim_shower.set_parameter(
                shp.core,
                np.array([
                    0, 0, f_coreas.attrs["CoreCoordinateVertical"] / 100
                ]))  # set core
            evt.add_sim_shower(sim_shower)

            # initialize coordinate transformation
            cs = coordinatesystems.cstrafo(
                sim_shower.get_parameter(shp.zenith),
                sim_shower.get_parameter(shp.azimuth),
                magnetic_field_vector=sim_shower.get_parameter(
                    shp.magnetic_field_vector))

            # add simulated pulses as sim station
            for idx, (name,
                      observer) in enumerate(f_coreas['observers'].items()):
                station_id = antenna_id(
                    name, idx)  # returns proper station id if possible

                station = NuRadioReco.framework.station.Station(station_id)
                if self.__det is None:
                    sim_station = coreas.make_sim_station(
                        station_id, corsika, observer, channel_ids=[0, 1, 2])
                else:
                    sim_station = coreas.make_sim_station(
                        station_id,
                        corsika,
                        observer,
                        channel_ids=self.__det.get_channel_ids(
                            self.__det.get_default_station_id()))
                station.set_sim_station(sim_station)
                evt.set_station(station)
                if self.__det is not None:
                    position = observer.attrs['position']
                    antenna_position = np.zeros(3)
                    antenna_position[0], antenna_position[1], antenna_position[
                        2] = -position[1] * units.cm, position[
                            0] * units.cm, position[2] * units.cm
                    antenna_position = cs.transform_from_magnetic_to_geographic(
                        antenna_position)
                    if not self.__det.has_station(station_id):
                        self.__det.add_generic_station({
                            'station_id':
                            station_id,
                            'pos_easting':
                            antenna_position[0],
                            'pos_northing':
                            antenna_position[1],
                            'pos_altitude':
                            antenna_position[2]
                        })
                    else:
                        self.__det.add_station_properties_for_event(
                            {
                                'pos_easting': antenna_position[0],
                                'pos_northing': antenna_position[1],
                                'pos_altitude': antenna_position[2]
                            }, station_id, evt.get_run_number(), evt.get_id())

            self.__t_per_event += time.time() - t_per_event
            self.__t += time.time() - t

            self.__current_input_file += 1
            if self.__det is None:
                yield evt
            else:
                self.__det.set_event(evt.get_run_number(), evt.get_id())
                yield evt, self.__det
Esempio n. 4
0
    def run(self, detector, output_mode=0):
        """
        Read in a random sample of stations from a CoREAS file.
        A number of random positions is selected within a certain radius.
        For each position the closest observer is selected and a simulated
        event is created for that observer.

        Parameters
        ----------
        detector: Detector object
            Detector description of the detector that shall be simulated
        output_mode: integer (default 0)
            0: only the event object is returned
            1: the function reuturns the event object, the current inputfilename, the distance between the choosen station and the requested core position,
               and the area in which the core positions are randomly distributed


        """
        while (self.__current_input_file < len(self.__input_files)):
            t = time.time()
            t_per_event = time.time()
            filesize = os.path.getsize(self.__input_files[self.__current_input_file])
            if(filesize < 18456 * 2):  # based on the observation that a file with such a small filesize is corrupt
                self.logger.warning("file {} seems to be corrupt, skipping to next file".format(self.__input_files[self.__current_input_file]))
                self.__current_input_file += 1
                continue
            corsika = h5py.File(self.__input_files[self.__current_input_file], "r")
            self.logger.info(
                "using coreas simulation {} with E={:2g} theta = {:.0f}".format(
                    self.__input_files[self.__current_input_file],
                    corsika['inputs'].attrs["ERANGE"][0] * units.GeV,
                    corsika['inputs'].attrs["THETAP"][0]
                )
            )
            positions = []
            for i, observer in enumerate(corsika['CoREAS']['observers'].values()):
                position = observer.attrs['position']
                positions.append(np.array([-position[1], position[0], 0]) * units.cm)
                self.logger.debug("({:.0f}, {:.0f})".format(position[0], position[1]))
            positions = np.array(positions)

            max_distance = self.__max_distace
            if(max_distance is None):
                max_distance = np.max(np.abs(positions[:, 0:2]))
            area = np.pi * max_distance ** 2

            if(output_mode == 0):
                n_cores = self.__n_cores * 100  # for output mode 1 we want always n_cores in star pattern. Therefore we generate more core positions to be able to select n_cores in the star pattern afterwards
            elif(output_mode == 1):
                n_cores = self.__n_cores
            else:
                raise ValueError('output mode {} not defined.'.format(output_mode))
            theta = self.__random_generator.rand(n_cores) * 2 * np.pi
            r = (self.__random_generator.rand(n_cores)) ** 0.5 * max_distance
            cores = np.array([r * np.cos(theta), r * np.sin(theta), np.zeros(n_cores)]).T

            zenith, azimuth, magnetic_field_vector = coreas.get_angles(corsika)
            cs = cstrafo.cstrafo(zenith, azimuth, magnetic_field_vector)
            positions_vBvvB = cs.transform_from_magnetic_to_geographic(positions.T)
            positions_vBvvB = cs.transform_to_vxB_vxvxB(positions_vBvvB).T
            dd = (positions_vBvvB[:, 0] ** 2 + positions_vBvvB[:, 1] ** 2) ** 0.5
            ddmax = dd.max()
            self.logger.info("star shape from: {} - {}".format(-dd.max(), dd.max()))

            cores_vBvvB = cs.transform_from_magnetic_to_geographic(cores.T)
            cores_vBvvB = cs.transform_to_vxB_vxvxB(cores_vBvvB).T
            dcores = (cores_vBvvB[:, 0] ** 2 + cores_vBvvB[:, 1] ** 2) ** 0.5
            mask_cores_in_starpattern = dcores <= ddmax

            if((not np.sum(mask_cores_in_starpattern)) and (output_mode == 1)):  # handle special case of no core position being generated within star pattern
                observer = corsika['CoREAS']['observers'].values()[0]

                evt = NuRadioReco.framework.event.Event(corsika['inputs'].attrs['RUNNR'], corsika['inputs'].attrs['EVTNR'])  # create empty event
                station = NuRadioReco.framework.station.Station(self.__station_id)
                sim_station = coreas.make_sim_station(self.__station_id, corsika, observer, detector.get_channel_ids(self.__station_id))

                station.set_sim_station(sim_station)
                evt.set_station(station)
                yield evt, self.__current_input_file, None, area

            cores_to_iterate = cores_vBvvB[mask_cores_in_starpattern]
            if(output_mode == 0):  # select first n_cores that are in star pattern
                if(np.sum(mask_cores_in_starpattern) < self.__n_cores):
                    self.logger.warning("only {0} cores contained in star pattern, returning {0} cores instead of {1} cores that were requested".format(np.sum(mask_cores_in_starpattern), self.__n_cores))
                else:
                    cores_to_iterate = cores_vBvvB[mask_cores_in_starpattern][:self.__n_cores]

            self.__t_per_event += time.time() - t_per_event
            self.__t += time.time() - t

            for iCore, core in enumerate(cores_to_iterate):
                t = time.time()
                # check if out of bounds

                distances = np.linalg.norm(core[:2] - positions_vBvvB[:, :2], axis=1)
                index = np.argmin(distances)
                distance = distances[index]
                key = list(corsika['CoREAS']['observers'].keys())[index]
                self.logger.info(
                    "generating core at ground ({:.0f}, {:.0f}), vBvvB({:.0f}, {:.0f}), nearest simulated station is {:.0f}m away at ground ({:.0f}, {:.0f}), vBvvB({:.0f}, {:.0f})".format(
                        cores[iCore][0],
                        cores[iCore][1],
                        core[0],
                        core[1],
                        distance / units.m,
                        positions[index][0],
                        positions[index][1],
                        positions_vBvvB[index][0],
                        positions_vBvvB[index][1]
                    )
                )
                t_event_structure = time.time()
                observer = corsika['CoREAS']['observers'].get(key)

                evt = NuRadioReco.framework.event.Event(self.__current_input_file, iCore)  # create empty event
                station = NuRadioReco.framework.station.Station(self.__station_id)
                channel_ids = detector.get_channel_ids(self.__station_id)
                sim_station = coreas.make_sim_station(self.__station_id, corsika, observer, channel_ids)
                station.set_sim_station(sim_station)
                evt.set_station(station)
                sim_shower = coreas.make_sim_shower(corsika, observer, detector, self.__station_id)
                evt.add_sim_shower(sim_shower)
                rd_shower = NuRadioReco.framework.radio_shower.RadioShower(station_ids=[station.get_id()])
                evt.add_shower(rd_shower)
                if(output_mode == 0):
                    self.__t += time.time() - t
                    self.__t_event_structure += time.time() - t_event_structure
                    yield evt
                elif(output_mode == 1):
                    self.__t += time.time() - t
                    self.__t_event_structure += time.time() - t_event_structure
                    yield evt, self.__current_input_file, distance, area
                else:
                    self.logger.debug("output mode > 1 not implemented")
                    raise NotImplementedError

            self.__current_input_file += 1
            station = NuRadioReco.framework.station.Station(101)
            trace = np.zeros((3, N))
            trace[1] = pulse
            trace[2] = pulse
            sim_station = NuRadioReco.framework.sim_station.SimStation(101)
            electric_field = NuRadioReco.framework.electric_field.ElectricField(
                [2])
            electric_field.set_trace(sampling_rate=1. / dt, trace=trace)
            electric_field[efp.azimuth] = 0
            electric_field[efp.zenith] = (90 + 45) * units.deg
            electric_field[efp.ray_path_type] = 'direct'
            sim_station.add_electric_field(electric_field)
            sim_station[stnp.zenith] = (90 + 45) * units.deg
            sim_station.set_is_neutrino()
            sim_station[stnp.zenith] = 0
            station.set_sim_station(sim_station)
            event.set_station(station)

            efieldToVoltageConverter.run(event, station, det)
            channelBandPassFilter.run(event,
                                      station,
                                      det,
                                      passband=[min_freq, max_freq])

            trace_bicone = station.get_channel(2).get_trace()

            SNRp2p_bicone[counter] = (trace_bicone.max() -
                                      trace_bicone.min()) / 2. / Vrms

            after_tunnel_diode = np.abs(
                triggerSimulator.tunnel_diode(station.get_channel(2)))