Package pipe_control :: Module spectrometer
[hide private]
[frames] | no frames]

Source Code for Module pipe_control.spectrometer

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2015 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for manipulating the spectrometer experimental information.""" 
 24   
 25  # Python module imports. 
 26  from math import modf, pi 
 27  from warnings import warn 
 28   
 29  # relax module imports. 
 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 
 34  from pipe_control import pipes 
 35  from pipe_control.pipes import check_pipe 
 36   
 37   
38 -def check_frequency_func(id=None):
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 # Check for the ID. 48 if not hasattr(cdp, 'spectrometer_frq') and id not in cdp.spectrometer_frq: 49 return RelaxNoFrqError(id=id)
50 51 # Create the checking object. 52 check_frequency = Check(check_frequency_func) 53 54
55 -def check_spectrometer_setup_func():
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 # No data structure. 63 if not hasattr(cdp, 'spectrometer_frq'): 64 return RelaxNoFrqError() 65 66 # An empty list. 67 if not len(cdp.spectrometer_frq): 68 return RelaxNoFrqError()
69 70 # Create the checking object. 71 check_spectrometer_setup = Check(check_spectrometer_setup_func) 72 73
74 -def copy_frequencies(pipe_from=None, pipe_to=None, id=None):
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 # Defaults. 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 # Test if the pipe_from and pipe_to data pipes exist. 94 check_pipe(pipe_from) 95 check_pipe(pipe_to) 96 97 # Get the data pipes. 98 dp_from = pipes.get_pipe(pipe_from) 99 dp_to = pipes.get_pipe(pipe_to) 100 101 # Test if the pipe_from pipe has frequency data. 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 # Set up the data structures if missing. 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 # Copy the frequency. 114 dp_to.spectrometer_frq[id] = dp_from.spectrometer_frq[id] 115 116 # New frequency. 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
122 -def delete_frequencies(id=None):
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 # Checks. 130 check_pipe() 131 check_frequency(id=id) 132 133 # Delete the frequency. 134 frq = cdp.spectrometer_frq[id] 135 del cdp.spectrometer_frq[id] 136 137 # Update the structures as needed. 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 # Cleanup. 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
149 -def frequency_checks(frq):
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 # No frequency given. 157 if frq == None: 158 return 159 160 # Make sure the precise value has been supplied. 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 # Check that the frequency value is reasonable. 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
172 -def get_frequency(id=None):
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 # Checks. 182 check_pipe() 183 check_frequency(id=id) 184 185 # Return the frequency in Hz. 186 return cdp.spectrometer_frq[id]
187 188
189 -def get_frequencies(units='Hz'):
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 # No frequency data. 206 if not hasattr(cdp, 'spectrometer_frq'): 207 return [] 208 209 # Convert the values. 210 frq = [] 211 for value in cdp.spectrometer_frq_list: 212 # Hertz. 213 if units == 'Hz': 214 frq.append(value) 215 216 # MHz. 217 elif units == 'MHz': 218 frq.append(value * 1e-6) 219 220 # Tesla. 221 elif units == 'T': 222 frq.append(value * 2.0 * pi / periodic_table.gyromagnetic_ratio('1H')) 223 224 # Unknown units. 225 else: 226 raise RelaxError("The units of '%s' should be one of 'Hz', 'MHz', or 'T'.") 227 228 # Return the frqs. 229 return frq
230 231
232 -def loop_frequencies():
233 """Generator function for looping over the spectrometer frequencies. 234 235 @return: The frequency. 236 @rtype: float 237 """ 238 239 # Loop over the frequencies. 240 for frq in cdp.spectrometer_frq_list: 241 yield frq
242 243
244 -def set_frequency(id=None, frq=None, units='Hz'):
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 # Test if the current data pipe exists. 256 check_pipe() 257 258 # Set up the data structures if missing. 259 if not hasattr(cdp, 'spectrometer_frq'): 260 cdp.spectrometer_frq = {} 261 cdp.spectrometer_frq_list = [] 262 cdp.spectrometer_frq_count = 0 263 264 # Unit conversion. 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 # Set the frequency. 277 cdp.spectrometer_frq[id] = frq * conv 278 279 # Some checks. 280 frequency_checks(cdp.spectrometer_frq[id]) 281 282 # New frequency. 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
288 -def set_temperature(id=None, temp=None):
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 # Test if the current data pipe exists. 298 check_pipe() 299 300 # Set up the dictionary data structure if it doesn't exist yet. 301 if not hasattr(cdp, 'temperature'): 302 cdp.temperature = {} 303 304 # Convert to a float. 305 temp = float(temp) 306 307 # Test the temperature has not already been set. 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 # Set the temperature. 312 cdp.temperature[id] = temp
313