Newer
Older
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
'''
Read partran1.out files..
'''
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]
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
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))