Exemple #1
0
def main():

    # get command-line arguments:
    parser = argparse.ArgumentParser()
    parser.add_argument('filename',
                        metavar='SIMTEL_FILE',
                        help='Input simtelarray file')
    parser.add_argument('-o',
                        '--output',
                        metavar='FILENAME',
                        help=('output filename (e.g. times.fits), which '
                              'can be any format supported by astropy.table'),
                        default='times.fits.gz')
    args = parser.parse_args()

    # setup output table
    events = Table(names=['EVENT_ID', 'T_REL', 'TRIGGERED_TELS'],
                   dtype=[np.int64, np.float64, np.uint8])

    events['TRIGGERED_TELS'].shape = (0, MAX_TELS)
    events['T_REL'].unit = u.s
    events['T_REL'].description = 'Time relative to first event'
    events.meta['INPUT'] = args.filename

    trigpattern = np.zeros(MAX_TELS)
    starttime = None

    try:
        pyhessio.file_open(args.filename)

        for run_id, event_id in pyhessio.move_to_next_event():

            ts, tns = pyhessio.get_central_event_gps_time()
            gpstime = Time(ts * u.s, tns * u.ns, format='gps', scale='utc')
            if starttime is None:
                starttime = gpstime

            reltime = (gpstime - starttime).sec

            # build the trigger pattern as a fixed-length array
            # (better for storage in FITS format)
            trigtels = pyhessio.get_telescope_with_data_list()
            trigpattern[:] = 0  # zero the trigger pattern
            trigpattern[trigtels] = 1  # set the triggered telescopes to 1

            events.add_row((event_id, reltime, trigpattern))

        events.write(args.output)
        print("Table written to '{}'".format(args.output))
        print(events)

    except Exception as err:
        print("ERROR: {}, stopping".format(err))

    finally:
        pyhessio.close_file()
Exemple #2
0
def main():

    # get command-line arguments:
    parser=argparse.ArgumentParser()
    parser.add_argument('filename', metavar='SIMTEL_FILE',
                        help='Input simtelarray file')
    parser.add_argument('-o','--output', metavar='FILENAME',
                        help=('output filename (e.g. times.fits), which '
                              'can be any format supported by astropy.table'),
                        default='times.fits.gz')
    args = parser.parse_args()

    # setup output table
    events = Table(names=['EVENT_ID', 'T_REL', 'TRIGGERED_TELS'],
                dtype=[np.int64, np.float64, np.uint8])

    events['TRIGGERED_TELS'].shape = (0, MAX_TELS)
    events['T_REL'].unit = u.s
    events['T_REL'].description = 'Time relative to first event'
    events.meta['INPUT'] = args.filename
    
    trigpattern = np.zeros(MAX_TELS)
    starttime = None
        
    try:
        pyhessio.file_open(args.filename)

        for run_id, event_id in pyhessio.move_to_next_event():

            ts, tns = pyhessio.get_central_event_gps_time()
            gpstime = Time(ts*u.s, tns*u.ns, format='gps', scale='utc')
            if starttime is None:
                starttime = gpstime

            reltime = (gpstime - starttime).sec
                                
            # build the trigger pattern as a fixed-length array
            # (better for storage in FITS format)
            trigtels = pyhessio.get_telescope_with_data_list()
            trigpattern[:] = 0        # zero the trigger pattern
            trigpattern[trigtels] = 1  # set the triggered telescopes to 1

            events.add_row((event_id, reltime, trigpattern))

        events.write(args.output)
        print("Table written to '{}'".format(args.output))
        print(events)

    except Exception as err:
        print("ERROR: {}, stopping".format(err))
        
    finally:
        pyhessio.close_file()
def from_hessio_to_astropytable(filename):

    h.file_open(filename)
    next(h.move_to_next_event())
    tel_id = h.get_telescope_ids()

    tel_table = Table()

    tel_table["TelID"] = tel_id

    tel_posX = [h.get_telescope_position(i)[0] for i in tel_id] * u.m
    tel_posY = [h.get_telescope_position(i)[1] for i in tel_id] * u.m
    tel_posZ = [h.get_telescope_position(i)[2] for i in tel_id] * u.m
    tel_table["TelX"] = tel_posX
    tel_table["TelX"].unit = u.m
    tel_table["TelY"] = tel_posY
    tel_table["TelY"].unit = u.m
    tel_table["TelZ"] = tel_posZ
    tel_table["TelZ"].unit = u.m

    for t in range(len(tel_id)):
        table = Table()
        pix_posX = h.get_pixel_position(tel_id[t])[0] * u.m
        pix_posY = h.get_pixel_position(tel_id[t])[1] * u.m
        pix_id = np.arange(len(pix_posX))
        pix_area = h.get_pixel_area(tel_id[t]) * u.m ** 2

        table["TelID"] = [tel_id[t] for i in range(len(pix_posX))]
        table["PixelID"] = pix_id
        table["PixX"] = pix_posX
        table["PixX"].unit = u.m
        table["PixY"] = pix_posY
        table["PixY"].unit = u.m
        table["PixArea"] = pix_area.value
        table["PixArea"].unit = pix_area.unit

        if t == 0:
            cam_table = table
        else:
            cam_table = join(cam_table, table, join_type="outer")

    return [tel_table, cam_table]
def from_hessio_to_astropytable(filename):

    h.file_open(filename)
    next(h.move_to_next_event())
    tel_id = h.get_telescope_ids()

    tel_table = Table()

    tel_table['TelID'] = tel_id

    tel_posX = [h.get_telescope_position(i)[0] for i in tel_id] * u.m
    tel_posY = [h.get_telescope_position(i)[1] for i in tel_id] * u.m
    tel_posZ = [h.get_telescope_position(i)[2] for i in tel_id] * u.m
    tel_table['TelX'] = tel_posX
    tel_table['TelX'].unit = u.m
    tel_table['TelY'] = tel_posY
    tel_table['TelY'].unit = u.m
    tel_table['TelZ'] = tel_posZ
    tel_table['TelZ'].unit = u.m

    for t in range(len(tel_id)):
        table = Table()
        pix_posX = h.get_pixel_position(tel_id[t])[0] * u.m
        pix_posY = h.get_pixel_position(tel_id[t])[1] * u.m
        pix_id = np.arange(len(pix_posX))
        pix_area = h.get_pixel_area(tel_id[t]) * u.m**2

        table['TelID'] = [tel_id[t] for i in range(len(pix_posX))]
        table['PixelID'] = pix_id
        table['PixX'] = pix_posX
        table['PixX'].unit = u.m
        table['PixY'] = pix_posY
        table['PixY'].unit = u.m
        table['PixArea'] = pix_area.value
        table['PixArea'].unit = pix_area.unit

        if t == 0:
            cam_table = table
        else:
            cam_table = join(cam_table, table, join_type='outer')

    return [tel_table, cam_table]
Exemple #5
0
def load_hessio(filename):
    """
    Function to open and load a hessio file
    
    Parameters
    ----------
    filename: string
        name of the file
    """
    print("Hessio file will be opened.")
    h.file_open(filename)
    next(h.move_to_next_event())
    #version = h.get...
    version = 'Feb2016'

    #Creating 3 dictionaries where the instrument configuration will be stored
    #The dictionaries themselves contain astropy.table.Table objects
    telescope = {}
    camera = {}
    optics = {}

    #--------------------------------------------------------------------------
    #Telescope configuration
    tel_table_prime = Table()
    tel_table_prime.meta = {'VERSION': version}

    try:
        tel_id = h.get_telescope_ids()
        tel_table_prime['TelID'] = tel_id
    except:
        pass
    try:
        tel_posX = [h.get_telescope_position(i)[0] for i in tel_id]
        tel_table_prime['TelX'] = tel_posX
        tel_table_prime['TelX'].unit = u.m
        tel_table_prime.meta['TelX_description'] =\
        'x-position of the telescope measured by...'
    except:
        pass
    try:
        tel_posY = [h.get_telescope_position(i)[1] for i in tel_id]
        tel_table_prime['TelY'] = tel_posY
        tel_table_prime['TelY'].unit = u.m
    except:
        pass
    try:
        tel_posZ = [h.get_telescope_position(i)[2] for i in tel_id]
        tel_table_prime['TelZ'] = tel_posZ
        tel_table_prime['TelZ'].unit = u.m
    except:
        pass
    try:
        tel_table['CameraClass'] = [h.get_camera_class(i) for i in tel_id]
    except:
        pass
    try:
        tel_table_prime['MirA'] = [h.get_mirror_area(i) for i in tel_id]
        tel_table_prime['MirA'].unit = u.m**2
    except:
        pass
    try:
        tel_table_prime['MirN'] = [h.get_mirror_number(i) for i in tel_id]
    except:
        pass
    try:
        tel_table_prime['FL'] = [h.get_optical_foclen(i) for i in tel_id]
        tel_table_prime['FL'].unit = u.m
    except:
        pass
    try:
        tel_table_prime.meta['TelNum'] = len(tel_posX)
    except:
        pass

    #Beside other tables containimng telescope configuration data, the main
    #telescope table is written into the telescope dictionary.
    telescope['TelescopeTable_Version%s' % version] = tel_table_prime

    #--------------------------------------------------------------------------
    #Camera and Optics configuration
    try:
        for t in range(len(tel_id)):

            cam_table_prime = Table()
            cam_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version}
            opt_table_prime = Table()
            opt_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version}

            try:
                pix_posX = h.get_pixel_position(tel_id[t])[0]
                pix_id = np.arange(len(pix_posX))
                cam_table_prime['PixID'] = pix_id
                cam_table_prime['PixX'] = pix_posX
                cam_table_prime['PixX'].unit = u.m
                cam_table_prime.meta['PixXDescription'] =\
                'x-position of the pixel measured by...'
            except:
                pass
            try:
                pix_posY = h.get_pixel_position(tel_id[t])[1]
                cam_table_prime['PixY'] = pix_posY
                cam_table_prime['PixY'].unit = u.m
            except:
                pass
            try:
                camera_class = CD.guess_camera_geometry(
                    pix_posX * u.m, pix_posY * u.m)
                pix_area_prime = camera_class.pix_area
                pix_type_prime = camera_class.pix_type
                pix_neighbors_prime = camera_class.pix_neighbors
            except:
                pass

            try:
                pix_area = h.get_pixel_area(tel_id[t])
                cam_table_prime['PixA'] = pix_area
                cam_table_prime['PixA'].unit = u.mm**2
            except:
                try:
                    cam_table_prime['PixA'] = pix_area_prime
                    cam_table_prime['PixA'].unit = u.mm**2
                except:
                    pass
            try:
                pix_type = h.get_pixel_type(tel_id[t])
            except:
                try:
                    pix_type = pix_type_prime
                except:
                    pix_type = 'unknown'
            cam_table_prime.meta['PixType'] = pix_type
            try:
                pix_neighbors = h.get_pixel_neighbor(tel_id[t])
                cam_table_prime['PixNeig'] = pix_neighbors
            except:
                try:
                    cam_table_prime['PixNeig'] = pix_neighbors_prime
                except:
                    pass

            #as long as no mirror IDs are given, use the following:
            opt_table_prime['MirrID'] = [1, 2]
            try:
                opt_table_prime.meta['MirNum'] = h.get_mirror_number(tel_id[t])
            except:
                pass
            try:
                opt_table_prime['MirArea'] = h.get_mirror_area(tel_id[t])
                opt_table_prime['MirArea'].unit = u.m**2
                opt_table_prime.meta['MirAreaDescription'] =\
                'Area of all mirrors'
            except:
                pass
            try:
                opt_table_prime['OptFocLen'] = h.get_optical_foclen(tel_id[t])
                opt_table_prime['OptFocLen'].unit = u.m
            except:
                pass

            #Beside other tables containing camera and optics configuration
            #data, the main  tables are written into the camera and optics
            #dictionary.
            camera['CameraTable_Version%s_TelID%i' % (version,tel_id[t])] \
            = cam_table_prime
            optics['OpticsTable_Version%s_TelID%i' % (version,tel_id[t])] \
            = opt_table_prime
    except:
        pass

    print('Astropy tables have been created.')
    h.close_file()
    print("Hessio file has been closed.")
    return (telescope, camera, optics)
Exemple #6
0
def hessio_event_source(url, max_events=None, allowed_tels=None):
    """A generator that streams data from an EventIO/HESSIO MC data file
    (e.g. a standard CTA data file.)

    Parameters
    ----------
    url : str
        path to file to open
    max_events : int, optional
        maximum number of events to read
    allowed_tels : list[int]
        select only a subset of telescope, if None, all are read. This can
        be used for example emulate the final CTA data format, where there
        would be 1 telescope per file (whereas in current monte-carlo,
        they are all interleaved into one file)

    """

    ret = pyhessio.file_open(url)

    if ret is not 0:
        raise RuntimeError("hessio_event_source failed to open '{}'"
                           .format(url))

    # the container is initialized once, and data is replaced within
    # it after each yield

    counter = 0
    eventstream = pyhessio.move_to_next_event()
    if allowed_tels is not None:
        allowed_tels = set(allowed_tels)
    event = EventContainer()
    event.meta.source = "hessio"

    # some hessio_event_source specific parameters
    event.meta.add_item('hessio__input', url)
    event.meta.add_item('hessio__max_events', max_events)

    for run_id, event_id in eventstream:

        event.dl0.run_id = run_id
        event.dl0.event_id = event_id
        event.dl0.tels_with_data = set(pyhessio.get_teldata_list())
        
        # handle telescope filtering by taking the intersection of
        # tels_with_data and allowed_tels
        if allowed_tels is not None:
            selected = event.dl0.tels_with_data & allowed_tels
            if len(selected) == 0:
                continue  # skip event
            event.dl0.tels_with_data = selected

        event.trig.tels_with_trigger \
            = pyhessio.get_central_event_teltrg_list()
        time_s, time_ns = pyhessio.get_central_event_gps_time()
        event.trig.gps_time = Time(time_s * u.s, time_ns * u.ns,
                                   format='gps', scale='utc')
        event.mc.energy = pyhessio.get_mc_shower_energy() * u.TeV
        event.mc.alt = Angle(pyhessio.get_mc_shower_altitude(), u.rad)
        event.mc.az = Angle(pyhessio.get_mc_shower_azimuth(), u.rad)
        event.mc.core_x = pyhessio.get_mc_event_xcore() * u.m
        event.mc.core_y = pyhessio.get_mc_event_ycore() * u.m
        event.mc.h_first_int = pyhessio.get_mc_shower_h_first_int() * u.m

        event.count = counter

        # this should be done in a nicer way to not re-allocate the
        # data each time (right now it's just deleted and garbage
        # collected)

        event.dl0.tel = dict()  # clear the previous telescopes
        event.mc.tel = dict()  # clear the previous telescopes

        for tel_id in event.dl0.tels_with_data:

            # fill pixel position dictionary, if not already done:
            if tel_id not in event.meta.pixel_pos:
                event.meta.pixel_pos[tel_id] \
                    = pyhessio.get_pixel_position(tel_id) * u.m
                event.meta.optical_foclen[
                    tel_id] = pyhessio.get_optical_foclen(tel_id) * u.m

            # fill telescope position dictionary, if not already done:
            if tel_id not in event.meta.tel_pos:
                event.meta.tel_pos[
                    tel_id] = pyhessio.get_telescope_position(tel_id) * u.m

            nchans = pyhessio.get_num_channel(tel_id)
            npix = pyhessio.get_num_pixels(tel_id)
            nsamples = pyhessio.get_num_samples(tel_id)
            event.dl0.tel[tel_id] = RawCameraData(tel_id)
            event.dl0.tel[tel_id].num_channels = nchans
            event.dl0.tel[tel_id].num_pixels = npix
            event.dl0.tel[tel_id].num_samples = nsamples
            event.mc.tel[tel_id] = MCCamera(tel_id)

            event.dl0.tel[tel_id].calibration \
                = pyhessio.get_calibration(tel_id)
            event.dl0.tel[tel_id].pedestal \
                = pyhessio.get_pedestal(tel_id)

            # load the data per telescope/chan
            for chan in range(nchans):
                event.dl0.tel[tel_id].adc_samples[chan] \
                    = pyhessio.get_adc_sample(channel=chan,
                                              telescope_id=tel_id)
                event.dl0.tel[tel_id].adc_sums[chan] \
                    = pyhessio.get_adc_sum(channel=chan,
                                           telescope_id=tel_id)
                event.mc.tel[tel_id].refshapes[chan] = \
                    pyhessio.get_ref_shapes(tel_id, chan)

            # load the data per telescope/pixel
            event.mc.tel[tel_id].photo_electrons \
                = pyhessio.get_mc_number_photon_electron(telescope_id=tel_id)
            event.mc.tel[tel_id].refstep = pyhessio.get_ref_step(tel_id)
            event.mc.tel[tel_id].lrefshape = pyhessio.get_lrefshape(tel_id)
            event.mc.tel[tel_id].time_slice = \
                pyhessio.get_time_slice(tel_id)
        yield event
        counter += 1

        if max_events is not None and counter >= max_events:
            return
Exemple #7
0
    def start(self):
        """ main event loop """

        for run_id, event_id in pyhessio.move_to_next_event():
            self.add_event_to_table(event_id)
Exemple #8
0
def load_hessio(filename):
    """
    Function to open and load a hessio file
    
    Parameters
    ----------
    filename: string
        name of the file
    """
    h.file_open(filename)
    print("Hessio file %s has been opened" % filename)
    next(h.move_to_next_event())
    tel_id = h.get_telescope_ids()
    
    tel_table = Table()
    
    tel_table['TelID']= tel_id
    
    tel_posX = [h.get_telescope_position(i)[0] for i in tel_id]
    tel_posY = [h.get_telescope_position(i)[1] for i in tel_id]
    tel_posZ = [h.get_telescope_position(i)[2] for i in tel_id]
    tel_table['TelX'] = tel_posX
    tel_table['TelX'].unit = u.m
    tel_table['TelY'] = tel_posY
    tel_table['TelY'].unit = u.m
    tel_table['TelZ'] = tel_posZ
    tel_table['TelZ'].unit = u.m
    #tel_table['CameraClass'] = [h.get_camera_class(i) for i in tel_id]
    tel_table['MirrorArea'] = [h.get_mirror_area(i) for i in tel_id]
    tel_table['MirrorArea'].unit = u.m**2
    tel_table['NMirrors'] = [h.get_mirror_number(i) for i in tel_id]
    tel_table['FL'] = [h.get_optical_foclen(i) for i in tel_id]
    tel_table['FL'].unit = u.m
    
    
    for t in range(len(tel_id)):       
        
        table = Table()
        pix_posX = h.get_pixel_position(tel_id[t])[0]
        pix_posY = h.get_pixel_position(tel_id[t])[1]       
        pix_id = np.arange(len(pix_posX))
        pix_area = h.get_pixel_area(tel_id[t])
         
        table['TelID'] = [tel_id[t] for i in range(len(pix_posX))]
        table['PixelID'] = pix_id
        table['PixX'] = pix_posX
        table['PixX'].unit = u.m
        table['PixY'] = pix_posY
        table['PixY'].unit = u.m
        table['PixArea'] = pix_area
        table['PixArea'].unit = u.mm**2
        
        if t == 0:
            cam_table = table
        else:
            cam_table = join(cam_table,table,join_type='outer')  
    
    #for t in range(len(tel_id)):
    #    table = Table()
    #    if t == 0:
    #        opt_table = table
    #    else:
    #        opt_table = join(opt_table,table,join_type='outer')
    opt_table = Table()
    print('Astropy tables have been created.')
    
    #to use this, one has to go through every event of the run...
    #n_channel = h.get_num_channel(tel_id)
    #ld.channel_num = n_channel
    #for chan in range(n_channel):
    #    ld.adc_samples.append(h.get_adc_sample(tel_id,chan).tolist())
    
    h.close_file()
    print("Hessio file has been closed.")
    return [tel_table,cam_table,opt_table]
Exemple #9
0
    def start(self):
        """ main event loop """

        for run_id, event_id in pyhessio.move_to_next_event():
            self.add_event_to_table(event_id)
 def nextevent_hessio():
     """
     Function to switch to the next event within the open hessio file
     """
     next(h.move_to_next_event())
Exemple #11
0
 def nextevent_hessio():
     """
     Function to switch to the next event within the open hessio file
     """
     next(h.move_to_next_event())
"""
import pyhessio as h
import sys
from astropy.table import Table
from astropy import units as u
from ctapipe import io

# TODO: use io.fits instead and make the table be variable length
# TODO: make this a tool (ctapipe-eventio2tels)

if __name__ == '__main__':

    filename = sys.argv.pop(1)

    h.file_open(filename)
    event = h.move_to_next_event()
    next(event)

    for telid in range(1, h.get_num_telescope()):
        try:
            px, py = h.get_pixel_position(telid)
            camtab = Table(names=['PIX_POS_X', 'PIX_POS_Y'],
                           data=[px * u.m, py * u.m])
            camtab.meta['N_PIX'] = h.get_num_pixels(telid)
            camtab.meta['N_SAMPS'] = h.get_num_samples(telid)
            camtab.meta['N_TIMES'] = h.get_pixel_timing_num_times_types(telid)
            camtab.meta['MIR_AREA'] = h.get_mirror_area(telid)
            geom = io.guess_camera_geometry(px * u.m, py * u.m)
            camtab.meta['TELCLASS'] = geom.cam_id
            camtab.meta['PIXTYPE'] = geom.pix_type
"""
import pyhessio as h
import sys
from astropy.table import Table
from astropy import units as u
from ctapipe import io

# TODO: use io.fits instead and make the table be variable length
# TODO: make this a tool (ctapipe-eventio2tels)

if __name__ == '__main__':

    filename = sys.argv.pop(1)

    h.file_open(filename)
    event = h.move_to_next_event()
    next(event)

    for telid in range(1, h.get_num_telescope()):
        try:
            px, py = h.get_pixel_position(telid)
            camtab = Table(names=['PIX_POS_X', 'PIX_POS_Y'],
                           data=[px * u.m, py * u.m])
            camtab.meta['N_PIX'] = h.get_num_pixels(telid)
            camtab.meta['N_SAMPS'] = h.get_num_samples(telid)
            camtab.meta['N_TIMES'] = h.get_pixel_timing_num_times_types(telid)
            camtab.meta['MIR_AREA'] = h.get_mirror_area(telid)
            geom = io.guess_camera_geometry(px * u.m, py * u.m)
            camtab.meta['TELCLASS'] = geom.cam_id
            camtab.meta['PIXTYPE'] = geom.pix_type
Exemple #14
0
def hessio_event_source(url, max_events=None, allowed_tels=None):
    """A generator that streams data from an EventIO/HESSIO MC data file
    (e.g. a standard CTA data file.)

    Parameters
    ----------
    url : str
        path to file to open
    max_events : int, optional
        maximum number of events to read
    allowed_tels : list[int]
        select only a subset of telescope, if None, all are read. This can
        be used for example emulate the final CTA data format, where there
        would be 1 telescope per file (whereas in current monte-carlo,
        they are all interleaved into one file)

    """

    ret = pyhessio.file_open(url)

    if ret is not 0:
        raise RuntimeError(
            "hessio_event_source failed to open '{}'".format(url))

    counter = 0
    eventstream = pyhessio.move_to_next_event()
    if allowed_tels is not None:
        allowed_tels = set(allowed_tels)
    container = Container("hessio_container")
    container.meta.add_item('hessio__input', url)
    container.meta.add_item('hessio__max_events', max_events)
    container.meta.add_item('pixel_pos', dict())
    container.meta.add_item('optical_foclen', dict())
    container.add_item("dl0", RawData())
    container.add_item("mc", MCShowerData())
    container.add_item("trig", CentralTriggerData())
    container.add_item("count")

    for run_id, event_id in eventstream:

        container.dl0.run_id = run_id
        container.dl0.event_id = event_id
        container.dl0.tels_with_data = set(pyhessio.get_teldata_list())

        # handle telescope filtering by taking the intersection of
        # tels_with_data and allowed_tels
        if allowed_tels is not None:
            selected = container.dl0.tels_with_data & allowed_tels
            if len(selected) == 0:
                continue  # skip event
            container.dl0.tels_with_data = selected

        container.trig.tels_with_trigger \
            = pyhessio.get_central_event_teltrg_list()
        time_s, time_ns = pyhessio.get_central_event_gps_time()
        container.trig.gps_time = Time(time_s * u.s,
                                       time_ns * u.ns,
                                       format='gps',
                                       scale='utc')
        container.mc.energy = pyhessio.get_mc_shower_energy() * u.TeV
        container.mc.alt = Angle(pyhessio.get_mc_shower_altitude(), u.rad)
        container.mc.az = Angle(pyhessio.get_mc_shower_azimuth(), u.rad)
        container.mc.core_x = pyhessio.get_mc_event_xcore() * u.m
        container.mc.core_y = pyhessio.get_mc_event_ycore() * u.m

        container.count = counter

        # this should be done in a nicer way to not re-allocate the
        # data each time (right now it's just deleted and garbage
        # collected)

        container.dl0.tel = dict()  # clear the previous telescopes

        for tel_id in container.dl0.tels_with_data:

            # fill pixel position dictionary, if not already done:
            if tel_id not in container.meta.pixel_pos:
                container.meta.pixel_pos[tel_id] \
                    = pyhessio.get_pixel_position(tel_id) * u.m
                container.meta.optical_foclen[
                    tel_id] = pyhessio.get_optical_foclen(tel_id) * u.m

            nchans = pyhessio.get_num_channel(tel_id)
            container.dl0.tel[tel_id] = RawCameraData(tel_id)
            container.dl0.tel[tel_id].num_channels = nchans

            # load the data per telescope/chan
            for chan in range(nchans):
                container.dl0.tel[tel_id].adc_samples[chan] \
                    = pyhessio.get_adc_sample(channel=chan,
                                              telescope_id=tel_id)
                container.dl0.tel[tel_id].adc_sums[chan] \
                    = pyhessio.get_adc_sum(channel=chan,
                                           telescope_id=tel_id)
        yield container
        counter += 1

        if max_events is not None and counter > max_events:
            return
def load_hessio(filename):
    """
    Function to open and load a hessio file
    
    Parameters
    ----------
    filename: string
        name of the file
    """
    print("Hessio file will be opened.")    
    h.file_open(filename)
    next(h.move_to_next_event())
    #version = h.get...
    version = 'Feb2016'
    
    #Creating 3 dictionaries where the instrument configuration will be stored
    #The dictionaries themselves contain astropy.table.Table objects
    telescope = {}
    camera = {}
    optics = {}  
    
    #--------------------------------------------------------------------------
    #Telescope configuration
    tel_table_prime = Table()
    tel_table_prime.meta = {'VERSION': version}    
    
    try: 
        tel_id = h.get_telescope_ids()
        tel_table_prime['TelID']= tel_id
    except: pass    
    try:
        tel_posX = [h.get_telescope_position(i)[0] for i in tel_id]
        tel_table_prime['TelX'] = tel_posX
        tel_table_prime['TelX'].unit = u.m
        tel_table_prime.meta['TelX_description'] =\
        'x-position of the telescope measured by...'
    except: pass
    try:
        tel_posY = [h.get_telescope_position(i)[1] for i in tel_id]
        tel_table_prime['TelY'] = tel_posY
        tel_table_prime['TelY'].unit = u.m
    except: pass
    try:
        tel_posZ = [h.get_telescope_position(i)[2] for i in tel_id]
        tel_table_prime['TelZ'] = tel_posZ
        tel_table_prime['TelZ'].unit = u.m
    except: pass
    try: tel_table['CameraClass'] = [h.get_camera_class(i) for i in tel_id]
    except: pass
    try:
        tel_table_prime['MirA'] = [h.get_mirror_area(i) for i in tel_id]
        tel_table_prime['MirA'].unit = u.m**2
    except: pass
    try:  tel_table_prime['MirN'] = [h.get_mirror_number(i) for i in tel_id]
    except: pass    
    try: 
        tel_table_prime['FL'] = [h.get_optical_foclen(i) for i in tel_id]
        tel_table_prime['FL'].unit = u.m
    except: pass
    try: tel_table_prime.meta['TelNum'] =  len(tel_posX)
    except: pass
    
    #Beside other tables containimng telescope configuration data, the main
    #telescope table is written into the telescope dictionary.
    telescope['TelescopeTable_Version%s' % version] = tel_table_prime
    
    #--------------------------------------------------------------------------
    #Camera and Optics configuration
    try:    
        for t in range(len(tel_id)):       
            
            cam_table_prime = Table()
            cam_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version}
            opt_table_prime = Table()
            opt_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version}
            
            try:
                pix_posX = h.get_pixel_position(tel_id[t])[0]
                pix_id = np.arange(len(pix_posX))
                cam_table_prime['PixID'] = pix_id                
                cam_table_prime['PixX'] = pix_posX
                cam_table_prime['PixX'].unit = u.m
                cam_table_prime.meta['PixXDescription'] =\
                'x-position of the pixel measured by...'
            except: pass
            try:
                pix_posY = h.get_pixel_position(tel_id[t])[1]
                cam_table_prime['PixY'] = pix_posY
                cam_table_prime['PixY'].unit = u.m
            except: pass
            try:
                camera_class = CD.guess_camera_geometry(pix_posX*u.m,pix_posY*u.m)
                pix_area_prime = camera_class.pix_area
                pix_type_prime = camera_class.pix_type
                pix_neighbors_prime = camera_class.pix_neighbors
            except: pass        
    
            try:
                pix_area = h.get_pixel_area(tel_id[t])
                cam_table_prime['PixA'] = pix_area
                cam_table_prime['PixA'].unit = u.mm**2
            except:
                try:
                    cam_table_prime['PixA'] = pix_area_prime
                    cam_table_prime['PixA'].unit = u.mm**2
                except: pass
            try: pix_type = h.get_pixel_type(tel_id[t])
            except:
                try: pix_type = pix_type_prime
                except: pix_type = 'unknown'
            cam_table_prime.meta['PixType'] = pix_type
            try:
                pix_neighbors = h.get_pixel_neighbor(tel_id[t])
                cam_table_prime['PixNeig'] = pix_neighbors
            except:
                try: cam_table_prime['PixNeig'] = pix_neighbors_prime
                except: pass       
            
            #as long as no mirror IDs are given, use the following:
            opt_table_prime['MirrID'] = [1,2]
            try:
                opt_table_prime.meta['MirNum'] = h.get_mirror_number(tel_id[t])
            except: pass
            try:
                opt_table_prime['MirArea'] = h.get_mirror_area(tel_id[t])
                opt_table_prime['MirArea'].unit = u.m**2
                opt_table_prime.meta['MirAreaDescription'] =\
                'Area of all mirrors'
            except: pass
            try:
                opt_table_prime['OptFocLen'] = h.get_optical_foclen(tel_id[t])
                opt_table_prime['OptFocLen'].unit = u.m
            except: pass
            
            #Beside other tables containing camera and optics configuration
            #data, the main  tables are written into the camera and optics
            #dictionary.
            camera['CameraTable_Version%s_TelID%i' % (version,tel_id[t])] \
            = cam_table_prime
            optics['OpticsTable_Version%s_TelID%i' % (version,tel_id[t])] \
            = opt_table_prime
    except: pass
        
    print('Astropy tables have been created.')
    h.close_file()
    print("Hessio file has been closed.")
    return(telescope,camera,optics)
Exemple #16
0
def hessio_event_source(url, max_events=None, allowed_tels=None):
    """A generator that streams data from an EventIO/HESSIO MC data file
    (e.g. a standard CTA data file.)

    Parameters
    ----------
    url : str
        path to file to open
    max_events : int, optional
        maximum number of events to read
    allowed_tels : list[int]
        select only a subset of telescope, if None, all are read. This can
        be used for example emulate the final CTA data format, where there
        would be 1 telescope per file (whereas in current monte-carlo,
        they are all interleaved into one file)

    """

    ret = pyhessio.file_open(url)

    if ret is not 0:
        raise RuntimeError("hessio_event_source failed to open '{}'"
                           .format(url))

    counter = 0
    eventstream = pyhessio.move_to_next_event()
    if allowed_tels is not None:
        allowed_tels = set(allowed_tels)
    container = Container("hessio_container")
    container.meta.add_item('hessio__input', url)
    container.meta.add_item('hessio__max_events', max_events)
    container.meta.add_item('pixel_pos', dict())
    container.meta.add_item('optical_foclen', dict())
    container.add_item("dl0", RawData())
    container.add_item("mc", MCEvent())
    container.add_item("trig", CentralTriggerData())
    container.add_item("count")

    for run_id, event_id in eventstream:

        container.dl0.run_id = run_id
        container.dl0.event_id = event_id
        container.dl0.tels_with_data = set(pyhessio.get_teldata_list())

        # handle telescope filtering by taking the intersection of
        # tels_with_data and allowed_tels
        if allowed_tels is not None:
            selected = container.dl0.tels_with_data & allowed_tels
            if len(selected) == 0:
                continue  # skip event
            container.dl0.tels_with_data = selected

        container.trig.tels_with_trigger \
            = pyhessio.get_central_event_teltrg_list()
        time_s, time_ns = pyhessio.get_central_event_gps_time()
        container.trig.gps_time = Time(time_s * u.s, time_ns * u.ns,
                                       format='gps', scale='utc')
        container.mc.energy = pyhessio.get_mc_shower_energy() * u.TeV
        container.mc.alt = Angle(pyhessio.get_mc_shower_altitude(), u.rad)
        container.mc.az = Angle(pyhessio.get_mc_shower_azimuth(), u.rad)
        container.mc.core_x = pyhessio.get_mc_event_xcore() * u.m
        container.mc.core_y = pyhessio.get_mc_event_ycore() * u.m

        container.count = counter

        # this should be done in a nicer way to not re-allocate the
        # data each time (right now it's just deleted and garbage
        # collected)

        container.dl0.tel = dict()  # clear the previous telescopes

        for tel_id in container.dl0.tels_with_data:

            # fill pixel position dictionary, if not already done:
            if tel_id not in container.meta.pixel_pos:
                container.meta.pixel_pos[tel_id] \
                    = pyhessio.get_pixel_position(tel_id) * u.m
                container.meta.optical_foclen[tel_id] = pyhessio.get_optical_foclen(tel_id) * u.m;

            # fill the photo electrons list
            if tel_id not in container.mc.photo_electrons:
                container.mc.photo_electrons[tel_id] = dict()
            for pix_id in range(pyhessio.get_num_pixels(tel_id)):
                container.mc.photo_electrons[tel_id][pix_id] = pyhessio.get_mc_number_photon_electron(tel_id, pix_id)[0]


            nchans = pyhessio.get_num_channel(tel_id)
            container.dl0.tel[tel_id] = RawCameraData(tel_id)
            container.dl0.tel[tel_id].num_channels = nchans

            # load the data per telescope/chan
            for chan in range(nchans):
                container.dl0.tel[tel_id].adc_samples[chan] \
                    = pyhessio.get_adc_sample(channel=chan,
                                              telescope_id=tel_id)
                container.dl0.tel[tel_id].adc_sums[chan] \
                    = pyhessio.get_adc_sum(channel=chan,
                                           telescope_id=tel_id)
        yield container
        counter += 1

        if max_events is not None and counter > max_events:
            return
Exemple #17
0
def hessio_event_source(url, max_events=None, single_tel=None):
    """A generator that streams data from an EventIO/HESSIO MC data file
    (e.g. a standard CTA data file.)

    Parameters
    ----------
    url : str
        path to file to open
    max_events : int, optional
        maximum number of events to read
    single_tel : int
        select only a single telescope, if None, all are read. This is
        to emulate the final CTA data format, where there would be 1
        telescope per file (whereas in current monte-carlo, they are
        all interleaved into one file)

    """

    ret = pyhessio.file_open(url)

    if ret is not 0:
        raise RuntimeError("hessio_event_source failed to open '{}'"
                           .format(url))

    counter = 0
    eventstream = pyhessio.move_to_next_event()
    container = Container("hessio_container")
    container.meta.add_item('hessio__input', url)
    container.meta.add_item('hessio__max_events', max_events)
    container.meta.add_item('pixel_pos', dict())
    container.add_item("dl0", RawData())
    container.add_item("count")

    for run_id, event_id in eventstream:

        container.dl0.run_id = run_id
        container.dl0.event_id = event_id
        container.dl0.tels_with_data = pyhessio.get_teldata_list()
        container.count = counter
        
        # handle single-telescope case (ignore others:
        if single_tel is not None:
            if single_tel not in container.dl0.tels_with_data:
                continue
            container.dl0.tels_with_data = [single_tel, ]

        # this should be done in a nicer way to not re-allocate the
        # data each time (right now it's just deleted and garbage
        # collected)

        container.dl0.tel = dict()  # clear the previous telescopes

        for tel_id in container.dl0.tels_with_data:

            # fill pixel position dictionary, if not already done:
            if tel_id not in container.meta.pixel_pos:
                container.meta.pixel_pos[
                    tel_id] = pyhessio.get_pixel_position(tel_id)

            nchans = pyhessio.get_num_channel(tel_id)
            container.dl0.tel[tel_id] = RawCameraData(tel_id)
            container.dl0.tel[tel_id].num_channels = nchans

            # load the data per telescope/chan
            for chan in range(nchans):
                container.dl0.tel[tel_id].adc_samples[chan] \
                    = pyhessio.get_adc_sample(channel=chan,
                                            telescope_id=tel_id)
                container.dl0.tel[tel_id].adc_sums[chan] \
                    = pyhessio.get_adc_sum(channel=chan,
                                         telescope_id=tel_id)
        yield container
        counter += 1

        if max_events is not None and counter > max_events:
            return