Source code for tesfdmtools.utils.readevents
#!/usr/bin/env python3
#
'''
.. module:: readevents
:synopsis: utility to read raw and processed event files
.. moduleauthor:: Cor de Vries <c.p.de.vries@sron.nl>
'''
import os,sys
import numpy
[docs]def readevents(filename):
'''
Read processed and raw event fle and return datacolumns
with the column names as listed in the event file
Args:
* `filename` = filename of the event file to read
Returns:
* `header` = dictionary of the file header containing the filename, channel and pixel
* `data` = dictionary having the column names as keys which contain the data
'''
nn=100000 # max number of events
try:
ff=open(filename,'r')
except:
sys.exit("Error opening file: ",filename)
header={}
j=0
for line in ff:
if line[0] == "#":
if line.find(':') > 0:
nam=line.split(':')[1].strip()
if line.find('inputfile') > 0:
header['inputfile']=os.path.basename(nam).rsplit('.',1)[0]
elif line.find('channel') > 0:
header['channel']=nam
elif line.find('pixel') > 0:
header['pixel']=nam
else:
cols=line.strip('# \n').split()
data={}
for col in cols:
data[col]=numpy.zeros(nn,dtype=float)
else:
items=line.strip(' \n').split()
for k,col in enumerate(cols):
data[col][j]=items[k]
j=j+1
if j == nn:
break
print("Number of events: ",j)
ff.close()
for col in cols:
data[col]=data[col][:j]
return (header,data)