Skip to content

Commit

Permalink
Add support for basic pv mirroring.
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Nicholls committed Apr 25, 2019
1 parent 5e4d9d9 commit 3cc8c5b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 20 deletions.
1 change: 1 addition & 0 deletions atip/ease.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def trigger_calc(lattice):
if len(fields) != 0:
val = elem.get_value(fields[0], pytac.SP, data_source=pytac.SIM)
elem.set_value(fields[0], val, data_source=pytac.SIM)
print("Recalculation manually triggered.")
break


Expand Down
5 changes: 3 additions & 2 deletions ioc/atip_ioc_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
ring_mode = 'DIAD'

# Create PVs.
server = atip_server.ATIPServer(ring_mode, os.path.join(here, 'pv_limits.csv'),
os.path.join(here, 'feedback.csv'))
server = atip_server.ATIPServer(ring_mode, os.path.join(here, 'limits.csv'),
os.path.join(here, 'feedback.csv'),
os.path.join(here, 'mirrored.csv'))

# Add special case out record for SOFB to write to.
builder.SetDeviceName('CS-CS-MSTAT-01')
Expand Down
65 changes: 48 additions & 17 deletions ioc/atip_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytac
from pytac.device import BasicDevice
from pytac.exceptions import HandleException, FieldException
from softioc import builder
from softioc import builder, device


class ATIPServer(object):
Expand All @@ -30,7 +30,8 @@ class ATIPServer(object):
_in_records because they are all
readback only.
"""
def __init__(self, ring_mode, limits_csv, feedback_csv):
def __init__(self, ring_mode, limits_csv=None, feedback_csv=None,
mirror_csv=None):
"""
Args:
ring_mode (string): The ring mode to create the lattice in.
Expand All @@ -46,8 +47,14 @@ def __init__(self, ring_mode, limits_csv, feedback_csv):
self._out_records = {}
self._rb_only_records = []
self._feedback_records = {}
self._mirrored_records = {}
print("Starting record creation.")
self._create_records(limits_csv)
self._create_feedback_records(feedback_csv)
if feedback_csv is not None:
self._create_feedback_records(feedback_csv)
if mirror_csv is not None:
self._create_mirror_records(mirror_csv)
print("Finished creating all {0} records.".format(self.total_records))

@property
def total_records(self):
Expand All @@ -63,12 +70,15 @@ def update_pvs(self):
for rb_record in self._rb_only_records:
index, field = self._in_records[rb_record]
if index == 0:
rb_record.set(self.lattice.get_value(field, units=pytac.ENG,
data_source=pytac.SIM))
value = self.lattice.get_value(field, units=pytac.ENG,
data_source=pytac.SIM)
rb_record.set(value)
else:
element = self.lattice[index - 1]
rb_record.set(element.get_value(field, units=pytac.ENG,
data_source=pytac.SIM))
value = self.lattice[index-1].get_value(field, units=pytac.ENG,
data_source=pytac.SIM)
rb_record.set(value)
if rb_record.name in self._mirrored_records:
self._mirrored_records[rb_record.name].set(value)

def _create_records(self, limits_csv):
"""Create all the standard records from both lattice and element Pytac
Expand All @@ -86,11 +96,11 @@ def _create_records(self, limits_csv):
load the pv limits.
"""
limits_dict = {}
csv_reader = csv.DictReader(open(limits_csv))
for line in csv_reader:
limits_dict[line['pv']] = (float(line['upper']),
float(line['lower']))
print("Starting record creation.")
if limits_csv is not None:
csv_reader = csv.DictReader(open(limits_csv))
for line in csv_reader:
limits_dict[line['pv']] = (float(line['upper']),
float(line['lower']))
bend_set = False
for element in self.lattice:
if element.type_ == 'BEND':
Expand Down Expand Up @@ -162,7 +172,7 @@ def on_update(value, name=set_pv):

def _create_feedback_records(self, feedback_csv):
"""Create all the feedback records from the .csv file at the location
passed, see create_feedback_csv.py for more information.
passed, see create_csv.py for more information.
Args:
feedback_csv (string): The filepath to the .csv file from which to
Expand All @@ -184,7 +194,26 @@ def _create_feedback_records(self, feedback_csv):
bpm_enabled_record = builder.Waveform("ENABLED", NELM=N_BPM,
initial_value=[0] * N_BPM)
self._feedback_records[(0, "bpm_enabled")] = bpm_enabled_record
print("Finished creating all {0} records.".format(self.total_records))

def _create_mirror_records(self, mirror_csv):
all_in_records = (self._in_records.keys() +
self._feedback_records.values())
record_names = {rec.name: rec for rec in all_in_records}
csv_reader = csv.DictReader(open(mirror_csv))
for line in csv_reader:
prefix, pv = line['mirror'].split(':', 1)
builder.SetDeviceName(prefix)
if isinstance(record_names[line['original']]._RecordWrapper__device,
device.ai):
mirror = builder.aIn(pv, initial_value=float(line['value']))
elif isinstance(record_names[line['original']]._RecordWrapper__device,
device.longin):
mirror = builder.aIn(pv, initial_value=float(line['value']))
else:
raise TypeError("Type {0} doesn't currently support mirroring,"
" please only mirror aIn and longIn records."
.format(type(line['original']._RecordWrapper__device)))
self._mirrored_records[line['original']] = mirror

def _on_update(self, name, value):
"""The callback function passed to out records, it is called after
Expand All @@ -198,9 +227,11 @@ def _on_update(self, name, value):
"""
in_record = self._out_records[name]
index, field = self._in_records[in_record]
self.lattice[index - 1].set_value(field, value, units=pytac.ENG,
data_source=pytac.SIM)
self.lattice[index-1].set_value(field, value, units=pytac.ENG,
data_source=pytac.SIM)
in_record.set(value)
if in_record.name in self._mirrored_records:
self._mirrored_records[in_record.name].set(value)

def set_feedback_record(self, index, field, value):
"""Set a value to the feedback in records, possible fields are:
Expand Down
23 changes: 22 additions & 1 deletion ioc/create_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ def generate_pv_limits():
return data


def generate_mirrored_pvs():
lattice = atip.utils.loader()
data = [("original", "mirror", "value"),
("SR23C-DI-TMBF-01:X:TUNE:TUNE", "SR23C-DI-TMBF-01:TUNE:TUNE",
lattice.get_value("tune_x", pytac.RB)),
("SR23C-DI-TMBF-01:Y:TUNE:TUNE", "SR23C-DI-TMBF-02:TUNE:TUNE",
lattice.get_value("tune_y", pytac.RB)),
("SR-DI-EMIT-01:HEMIT", "SR-DI-EMIT-01:HEMIT_MEAN",
lattice.get_value("emittance_x", pytac.RB)),
("SR-DI-EMIT-01:VEMIT", "SR-DI-EMIT-01:VEMIT_MEAN",
lattice.get_value("emittance_y", pytac.RB))]
return data


def write_data_to_file(data, filename):
# Write the collected data to the .csv file.
here = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -104,7 +118,12 @@ def parse_arguments():
parser.add_argument(
"--limits",
help="Filename for output pv limits CSV file",
default="pv_limits.csv",
default="limits.csv",
)
parser.add_argument(
"--mirrored",
help="Filename for output pv limits CSV file",
default="mirrored.csv",
)
return parser.parse_args()

Expand All @@ -116,3 +135,5 @@ def parse_arguments():
write_data_to_file(data, args.feedback)
data = generate_pv_limits()
write_data_to_file(data, args.limits)
data = generate_mirrored_pvs()
write_data_to_file(data, args.mirrored)
File renamed without changes.
5 changes: 5 additions & 0 deletions ioc/mirrored.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
original,mirror,value
SR23C-DI-TMBF-01:X:TUNE:TUNE,SR23C-DI-TMBF-01:TUNE:TUNE,0.18903188633285595
SR23C-DI-TMBF-01:Y:TUNE:TUNE,SR23C-DI-TMBF-02:TUNE:TUNE,0.27693795412112365
SR-DI-EMIT-01:HEMIT,SR-DI-EMIT-01:HEMIT_MEAN,3.068016684803271
SR-DI-EMIT-01:VEMIT,SR-DI-EMIT-01:VEMIT_MEAN,7.979619384378855

0 comments on commit 3cc8c5b

Please sign in to comment.