ess.TraceWin

Main description of this module is not written yet

class ess.TraceWin.density(filename, envelope=None)[source]

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, ..

merge(objects)[source]

Merge with list of objects

savetohdf(filename='Density.h5', group='TraceWin', force=False)[source]

Saves data to HDF5

class ess.TraceWin.density_file(filename, envelope=None)[source]
class ess.TraceWin.dst(filename=None, freq=352.21, mass=938.272, Ib=0.0)[source]

Simple class to read in a TraceWin distribution file

Class afterwards hold the following dictionary items: - x [m] - xp [rad] - y [m] - yp [rad] - phi [rad] - E [MeV] (kinetic energy)

append(x=0.0, xp=0.0, y=0.0, yp=0.0, E=0.0, phi=0.0)[source]

Append one particle to the distribution

  • Kinetic Energy in MeV

  • x,y in m

  • xp,yp in rad

  • phi in rad

append_many(array)[source]

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

combine_dst(other)[source]

Appends the particles from another dst object to this one

remove(i=None)[source]

Removes all particles from the distribution, or the line specified by i

save(filename, toutatis=False)[source]

Save the distribution file so it can be read by TraceWin again

Parameters
  • filename – Name of file

  • toutatis – Include 7th column of zeros

Stolen from Ryoichi’s func.py (with permission)

subplot(index, x, y=None, nb=100, mask=None, xlim=None, ylim=None, norm=None, **kwargs)[source]

Create a subplot histogram similar to TraceWin.

Parameters
  • index – The plot index (e.g. 221, 222 etc)

  • x – The first coordinate

  • y – [optional] second coordinate if 2D histogram

  • nb – Number of bins

  • mask – Optional mask (see example)

  • xlim – Limit x range in histogram

  • ylim – Limit y range in 2D histogram

  • norm – Normalization factor

  • kwargs – Arguments passed to plt.hist2d, or plt.hist if 1D plot

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()
class ess.TraceWin.envDiag(filename)[source]

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.

getElement(elementId)[source]

Returns the element dictionary for the given ID

getElementAtLoc(location)[source]

Returns a list of elements at the location requested

getParameterFromAll(parameter)[source]

Returns a list containing the given parameter from all DIAGS, ordered by the location of the DIAGs

printTable()[source]

Make a pretty print of the content

class ess.TraceWin.envelope(filename)[source]

Read an envelope file Create one by saving envelope data plot to ascii

Example:

from ess import TraceWin
from matplotlib import pyplot as plt
data = TraceWin.envelope('envelope.txt')
print(data.keys())
for key in data:
    print(key, data.unit(key))
    if 'rms_' in key:
        plt.plot(data['position'], data[key]/max(data[key]), label=f"{key} [{data.unit(key)}]")
plt.legend()
plt.xlabel(f"Position [{data.unit('position')}]")
plt.show()
unit(column)[source]

TODO gam-1

class ess.TraceWin.field_map(filename)[source]

Class to read in the field map structures

WARNING: Work in progress!!

interpolate(npoints: tuple, method='cubic')[source]

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’

class ess.TraceWin.partran(filename)[source]

Read partran1.out files..

This class can also read tracewin.out (same format)

class ess.TraceWin.plt(filename, flag_remove_loss=True)[source]

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] - mc2 [MeV] - Nelp [m] (locations)

each plt[i], where i is element number, holds:
  • Zgen [cm] (location)

  • phase0 [deg] (ref phase)

  • wgen [MeV] (ref energy)

  • x [array, m]

  • xp [array, rad]

  • y [array, m]

  • yp [array, rad]

  • phi [array, rad]

  • E [array, MeV]

  • l [array] (is lost)

Example:

plt=ess.TraceWin.plt('calc/dtl1.plt')
for i in [97,98]:
    data=plt[i]
    if data:
      print(data['x'])
calc_avg()[source]

Calculates averages of 6D coordinates at each element, such that e.g. self.avg[“x”] gives average X at each location.

Units: m, rad, MeV

calc_minmax(pmin=5, pmax=95)[source]

Calculates min/max values of beam coordinates in percentile, pmin is lower and pmax upper.

Units: cm

calc_rel()[source]

Calculates relativistic gamma/beta at each position, based on AVERAGE beam energy (NOT necessarily reference)

calc_s()[source]

Generates self.s which holds the position of each element in metres

calc_sigma()[source]

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

calc_std()[source]

Calculates the beam sizes

calc_twiss()[source]

Calculates emittance, beta, alfa, gamma for each plane, x-xp, y-yp, and E-phi

get_dst(index)[source]

Returns the dst corresponding to the given index

save_dst(index, filename)[source]

Saves the dst at the specified index to file

Returns the same dst object.

class ess.TraceWin.project(project_fname=None, settings_fname=None)[source]

Read and modify TraceWin project files

Example:

p = project('SPK.ini')
for diff in p.compare_to('MEBT.ini'):
    print(diff)
p.set('main:beam1_energy', 89e6)
p.save()
check_rule(rule=None, fail_on_err=False, fix_if_possible=False)[source]

Validate that we still obey the rule if rule is not given, check all rules

compare_to(other)[source]

Compare the settings of this file to a different project file

Parameters

other – project object, or file path to other project file

get(parameter)[source]

Get the setting of the parameter

get_options(parameter)[source]

Get the possible options for parameter as specified in the reference file

get_type(parameter)[source]

Get the type of parameter as specified in the reference file

d : double value i : integer value, int:list : integer representing a list selection bool:list : booleans representing a list selection

For int:list and bool:list, recommend to use get_options() to figure out how to set as a user.

print_settings(settings=None)[source]

Print the settings given, or all by default

Parameters

settings – List of the settings to print

save(fname=None)[source]

Save the project file

If fname not given, overwrite original file

set(parameter, value)[source]

Set the new value for parameter