Skip to content

Commit 53689ce

Browse files
Full example with BPMs
1 parent b557d1a commit 53689ce

File tree

7 files changed

+90
-103
lines changed

7 files changed

+90
-103
lines changed

pyaml/bpm/bpm_model.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
from numpy.typing import NDArray
55

6+
from ..configuration.catalog import Catalog
67
from ..control.deviceaccess import DeviceAccess
78

89

@@ -13,7 +14,7 @@ class BPMModel(metaclass=ABCMeta):
1314
"""
1415

1516
@abstractmethod
16-
def get_pos_devices(self) -> list[DeviceAccess | None]:
17+
def get_pos_devices(self, name: str, catalog: Catalog) -> list[DeviceAccess | None]:
1718
"""
1819
Get device handles used for position reading
1920
@@ -25,7 +26,7 @@ def get_pos_devices(self) -> list[DeviceAccess | None]:
2526
pass
2627

2728
@abstractmethod
28-
def get_tilt_device(self) -> DeviceAccess | None:
29+
def get_tilt_device(self, name: str, catalog: Catalog) -> DeviceAccess | None:
2930
"""
3031
Get device handle used for tilt access
3132
@@ -37,7 +38,9 @@ def get_tilt_device(self) -> DeviceAccess | None:
3738
pass
3839

3940
@abstractmethod
40-
def get_offset_devices(self) -> list[DeviceAccess | None]:
41+
def get_offset_devices(
42+
self, name: str, catalog: Catalog
43+
) -> list[DeviceAccess | None]:
4144
"""
4245
Get device handles used for offset access
4346

pyaml/bpm/bpm_simple_model.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pyaml.bpm.bpm_model import BPMModel
66

77
from ..common.element import __pyaml_repr__
8+
from ..configuration.catalog import Catalog
89
from ..control.deviceaccess import DeviceAccess
910

1011
# Define the main class name for this module
@@ -31,8 +32,6 @@ class ConfigModel(BaseModel):
3132

3233
model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid")
3334

34-
x_pos: DeviceAccess | None
35-
y_pos: DeviceAccess | None
3635
x_pos_index: int | None = None
3736
y_pos_index: int | None = None
3837

@@ -45,39 +44,44 @@ class BPMSimpleModel(BPMModel):
4544

4645
def __init__(self, cfg: ConfigModel):
4746
self._cfg = cfg
48-
self.__x_pos = cfg.x_pos
49-
self.__y_pos = cfg.y_pos
5047

51-
def get_pos_devices(self) -> list[DeviceAccess | None]:
48+
def get_pos_devices(self, name: str, catalog: Catalog) -> list[DeviceAccess | None]:
5249
"""
5350
Get device handles used for position reading
5451
5552
Returns
5653
-------
5754
list[DeviceAccess]
58-
Array of DeviceAcess
55+
Array of DeviceAccess
5956
"""
60-
return [self.__x_pos, self.__y_pos]
61-
62-
def get_tilt_device(self) -> DeviceAccess | None:
57+
if self.is_pos_indexed():
58+
dev = catalog.get_one(name)
59+
pos_devices = [dev, dev]
60+
else:
61+
pos_devices = catalog.get_many(name)[:2]
62+
return pos_devices
63+
64+
def get_tilt_device(self, name: str, catalog: Catalog) -> DeviceAccess | None:
6365
"""
6466
Get device handle used for tilt access
6567
6668
Returns
6769
-------
6870
list[DeviceAccess]
69-
Array of DeviceAcess
71+
Array of DeviceAccess
7072
"""
7173
return None
7274

73-
def get_offset_devices(self) -> list[DeviceAccess | None]:
75+
def get_offset_devices(
76+
self, name: str, catalog: Catalog
77+
) -> list[DeviceAccess | None]:
7478
"""
7579
Get device handles used for offset access
7680
7781
Returns
7882
-------
7983
list[DeviceAccess]
80-
Array of DeviceAcess
84+
Array of DeviceAccess
8185
"""
8286
return [None, None]
8387

pyaml/bpm/bpm_tiltoffset_model.py

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pyaml.bpm.bpm_simple_model import BPMSimpleModel
77

88
from ..common.element import __pyaml_repr__
9+
from ..configuration.catalog import Catalog
910
from ..control.deviceaccess import DeviceAccess
1011

1112
# Define the main class name for this module
@@ -20,27 +21,18 @@ class ConfigModel(BaseModel):
2021
2122
Parameters
2223
----------
23-
x_pos : DeviceAccess, optional
24-
Horizontal position device
25-
y_pos : DeviceAccess, optional
26-
Vertical position device
27-
x_offset : DeviceAccess, optional
28-
Horizontal BPM offset device
29-
y_offset : DeviceAccess, optional
30-
Vertical BPM offset device
31-
tilt : DeviceAccess, optional
32-
BPM tilt device
24+
x_pos_index : int, optional
25+
Index in the array when specified, otherwise scalar
26+
value is expected
27+
y_pos_index : int, optional
28+
Index in the array when specified, otherwise scalar
29+
value is expected
3330
"""
3431

3532
model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid")
3633

37-
x_pos: DeviceAccess | None
38-
y_pos: DeviceAccess | None
3934
x_pos_index: int | None = None
4035
y_pos_index: int | None = None
41-
x_offset: DeviceAccess | None
42-
y_offset: DeviceAccess | None
43-
tilt: DeviceAccess | None
4436

4537

4638
class BPMTiltOffsetModel(BPMSimpleModel):
@@ -51,44 +43,34 @@ class BPMTiltOffsetModel(BPMSimpleModel):
5143

5244
def __init__(self, cfg: ConfigModel):
5345
super().__init__(cfg)
54-
self.__x_pos = cfg.x_pos
55-
self.__y_pos = cfg.y_pos
56-
self.__x_offset = cfg.x_offset
57-
self.__y_offset = cfg.y_offset
58-
self.__tilt = cfg.tilt
5946

60-
def get_pos_devices(self) -> list[DeviceAccess | None]:
61-
"""
62-
Get device handles used for position reading
63-
64-
Returns
65-
-------
66-
list[DeviceAccess]
67-
Array of DeviceAcess
68-
"""
69-
return [self.__x_pos, self.__y_pos]
70-
71-
def get_tilt_device(self) -> DeviceAccess | None:
47+
def get_tilt_device(self, name: str, catalog: Catalog) -> DeviceAccess | None:
7248
"""
7349
Get device handle used for tilt access
7450
7551
Returns
7652
-------
7753
DeviceAccess
78-
DeviceAcess
54+
The DeviceAccess for tilt
7955
"""
80-
return self.__tilt
56+
if catalog.has_reference(name + "/tilt"):
57+
return catalog.get_one(name + "/tilt")
58+
return None
8159

82-
def get_offset_devices(self) -> list[DeviceAccess | None]:
60+
def get_offset_devices(
61+
self, name: str, catalog: Catalog
62+
) -> list[DeviceAccess | None]:
8363
"""
8464
Get device handles used for offset access
8565
8666
Returns
8767
-------
8868
list[DeviceAccess]
89-
Array of DeviceAcess
69+
Array of 2 DeviceAccess: [x_offset, y_offset]
9070
"""
91-
return [self.__x_offset, self.__y_offset]
71+
if catalog.has_reference(name + "/offsets"):
72+
return catalog.get_many(name + "/offsets")[:2]
73+
return [None, None]
9274

9375
def __repr__(self):
9476
return __pyaml_repr__(self)

pyaml/common/element_holder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def fill_bpm_array(self, arrayName: str, elementNames: list[str]):
190190
arrayName, elementNames, self.get_bpm, BPMArray, self.__BPM_ARRAYS
191191
)
192192

193-
def get_bpm(self, name: str) -> Element:
193+
def get_bpm(self, name: str) -> BPM:
194194
return self.__get("BPM", name, self.__BPMS)
195195

196196
def add_bpm(self, bpm: BPM):

pyaml/configuration/catalog.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,21 @@ def find(self, pattern: str) -> dict[str, CatalogValue]:
153153
def keys(self) -> list[str]:
154154
"""Return all catalog reference keys."""
155155
return list(self._entries.keys())
156+
157+
# ------------------------------------------------------------------
158+
159+
def has_reference(self, reference: str) -> bool:
160+
"""
161+
Return True if the reference exists in the catalog.
162+
163+
Parameters
164+
----------
165+
reference : str
166+
Catalog reference key.
167+
168+
Returns
169+
-------
170+
bool
171+
True if the reference exists, False otherwise.
172+
"""
173+
return reference in self._entries

pyaml/control/controlsystem.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ def fill_device(self, elements: list[Element]):
253253
self.add_magnet(m)
254254

255255
elif isinstance(e, BPM):
256-
hDev = e.model.get_pos_devices()[0]
257-
vDev = e.model.get_pos_devices()[1]
258-
tiltDev = e.model.get_tilt_device()
259-
hOffsetDev = e.model.get_offset_devices()[0]
260-
vOffsetDev = e.model.get_offset_devices()[1]
256+
hDev = e.model.get_pos_devices(e.get_name(), self._catalog)[0]
257+
vDev = e.model.get_pos_devices(e.get_name(), self._catalog)[1]
258+
tiltDev = e.model.get_tilt_device(e.get_name(), self._catalog)
259+
hOffsetDev = e.model.get_offset_devices(e.get_name(), self._catalog)[0]
260+
vOffsetDev = e.model.get_offset_devices(e.get_name(), self._catalog)[1]
261261
ahDev = self.attach_indexed(hDev, e.model.x_pos_index())
262262
avDev = self.attach_indexed(vDev, e.model.y_pos_index())
263263
atiltDev = self.attach_indexed(tiltDev, e.model.tilt_index())

tests/config/bpms.yaml

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,64 +16,20 @@ devices:
1616
name: BPM_C01-01
1717
model:
1818
type: pyaml.bpm.bpm_tiltoffset_model
19-
x_pos:
20-
type: tango.pyaml.attribute_read_only
21-
attribute: srdiag/bpm/c01-01/SA_HPosition
22-
unit: mm
23-
y_pos:
24-
type: tango.pyaml.attribute_read_only
25-
attribute: srdiag/bpm/c01-01/SA_VPosition
26-
unit: mm
27-
x_offset:
28-
type: tango.pyaml.attribute
29-
attribute: srdiag/bpm/c01-01/HOffset
30-
unit: mm
31-
y_offset:
32-
type: tango.pyaml.attribute
33-
attribute: srdiag/bpm/c01-01/VOffset
34-
unit: mm
35-
tilt:
36-
type: tango.pyaml.attribute
37-
attribute: srdiag/bpm/c01-01/Tilt_Angle
38-
unit: rad
3919
- type: pyaml.bpm.bpm
4020
name: BPM_C01-02
4121
model:
4222
type: pyaml.bpm.bpm_simple_model
43-
x_pos:
44-
type: tango.pyaml.attribute_read_only
45-
attribute: srdiag/bpm/c01-02/SA_HPosition
46-
unit: mm
47-
y_pos:
48-
type: tango.pyaml.attribute_read_only
49-
attribute: srdiag/bpm/c01-02/SA_VPosition
50-
unit: mm
5123
- type: pyaml.bpm.bpm
5224
name: BPM_C01-03
5325
model:
5426
type: pyaml.bpm.bpm_simple_model
55-
x_pos:
56-
type: tango.pyaml.attribute_read_only
57-
attribute: srdiag/bpm/c01-03/SA_HPosition
58-
unit: mm
59-
y_pos:
60-
type: tango.pyaml.attribute_read_only
61-
attribute: srdiag/bpm/c01-03/SA_VPosition
62-
unit: mm
6327
- type: pyaml.bpm.bpm
6428
name: BPM_C01-04
6529
model:
6630
type: pyaml.bpm.bpm_simple_model
6731
x_pos_index: 0
6832
y_pos_index: 1
69-
x_pos:
70-
type: tango.pyaml.attribute_read_only
71-
attribute: srdiag/bpm/c01-04/Position
72-
unit: mm
73-
y_pos:
74-
type: tango.pyaml.attribute_read_only
75-
attribute: srdiag/bpm/c01-04/Position
76-
unit: mm
7733
- type: pyaml.magnet.cfm_magnet
7834
name: SH1A-C01 #Name of the element in the lattice model
7935
mapping:
@@ -88,14 +44,38 @@ devices:
8844
control_system_catalog:
8945
type: pyaml.configuration.catalog
9046
refs:
47+
- type: pyaml.configuration.catalog_entry
48+
reference: BPM_C01-01
49+
devices:
50+
- type: tango.pyaml.attribute_read_only
51+
attribute: srdiag/bpm/c01-01/SA_HPosition
52+
unit: mm
53+
- type: tango.pyaml.attribute_read_only
54+
attribute: srdiag/bpm/c01-01/SA_VPosition
55+
unit: mm
56+
- type: pyaml.configuration.catalog_entry
57+
reference: BPM_C01-01/offsets
58+
devices:
59+
- type: tango.pyaml.attribute
60+
attribute: srdiag/bpm/c01-01/HOffset
61+
unit: mm
62+
- type: tango.pyaml.attribute
63+
attribute: srdiag/bpm/c01-01/VOffset
64+
unit: mm
65+
- type: pyaml.configuration.catalog_entry
66+
reference: BPM_C01-01/tilt
67+
device:
68+
type: tango.pyaml.attribute
69+
attribute: srdiag/bpm/c01-01/Tilt_Angle
70+
unit: rad
9171
- type: pyaml.configuration.catalog_entry
9272
reference: BPM_C01-02
9373
devices:
9474
- type: tango.pyaml.attribute_read_only
9575
attribute: srdiag/bpm/c01-02/SA_HPosition
9676
unit: mm
9777
- type: tango.pyaml.attribute_read_only
98-
attribute: srdiag/bpm/c01-02/SA_HPosition
78+
attribute: srdiag/bpm/c01-02/SA_VPosition
9979
unit: mm
10080
- type: pyaml.configuration.catalog_entry
10181
reference: BPM_C01-03

0 commit comments

Comments
 (0)