pytac.lattice#

Representation of a lattice object which contains all the elements of the machine.

Members

EpicsLattice

EPICS-aware lattice class.

Lattice

Representation of a lattice.

class pytac.lattice.Lattice(name: str, symmetry: int | None = None)[source]#

Representation of a lattice.

Represents a lattice object that contains all elements of the ring. It has a name and a control system to be used for unit conversion.

Attributes:

name#

The name of the lattice.

symmetry#

The symmetry of the lattice (the number of cells).

Parameters:
  • name – The name of the lattice.

  • symmetry – The symmetry of the lattice (the number of cells).

Methods:

property cell_length: float | None#

The average length of a cell in the lattice.

property cell_bounds: list[int] | None#

The indexes of elements in which a cell boundary occurs.

Examples

A lattice of 5 equal length elements with 2 fold symmetry would return [1, 4, 5] 1 - because it is the start of the first cell. 4 - because it is the first element in the second cell as the boundary between the first and second cells occurs halfway into the length of element 3. 5 - (len(lattice)) because it is the end of the second (last) cell.

set_data_source(data_source: DataSource, data_source_type: str) None[source]#

Add a data source to the lattice.

Parameters:
  • data_source – the data source to be set.

  • data_source_type – the type of the data source being set: pytac.LIVE or pytac.SIM.

get_fields()[source]#

Get the fields defined on the lattice.

Includes all fields defined by all data sources.

Returns:

A dictionary of all the fields defined on the lattice,

separated by data source(key).

Return type:

dict

add_device(field, device, uc)[source]#

Add device and unit conversion objects to a given field.

A DeviceDataSource must be set before calling this method, this defaults to pytac.LIVE as that is the only data source that currently uses devices.

Parameters:
  • field (str) – The key to store the unit conversion and device objects.

  • device (Device) – The device object used for this field.

  • uc (UnitConv) – The unit conversion object used for this field.

Raises:

DataSourceException – if no DeviceDataSource is set.

get_device(field)[source]#

Get the device for the given field.

A DeviceDataSource must be set before calling this method, this defaults to pytac.LIVE as that is the only data source that currently uses devices.

Parameters:

field (str) – The lookup key to find the device on the lattice.

Returns:

The device on the given field.

Return type:

Device

Raises:

DataSourceException – if no DeviceDataSource is set.

get_unitconv(field)[source]#

Get the unit conversion option for the specified field.

Parameters:

field (str) – The field associated with this conversion.

Returns:

The object associated with the specified field.

Return type:

UnitConv

Raises:

FieldException – if no unit conversion object is present.

set_unitconv(field, uc)[source]#

Set the unit conversion option for the specified field.

Parameters:
  • field (str) – The field associated with this conversion.

  • uc (UnitConv) – The unit conversion object to be set.

get_value(field, handle='readback', units='default', data_source='default', throw=True)[source]#

Get the value for a field on the lattice.

Returns the value of a field on the lattice. This value is uniquely identified by a field and a handle. The returned value is either in engineering or physics units. The data_source flag returns either real or simulated values.

Parameters:
  • field (str) – The requested field.

  • handle (str) – pytac.SP or pytac.RB.

  • units (str) – pytac.ENG or pytac.PHYS returned.

  • data_source (str) – pytac.LIVE or pytac.SIM.

  • throw (bool) – On failure: if True, raise ControlSystemException; if False, return None and log a warning.

Returns:

The value of the requested field

Return type:

float

Raises:
set_value(field, value, units='default', data_source='default', throw=True)[source]#

Set the value for a field.

This value can be set on the machine or the simulation.

Parameters:
  • field (str) – The requested field.

  • value (float) – The value to set.

  • units (str) – pytac.ENG or pytac.PHYS.

  • data_source (str) – pytac.LIVE or pytac.SIM.

  • throw (bool) – On failure: if True, raise ControlSystemException: if False, log a warning.

Raises:
get_length()[source]#

Returns the length of the lattice, in meters.

Returns:

The length of the lattice (m).

Return type:

float

add_element(element)[source]#

Append an element to the lattice and update its lattice reference.

Parameters:

element (Element) – element to append.

get_elements(family=None, cell=None)[source]#

Get the elements of a family from the lattice.

If no family is specified it returns all elements. Elements are returned in the order they exist in the ring.

Parameters:
  • family (str) – requested family.

  • cell (int) – restrict elements to those in the specified cell.

Returns:

list containing all elements of the specified family.

Return type:

list

Raises:

ValueError – if there are no elements in the specified cell or family.

get_all_families()[source]#

Get all families of elements in the lattice.

Returns:

all defined families.

Return type:

set

get_family_s(family)[source]#

Get s positions for all elements from the same family.

Parameters:

family (str) – requested family.

Returns:

list of s positions for each element.

Return type:

list

get_element_devices(family, field)[source]#

Get devices for a specific field for elements in the specfied family.

Typically all elements of a family will have devices associated with the same fields - for example, BPMs each have a device for fields ‘x’ and ‘y’.

Parameters:
  • family (str) – family of elements.

  • field (str) – field specifying the devices.

Returns:

devices for specified family and field.

Return type:

list

get_element_device_names(family, field)[source]#

Get the names for devices attached to a specific field for elements in the specfied family.

Typically all elements of a family will have devices associated with the same fields - for example, BPMs each have a device for fields ‘x’ and ‘y’.

Parameters:
  • family (str) – family of elements.

  • field (str) – field specifying the devices.

Returns:

device names for specified family and field.

Return type:

list

get_element_values(family, field, handle='readback', units='default', data_source='default', throw=True, dtype=None)[source]#

Get the value of the given field for all elements in the given family in the lattice.

Parameters:
  • family (str) – family of elements to request the values of.

  • field (str) – field to request values for.

  • handle (str) – pytac.RB or pytac.SP.

  • units (str) – pytac.ENG or pytac.PHYS.

  • data_source (str) – pytac.LIVE or pytac.SIM.

  • throw (bool) – On failure: if True, raise ControlSystemException; if False, None will be returned for any PV that fails and a warning will be logged.

  • dtype (numpy.dtype) – if None, return a list. If not None, return a numpy array of the specified type.

Returns:

The requested values.

Return type:

list or numpy.array

set_element_values(family, field, values, units='default', data_source='default', throw=True)[source]#

Set the value of the given field for all elements in the given family in the lattice to the given values.

Parameters:
  • family (str) – family of elements on which to set values.

  • field (str) – field to set values for.

  • values (sequence) – A list of values to assign.

  • units (str) – pytac.ENG or pytac.PHYS.

  • data_source (str) – pytac.LIVE or pytac.SIM.

  • throw (bool) – On failure, if True raise ControlSystemException, if False return a list of True and False values corresponding to successes and failures and log a warning for each PV that fails.

Raises:

IndexError – if the given list of values doesn’t match the number of elements in the family.

set_default_units(units: str) None[source]#

Sets the default unit type for the lattice and all its elements.

Parameters:

default_units – The default unit type to be set across the entire lattice, pytac.ENG or pytac.PHYS.

Raises:

UnitsException – if specified default unit type is not a valid unit type.

set_default_data_source(data_source_type: str) None[source]#

Sets the default data source for the lattice and all its elements.

Parameters:

data_source_type – The default data source to be set across the entire lattice, pytac.LIVE or pytac.SIM.

Raises:

DataSourceException – if specified default data source is not a valid data source.

get_default_units()[source]#

Get the default unit type, pytac.ENG or pytac.PHYS.

Returns:

the default unit type for the entire lattice.

Return type:

str

get_default_data_source()[source]#

Get the default data source, pytac.LIVE or pytac.SIM.

Returns:

the default data source for the entire lattice.

Return type:

str

convert_family_values(family, field, values, origin, target)[source]#

Convert the given values according to the given origin and target units, using the unit conversion objects for the given field on the elements in the given family.

Parameters:
  • family (str) – the family of elements which the values belong to.

  • field (str) – the field on the elements which the values are from.

  • values (sequence) – values to be converted.

  • origin (str) – pytac.ENG or pytac.PHYS.

  • target (str) – pytac.ENG or pytac.PHYS.

class pytac.lattice.EpicsLattice(name, epics_cs, symmetry=None)[source]#

EPICS-aware lattice class.

Allows efficient get_element_values() and set_element_values() methods, and adds get_pv_names() method.

Attributes:

name#

The name of the lattice.

Type:

str

symmetry#

The symmetry of the lattice (the number of cells).

Type:

int

Parameters:
  • name (str) – The name of the epics lattice.

  • epics_cs (ControlSystem) – The control system used to store the values on a PV.

  • symmetry (int) – The symmetry of the lattice (the number of cells).

Methods:

get_pv_name(field, handle)[source]#

Get the PV name for a specific field, and handle on this lattice.

Parameters:
  • field (str) – The requested field.

  • handle (str) – pytac.RB or pytac.SP.

Returns:

The readback or setpoint PV for the specified field.

Return type:

str

get_element_pv_names(family, field, handle)[source]#

Get the PV names for the given field, and handle, on all elements in the given family in the lattice.

Assume that the elements are EpicsElements that have the get_pv_name() method.

Parameters:
  • family (str) – The requested family.

  • field (str) – The requested field.

  • handle (str) – pytac.RB or pytac.SP.

Returns:

A list of PV names, strings.

Return type:

list

get_element_values(family, field, handle='readback', units='default', data_source='default', throw=True, dtype=None)[source]#

Get the value of the given field for all elements in the given family in the lattice.

Parameters:
  • family (str) – family of elements to request the values of.

  • field (str) – field to request values for.

  • handle (str) – pytac.RB or pytac.SP.

  • units (str) – pytac.ENG or pytac.PHYS.

  • data_source (str) – pytac.LIVE or pytac.SIM.

  • throw (bool) – On failure: if True, raise ControlSystemException; if False, None will be returned for any PV that fails and a warning will be logged.

  • dtype (numpy.dtype) – if None, return a list. If not None, return a numpy array of the specified type.

Returns:

The requested values.

Return type:

list or numpy.array

set_element_values(family, field, values, units='default', data_source='default', throw=True)[source]#

Set the value of the given field for all elements in the given family in the lattice to the given values.

Parameters:
  • family (str) – family of elements on which to set values.

  • field (str) – field to set values for.

  • values (sequence) – A list of values to assign.

  • units (str) – pytac.ENG or pytac.PHYS.

  • data_source (str) – pytac.LIVE or pytac.SIM.

  • throw (bool) – On failure: if True, raise ControlSystemException: if False, log a warning.

Raises:

IndexError – if the given list of values doesn’t match the number of elements in the family.