1- from typing import Dict , List , Tuple
1+ from typing import TYPE_CHECKING , Optional , Tuple
22
33import numpy as np
44
5- from ..common .element_holder import ElementHolder
5+ if TYPE_CHECKING :
6+ from ..common .element_holder import ElementHolder
67from ..common .exception import PyAMLException
78
89
910class pySCInterface :
1011 def __init__ (
1112 self ,
12- element_holder : ElementHolder ,
13- bpm_array_name : str = "BPM" ,
14- hcorr_array_name : str = "HCorr" ,
15- vcorr_array_name : str = "VCorr" ,
16- rf_plant_name : str = "RF" ,
13+ element_holder : "ElementHolder" ,
14+ bpm_array_name : str ,
15+ rf_plant_name : Optional [str ] = None ,
1716 ):
1817 self .element_holder = element_holder
1918
2019 self .bpm_array = element_holder .get_bpms (bpm_array_name )
2120
22- # We could generalize to arbitrary arrays.
23- # Presently, pySC only uses set_many and get_many to set corrector strengths
24- # when doing orbit correction.
25- # Technically we don't need to define it, because we can ask for the trims to
26- # make for an orbit correction and then apply them in pyAML.
27- # Only get and set are used for other measurements.
28- self .hcorr_array_name = hcorr_array_name
29- self .hcorr_array = element_holder .get_magnets (hcorr_array_name )
30- self .hcorr_names = self .hcorr_array .names ()
31- self .hcorr_name_to_index = {
32- name : ii for ii , name in enumerate (self .hcorr_names )
33- }
34-
35- self .vcorr_array_name = vcorr_array_name
36- self .vcorr_array = element_holder .get_magnets (vcorr_array_name )
37- self .vcorr_names = self .vcorr_array .names ()
38- self .vcorr_name_to_index = {
39- name : ii for ii , name in enumerate (self .vcorr_names )
40- }
41-
4221 self .rf_plant_name = rf_plant_name
43- self .rf_plant = element_holder .get_rf_plant (self .rf_plant_name )
22+ if rf_plant_name is not None :
23+ self .rf_plant = element_holder .get_rf_plant (self .rf_plant_name )
24+ else :
25+ self .rf_plant = None
4426
4527 def get_orbit (self ) -> Tuple [np .array , np .array ]:
4628 # we should wait here somehow according to polling rate
@@ -56,112 +38,13 @@ def set(self, name: str, value: float) -> None:
5638 magnet .strength .set (value = value ) # ideally set_and_wait but not implemented
5739 return
5840
59- def get_many (self , names : List [str ]) -> Dict [str , float ]:
60- get_hcorr = False
61- get_vcorr = False
62- name_to_array = {}
63-
64- # check if all names are accounted for and which strength arrays to get.
65- for name in names :
66- if name in self .hcorr_names :
67- get_hcorr = True
68- name_to_array [name ] = "hcorr"
69- elif name in self .vcorr_names :
70- get_vcorr = True
71- name_to_array [name ] = "vcorr"
72- else :
73- raise PyAMLException (
74- f"{ name } was not found in magnet arrays "
75- f"{ self .hcorr_array_name } and { self .vcorr_array_name } "
76- )
77-
78- # do actual get
79- if get_hcorr :
80- hcorr_strengths = self .hcorr_array .strengths .get ()
81- else :
82- hcorr_strengths = []
83-
84- if get_vcorr :
85- vcorr_strengths = self .vcorr_array .strengths .get ()
86- else :
87- vcorr_strengths = []
88-
89- # prepare data to return
90- data = {}
91- for name in names :
92- if name_to_array [name ] == "hcorr" :
93- hcorr_index = self .hcorr_name_to_index [name ]
94- data [name ] = hcorr_strengths [hcorr_index ]
95- elif name_to_array [name ] == "vcorr" :
96- vcorr_index = self .vcorr_name_to_index [name ]
97- data [name ] = vcorr_strengths [vcorr_index ]
98- else :
99- raise PyAMLException ("BUG: This should not happen." )
100-
101- return data
102-
103- def set_many (self , names_values : Dict [str , float ]) -> None :
104- set_hcorr = False
105- set_vcorr = False
106- name_to_array = {}
107-
108- names = list (names_values .keys ())
109- # check if all names are accounted for and which strength arrays to get/set.
110- for name in names :
111- if name in self .hcorr_names :
112- set_hcorr = True
113- name_to_array [name ] = "hcorr"
114- elif name in self .vcorr_names :
115- set_vcorr = True
116- name_to_array [name ] = "vcorr"
117- else :
118- raise PyAMLException (
119- f"{ name } was not found in magnet arrays "
120- f"{ self .hcorr_array_name } and { self .vcorr_array_name } "
121- )
122-
123- # first do get
124- if set_hcorr :
125- hcorr_strengths = self .hcorr_array .strengths .get ()
126- else :
127- hcorr_strengths = []
128-
129- if set_vcorr :
130- vcorr_strengths = self .vcorr_array .strengths .get ()
131- else :
132- vcorr_strengths = []
133-
134- # change hcorr_strengths and vcorr_strengths according to names_values
135- # (input data). We could check if original setpoint and new setpoint are not
136- # the same to avoid redundant set.
137- for name in names :
138- if name_to_array [name ] == "hcorr" :
139- hcorr_index = self .hcorr_name_to_index [name ]
140- hcorr_strengths [hcorr_index ] = names_values [name ]
141- elif name_to_array [name ] == "vcorr" :
142- vcorr_index = self .vcorr_name_to_index [name ]
143- vcorr_strengths [vcorr_index ] = names_values [name ]
144- else :
145- raise PyAMLException ("BUG: This should not happen." )
146-
147- # TODO: we should check first if any value goes out of range before starting to
148- # set anything
149- # TODO: can weset everything together instead of settings the arrays one by one?
150-
151- if set_hcorr :
152- self .hcorr_array .strengths .set (
153- hcorr_strengths
154- ) # ideally set_and_wait but not implemented
155- if set_vcorr :
156- self .vcorr_array .strengths .set (
157- vcorr_strengths
158- ) # ideally set_and_wait but not implemented
159-
160- return
161-
16241 def get_rf_main_frequency (self ) -> float :
42+ if self .rf_plant is None :
43+ raise PyAMLException ("RF plant name was not provided." )
16344 return self .rf_plant .frequency .get ()
16445
16546 def set_rf_main_frequency (self , value : float ) -> None :
47+ if self .rf_plant is None :
48+ raise PyAMLException ("RF plant name was not provided." )
16649 self .rf_plant .frequency .set (value )
16750 return
0 commit comments