Skip to content

Commit b321354

Browse files
committed
Remove redundant code for element pv creation
The bend elements had their own code to create their pvs. Now they use the same code as for regular elements. I had to refactor the function a little bit to keep the same behaviour as before. This being that only 1 pv is created for all bend elements and each new element adds its index to a list stored in this pv
1 parent 56a5cb8 commit b321354

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

src/virtac/atip_server.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -163,48 +163,22 @@ def _create_records(self, limits_csv, disable_emittance):
163163
float(line["lower"]),
164164
int(line["precision"]),
165165
)
166+
166167
bend_in_record = None
167168
for element in self.lattice:
168-
if element.type_.upper() == "BEND":
169-
# Create bends only once as they all share a single PV.
170-
if bend_in_record is None:
171-
value = element.get_value(
172-
"b0", units=pytac.ENG, data_source=pytac.SIM
173-
)
174-
get_pv = element.get_pv_name("b0", pytac.RB)
175-
upper, lower, precision = limits_dict.get(
176-
get_pv, (None, None, None)
177-
)
178-
builder.SetDeviceName(get_pv.split(":", 1)[0])
179-
in_record = builder.aIn(
180-
get_pv.split(":", 1)[1],
181-
LOPR=lower,
182-
HOPR=upper,
183-
PREC=precision,
184-
MDEL="-1",
185-
initial_value=value,
186-
)
187-
set_pv = element.get_pv_name("b0", pytac.SP)
188-
upper, lower, precision = limits_dict.get(
189-
set_pv, (None, None, None)
190-
)
191-
builder.SetDeviceName(set_pv.split(":", 1)[0])
192-
out_record = builder.aOut(
193-
set_pv.split(":", 1)[1],
194-
LOPR=lower,
195-
HOPR=upper,
196-
PREC=precision,
197-
initial_value=value,
198-
on_update_name=self._on_update,
199-
always_update=True,
200-
)
201-
self._in_records[in_record] = ([element.index], "b0")
202-
self._out_records[out_record] = in_record
203-
bend_in_record = in_record
204-
else:
169+
# There is only 1 bend PV in the lattice, if it has already been defined and
170+
# we have another bend element, then just register this element with the
171+
# existing pv. Otherwise create a new PV for the element
172+
if element.type_.upper() == "BEND" and bend_in_record is not None:
173+
# record[0] may be an int or a list of ints
174+
if isinstance(self._in_records[bend_in_record][0], list):
205175
self._in_records[bend_in_record][0].append(element.index)
176+
else:
177+
self._in_records[bend_in_record] = [
178+
self._in_records[bend_in_record][0],
179+
element.index,
180+
]
206181
else:
207-
# Create records for all other families.
208182
for field in element.get_fields()[pytac.SIM]:
209183
value = element.get_value(
210184
field, units=pytac.ENG, data_source=pytac.SIM
@@ -223,6 +197,7 @@ def _create_records(self, limits_csv, disable_emittance):
223197
initial_value=value,
224198
)
225199
self._in_records[in_record] = (element.index, field)
200+
226201
try:
227202
set_pv = element.get_pv_name(field, pytac.SP)
228203
except HandleException:
@@ -242,6 +217,9 @@ def _create_records(self, limits_csv, disable_emittance):
242217
always_update=True,
243218
)
244219
self._out_records[out_record] = in_record
220+
if element.type_.upper() == "BEND" and bend_in_record is None:
221+
bend_in_record = in_record
222+
245223
# Now for lattice fields.
246224
lat_fields = self.lattice.get_fields()
247225
lat_fields = set(lat_fields[pytac.LIVE]) & set(lat_fields[pytac.SIM])

0 commit comments

Comments
 (0)