Skip to content

Commit

Permalink
Merge pull request #108 from fact-project/undo_recarray_append
Browse files Browse the repository at this point in the history
Undo recarray append
  • Loading branch information
maxnoe authored Dec 7, 2018
2 parents 6fa2abe + d011635 commit 7f46fa8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
31 changes: 17 additions & 14 deletions eventio/simtel/camorgan.pyx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# cython: language_level=3
import cython
from cpython cimport array
import numpy as np
cimport numpy as np


INT32 = np.int32
ctypedef np.int32_t INT32_t


cdef short bytes_to_short(const unsigned char b0, const unsigned char b1):
return ((<short> b0) << 8) | (<short> b1)
INT16 = np.int16
ctypedef np.int16_t INT16_t


@cython.wraparound(False) # disable negative indexing
Expand All @@ -18,18 +14,25 @@ cpdef read_sector_information(
unsigned long n_pixels,
unsigned long offset = 0,
):
cdef unsigned long pos = 0
cdef unsigned long i
cdef short n = 0
cdef np.ndarray[INT32_t, ndim=1] sector
cdef unsigned long i, j
cdef short n
cdef INT16_t* short_ptr

cdef list sectors = []
cdef array.array sector

cdef unsigned long pos = 0
for i in range(n_pixels):
n = bytes_to_short(data[pos + offset + 1], data[pos + offset])

short_ptr = <INT16_t*> &data[pos + offset]
n = short_ptr[0]
pos += 2

sector = np.frombuffer(data, dtype=INT32, count=n, offset=offset + pos)
pos += 2 * n
sector = array.array('h')
for j in range(n):
short_ptr = <INT16_t*> &data[pos + offset]
sector.append(short_ptr[0])
pos += 2

# FIXME:
# according to a comment in the c-sources
Expand Down
7 changes: 2 additions & 5 deletions eventio/simtel/objects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
''' Implementations of the simtel_array EventIO object types '''
import numpy as np
from numpy.lib.recfunctions import append_fields
from io import BytesIO
import struct
from ..base import EventIOObject, read_next_header_sublevel
Expand Down Expand Up @@ -1154,12 +1153,10 @@ def parse(self):
''' '''
assert_version_in(self, (1, 2))
self.seek(0)
byte_stream = BytesIO(self.read())

array = read_array(
byte_stream, dtype=self.dtypes[self.header.version], count=1
return read_array(
self, dtype=self.dtypes[self.header.version], count=1
)
return append_fields(array, 'event_id', [self.header.id])[0]


class CameraMonitoring(EventIOObject):
Expand Down
7 changes: 6 additions & 1 deletion eventio/simtel/simtelfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(self, path, allowed_telescopes=None):
# we save them here for later use
self.init_mc_showers = []
self.init_mc_events = []
self.init_mc_event_ids = []

self.telescope_descriptions = defaultdict(dict)
first = True
Expand All @@ -120,6 +121,7 @@ def __init__(self, path, allowed_telescopes=None):

elif isinstance(o, MCEvent):
self.init_mc_events.append(o.parse())
self.init_mc_event_ids.append(o.header.id)
msg = 'Unexpectd MCEvent in telescope description block'
else:
msg = 'Skipping unexpected object of type {}'.format(
Expand All @@ -140,12 +142,14 @@ def __iter__(self):

current_mc_shower = None
current_mc_event = None
current_mc_event_id = None

# check if some showers or events were already read in __init__
if len(self.init_mc_showers) > 0:
current_mc_shower = self.init_mc_showers[-1]
if len(self.init_mc_events) > 0:
current_mc_event = self.init_mc_events[-1]
current_mc_event_id = self.init_mc_event_ids[-1]

current_photoelectron_sum = None
current_photoelectrons = {}
Expand All @@ -160,6 +164,7 @@ def __iter__(self):

elif isinstance(o, MCEvent):
current_mc_event = o.parse()
current_mc_event_id = o.header.id

elif isinstance(o, iact.TelescopeData):
current_photoelectrons = parse_photoelectrons(o)
Expand All @@ -177,7 +182,7 @@ def __iter__(self):
continue

event_data = {
'event_id': current_mc_event['event_id'],
'event_id': current_mc_event_id,
'mc_shower': current_mc_shower,
'mc_event': current_mc_event,
'telescope_events': array_event['telescope_events'],
Expand Down
2 changes: 1 addition & 1 deletion eventio/simtel/tests/test_simtel_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_2003_3_objects():
for sector in cam_organ['sectors']:
# sector must never contain a zero, unless it is in the
# very first element
assert (sector[1:] == 0).sum() == 0
assert all(s != 0 for s in sector[1:])
# print(pixel_id, sector)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def finalize_options(self):

setup(
name='eventio',
version='0.9.0',
version='0.9.1',
description='Python read-only implementation of the EventIO file format',
long_description=long_description,
url='https://github.com/fact-project/pyeventio',
Expand Down

0 comments on commit 7f46fa8

Please sign in to comment.