modifications to write_to_tecmag, allow 2D experiments (by Oleg Petrov)

This commit is contained in:
Markus Rosenstihl 2016-02-15 12:42:45 +00:00
parent eed1d10f63
commit 6dc67edaaf

View File

@ -279,40 +279,55 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
# ------------- added by Oleg Petrov, 10 Sep 2013 -----------------------
def write_to_tecmag(self, destination=sys.stdout, nrecords=1,\
frequency=100e6,\
frequency=100.,\
last_delay = 1.,\
receiver_phase=0.,\
nucleus='1H'):
"""
writes the data to a binary file in TecMag format;
destination can be a file object or a filename;
nrecords determines the 2nd dimension of data;
"""
nrecords determines an indirect dimension in 2D experiments;
"""
if self.job_id == None or self.n == 0:
raise ValueError("write_to_tecmag: cannot get a record number")
else:
record = self.job_id/self.n + 1
else:
record = (self.job_id/self.n)%nrecords + 1
the_destination=destination
if type(destination) in types.StringTypes:
if record == 1 and os.path.exists(destination):
os.rename(destination, os.path.dirname(destination)+'/~'+os.path.basename(destination))
the_destination=file(destination, "ab")
self.lock.acquire()
try:
npts = [len(self), nrecords, 1, 1]
data_offset = 2*4*npts[0]*npts[1]*npts[2]*npts[3] # length of data section
dwell = 1./self.get_sampling_rate()
sw = 0.5/dwell
base_freq = [frequency, 100., 0., 0.]
base_freq = [frequency, frequency, 0., 0.]
offset_freq = [0., 0., 0., 0.]
ob_freq = [sum(x) for x in zip(base_freq, offset_freq)]
date = self.time_period[0].strftime("%Y/%m/%d %H:%M:%S")
if record == 1:
buff = ctypes.create_string_buffer(1056)
# data handling:
ch_no=self.get_number_of_channels()
ydata = map(self.get_ydata, xrange(ch_no))
if ch_no == 1:
ydata = [ydata, numpy.zeros(len(ydata))]
# data is arranged in RIRIRIRI blocks in linear order:
data = numpy.append([ydata[0]], [ydata[1]], axis=0)
data = data.T
data = data.flatten()
if record == 1:
the_destination=file(destination, "wb")
# allocate space for all records in advance:
buff = ctypes.create_string_buffer(1056+data_offset+2068)
struct.pack_into('8s', buff, 0, 'TNT1.005') # 'TNT1.000' version ID
struct.pack_into('4s', buff, 8, 'TMAG') # 'TMAG' tag
@ -335,8 +350,8 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
struct.pack_into('h', buff, 216, 1) # obs_channel
struct.pack_into('42s', buff, 218, 42*'2') # space2[42]
struct.pack_into('4d', buff, 260, sw, 0., 0., 0.) # sw[4], sw = 0.5/dwell
struct.pack_into('4d', buff, 292, dwell, 0., 0., 0.) # dwell[4]
struct.pack_into('4d', buff, 260, sw, sw, 0., 0.) # sw[4], sw = 0.5/dwell
struct.pack_into('4d', buff, 292, dwell, dwell, 0., 0.) # dwell[4]
struct.pack_into('d', buff, 324, sw) # filter, = 0.5/dwell
struct.pack_into('d', buff, 340, (npts[0]*dwell)) # acq_time
struct.pack_into('d', buff, 348, 1.) # last_delay (5*T1 minus sequence length)
@ -350,44 +365,34 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
struct.pack_into('32s', buff, 884, date) # date[32]
struct.pack_into('16s', buff, 916, nucleus) # nucleus[16]
# TECMAG Structure total => 1024
# TECMAG Structure total => 1024
struct.pack_into('4s', buff, 1044, 'DATA') # 'DATA' tag
struct.pack_into('?', buff, 1048, True) # BOOLean
struct.pack_into('i', buff, 1052, 2*4*npts[0]*npts[1]) # length of data (2 = [Re, Im]; 4 = len(c_float))
struct.pack_into('i', buff, 1052, data_offset) # length of data
struct.pack_into('%sf' % (2*npts[0]), buff, 1056, *data) # actual data (one record)
struct.pack_into('4s', buff, 1056+data_offset, 'TMG2') # 'TMG2' tag
struct.pack_into('?', buff, 1056+data_offset+4, True) # BOOLean
struct.pack_into('i', buff, 1056+data_offset+8, 2048) # length of Tecmag2 struct
# Leave TECMAG2 structure empty:
struct.pack_into('52s', buff, 1056+data_offset+372, 52*'2') # space[52]
struct.pack_into('866s', buff, 1056+data_offset+1194, 866*'2') # space[610]+names+strings
# TECMAG2 Structure total => 2048
struct.pack_into('4s', buff, 1056+data_offset+2060, 'PSEQ') # 'PSEQ' tag 658476
struct.pack_into('?', buff, 1056+data_offset+2064, False) # BOOLean 658480
the_destination.write(buff)
# append data to the file as they appear in an experiment (RIRIRIRI blocks in linear order):
ch_no=self.get_number_of_channels()
ydata = map(self.get_ydata, xrange(ch_no))
if ch_no == 1:
ydata = [ydata, numpy.zeros(len(ydata))]
data = numpy.append([ydata[0]], [ydata[1]], axis=0)
data = data.T
data = data.flatten()
buff = ctypes.create_string_buffer(4*len(data))
struct.pack_into('%sf' % len(data), buff, 0, *data)
the_destination.write(buff)
if record == nrecords:
buff = ctypes.create_string_buffer(2068)
struct.pack_into('4s', buff, 0, 'TMG2') # 'TMG2' tag
struct.pack_into('?', buff, 4, True) # BOOLean
struct.pack_into('i', buff, 8, 2048) # length of Tecmag2 struct
# Leave TECMAG2 structure empty:
struct.pack_into('52s', buff, 372, 52*'2') # space[52]
struct.pack_into('866s', buff, 1194, 866*'2') # space[610]+names+strings
# TECMAG2 Structure total => 2048
struct.pack_into('4s', buff, 2060, 'PSEQ') # 'PSEQ' tag 658476
struct.pack_into('?', buff, 2064, False) # BOOLean 658480
else:
the_destination=file(destination, "rb+")
buff = ctypes.create_string_buffer(4*2*npts[0])
struct.pack_into('%sf' % (2*npts[0]), buff, 0, *data)
the_destination.seek(1056+4*2*npts[0]*(record-1))
the_destination.write(buff)
the_destination = None
@ -560,7 +565,7 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
r = Accumulation(x = numpy.array(self.x, dtype="Float64"), y = tmp_y, y_2 = tmp_ysquare, n = self.n, index = self.index, sampl_freq = self.sampling_rate, error = self.use_error)
else:
r = Accumulation(x = numpy.array(self.x, dtype="Float64"), y = tmp_y, n = self.n, index = self.index, sampl_freq = self.sampling_rate, error = self.use_error)
r.job_id = self.job_id # added by Oleg Petrov
self.lock.release()
return r