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.errors import RelaxError, RelaxNoFrqError
31 from lib.physical_constants import g1H
32 from lib.warnings import RelaxWarning
33 from pipe_control import pipes
34
35
37 """Check that the frequency for the given ID has been set.
38
39 @param id: The experiment ID string.
40 @type id: str
41 """
42
43
44 if not hasattr(cdp, 'spectrometer_frq') or id not in cdp.spectrometer_frq.keys():
45 raise RelaxNoFrqError(id=id)
46
47
49 """Copy the frequency information from one data pipe to another.
50
51 @keyword pipe_from: The data pipe to copy the frequency information from. This defaults to the current data pipe.
52 @type pipe_from: str
53 @keyword pipe_to: The data pipe to copy the frequency information to. This defaults to the current data pipe.
54 @type pipe_to: str
55 @param id: The experiment ID string.
56 @type id: str
57 """
58
59
60 if pipe_from == None and pipe_to == None:
61 raise RelaxError("The pipe_from and pipe_to arguments cannot both be set to None.")
62 elif pipe_from == None:
63 pipe_from = pipes.cdp_name()
64 elif pipe_to == None:
65 pipe_to = pipes.cdp_name()
66
67
68 pipes.test(pipe_from)
69 pipes.test(pipe_to)
70
71
72 dp_from = pipes.get_pipe(pipe_from)
73 dp_to = pipes.get_pipe(pipe_to)
74
75
76 if not hasattr(dp_from, 'spectrometer_frq'):
77 raise RelaxNoFrqError(pipe_from)
78 elif id not in dp_from.spectrometer_frq:
79 raise RelaxNoFrqError(pipe_from, id=id)
80
81
82 if not hasattr(dp_to, 'spectrometer_frq'):
83 dp_to.spectrometer_frq = {}
84 dp_to.spectrometer_frq_list = []
85 dp_to.spectrometer_frq_count = 0
86
87
88 dp_to.spectrometer_frq[id] = dp_from.spectrometer_frq[id]
89
90
91 if dp_to.spectrometer_frq[id] not in dp_to.spectrometer_frq_list:
92 dp_to.spectrometer_frq_list.append(dp_to.spectrometer_frq[id])
93 dp_to.spectrometer_frq_count += 1
94
95
97 """Delete the spectrometer frequency corresponding to the experiment ID.
98
99 @keyword id: The experiment ID string.
100 @type id: str
101 """
102
103
104 pipes.test()
105 check_frequency(id=id)
106
107
108 frq = cdp.spectrometer_frq[id]
109 del cdp.spectrometer_frq[id]
110
111
112 if frq in cdp.spectrometer_frq_list and frq not in cdp.spectrometer_frq.values():
113 cdp.spectrometer_frq_list.pop(cdp.spectrometer_frq_list.index(frq))
114 cdp.spectrometer_frq_count = len(cdp.spectrometer_frq_list)
115
116
117 if len(cdp.spectrometer_frq) == 0:
118 del cdp.spectrometer_frq
119 del cdp.spectrometer_frq_list
120 del cdp.spectrometer_frq_count
121
122
124 """Perform a number of checks on the given proton frequency.
125
126 @param frq: The proton frequency value in Hertz.
127 @type frq: float or None
128 """
129
130
131 if frq == None:
132 return
133
134
135 frac, integer = modf(frq / 1e6)
136 if frac == 0.0 or frac > 0.99999:
137 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."))
138
139
140 if frq < 1e8:
141 warn(RelaxWarning("The proton frequency of %s Hz appears to be too low." % frq))
142 if frq > 2e9:
143 warn(RelaxWarning("The proton frequency of %s Hz appears to be too high." % frq))
144
145
147 """Return the frequency corresponding to the given ID.
148
149 @param id: The experiment ID string.
150 @type id: str
151 @return: The spectrometer proton frequency in Hertz for the given ID.
152 @rtype: float
153 """
154
155
156 pipes.test()
157 check_frequency(id=id)
158
159
160 return cdp.spectrometer_frq[id]
161
162
164 """Return a list of all the current spectrometer frequencies.
165
166 The returned values can be changed with the units argument which can have the following values:
167
168 - 'Hz' will return the proton frequency (wH),
169 - 'MHz' will return the proton frequency in megahertz,
170 - 'T' will return the B0 field in Tesla.
171
172
173 @keyword units: The magnetic field units to return. This can be one of 'Hz', 'MHz', or 'T'.
174 @type units: str
175 @return: The frequency list for the current data pipe.
176 @rtype: list of float
177 """
178
179
180 if not hasattr(cdp, 'spectrometer_frq'):
181 return []
182
183
184 frq = []
185 for value in cdp.spectrometer_frq_list:
186
187 if units == 'Hz':
188 frq.append(value)
189
190
191 elif units == 'MHz':
192 frq.append(value * 1e-6)
193
194
195 elif units == 'T':
196 frq.append(value * 2.0 * pi / g1H)
197
198
199 else:
200 raise RelaxError("The units of '%s' should be one of 'Hz', 'MHz', or 'T'.")
201
202
203 return frq
204
205
207 """Generator function for looping over the spectrometer frequencies.
208
209 @return: The frequency.
210 @rtype: float
211 """
212
213
214 for frq in cdp.spectrometer_frq_list:
215 yield frq
216
217
219 """Set the spectrometer frequency of the experiment.
220
221 @keyword id: The experiment ID string (allowing for multiple experiments per data pipe).
222 @type id: str
223 @keyword frq: The spectrometer frequency in Hertz.
224 @type frq: float
225 @keyword units: The units of frequency. This can be one of "Hz", "kHz", "MHz", or "GHz".
226 @type units: str
227 """
228
229
230 pipes.test()
231
232
233 if not hasattr(cdp, 'spectrometer_frq'):
234 cdp.spectrometer_frq = {}
235 cdp.spectrometer_frq_list = []
236 cdp.spectrometer_frq_count = 0
237
238
239 if units == 'Hz':
240 conv = 1.0
241 elif units == 'kHz':
242 conv = 1e3
243 elif units == 'MHz':
244 conv = 1e6
245 elif units == 'GHz':
246 conv = 1e9
247 else:
248 raise RelaxError("The frequency units of '%s' are unknown." % units)
249
250
251 cdp.spectrometer_frq[id] = frq * conv
252
253
254 frequency_checks(cdp.spectrometer_frq[id])
255
256
257 if cdp.spectrometer_frq[id] not in cdp.spectrometer_frq_list:
258 cdp.spectrometer_frq_list.append(cdp.spectrometer_frq[id])
259 cdp.spectrometer_frq_count += 1
260
261
263 """Set the experimental temperature.
264
265 @keyword id: The experiment ID string (allowing for multiple experiments per data pipe).
266 @type id: str
267 @keyword temp: The temperature in Kelvin.
268 @type temp: float
269 """
270
271
272 pipes.test()
273
274
275 if not hasattr(cdp, 'temperature'):
276 cdp.temperature = {}
277
278
279 temp = float(temp)
280
281
282 if id in cdp.temperature and cdp.temperature[id] != temp:
283 raise RelaxError("The temperature for the experiment '%s' has already been set to %s K." % (id, cdp.temperature[id]))
284
285
286 cdp.temperature[id] = temp
287