From 464d0fad6154b4bec08f5f2c54fe988fe130413f Mon Sep 17 00:00:00 2001 From: Yngve Inntjore Levinsen <Yngve.Levinsen@esss.se> Date: Mon, 20 Oct 2014 10:25:08 +0200 Subject: [PATCH] adding ess --- ess/TraceWin.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ ess/__init__.py | 1 + 2 files changed, 60 insertions(+) create mode 100644 ess/TraceWin.py create mode 100644 ess/__init__.py diff --git a/ess/TraceWin.py b/ess/TraceWin.py new file mode 100644 index 0000000..0dced8a --- /dev/null +++ b/ess/TraceWin.py @@ -0,0 +1,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] diff --git a/ess/__init__.py b/ess/__init__.py new file mode 100644 index 0000000..bbca541 --- /dev/null +++ b/ess/__init__.py @@ -0,0 +1 @@ +import TraceWin -- GitLab