Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class dst:
def __init__(self, filename):
'''
Simple class to read in a
TraceWin distribution file
Class afterwards hold the following
dictionary items:
- x [cm]
- xp [rad]
- y [cm]
- yp [rad]
- phi [rad]
- E [MeV]
'''
# easy storage..
self.filename=filename
# used to create dict behaviour..
self._columns=['x','xp','y','yp','phi','E']
# read in the file..
self._readBinaryFile()
def _readBinaryFile(self):
'''
Thanks Emma!
'''
import numpy
fin=file(self.filename,'r')
# dummy, Np, Ib, freq, dummy
Header_type = numpy.dtype([
('dummy12', numpy.int16),
('Np', numpy.int32),
('Ib', numpy.float64),
('freq', numpy.float64),
('dummy3', numpy.int8)
])
Header=numpy.fromfile(fin, dtype=Header_type, count=1)
self.Np=Header['Np'][0]
self.Ib=Header['Ib'][0]
self.freq=Header['freq'][0]
Table=numpy.fromfile(fin, dtype=numpy.float64, count=self.Np*6)
self._data=Table.reshape(self.Np,6)
Footer=numpy.fromfile(fin, dtype='f64', count=1)
self.mass=Footer[0]
def __getitem__(self, key):
'''
makes the class function as a dictionary
e.g. dst['x'] returns the x array..
'''
if key in self._columns:
i=self._columns.index(key)
return self._data[:,i]