damaris-script-library/Scripts/EXSY_2H/op_exsy2h_res.py

170 lines
6.3 KiB
Python
Raw Normal View History

2015-09-30 11:09:36 +00:00
# -*- coding: iso-8859-1 -*-
from numpy import *
from scipy.signal import *
from scipy.optimize import *
from scipy.fftpack import fft, ifft
from os import path, rename
def result():
measurement = MeasurementResult('Magnetization')
measurement_range = [0.0, 10e-6]
measurement_ranging = False
suffix = '' # output file name's suffix and...
counter = 0 # counter for arrayed 2D experiments
# npts = 0
# loop over the incoming results:
for timesignal in results:
if not isinstance(timesignal,ADC_Result):
continue
# read experiment parameters:
pars = timesignal.get_description_dictionary()
# ---------------- digital filter ------------------
# get actual sampling rate of timesignal:
sampling_rate = timesignal.get_sampling_rate()
# get user-defined spectrum width:
spec_width = pars['SW']
# specify cutoff frequency, in relative units:
cutoff = spec_width / sampling_rate
if cutoff < 1: # otherwise no filter applied
# number of filter's coefficients:
numtaps = 29
# use firwin to create a lowpass FIR filter:
fir_coeff = firwin(numtaps, cutoff)
# downsize x according to user-defined spectral window:
skip = int(sampling_rate / spec_width)
timesignal.x = timesignal.x[::skip]
for i in range(2):
# apply the filter to ith channel:
timesignal.y[i] = lfilter(fir_coeff, 1.0, timesignal.y[i])
# zeroize first N-1 "corrupted" samples:
timesignal.y[i][:numtaps-1] = 0.0
# circular left shift of y:
timesignal.y[i] = roll(timesignal.y[i], -(numtaps-1))
# downsize y to user-defined number of samples (SI):
timesignal.y[i] = timesignal.y[i][::skip]
# update the sampling_rate attribute of the signal's:
timesignal.set_sampling_rate(spec_width)
# ----------------------------------------------------
# rotate timesignal according to current receiver's phase:
timesignal.phase(pars['rec_phase'])
# provide timesignal to the display tab:
data['Current scan'] = timesignal
# accumulate...
if not locals().get('accu'):
accu = Accumulation()
# skip dummy scans, if any:
if pars['run'] < 0: continue
# add up:
accu += timesignal
# provide accumulation to the display tab:
data['Accumulation'] = accu
# check whether accumulation is complete:
# ------------------------------------------------------------------------------------
# The hypercomplex technique implies recording two data sets for each t1 value.
# One dataset (a cosine-modulated signal) is stored in odd records as Re, while
# the other dataset (a sine-modulated signal) in even records as Im, totally 2*SI1
# records. Henceforth, accu represents one such a record.
# ------------------------------------------------------------------------------------
if accu.n == pars['NS']:
# compute the initial phase of FID:
phi0 = arctan2(accu.y[1][0], accu.y[0][0]) * 180 / pi
if not 'ref' in locals(): ref = phi0
print 'phi0 = ', phi0
# rotate every other record by 90<39> so that States algorithm is applicable:
rec = (accu.job_id/accu.n)%(2*pars['SI1']) + 1
if rec%2 == 0:
accu.phase(90)
coeff = 1.5
accu.y[0] *= coeff # XY-balancing
accu.y[1] *= coeff
else: # baseline correction )))))
tmp = fft(accu.y[0]+1j*accu.y[1])
[start, stop] = len(accu.y[0])*array([0.4, 0.6])
tmp -= mean(tmp.real[start:stop])
tmp = ifft(tmp)
accu.y[0] = tmp.real
accu.y[1] = tmp.imag
# check whether it is an arrayed experiment:
var_key = pars.get('VAR_PAR')
if var_key:
# get variable parameter's value:
var_value = pars.get(var_key)
# provide signal recorded with this var_value to the display tab:
data['Accumulation'+"/"+var_key+"=%e"%(var_value)] = accu
# measure signal intensity vs. var_value:
if measurement_ranging == True:
[start, stop] = accu.get_sampling_rate() * array(measurement_range)
measurement[var_value] = sum(accu.y[0][int(start):int(stop)])
else:
# measurement[var_value] = sum(accu.y[0][0:31])
measurement[var_value+counter*1e-6] = sum(accu.y[0][0:31])
# provide measurement to the display tab:
data[measurement.get_title()] = measurement
# update the file name suffix:
counter2D = counter/(2*pars['SI1'])+1
suffix = '_' + str(counter2D)
counter += 1
# save accu if required:
outfile = pars.get('OUTFILE')
if outfile:
datadir = pars.get('DATADIR')
# write raw data in Tecmag format:
filename = datadir+outfile+suffix+'.tnt'
accu.write_to_tecmag(filename,\
nrecords=2*pars['SI1'],\
frequency=pars['SW']+pars['O1'])
# write parameters in a text file:
filename = datadir+outfile+suffix+'.par'
if path.exists(filename):
rename(filename, datadir+'~'+outfile+suffix+'.par')
fileobject = open(filename, 'w')
for key in sorted(pars.iterkeys()):
if key=='run': continue
if key=='rec_phase': continue
fileobject.write('%s%s%s'%(key,'=', pars[key]))
fileobject.write('\n')
fileobject.close()
# reset accumulation:
del accu
pass