Newer
Older
TraceWin distribution file
Class afterwards hold the following
dictionary items:
- x [m]
- xp [rad]
- y [m]
- yp [rad]
- phi [rad]
- E [MeV] (kinetic energy)
def __init__(self,
filename=None,
freq=352.21,
mass=938.272,
Ib=0.0):
if filename:
# read in the file..
self._readBinaryFile()
else:
import numpy
def append(self, x=0.0, xp=0.0, y=0.0, yp=0.0, E=0.0, phi=0.0):
'''
Append one particle to the distribution
- Kinetic Energy in MeV
- x,y in m
- xp,yp in rad
- phi in rad
'''
import numpy
self._data = numpy.append(self._data, [[x, xp, y, yp, phi, E]], 0)
self.Np += 1
Append a matrix of particle vectors.
Matrix on form 6xN, where N is number of particles.
Each row should hold [x,xp,y,yp,phi,E]
Units m,rad, MeV
'''
import numpy
self._data = numpy.append(self._data, array, 0)
self.Np += len(array)
def combine_dst(self, other):
'''
Appends the particles from another dst object to this one
'''
if self.mass != other.mass:
raise ValueError("You are trying to add two distributions with differing mass: {} and {}".format( self.mass, other.mass))
if self.freq != other.freq:
raise ValueError("You are trying to add two distributions with differing freq: {} and {}".format( self.freq, other.freq))
self.append_many(other._data)
self.Ib = ( self.Ib*self.Np + other.Ib*other.Np ) / ( self.Np + other.Np)
def remove(self, i=None):
'''
Removes all particles from the distribution, or the line specified by i
'''
import numpy
if i is None:
self._data=numpy.delete(self._data,numpy.s_[:], 0)
self.Np=0
else:
self.Np-=1
# 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]
# Some toutatis distributions has an undocumented 7th line of 0's
Table = numpy.fromfile(fin, dtype=numpy.float64, count=self.Np*7+1)
elif len(Table)==self.Np*6+1: # this is true in most cases
raise ValueError("Incorrect table dimensions found:", len(Table))
# makes the class function as a dictionary
# e.g. dst['x'] returns the x array..
except:
raise ValueError("Available keys: "+str(self._columns))
def __setitem__(self, key, value):
try:
i=self._columns.index(key)
except:
raise ValueError("Available keys: "+str(self._columns))
def save(self, filename, toutatis=False):
'''
Save the distribution file
so it can be read by TraceWin again
:param filename: Name of file
:param toutatis: Include 7th column of zeros
Stolen from Ryoichi's func.py (with permission)
'''
from struct import pack
fout=open(filename, 'wb')
fout.write(pack('b', 125))
fout.write(pack('b', 100))
fout.write(pack('i', self.Np))
fout.write(pack('d', self.Ib))
fout.write(pack('d', self.freq))
fout.write(pack('b', 125))
if toutatis and data.shape[1]==6:
data = numpy.append(data,numpy.zeros((len(data),1)),1)
elif not toutatis and data.shape[1]==7:
fout.write(pack('{}d'.format(len(data)),*data))
def subplot(self, index, x, y=None, nb=100, mask=None):
'''
Create a subplot histogram similar to TraceWin.
Example::
import numpy as np
from ess import TraceWin
from matplotlib import pyplot as plt
data=TraceWin.dst('part_dtl1.dst')
m=np.where(data['E']>3.5)
data.subplot(221,'x','xp',mask=m)
data.subplot(222,'y','yp',mask=m)
data.subplot(223,'phi','E',mask=m)
data.subplot(224,'x','y',mask=m)
plt.show()
'''
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import numpy as np
units={ 'x': 'mm', 'y': 'mm',
'xp': 'mrad', 'yp': 'mrad',
'E': 'MeV', 'phi': 'deg'
}
# get X and Y data
dx=np.array(self[x])
if x in ['x','y','xp','yp']:
dx*=1e3
if y in ['x','y','xp','yp']:
dy*=1e3
if x in ['phi']:
dx-=np.average(dx)
dx*=180/np.pi
if y in ['phi']:
dy-=np.average(dy)
dy*=180/np.pi
if x in ['E'] and max(dx)<0.1:
dx*=1e3
units['E']='keV'
if y in ['E'] and max(dy)<0.1:
dy*=1e3
units['E']='keV'
if y!=None:
plt.hist2d(dx, dy, bins=nb, norm=LogNorm())
plt.title('{} [{}] - {} [{}]'.format(x,units[x],y,units[y]))
hist,bin_edges=np.histogram(dx,bins=nb)
b=bin_edges[:-1]+0.5*(bin_edges[1]-bin_edges[0])
plt.plot(b,hist*0.2*(max(dy)-min(dy))/max(hist)+min(dy),'k',lw=1.5,drawstyle='steps')
hist,bin_edges=np.histogram(dy,bins=nb)
b=bin_edges[:-1]+0.0*(bin_edges[1]-bin_edges[0])
plt.plot(hist*0.2*(max(dx)-min(dx))/max(hist)+min(dx),b,'k',lw=1.5,drawstyle='steps')
else:
# plot a simple 1D histogram..
plt.hist(dx, bins=nb)
plt.title('{} [{}]'.format(x,units[x]))
Simple class to read in a
TraceWin plot file
Class afterwards hold the following
dictionary items:
- Ne (number of locations)
- Np (number of particles)
- Ib [A] (beam current)
- freq [MHz]
each plt[i], where i is element number, holds:
- Zgen [cm] (location)
- phase0 [deg] (ref phase)
- wgen [MeV] (ref energy)
- yp [array, rad]
- phi [array, rad]
- E [array, MeV]
- l [array] (is lost)
plt=ess.TraceWin.plt('calc/dtl1.plt')
for i in [97,98]:
data=plt[i]
if data:
print(data['x'])
'''
def __init__(self, filename):
# easy storage..
self.filename=filename
# used to create dict behaviour..
self._columns=['x','xp','y','yp','phi','E', 'l']
# read in the file..
self._readBinaryFile()
def _readBinaryFile(self):
# Thanks Emma!
import numpy
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# dummy, Np, Ib, freq, dummy
Header_type = numpy.dtype([
('dummy12', numpy.int16),
('Ne', numpy.int32),
('Np', numpy.int32),
('Ib', numpy.float64),
('freq', numpy.float64),
('mc2', numpy.float64),
])
SubHeader_type = numpy.dtype([
('dummy12', numpy.int8),
('Nelp', numpy.int32),
('Zgen', numpy.float64),
('phase0', numpy.float64),
('wgen', numpy.float64),
])
Header=numpy.fromfile(fin, dtype=Header_type, count=1)
self.Np=Header['Np'][0]
self.Ne=Header['Ne'][0]
self.Ib=Header['Ib'][0]
self.freq=Header['freq'][0]
self.mc2=Header['mc2'][0]
self._data=[]
self.Nelp=[]
i=0
while i<self.Ne:
SubHeader=numpy.fromfile(fin, dtype=SubHeader_type, count=1)
# unfinished files need this fix (simulation still running)
i=SubHeader['Nelp'][0]
self.Nelp.append(i)
Table=numpy.fromfile(fin, dtype=numpy.float32, count=self.Np*7)
Table=Table.reshape(self.Np,7)
data={}
for key in ['Zgen','phase0','wgen']:
data[key]=SubHeader[key][0]
for j in range(7):
# convert x,y from cm to m
if c in ['x', 'y']:
data[c]*=1e-2
self._data.append(data)
def __getitem__(self, key):
if key in self.Nelp:
ret={}
# some particles are lost, exclude those:
lost_mask=self._data[i]['l']==0
for key in self._data[i]:
if isinstance(self._data[i][key], numpy.ndarray):
ret[key]=self._data[i][key][lost_mask]
else:
ret[key]=self._data[i][key]
return ret
def calc_s(self):
'''
Generates self.s which holds
the position of each element
in metres
'''
import numpy
self.s=[]
for i in self.Nelp:
self.s.append(self[i]['Zgen']/100.0)
self.s=numpy.array(self.s)
def calc_avg(self):
'''
Calculates averages of 6D coordinates at each
element, such that e.g.
self.avg["x"] gives average X at each location.
'''
import numpy
self.avg=dict(x=[], xp=[], y=[], yp=[], E=[], phi=[])
for i in self.Nelp:
data=self[i]
for v in vals:
self.avg[v].append(numpy.average(data[v]))
at each position, based on
for i,j in zip(self.Nelp,range(len(self.Nelp))):
Eavg = self.avg['E'][j]
self.gamma.append((self.mc2 + Eavg) / self.mc2)
self.beta.append(numpy.sqrt(1. - 1. / self.gamma[-1]**2))
self.gamma = numpy.array(self.gamma)
self.beta = numpy.array(self.beta)
'''
Calculates min/max values of beam coordinates
in percentile, pmin is lower and pmax upper.
Units: cm
'''
import numpy
self.min = dict(x=[], xp=[], y=[], yp=[], E=[])
self.max = dict(x=[], xp=[], y=[], yp=[], E=[])
self.min[v].append(numpy.percentile(data[v], pmin))
self.max[v].append(numpy.percentile(data[v], pmax))
for v in self.min.keys():
self.min[v]=numpy.array(self.min[v])
self.max[v]=numpy.array(self.max[v])
def calc_sigma(self):
'''
Calculates the sigma matrix
Creates self.sigma such that self.sigma[i,j]
returns the sigma matrix for value i,j.
The numbering is:
0: x
1: xp
2: y
3: yp
4: E
5: phi
for j in range(len(self.Nelp)):
self.sigma.append([[numpy.mean((data[n] - self.avg[n][j]) * (data[m] - self.avg[m][j])) for n in vals] for m in vals])
def calc_std(self):
'''
Calculates the beam sizes
'''
import numpy
if not hasattr(self, 'sigma'):
self.calc_sigma()
for j in range(len(vals)):
v = vals[j]
self.std[v] = numpy.sqrt(self.sigma[:, j, j])
def calc_twiss(self):
'''
Calculates emittance, beta, alfa, gamma
for each plane, x-xp, y-yp, and E-phi
if not hasattr(self, 'sigma'):
self.calc_sigma()
if not hasattr(self, 'gamma'):
self.calc_rel()
for j in range(len(self.Nelp)):
self.twiss_eps.append([numpy.sqrt(numpy.linalg.det(self.sigma[j][i:i + 2][:, i:i + 2])) for i in (0, 2, 4)])
self.twiss_eps = numpy.array(self.twiss_eps)
# Calculate normalized emittance:
# TODO: this is NOT correct normalization for longitudinal
for i in range(3):
self.twiss_eps_normed[:, i] *= self.gamma * self.beta
# Calculate beta:
# This is a factor 10 different from what TraceWin plots
self.twiss_beta = [[self.sigma[j][i][i] / self.twiss_eps[j, i // 2] for i in (0, 2, 4)] for j in range(len(self.Nelp))]
self.twiss_beta = numpy.array(self.twiss_beta)
self.twiss_alpha = [[-self.sigma[j][i][i + 1] / self.twiss_eps[j, i // 2] for i in (0, 2, 4)] for j in range(len(self.Nelp))]
self.twiss_alpha = numpy.array(self.twiss_alpha)
def get_dst(self, index):
'''
Returns the dst corresponding to the given index
'''
dset = self[index]
_dst = dst()
_dst.freq = self.freq
_dst.Ib = self.Ib * 1000
_dst.Np = len(dset['x'])
_dst.mass = self.mc2
_dst._data = numpy.array([dset['x'],
dset['xp'],
dset['y'],
dset['yp'],
dset['phi'],
dset['E']]).transpose()
'''
Saves the dst at the specified index to file
Returns the same dst object.
'''
class density_file:
'''
Simple class to read a TraceWin density file
into a pythonized object
Class afterwards hold the same items as
found in the TraceWin documentation:
z, nelp, ib, Np, Xouv, Youv, dXouv, ..
def __init__(self, filename, envelope=None):
self.filename = filename
self.fin = open(self.filename, 'r')
if envelope is None: # try to guess
if filename.split('/')[-1].split('.')[0] == 'Density_Env':
self.envelope = True
# first we simply count how many elements we have:
self.Xouv = numpy.zeros(counter)
self.Youv = numpy.zeros(counter)
if self.version >= 9:
self.dXouv = numpy.zeros(counter)
self.dYouv = numpy.zeros(counter)
self.moy = numpy.zeros((counter, 7))
self.moy2 = numpy.zeros((counter, 7))
self._max = numpy.zeros((counter, 7))
self._min = numpy.zeros((counter, 7))
if self.version >= 11:
self.phaseF = numpy.zeros((counter))
self.phaseG = numpy.zeros((counter))
if self.version >= 10:
self.maxR = numpy.zeros((counter, 7))
self.minR = numpy.zeros((counter, 7))
if self.version >= 5:
self.rms_size = numpy.zeros((counter, 7))
self.rms_size2 = numpy.zeros((counter, 7))
if self.version >= 6:
self.min_pos_moy = numpy.zeros((counter, 7))
self.max_pos_moy = numpy.zeros((counter, 7))
if self.version >= 7:
self.rms_emit = numpy.zeros((counter, 3))
self.rms_emit2 = numpy.zeros((counter, 3))
if self.version >= 8:
self.energy_accept = numpy.zeros(counter)
self.phase_ouv_pos = numpy.zeros(counter)
self.phase_ouv_neg = numpy.zeros(counter)
self.lost = numpy.zeros((counter, self.Nrun))
self.powlost = numpy.zeros((counter, self.Nrun))
self.lost2 = numpy.zeros(counter)
self.Minlost = numpy.zeros(counter)
self.Maxlost = numpy.zeros(counter)
self.powlost2 = numpy.zeros(counter)
self.Minpowlost = numpy.zeros(counter)
self.Maxpowlost = numpy.zeros(counter)
self.i += 1
if sys.flags.debug and self.i % 100 == 0:
print('Read status', self.i)
def _getHeader(self):
import numpy
# header..
version = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0]
year = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0]
# in case we did not read all data, this will detect our mistake:
shift = 0
while year != 2011 or version not in [8, 9, 10, 11, 12]:
shift += 1
version = year
year = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0]
print(year, version)
raise ValueError("ERROR, shifted " + str(shift * 2) + " bytes")
self.vlong = numpy.fromfile(self.fin, dtype=numpy.int16, count=1)[0]
self.Nrun = numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0]
def _skipAndCount(self):
import numpy
self._getHeader()
if self.version == 8:
numpy.fromfile(self.fin, dtype=numpy.int16, count=292 // 2)
elif self.version == 9:
numpy.fromfile(self.fin, dtype=numpy.int16, count=300 // 2)
elif self.version == 10:
numpy.fromfile(self.fin, dtype=numpy.int16, count=356 // 2)
else:
raise TypeError("It is not possible to read this format..")
elif self.Nrun > 1:
# WARN not 100% sure if this is correct..
if self.version <= 9:
numpy.fromfile(self.fin, dtype=numpy.int16, count=((5588 + self.Nrun * 12) // 2))
elif self.version == 10:
numpy.fromfile(self.fin, dtype=numpy.int16, count=((20796 + self.Nrun * 12) // 2))
else:
raise TypeError("It is not possible to read this format..")
elif self.version == 8:
numpy.fromfile(self.fin, dtype=numpy.int16, count=12344 // 2)
elif self.version == 9:
numpy.fromfile(self.fin, dtype=numpy.int16, count=12352 // 2)
elif self.version == 10:
numpy.fromfile(self.fin, dtype=numpy.int16, count=12408 // 2)
elif self.version == 11:
numpy.fromfile(self.fin, dtype=numpy.int16, count=12416 // 2)
raise TypeError("It is not possible to read this format..")
def _get_7dim_array(array):
return dict(x=array[0],
y=array[1],
phase=array[2],
energy=array[3],
r=array[4],
z=array[5],
dpp=array[6],
)
def _getFullContent(self):
import numpy
# (though only if we are SURE about content!)
ver,year,vlong = numpy.fromfile(self.fin, dtype=numpy.int16, count=3)
Nrun = numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0]
self.nelp[self.i] = numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0]
self.ib[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
self.z[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
self.Xouv[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
self.Youv[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
if self.version >= 9:
dXouv = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
dYouv = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
step = numpy.fromfile(self.fin, dtype=numpy.int32, count=1)[0]
n = 7 # x [m], y[m], Phase [deg], Energy [MeV], R[m], Z[m], dp/p
self.moy[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:]
self.moy2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:]
self._max[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:]
self._min[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:]
if self.version >= 11:
self.phaseF[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
self.phaseG[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
if self.version >= 10:
self.maxR[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:]
self.minR[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)[:]
if self.version >= 5:
self.rms_size[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)
self.rms_size2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)
if self.version >= 6:
self.min_pos_moy[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)
self.max_pos_moy[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=n)
if self.version >= 7:
self.rms_emit[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=3)[:]
self.rms_emit2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=3)[:]
if self.version >= 8:
self.energy_accept[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)
self.phase_ouv_pos[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)
self.phase_ouv_neg[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)
self.Np[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0]
for i in range(self.Nrun):
self.lost[self.i, i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0]
self.powlost[self.i, i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
self.lost2[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0]
self.Minlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0]
self.Maxlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.int64, count=1)[0]
self.powlost2[self.i] = numpy.fromfile(self.fin, dtype=numpy.float64, count=1)[0]
self.Minpowlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
self.Maxpowlost[self.i] = numpy.fromfile(self.fin, dtype=numpy.float32, count=1)[0]
if self.vlong == 1:
tab = numpy.fromfile(self.fin, dtype=numpy.uint64, count=n * step)
tab = numpy.fromfile(self.fin, dtype=numpy.uint32, count=n * step)
if self.ib[self.i] > 0:
tabp = numpy.fromfile(self.fin, dtype=numpy.uint32, count=3 * step)
'''
returns the average of the parameter
weighted by how many Nruns in self and other object
This allows for different lengths of the two arrays..
mine = getattr(self, param)
new = getattr(other, param)
if len(mine) > len(new):
ret[:len(new)] = (mine[:len(new)] * self.Nrun + new * other.Nrun) / (self.Nrun + other.Nrun)
elif len(mine) < len(new):
ret[:len(mine)] = (mine * self.Nrun + new[:len(mine)] * other.Nrun) / (self.Nrun + other.Nrun)
ret = (mine * self.Nrun + new * other.Nrun) / (self.Nrun + other.Nrun)
'''
returns the sum of the parameter
This allows for different lengths of the two arrays..
'''
mine = getattr(self, param)
new = getattr(other, param)
if len(mine) > len(new):
ret = mine.copy()
ret[:len(new)] += new
ret = new.copy()
ret[:len(mine)] += mine
else:
'''
returns the concatenation of the two matrices
This allows for different lengths of the two arrays/matrices..
'''
import numpy
mine = getattr(self, param)
new = getattr(other, param)
ret = numpy.zeros((max([len(mine), len(new)]), len(mine[0]) + len(new[0])))
ret[:len(mine), :len(mine[0])] = mine
ret[:len(new), len(mine[0]):] = new
'''
returns the function applied on the parameter
This allows for different lengths of the two arrays..
'''
mine = getattr(self, param)
new = getattr(other, param)
if len(mine) > len(new):
ret[:len(new)] = function(mine[:len(new)], new)
elif len(mine) < len(new):
'''
Merge with list of objects
'''
import numpy
raise TypeError("You tried to merge a non-list")
# for now we only allow objects with same version..
for o in objects:
if self.version != o.version:
raise ValueError("Cannot merge files with differing version")
# merge info..
for o in objects:
raise ValueError("Sorry, not implemented yet. Complain to Yngve")
# this looks strange to me, but it is what TraceWin does..
self.moy = self._sum_merge(o, 'moy')
self.moy2 = self._sum_merge(o, 'moy')
self._max = self._fun_merge(o, numpy.maximum, '_max')
self._min = self._fun_merge(o, numpy.minimum, '_min')
# this looks strange to me, but it is what TraceWin does..
self.rms_size = self._sum_merge(o, 'rms_size')
self.rms_size2 = self._sum_merge(o, 'rms_size2')
if self.version >= 6:
self.max_pos_moy = self._fun_merge(o, numpy.maximum, 'max_pos_moy')
self.min_pos_moy = self._fun_merge(o, numpy.minimum, 'min_pos_moy')
# this looks strange to me, but it is what TraceWin does..
self.rms_emit = self._sum_merge(o, 'rms_emit')
self.rms_emit2 = self._sum_merge(o, 'rms_emit2')
# Warning: TraceWin does NOT merge these data in any way
self.energy_accept = self._avg_merge(o, 'energy_accept')
self.phase_ouv_pos = self._avg_merge(o, 'phase_ouv_pos')
self.phase_ouv_neg = self._avg_merge(o, 'phase_ouv_neg')
# Note, we don't get into the problem of differing table sizes
# particles are lost, because we have written zeroes for
# the rest of the tables
self.lost = self._concatenate_merge(o, 'lost')
self.powlost = self._concatenate_merge(o, 'powlost')
self.lost2 = self._sum_merge(o, 'lost2')
self.powlost2 = self._sum_merge(o, 'powlost2')
self.Minlost = self._fun_merge(o, numpy.minimum, 'Minlost')
self.Maxlost = self._fun_merge(o, numpy.maximum, 'Maxlost')
self.Minpowlost = self._fun_merge(o, numpy.minimum, 'Minpowlost')
self.Maxpowlost = self._fun_merge(o, numpy.maximum, 'Maxpowlost')
# Note: We are ignoring tab/tabp data...
# merge final info (make sure to do this last!)
self.Np = self._sum_merge(o, 'Np')
self.Nrun += o.Nrun
def savetohdf(self, filename='Density.h5', group='TraceWin', force=False):
if force:
del fout[group]
else:
if sys.flags.debug:
print("Group {} already exist in {}".format(group, filename))
group = fout.create_group(group)
# header attributes..
group.attrs['version'] = self.version
group.attrs['year'] = self.year
group.attrs['Nrun'] = self.Nrun
group.attrs['vlong'] = self.vlong
arrays = ['z', 'nelp', 'ib', 'Np', 'Xouv', 'Youv']
array_units = ['m', '', 'mA', '', 'm', 'm']
if self.version >= 8:
arrays += ['energy_accept', 'phase_ouv_pos', 'phase_ouv_neg']
array_units += [ 'eV', 'deg', 'deg']
if partran:
arrays += [ 'lost2', 'Minlost', 'Maxlost', 'powlost2', 'Minpowlost', 'Maxpowlost']
array_units += [ '', '', '', 'W*w', 'W', 'W']
# 7 numbers per location..
coordinates = ['moy', 'moy2', '_max', '_min']
coordinate_units = ['m', 'm*m', 'm', 'm']
if self.version >= 5 and partran:
coordinates += ['rms_size', 'rms_size2']
coordinate_units += [ 'm', 'm*m']
if self.version >= 6 and partran:
coordinates += ['min_pos_moy', 'max_pos_moy']
coordinate_units += [ 'm', 'm']
for val, unit in zip(arrays, array_units):
data_set = group.create_dataset(val, (length,), dtype='f')
data_set[...] = getattr(self, val)
for val, unit in zip(coordinates, coordinate_units):
data_set = group.create_dataset(val, (length, 7), dtype='f')
data_set[...] = getattr(self, val)
emit_data = ['rms_emit', 'rms_emit2']
emit_units = ['m*rad', 'm*m*rad*rad']
for val, unit in zip(emit_data, emit_units):
data_set = group.create_dataset(val, (length, 3), dtype='f')
data_set[...] = getattr(self, val)
data = ['lost', 'powlost']
units = ['', 'W']
for val, unit in zip(data, units):
data_set = group.create_dataset(val, (length, self.Nrun), dtype='f')
data_set[...] = getattr(self, val)
class remote_data_merger:
def __init__(self, base='.'):
import os
if os.path.exists(filepath):
raise ValueError("Could not find file " + filepath)
if fname not in self._files:
self._files.append(fname)
'''
Creates a string to be written to file
each line is a list.
If filename is given, writes directly to output file.
'''
if self._files:
for f in self._files:
string = open(f, 'r').read()
split = string.split('$$$')
if split[9] != 'Data_Error':
raise ValueError("Magic problem, please complain to Yngve")
h1 = [thisdata[0] + " (std in paranthesis)"]
h2 = thisdata[2:10]
d1.append(thisdata[1].split())
d2.append(thisdata[10])
d3.append(thisdata[11])
# fix d1:
for i in range(len(d1)):
for j in range(len(d1[0])):
d1[i][j] = float(d1[i][j])
d1 = np.array(d1)
means = d1.mean(axis=0)
stds = d1.std(axis=0)
d1 = []
for i in range(len(stds)):
for i in range(len(stds)):
if stds[i] / means[i] > 1e-8:
d1.append('%f(%f)' % (means[i], stds[i]))
else: # error is 0
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
class envDiag():
'''
Read ENV_diag1.dat file
This contains e.g. the absolute phase at each diag
For now we do not read in all info from the file,
so feel free to request or add anything else you would like.
'''
def __init__(self, filename):
self.filename = filename
self.elements = {}
# Needed to get an ordered dictionary:
self._elementList = []
self._readAsciiFile()
self.units = {}
self._setUnits()
def _readAsciiFile(self):
'''
Read the file
'''
current = None
for line in open(self.filename, 'r'):
lsp = line.split()
if lsp[0] == 'DIAG':
self.elements[int(lsp[2])] = {}
self._elementList.append(int(lsp[2]))
current = self.elements[int(lsp[2])]
current['loc'] = float(lsp[4])
elif lsp[0] == 'Ibeam:':
current['current'] = float(lsp[1])
elif lsp[0] == 'Positions':
current['phase'] = float(lsp[5])
current['energy'] = float(lsp[6])
elif lsp[0] == 'RMS':
current['x_rms'] = float(lsp[4]) * 0.01
current['y_rms'] = float(lsp[5]) * 0.01
current['phase_rms'] = float(lsp[6])
current['energy_rms'] = float(lsp[7])
elif lsp[0] == 'Emittances':
current['emit_x'] = float(lsp[3])
current['emit_y'] = float(lsp[4])
current['emit_z'] = float(lsp[5])
elif lsp[0] == 'Emittances99':
current['emit99_x'] = float(lsp[3])
current['emit99_y'] = float(lsp[4])
current['emit99_z'] = float(lsp[5])
elif lsp[0] == 'Twiss':
if lsp[1] == 'Alpha' and lsp[3] == '(XXp,':
current['alpha_x'] = float(lsp[6])
current['alpha_y'] = float(lsp[7])
elif lsp[1] == 'Alpha' and lsp[3] == '(ZDp/p)':
current['alpha_z'] = float(lsp[5])
elif lsp[1] == 'Beta':
current['beta_x'] = float(lsp[5])
current['beta_y'] = float(lsp[6])
current['beta_z'] = float(lsp[7])
def _setUnits(self):
'''
Set the units for each element in the element dictionary
(empty string for all undefined)
'''
for key in ['loc', 'x_rms', 'y_rms']:
self.units[key] = 'm'
for key in ['emit_x', 'emit_y', 'emit_z', 'emit99_x', 'emit99_y', 'emit99_z' ]:
self.units[key] = 'Pi.mm.mrad'
for key in ['current' ]:
self.units[key] = 'mA'
for key in ['energy', 'energy_rms' ]:
self.units[key] = 'MeV'
for key in ['phase', 'phase_rms' ]:
self.units[key] = 'deg'
for key in ['beta_x', 'beta_y', 'beta_z' ]:
self.units[key] = 'mm/mrad'
for element in self.elements:
for key in self.elements[element]:
if key not in self.units:
self.units[key] = ''
def printTable(self):
'''
Make a pretty print of the content
'''
first = True
rjust = 12
for ekey in self._elementList:
element = self.elements[ekey]
# Print header if this is the first element..
if first:
keys = [key for key in element]
print('#', end = ' ')
print('NUM'.rjust(rjust), end = ' ')
for key in keys:
print(key.rjust(rjust), end = ' ')
print()
print('#', end = ' ')
print(''.rjust(rjust), end = ' ')
for key in keys:
print(self.units[key].rjust(rjust), end = ' ')
print()
first = False
print(' ' + str(ekey).rjust(rjust), end = ' ')
for key in keys:
num = element[key]
if isinstance(num, float):
strnum = '{:.5e}'.format(num)
else:
strnum = str(element[key])
print(strnum.rjust(rjust), end = ' ')
print()
def getElement(self, elementId):
'''
Returns the element dictionary for the given ID
'''
return self.elements[elementId]
def getElementAtLoc(self, location):
'''
Returns a list of elements at the location requested
'''
ret = []
for key in self.elements:
if abs(self.elements[key]['loc'] - location) < 1e-6:
ret.append(self.elements[key])
return ret
def getParameterFromAll(self, parameter):
'''
Returns a list containing the given parameter from all DIAGS,
ordered by the location of the DIAGs
'''
if parameter == 'NUM':
return self._elementList[:]
ret = []
for key in self._elementList:
ret.append(self.elements[key][parameter])
return ret
This class can also read tracewin.out (same format)
def __init__(self, filename):
self.filename = filename
self._readAsciiFile()
def _readAsciiFile(self):
import numpy
line = stream.readline()
if line.strip()[0] == '#':
self.columns = ['NUM'] + line.split()[1:]
self.data = numpy.loadtxt(stream)
class field_map:
'''
Class to read in the field map structures
WARNING: Work in progress!!
'''
def __init__(self, filename):
self._load_data(filename)
def _load_data(self, filename):
import os
import numpy
if not os.path.isfile(filename):
raise ValueError("Cannot find file {}".format(filename))
fin = open(filename, 'r')
l = fin.readline().split()
self.start = []
self.end = []
numindexes = []
while len(l) > 1:
numindexes.append(int(l[0]) + 1)
if len(l) == 2:
self.start.append(0.0)
self.end.append(float(l[1]))
else:
self.start.append(float(l[1]))
self.end.append(float(l[2]))
if len(self.start) == 1:
self.z = numpy.mgrid[self.start[0]:self.end[0]:numindexes[0]*1j]
print(new_z,self.z)
elif len(self.start) == 2:
self.z, self.x = numpy.mgrid[self.start[0]:self.end[0]:numindexes[0]*1j,
self.start[1]:self.end[1]:numindexes[1]*1j]
elif len(self.start) == 3:
self.z, self.x, self.y = numpy.mgrid[self.start[0]:self.end[0]:numindexes[0]*1j,
self.start[1]:self.end[1]:numindexes[1]*1j,
self.start[2]:self.end[2]:numindexes[2]*1j]
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
def get_flat_fieldmap(self):
totmapshape = 1
for i in self.map.shape:
totmapshape *= i
return self.map.reshape(totmapshape)
def interpolate(self, npoints: tuple, method='cubic'):
'''
Interpolate the map into a new mesh
Each value should be an integer with the number of mesh points in each dimension
intervals should be tuple-like with same number of elements
as the map dimension, e.g. [0.8,0.8] for 2D
Can also be a float if you want same interpolation factor in all planes
method can be 'linear', 'nearest' or 'cubic'
'''
import numpy
from scipy.interpolate import griddata
values=self.map.flatten()
if len(self.start) == 1:
points=self.z[:]
self.z = numpy.mgrid[self.start[0]:self.end[0]:npoints[0]*1j]
self.map=griddata(points,values,self.z)
if len(self.start) == 2:
points=numpy.array([self.z.flatten(), self.x.flatten()]).transpose()
self.z, self.x = numpy.mgrid[self.start[0]:self.end[0]:npoints[0]*1j,
self.start[1]:self.end[1]:npoints[1]*1j]
self.map=griddata(points,values,(self.z,self.x))
if len(self.start) == 3:
points=numpy.array([self.z.flatten(), self.x.flatten(), self.y.flatten()]).transpose()
self.z, self.x, self.y = numpy.mgrid[self.start[0]:self.end[0]:npoints[0]*1j,
self.start[1]:self.end[1]:npoints[1]*1j,
self.start[2]:self.end[2]:npoints[2]*1j]
self.map=griddata(points,values,(self.z,self.x,self.y))
self.header[0]=npoints[0]-1
self.header[2]=npoints[1]-1
self.header[5]=npoints[2]-1
def savemap(self, filename):
fout = open(filename, 'w')
for n, s in zip(self.map.shape, self.size):
fout.write('{} {}\n'.format(n - 1, s))
fout.write('{}\n'.format(self.norm))
totmapshape *= i
data = self.map.reshape(totmapshape)
for j in data:
fout.write('{}\n'.format(j))