Пример #1
0
# the FOV
cvmag_lim = 6.1
catalog_star_table = sdt[sdt.vmag < cvmag_lim]
catalog_stars = stars[sdt.vmag < cvmag_lim]
# Define telescope frame
location = EarthLocation.from_geodetic(lon=14.974609,
                                       lat=37.693267,
                                       height=1750)
obstime = Time("2019-05-09T01:37:54.728026")
altaz_frame = AltAz(location=location, obstime=obstime)

# Get pixel coordinates from TargetCalib
from target_calib import CameraConfiguration

camera_config = CameraConfiguration("1.1.0")
mapping = camera_config.GetMapping()
xpix = np.array(mapping.GetXPixVector())  # * u.m
ypix = np.array(mapping.GetYPixVector())  # * u.m
pos = np.array(
    [np.array(mapping.GetXPixVector()),
     np.array(mapping.GetYPixVector())])
size = mapping.GetSize() * u.m
area = size**2
pix_area = np.full(xpix.size, area) * area.unit
focal_length = u.Quantity(2.15191, u.m)
from ssm.pointing.astrometry import (
    rotang,
    rot_matrix,
    generate_hotspots,
    StarPatternMatch,
    matchpattern,
Пример #2
0
class SimtelReader(WaveformReader):
    def __init__(self, path, max_events=None):
        """
        Reads simtelarray files utilising the SimTelEventSource from ctapipe

        Parameters
        ----------
        path : str
            Path to the simtel file
        max_events : int
            Maximum number of events to read from the file
        """
        super().__init__(path, max_events)

        try:
            from ctapipe.io import SimTelEventSource, EventSeeker
            from ctapipe.coordinates import EngineeringCameraFrame
        except ModuleNotFoundError:
            msg = "Cannot find ctapipe installation"
            raise ModuleNotFoundError(msg)

        try:
            from target_calib import CameraConfiguration
        except ModuleNotFoundError:
            msg = ("Cannot find TARGET libraries, please follow installation "
                   "instructions from https://forge.in2p3.fr/projects/gct/"
                   "wiki/Installing_CHEC_Software")
            raise ModuleNotFoundError(msg)

        self.path = path
        reader = SimTelEventSource(input_url=path,
                                   max_events=max_events,
                                   back_seekable=True)
        self.seeker = EventSeeker(reader)

        first_event = self.seeker[0]
        tels = list(first_event.r0.tels_with_data)
        self.tel = tels[0]
        shape = first_event.r0.tel[self.tel].waveform.shape
        _, self.n_pixels, self.n_samples = shape
        self.n_modules = self.n_pixels // 64
        self.index = 0

        n_modules = 32
        camera_version = "1.1.0"
        self._camera_config = CameraConfiguration(camera_version)
        tc_mapping = self._camera_config.GetMapping(n_modules == 1)
        self.mapping = get_clp_mapping_from_tc_mapping(tc_mapping)
        n_rows = self.mapping.metadata['n_rows']
        n_columns = self.mapping.metadata['n_columns']
        camera_geom = first_event.inst.subarray.tel[tels[0]].camera
        engineering_frame = EngineeringCameraFrame(n_mirrors=2)
        engineering_geom = camera_geom.transform_to(engineering_frame)
        pix_x = engineering_geom.pix_x.value
        pix_y = engineering_geom.pix_y.value
        row, col = get_row_column(pix_x, pix_y)
        camera_2d = np.zeros((n_rows, n_columns), dtype=np.int)
        camera_2d[row, col] = np.arange(self.n_pixels, dtype=np.int)
        self.pixel_order = camera_2d[self.mapping['row'], self.mapping['col']]

        self.reference_pulse_path = self._camera_config.GetReferencePulsePath()
        self.camera_version = self._camera_config.GetVersion()

        self.gps_time = None
        self.mc_true = None
        self.mc = None
        self.pointing = None
        self.mcheader = None

    def _get_event(self, iev):
        event = self.seeker[iev]
        self._fill_event_containers(event)
        waveforms = event.r1.tel[self.tel].waveform[0]
        waveforms = waveforms[self.pixel_order]
        return waveforms

    @staticmethod
    def is_compatible(path):
        # read the first 4 bytes
        with open(path, 'rb') as f:
            marker_bytes = f.read(4)

        # if file is gzip, read the first 4 bytes with gzip again
        if marker_bytes[0] == 0x1f and marker_bytes[1] == 0x8b:
            with gzip.open(path, 'rb') as f:
                marker_bytes = f.read(4)

        # check for the simtel magic marker
        int_marker, = struct.unpack('I', marker_bytes)
        return int_marker == 3558836791 or int_marker == 931798996

    def __iter__(self):
        for event in self.seeker:
            self._fill_event_containers(event)
            waveforms = event.r1.tel[self.tel].waveform[0]
            waveforms = waveforms[self.pixel_order]
            yield waveforms

    def _fill_event_containers(self, event):
        self.index = event.count
        self.run_id = event.r0.obs_id
        self.gps_time = event.trig.gps_time

        self.mc_true = event.mc.tel[self.tel].photo_electron_image
        self.mc_true = self.mc_true[self.pixel_order]

        self.mc = dict(iev=self.index,
                       t_cpu=self.t_cpu,
                       energy=event.mc.energy.value,
                       alt=event.mc.alt.value,
                       az=event.mc.az.value,
                       core_x=event.mc.core_x.value,
                       core_y=event.mc.core_y.value,
                       h_first_int=event.mc.h_first_int.value,
                       x_max=event.mc.x_max.value,
                       shower_primary_id=event.mc.shower_primary_id)

        self.pointing = dict(
            iev=self.index,
            t_cpu=self.t_cpu,
            azimuth_raw=event.mc.tel[self.tel].azimuth_raw,
            altitude_raw=event.mc.tel[self.tel].altitude_raw,
            azimuth_cor=event.mc.tel[self.tel].azimuth_cor,
            altitude_cor=event.mc.tel[self.tel].altitude_cor,
        )

        if self.mcheader is None:
            mch = event.mcheader
            self.mcheader = dict(
                corsika_version=mch.corsika_version,
                simtel_version=mch.simtel_version,
                energy_range_min=mch.energy_range_min.value,
                energy_range_max=mch.energy_range_max.value,
                prod_site_B_total=mch.prod_site_B_total.value,
                prod_site_B_declination=mch.prod_site_B_declination.value,
                prod_site_B_inclination=mch.prod_site_B_inclination.value,
                prod_site_alt=mch.prod_site_alt.value,
                spectral_index=mch.spectral_index,
                shower_prog_start=mch.shower_prog_start,
                shower_prog_id=mch.shower_prog_id,
                detector_prog_start=mch.detector_prog_start,
                detector_prog_id=mch.detector_prog_id,
                num_showers=mch.num_showers,
                shower_reuse=mch.shower_reuse,
                max_alt=mch.max_alt.value,
                min_alt=mch.min_alt.value,
                max_az=mch.max_az.value,
                min_az=mch.min_az.value,
                diffuse=mch.diffuse,
                max_viewcone_radius=mch.max_viewcone_radius.value,
                min_viewcone_radius=mch.min_viewcone_radius.value,
                max_scatter_range=mch.max_scatter_range.value,
                min_scatter_range=mch.min_scatter_range.value,
                core_pos_mode=mch.core_pos_mode,
                injection_height=mch.injection_height.value,
                atmosphere=mch.atmosphere,
                corsika_iact_options=mch.corsika_iact_options,
                corsika_low_E_model=mch.corsika_low_E_model,
                corsika_high_E_model=mch.corsika_high_E_model,
                corsika_bunchsize=mch.corsika_bunchsize,
                corsika_wlen_min=mch.corsika_wlen_min.value,
                corsika_wlen_max=mch.corsika_wlen_max.value,
                corsika_low_E_detail=mch.corsika_low_E_detail,
                corsika_high_E_detail=mch.corsika_high_E_detail,
            )

    @property
    def n_events(self):
        return len(self.seeker)

    @property
    def t_cpu(self):
        return pd.to_datetime(self.gps_time.value, unit='s')
Пример #3
0
 def tc_mapping(self):
     from target_calib import CameraConfiguration
     version = self.mapping.metadata.version
     camera_config = CameraConfiguration(version)
     return camera_config.GetMapping(self.n_modules == 1)
Пример #4
0
class TIOReader:
    """
    Reader for the R0 and R1 tio files
    """
    def __init__(self, path, max_events=None):
        try:
            from target_io import WaveformArrayReader
            from target_calib import CameraConfiguration
        except ModuleNotFoundError:
            msg = ("Cannot find TARGET libraries, please follow installation "
                   "instructions from https://forge.in2p3.fr/projects/gct/"
                   "wiki/Installing_CHEC_Software")
            raise ModuleNotFoundError(msg)

        if not os.path.exists(path):
            raise FileNotFoundError("File does not exist: {}".format(path))
        self.path = path

        self._reader = WaveformArrayReader(self.path, 2, 1)

        self.is_r1 = self._reader.fR1
        self.n_events = self._reader.fNEvents
        self.run_id = self._reader.fRunID
        self.n_pixels = self._reader.fNPixels
        self.n_modules = self._reader.fNModules
        self.n_tmpix = self.n_pixels // self.n_modules
        self.n_samples = self._reader.fNSamples

        self._camera_config = CameraConfiguration(self._reader.fCameraVersion)
        self.tc_mapping = self._camera_config.GetMapping(self.n_modules == 1)

        self._pixel = self._PixelWaveforms(self)

        self.n_cells = self._camera_config.GetNCells()
        self.camera_version = self._camera_config.GetVersion()
        self.reference_pulse_path = self._camera_config.GetReferencePulsePath()

        self.current_tack = None
        self.current_cpu_ns = None
        self.current_cpu_s = None

        self.first_cell_ids = np.zeros(self.n_pixels, dtype=np.uint16)

        if self.is_r1:
            self.samples = np.zeros((self.n_pixels, self.n_samples),
                                    dtype=np.float32)
            self.get_tio_event = self._reader.GetR1Event
        else:
            self.samples = np.zeros((self.n_pixels, self.n_samples),
                                    dtype=np.uint16)
            self.get_tio_event = self._reader.GetR0Event

        if max_events and max_events < self.n_events:
            self.n_events = max_events

    def _get_event(self, iev):
        self.index = iev
        self.get_tio_event(iev, self.samples, self.first_cell_ids)
        self.current_tack = self._reader.fCurrentTimeTack
        self.current_cpu_ns = self._reader.fCurrentTimeNs
        self.current_cpu_s = self._reader.fCurrentTimeSec
        return self.samples

    def __iter__(self):
        for iev in range(self.n_events):
            yield self._get_event(iev)

    def __getitem__(self, iev):
        return np.copy(self._get_event(iev))

    class _PixelWaveforms:
        def __init__(self, tio_reader):
            self.reader = tio_reader

        def __getitem__(self, p):
            if not isinstance(p, list) and not isinstance(p, np.ndarray):
                p = [p]

            n_events = self.reader.n_events
            n_pixels = len(p)
            n_samples = self.reader.n_samples
            waveforms = np.zeros((n_events, n_pixels, n_samples))

            for iev, wf in enumerate(self.reader):
                waveforms[iev] = wf[p]

            return waveforms

    @property
    def pixel(self):
        return self._pixel

    @property
    def mapping(self):
        return get_clp_mapping_from_tc_mapping(self.tc_mapping)

    def get_sn(self, tm):
        if tm >= self.n_modules:
            raise IndexError("Requested TM out of range: {}".format(tm))
        return self._reader.GetSN(tm)

    @staticmethod
    def is_compatible(filepath):
        try:
            h = fits.getheader(filepath, 0)
            if 'EVENT_HEADER_VERSION' not in h:
                return False
        except IOError:
            return False
        return True
Пример #5
0
class TIOReader(WaveformReader):
    def __init__(self, path, max_events=None,
                 skip_events=2, skip_end_events=1):
        """
        Utilies TargetIO to read R0 and R1 tio files. Enables easy access to
        the waveforms for anaylsis.

        Waveforms can be read from the file by either indexing this reader or
        iterating over it:

        >>> path = "/path/to/file_r0.tio"
        >>> reader = TIOReader(path)
        >>> wf = reader[3]  # Obtain the waveforms for the third event

        >>> path = "/path/to/file_r0.tio"
        >>> reader = TIOReader(path)
        >>> wfs = reader[:10]  # Obtain the waveforms for the first 10 events

        >>> path = "/path/to/file_r0.tio"
        >>> reader = TIOReader(path)
        >>> for wf in reader:  # Iterate over all events in the file
        >>>    print(wf)

        Parameters
        ----------
        path : str
            Path to the _r0.tio or _r1.tio file
        max_events : int
            Maximum number of events to read from the file
        """
        super().__init__(path, max_events)

        try:
            from target_io import WaveformArrayReader
            from target_calib import CameraConfiguration
        except ModuleNotFoundError:
            msg = ("Cannot find TARGET libraries, please follow installation "
                   "instructions from https://forge.in2p3.fr/projects/gct/"
                   "wiki/Installing_CHEC_Software")
            raise ModuleNotFoundError(msg)

        self._reader = WaveformArrayReader(
            self.path, skip_events, skip_end_events
        )

        self.is_r1 = self._reader.fR1
        self._n_events = self._reader.fNEvents
        self.run_id = self._reader.fRunID
        self.n_pixels = self._reader.fNPixels
        self.n_superpixels_per_module = self._reader.fNSuperpixelsPerModule
        self.n_modules = self._reader.fNModules
        self.n_tmpix = self.n_pixels // self.n_modules
        self.n_samples = self._reader.fNSamples

        self._camera_config = CameraConfiguration(self._reader.fCameraVersion)
        self.tc_mapping = self._camera_config.GetMapping(self.n_modules == 1)

        self.n_cells = self._camera_config.GetNCells()
        self.camera_version = self._camera_config.GetVersion()
        self.reference_pulse_path = self._camera_config.GetReferencePulsePath()

        if self.is_r1:
            self.dtype = np.float32
            self.get_tio_event = self._reader.GetR1Event
        else:
            self.dtype = np.uint16
            self.get_tio_event = self._reader.GetR0Event

        if max_events and max_events < self._n_events:
            self._n_events = max_events

    def _get_event(self, iev):
        samples = np.zeros((self.n_pixels, self.n_samples), self.dtype)
        (first_cell_id,  stale,  missing_packets,
         t_tack, t_cpu_s, t_cpu_ns) = self.get_tio_event(iev, samples)
        waveform = Waveform(
            input_array=samples,
            iev=iev,
            is_r1=self.is_r1,
            first_cell_id=first_cell_id,
            missing_packets=missing_packets,
            stale=stale,
            t_tack=t_tack,
            t_cpu_container=(t_cpu_s, t_cpu_ns),
        )
        return waveform

    @staticmethod
    def is_compatible(path):
        with open(path, 'rb') as f:
            marker_bytes = f.read(1024)

        # if file is gzip, read the first 4 bytes with gzip again
        if marker_bytes[0] == 0x1f and marker_bytes[1] == 0x8b:
            with gzip.open(path, 'rb') as f:
                marker_bytes = f.read(1024)

        if b'FITS' not in marker_bytes:
            return False

        try:
            h = fits.getheader(path, 0)
            if 'EVENT_HEADER_VERSION' not in h:
                return False
        except IOError:
            return False
        return True

    @property
    def n_events(self):
        return self._n_events

    @property
    def mapping(self):
        return get_clp_mapping_from_tc_mapping(self.tc_mapping)

    def get_sn(self, tm):
        """
        Get the SN of the TARGET module in a slot

        Parameters
        ----------
        tm : int
            Slot number for the TARGET module

        Returns
        -------
        int
            Serial number of the TM
        """
        if tm >= self.n_modules:
            raise IndexError("Requested TM out of range: {}".format(tm))
        return self._reader.GetSN(tm)

    def get_sipm_temp(self, tm):
        if tm >= self.n_modules:
            raise IndexError("Requested TM out of range: {}".format(tm))
        return self._reader.GetSiPMTemp(tm)

    def get_primary_temp(self, tm):
        if tm >= self.n_modules:
            raise IndexError("Requested TM out of range: {}".format(tm))
        return self._reader.GetPrimaryTemp(tm)

    def get_sp_dac(self, tm, sp):
        if tm >= self.n_modules:
            raise IndexError("Requested TM out of range: {}".format(tm))
        if sp >= self.n_superpixels_per_module:
            raise IndexError("Requested SP out of range: {}".format(sp))
        return self._reader.GetSPDAC(tm, sp)

    def get_sp_hvon(self, tm, sp):
        if tm >= self.n_modules:
            raise IndexError("Requested TM out of range: {}".format(tm))
        if sp >= self.n_superpixels_per_module:
            raise IndexError("Requested SP out of range: {}".format(sp))
        return self._reader.GetSPHVON(tm, sp)
def main():
    file_dir = os.path.dirname(os.path.abspath(__file__))
    output_dir = os.path.join(file_dir, "outputs")
    output_path = os.path.join(output_dir, "checs_pixel_mapping.dat")

    config = CameraConfiguration("1.1.0")
    mapping = config.GetMapping()
    mappingsp = mapping.GetMappingSP()

    qe_arr = np.load(os.path.join(file_dir, 'outputs/checs_qe_variation.npy'))
    gain_arr = np.load(os.path.join(file_dir, 'outputs/checs_gain_variation.npy'))

    with open(output_path, 'w') as f:
        pixtype = """# PixType format:
# Par.  1: pixel type (here always 1)
#       2: PMT type (must be 0)
#       3: cathode shape type
#       4: visible cathode diameter [cm]
#       5: funnel shape type (see above)
#       6: funnel diameter (flat-to-flat for hex.) [cm]
#       7: depth of funnel [cm]
#       8: a) funnel efficiency "filename",   b) funnel plate transparency
#       9: a) optional wavelength "filename", b) funnel wall reflectivity
# In case a) in column 8, columns 3+7 are not used. If in case a) the
# optional file name for the wavelength dependence is provided, the
# overall scale in the file provided as parameter 8 is ignored because
# it is rescaled such that the average over all mirrors is equal to
# the given wavelength dependent value.
#
# Shape types: 0: circ., 1: hex(flat x), 2: sq., 3: hex(flat y)
#
#Angular Dep currently all set to one for checks

# Note that pixel size is scale from the actual 6.125 mm at the focal length
# planned with GATE telescopes (228.3 cm) to the focal length of ASTRI (215 cm).
# Similarly scaled are the pixel positions.
PixType    1 0 2 0.623   2 0.623 0.0   "transmission_pmma_vs_theta_20150422.dat" "transmission_pmma_vs_lambda_meas0deg_coat_82raws.dat"

# Pixel format:
# Par.  1: pixel number (starting at 0)
#       2: pixel type (must be 1)
#       3: x position [cm]
#       4: y position [cm]
#       5: drawer/module number
#       6: board number in module
#       7: channel number n board
#       8: board Id number ('0x....')
#       9: pixel on (is on if parameter is missing)
#       10: relative QE or PDE (1 if unused)
#       11: relative gain (1 if unused)

"""

        f.write(pixtype)

        for i in range(mapping.GetNPixels()):
            ipix = mapping.GetPixel(i)
            xpix = mapping.GetXPix(i) * 10**2
            ypix = mapping.GetYPix(i) * 10**2
            imod = mapping.GetSlot(i)
            ichan = mapping.GetTMPixel(i)
            qe = 1#qe_arr[i]
            gain = 1#gain_arr[i]
            l = "Pixel\t{}\t1\t{:.5f}\t{:.5f}\t{}\t0\t{}\t0x00\t1\t{:.5f}\t{:.5f}\n"
            lf = l.format(ipix, xpix, ypix, imod, ichan, qe, gain)
            f.write(lf)

        f.write('\n')

        for i in range(mappingsp.GetNSuperPixels()):
            nei = mappingsp.GetNeighbours(i, True)
            f.write("MajorityTrigger * of ")
            for isp in [i, *nei]:
                con = list(mappingsp.GetContainedPixels(isp))
                rows = [mapping.GetRow(j) for j in con]
                cols = [mapping.GetColumn(j) for j in con]
                min_r = np.min(rows)
                min_c = np.min(cols)
                con_bl = con[np.where((rows == min_r) & (cols == min_c))[0][0]]
                con.remove(con_bl)
                f.write('{}[{},{},{}] '.format(con_bl, *con))
            f.write('\n')