1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for manipulating the spectrometer experimental information."""
24
25
26 from math import modf, pi
27 from warnings import warn
28
29
30 from lib.checks import Check
31 from lib.errors import RelaxError, RelaxNoFrqError
32 from lib.periodic_table import periodic_table
33 from lib.warnings import RelaxWarning, RelaxNoFrqWarning
34 from pipe_control import pipes
35 from pipe_control.pipes import check_pipe
36
37
39 """Check that the frequency for the given ID has been set.
40
41 @keyword id: The experiment ID string.
42 @type id: str
43 @return: The initialised RelaxError object or nothing.
44 @rtype: None or RelaxError instance
45 """
46
47
48 if not hasattr(cdp, 'spectrometer_frq') and id not in cdp.spectrometer_frq:
49 return RelaxNoFrqError(id=id)
50
51
52 check_frequency = Check(check_frequency_func)
53
54
56 """Check that spectrometer frequencies have been set up.
57
58 @return: The initialised RelaxError object or nothing.
59 @rtype: None or RelaxError instance
60 """
61
62
63 if not hasattr(cdp, 'spectrometer_frq'):
64 return RelaxNoFrqError()
65
66
67 if not len(cdp.spectrometer_frq):
68 return RelaxNoFrqError()
69
70
71 check_spectrometer_setup = Check(check_spectrometer_setup_func)
72
73
75 """Copy the frequency information from one data pipe to another.
76
77 @keyword pipe_from: The data pipe to copy the frequency information from. This defaults to the current data pipe.
78 @type pipe_from: str
79 @keyword pipe_to: The data pipe to copy the frequency information to. This defaults to the current data pipe.
80 @type pipe_to: str
81 @param id: The experiment ID string.
82 @type id: str
83 """
84
85
86 if pipe_from == None and pipe_to == None:
87 raise RelaxError("The pipe_from and pipe_to arguments cannot both be set to None.")
88 elif pipe_from == None:
89 pipe_from = pipes.cdp_name()
90 elif pipe_to == None:
91 pipe_to = pipes.cdp_name()
92
93
94 check_pipe(pipe_from)
95 check_pipe(pipe_to)
96
97
98 dp_from = pipes.get_pipe(pipe_from)
99 dp_to = pipes.get_pipe(pipe_to)
100
101
102 if not hasattr(dp_from, 'spectrometer_frq'):
103 raise RelaxNoFrqError(pipe_from)
104 elif id not in dp_from.spectrometer_frq:
105 raise RelaxNoFrqError(pipe_from, id=id)
106
107
108 if not hasattr(dp_to, 'spectrometer_frq'):
109 dp_to.spectrometer_frq = {}
110 dp_to.spectrometer_frq_list = []
111 dp_to.spectrometer_frq_count = 0
112
113
114 dp_to.spectrometer_frq[id] = dp_from.spectrometer_frq[id]
115
116
117 if dp_to.spectrometer_frq[id] not in dp_to.spectrometer_frq_list:
118 dp_to.spectrometer_frq_list.append(dp_to.spectrometer_frq[id])
119 dp_to.spectrometer_frq_count += 1
120
121
123 """Delete the spectrometer frequency corresponding to the experiment ID.
124
125 @keyword id: The experiment ID string.
126 @type id: str
127 """
128
129
130 check_pipe()
131 check_frequency(id=id)
132
133
134 frq = cdp.spectrometer_frq[id]
135 del cdp.spectrometer_frq[id]
136
137
138 if frq in cdp.spectrometer_frq_list and frq not in list(cdp.spectrometer_frq.values()):
139 cdp.spectrometer_frq_list.pop(cdp.spectrometer_frq_list.index(frq))
140 cdp.spectrometer_frq_count = len(cdp.spectrometer_frq_list)
141
142
143 if len(cdp.spectrometer_frq) == 0:
144 del cdp.spectrometer_frq
145 del cdp.spectrometer_frq_list
146 del cdp.spectrometer_frq_count
147
148
150 """Perform a number of checks on the given proton frequency.
151
152 @param frq: The proton frequency value in Hertz.
153 @type frq: float or None
154 """
155
156
157 if frq == None:
158 return
159
160
161 frac, integer = modf(frq / 1e6)
162 if frac == 0.0 or frac > 0.99999:
163 warn(RelaxWarning("The precise spectrometer frequency should be supplied, a value such as 500000000 or 5e8 for a 500 MHz machine is not acceptable. Please see the 'sfrq' parameter in the Varian procpar file or the 'SFO1' parameter in the Bruker acqus file."))
164
165
166 if frq < 1e8:
167 warn(RelaxWarning("The proton frequency of %s Hz appears to be too low." % frq))
168 if frq > 2e9:
169 warn(RelaxWarning("The proton frequency of %s Hz appears to be too high." % frq))
170
171
173 """Return the frequency corresponding to the given ID.
174
175 @param id: The experiment ID string.
176 @type id: str
177 @return: The spectrometer proton frequency in Hertz for the given ID.
178 @rtype: float
179 """
180
181
182 check_pipe()
183 check_frequency(id=id)
184
185
186 return cdp.spectrometer_frq[id]
187
188
190 """Return a list of all the current spectrometer frequencies.
191
192 The returned values can be changed with the units argument which can have the following values:
193
194 - 'Hz' will return the proton frequency (wH),
195 - 'MHz' will return the proton frequency in megahertz,
196 - 'T' will return the B0 field in Tesla.
197
198
199 @keyword units: The magnetic field units to return. This can be one of 'Hz', 'MHz', or 'T'.
200 @type units: str
201 @return: The frequency list for the current data pipe.
202 @rtype: list of float
203 """
204
205
206 if not hasattr(cdp, 'spectrometer_frq'):
207 return []
208
209
210 frq = []
211 for value in cdp.spectrometer_frq_list:
212
213 if units == 'Hz':
214 frq.append(value)
215
216
217 elif units == 'MHz':
218 frq.append(value * 1e-6)
219
220
221 elif units == 'T':
222 frq.append(value * 2.0 * pi / periodic_table.gyromagnetic_ratio('1H'))
223
224
225 else:
226 raise RelaxError("The units of '%s' should be one of 'Hz', 'MHz', or 'T'.")
227
228
229 return frq
230
231
233 """Generator function for looping over the spectrometer frequencies.
234
235 @return: The frequency.
236 @rtype: float
237 """
238
239
240 for frq in cdp.spectrometer_frq_list:
241 yield frq
242
243
245 """Set the spectrometer frequency of the experiment.
246
247 @keyword id: The experiment ID string (allowing for multiple experiments per data pipe).
248 @type id: str
249 @keyword frq: The spectrometer frequency in Hertz.
250 @type frq: float
251 @keyword units: The units of frequency. This can be one of "Hz", "kHz", "MHz", or "GHz".
252 @type units: str
253 """
254
255
256 check_pipe()
257
258
259 if not hasattr(cdp, 'spectrometer_frq'):
260 cdp.spectrometer_frq = {}
261 cdp.spectrometer_frq_list = []
262 cdp.spectrometer_frq_count = 0
263
264
265 if units == 'Hz':
266 conv = 1.0
267 elif units == 'kHz':
268 conv = 1e3
269 elif units == 'MHz':
270 conv = 1e6
271 elif units == 'GHz':
272 conv = 1e9
273 else:
274 raise RelaxError("The frequency units of '%s' are unknown." % units)
275
276
277 cdp.spectrometer_frq[id] = frq * conv
278
279
280 frequency_checks(cdp.spectrometer_frq[id])
281
282
283 if cdp.spectrometer_frq[id] not in cdp.spectrometer_frq_list:
284 cdp.spectrometer_frq_list.append(cdp.spectrometer_frq[id])
285 cdp.spectrometer_frq_count += 1
286
287
289 """Set the experimental temperature.
290
291 @keyword id: The experiment ID string (allowing for multiple experiments per data pipe).
292 @type id: str
293 @keyword temp: The temperature in Kelvin.
294 @type temp: float
295 """
296
297
298 check_pipe()
299
300
301 if not hasattr(cdp, 'temperature'):
302 cdp.temperature = {}
303
304
305 temp = float(temp)
306
307
308 if id in cdp.temperature and cdp.temperature[id] != temp:
309 raise RelaxError("The temperature for the experiment '%s' has already been set to %s K." % (id, cdp.temperature[id]))
310
311
312 cdp.temperature[id] = temp
313