SimpReader(filename, format, b0=None, nucleus=None)
A class to read and process NMR data from SIMPSON files.
Attributes:
filename (str): The name of the file to read.
format (str): The format of the file (spe, fid, xreim).
b0 (str, optional): The magnetic field strength in MHz or T.
nucleus (str, optional): The nucleus type.
Example:
reader = SimpReader('spe_file', 'spe', b0='9.4T', nucleus='13C')
Source code in simpyson/io.py
| def __init__(self, filename, format, b0=None, nucleus=None):
self.filename = filename
self.format = format
self.b0 = b0
self.nucleus = nucleus
self._read_file()
|
to_spe
Converts FID data to spectrum (SPE).
Raises:
ValueError: If the format is not FID.
Returns:
SimpReader: A new SimpReader instance with SPE format data.
Example:
spectrum = reader.to_spe()
Source code in simpyson/io.py
| def to_spe(self):
"""
Converts FID data to spectrum (SPE).
Raises:
ValueError: If the format is not FID.
Returns:
SimpReader: A new SimpReader instance with SPE format data.
Example:
spectrum = reader.to_spe()
"""
if self.format != 'fid':
raise ValueError('Only FID format can be converted to SPE.')
spectrum = copy.deepcopy(self)
npoints = spectrum.data['np']
sw = spectrum.data['sw']
raw_signal = spectrum.data['real'] + 1j * spectrum.data['imag']
signal = np.fft.fftshift(np.fft.fft(raw_signal))
real = np.real(signal)
imag = np.imag(signal)
hz = np.linspace(-sw/2, sw/2, int(npoints))
spectrum.data = {'real': real, 'imag': imag, 'np': npoints, 'sw': sw, 'hz': hz}
if spectrum.b0 is not None and spectrum.nucleus is not None:
dir = os.path.dirname(os.path.realpath(__file__))
isotope_file = os.path.join(dir, 'isotope_data.json')
isotope = int(''.join(filter(str.isdigit, spectrum.nucleus)))
element = ''.join(filter(str.isalpha, spectrum.nucleus))
b0_unit = ''.join(filter(str.isalpha, spectrum.b0)).lower()
with open(isotope_file) as f:
data = json.load(f)
if element in data:
gamma = data[element][str(isotope)]['Gamma']
else:
raise ValueError('Nucleus not found.')
if b0_unit == 't':
b0 = int(''.join(filter(str.isdigit, spectrum.b0)))
ppm = hz / (b0 * gamma)
elif b0_unit == 'mhz':
# Convert from 1H MHz to MHz of the nucleus
b0 = int(''.join(filter(str.isdigit, spectrum.b0)))
gamma_h = data['H']['1']['Gamma']
ppm = hz / (b0/(gamma_h/gamma))
else:
raise ValueError('B0 unit must be T or MHz.')
spectrum.data['ppm'] = ppm
spectrum.format = 'spe'
return spectrum
|