Skip to content

Commit

Permalink
Merge pull request #131 from cta-observatory/pycorsikaio
Browse files Browse the repository at this point in the history
Use pycorsikaio, fixes #130
  • Loading branch information
maxnoe authored Jan 17, 2019
2 parents 54a9aef + 5c428d7 commit f15ab87
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 362 deletions.
9 changes: 5 additions & 4 deletions eventio/iact/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ def __iter__(self):
x_offset=array_offsets[reuse]['x'],
y_offset=array_offsets[reuse]['y'],
weight=array_offsets[reuse]['weight'],
event_id=header.event_id,
event_number=header['event_number'],
reuse=reuse + 1,
n_photons=n_photons,
n_bunches=n_bunches,
longitudinal=longitudinal,
)

obj = next(self)

check_type(obj, EventEnd)
Expand All @@ -176,7 +177,7 @@ def __iter__(self):
[
'header', 'photon_bunches',
'time_offset', 'x_offset', 'y_offset', 'weight',
'event_id', 'reuse',
'event_number', 'reuse',
'n_photons', 'n_bunches',
'longitudinal',
]
Expand Down Expand Up @@ -228,9 +229,9 @@ class Event(EventTuple):
Only different from 1 if importance sampling was used.
'''
def __repr__(self):
return '{}(event_id={}, reuse={}, n_telescopes={}, n_photons={})'.format(
return '{}(event_number={}, reuse={}, n_telescopes={}, n_photons={})'.format(
self.__class__.__name__,
self.event_id,
self.event_number,
self.reuse,
len(self.n_bunches),
self.n_photons,
Expand Down
71 changes: 22 additions & 49 deletions eventio/iact/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
import struct
import numpy as np
from numpy.lib.recfunctions import append_fields
from io import BytesIO
from corsikaio.subblocks import (
parse_run_header,
parse_run_end,
parse_event_header,
parse_event_end,
)


from ..tools import (
read_short, read_int, read_float, read_from, read_eventio_string,
)
from ..base import EventIOObject
from ..exceptions import WrongSize
from .parse_corsika_data import (
parse_corsika_event_header,
parse_corsika_run_header,
)
from ..version_handling import assert_version_in



__all__ = [
'RunHeader',
'TelescopeDefinition',
Expand Down Expand Up @@ -45,19 +50,12 @@ def parse(self):
Returns a dictionary with the items of the run header block
'''
self.seek(0)
data = self.read()
n, = struct.unpack('i', data[:4])
stream = BytesIO(self.read())
n = read_int(stream)
if n != 273:
raise WrongSize(
'Expected 273 bytes, but found {}'.format(n))
raise WrongSize('Expected 273 floats, but found {}'.format(n))

block = np.frombuffer(
data,
dtype=np.float32,
count=n,
offset=4,
)
return parse_corsika_run_header(block)
return parse_run_header(stream.read())


class TelescopeDefinition(EventIOObject):
Expand Down Expand Up @@ -120,16 +118,9 @@ def parse(self):
n, = struct.unpack('i', data[:4])
if n != 273:
raise WrongSize(
'Expected 273 bytes, but found {}'.format(n))

block = np.frombuffer(
data,
dtype=np.float32,
count=n,
offset=4,
)
'Expected 273 floats, but found {}'.format(n))

return parse_corsika_event_header(block)
return parse_event_header(data[4:])


class ArrayOffsets(EventIOObject):
Expand Down Expand Up @@ -337,17 +328,9 @@ def parse(self):
self.seek(0)
n = read_int(self)
if n != 273:
raise WrongSize(
'Expected 273 bytes, but found {}'.format(n))

dtype = np.dtype('float32')
block = np.frombuffer(
self.read(n * dtype.itemsize),
dtype=dtype,
count=n,
)
raise WrongSize('Expected 3 floats, but found {}'.format(n))

return block
return parse_event_end(self.read())


class RunEnd(EventIOObject):
Expand All @@ -365,15 +348,11 @@ def parse(self):

self.seek(0)
n = read_int(self)

dtype = np.dtype('float32')
block = np.frombuffer(
self.read(n * dtype.itemsize),
dtype=dtype,
count=n,
)

return block
if n != 3:
raise WrongSize('Expected 3 floats, but found {}'.format(n))
d = bytearray(self.read())
d.extend(b'\x00' * (270 * 4))
return parse_run_end(d)


class Longitudinal(EventIOObject):
Expand Down Expand Up @@ -423,9 +402,3 @@ def parse(self):
input_card.extend(read_eventio_string(self))
input_card.append(ord('\n'))
return input_card


def remove_ascii_control_characters(string):
''' See http://stackoverflow.com/a/4324823/3838691 '''
mapping = dict.fromkeys(range(32))
return string.translate(mapping)
Loading

0 comments on commit f15ab87

Please sign in to comment.