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

Source Code for Module pipe_control.spectrometer

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2013 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.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   
36 -def check_frequency(id=None):
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 # Check for the ID. 44 if not hasattr(cdp, 'spectrometer_frq') or id not in cdp.spectrometer_frq.keys(): 45 raise RelaxNoFrqError(id=id)
46 47
48 -def copy_frequencies(pipe_from=None, pipe_to=None, id=None):
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 # Defaults. 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 # Test if the pipe_from and pipe_to data pipes exist. 68 pipes.test(pipe_from) 69 pipes.test(pipe_to) 70 71 # Get the data pipes. 72 dp_from = pipes.get_pipe(pipe_from) 73 dp_to = pipes.get_pipe(pipe_to) 74 75 # Test if the pipe_from pipe has frequency data. 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 # Set up the data structures if missing. 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 # Copy the frequency. 88 dp_to.spectrometer_frq[id] = dp_from.spectrometer_frq[id] 89 90 # New frequency. 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
96 -def delete_frequencies(id=None):
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 # Checks. 104 pipes.test() 105 check_frequency(id=id) 106 107 # Delete the frequency. 108 frq = cdp.spectrometer_frq[id] 109 del cdp.spectrometer_frq[id] 110 111 # Update the structures as needed. 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 # Cleanup. 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
123 -def frequency_checks(frq):
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 # No frequency given. 131 if frq == None: 132 return 133 134 # Make sure the precise value has been supplied. 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 # Check that the frequency value is reasonable. 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
146 -def get_frequency(id=None):
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 # Checks. 156 pipes.test() 157 check_frequency(id=id) 158 159 # Return the frequency in Hz. 160 return cdp.spectrometer_frq[id]
161 162
163 -def get_frequencies(units='Hz'):
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 # No frequency data. 180 if not hasattr(cdp, 'spectrometer_frq'): 181 return [] 182 183 # Convert the values. 184 frq = [] 185 for value in cdp.spectrometer_frq_list: 186 # Hertz. 187 if units == 'Hz': 188 frq.append(value) 189 190 # MHz. 191 elif units == 'MHz': 192 frq.append(value * 1e-6) 193 194 # Tesla. 195 elif units == 'T': 196 frq.append(value * 2.0 * pi / g1H) 197 198 # Unknown units. 199 else: 200 raise RelaxError("The units of '%s' should be one of 'Hz', 'MHz', or 'T'.") 201 202 # Return the frqs. 203 return frq
204 205
206 -def loop_frequencies():
207 """Generator function for looping over the spectrometer frequencies. 208 209 @return: The frequency. 210 @rtype: float 211 """ 212 213 # Loop over the frequencies. 214 for frq in cdp.spectrometer_frq_list: 215 yield frq
216 217
218 -def set_frequency(id=None, frq=None, units='Hz'):
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 # Test if the current data pipe exists. 230 pipes.test() 231 232 # Set up the data structures if missing. 233 if not hasattr(cdp, 'spectrometer_frq'): 234 cdp.spectrometer_frq = {} 235 cdp.spectrometer_frq_list = [] 236 cdp.spectrometer_frq_count = 0 237 238 # Unit conversion. 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 # Set the frequency. 251 cdp.spectrometer_frq[id] = frq * conv 252 253 # Some checks. 254 frequency_checks(cdp.spectrometer_frq[id]) 255 256 # New frequency. 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
262 -def set_temperature(id=None, temp=None):
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 # Test if the current data pipe exists. 272 pipes.test() 273 274 # Set up the dictionary data structure if it doesn't exist yet. 275 if not hasattr(cdp, 'temperature'): 276 cdp.temperature = {} 277 278 # Convert to a float. 279 temp = float(temp) 280 281 # Test the temperature has not already been set. 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 # Set the temperature. 286 cdp.temperature[id] = temp
287