-
Notifications
You must be signed in to change notification settings - Fork 6
/
agilent_extra_cs.py
296 lines (259 loc) · 10.1 KB
/
agilent_extra_cs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# -*- coding: utf-8 -*-
import struct
import numpy as np
from aston.cache import cache
from aston.trace import Trace
from aston.tracefile import TraceFile
class AgilentCSPump(TraceFile):
mime = 'application/vnd-agilent-chemstation-pump'
traces = []
# traces = ['mslva', 'mslvb', 'mslvc', 'mslvd', 'mflow']
@property
def info(self):
d = super(AgilentCSPump, self).info
fd = read_reg_file(open(self.filename, 'rb'))
if fd.get('TIMETABLE', False):
# get solv_B, solv_C, solv_D, flow
# calculate solv_A
pass
else:
if fd.get('SOLV_RATIO_A', False):
pass
if fd.get('SOLV_RATIO_B', False):
pass
if fd.get('SOLV_RATIO_C', False):
pass
if fd.get('SOLV_RATIO_D', False):
pass
if fd.get('FLOW', False):
pass
return d
def _trace(self, name, twin):
# TODO: return method traces
pass
class AgilentCSFraction(TraceFile):
mime = 'application/vnd-agilent-chemstation-fraction'
traces = ['*fxn']
def events(self, name='fxn', twin=None):
# TODO: use twin
evts = super(AgilentCSFraction, self).events(name, twin)
if name != 'fxn':
return evts
acq_file = open(self.filename, 'rb')
acq_file.seek(0x19)
vers = acq_file.read(7)
if vers.startswith(b'A'):
print('FXN', self.filename)
elif vers == b'notused':
acq_file.seek(0xED)
nfxns = struct.unpack('<I', acq_file.read(4))[0]
acq_file.seek(0x33F)
for _ in range(nfxns):
d = struct.unpack('<Q9fI4f7I', acq_file.read(92))
evts.append({'t0': d[13] / 60000., 't1': d[14] / 60000.})
acq_file.seek(acq_file.tell() + 264)
for i in range(nfxns):
slen = struct.unpack('<H', acq_file.read(2))[0] - 1
evts[i]['name'] = acq_file.read(2 * slen).decode('utf-16')
return evts
class AgilentCSFlowInject(TraceFile):
mime = 'application/vnd-agilent-chemstation-flowinject'
traces = ['*fia']
def events(self, name='fia', twin=None):
# TODO: use twin
evts = super(AgilentCSFlowInject, self).events(name, twin)
if name != 'fia':
return evts
acq_file = open(self.filename, 'rb')
acq_file.seek(0x19)
vers = acq_file.read(7)
if vers.startswith(b'A'):
d = read_reg_file(acq_file)
if not d.get(u'FIARun', False):
return []
prev_f = d['FIASeriesInfo'][1][1]
for f in d['FIASeriesInfo'][1][2:]:
evts.append({'t0': prev_f[0], 't1': f[0], 'name': prev_f[2]})
prev_f = f
else:
if len(evts) > 0:
off_t = evts[-1]['t1'] - evts[-1]['t0']
evts.append({'t0': prev_f[0], 't1': prev_f[0] + off_t,
'name': prev_f[2]})
elif vers == b'notused':
# TODO: get fia from new-style *.REG files.
print('FIA', self.filename)
pass
return evts
class AgilentCSLC(TraceFile):
mime = 'application/vnd-agilent-chemstation-lcstat'
_tr_names = {'pres': 'PMP1, Pressure', 'flow': 'PMP1, Flow',
'slva': 'PMP1, Solvent A', 'slvb': 'PMP1, Solvent B',
'slvc': 'PMP1, Solvent C', 'slvd': 'PMP1, Solvent D'}
@property
@cache(maxsize=1)
def traces(self):
traces = []
for abrv, full in self._tr_names.items():
df = read_multireg_file(open(self.filename, 'rb'), title=full)
if 'Trace' in df:
traces.append(abrv)
return traces
def _trace(self, name, twin):
# TODO: read info from new style REG files
df = read_multireg_file(open(self.filename, 'rb'),
title=self._tr_names[name])
if 'Trace' not in df:
return Trace([], [])
ts = df['Trace']
ts.name = name
return ts.twin(twin)
def read_multireg_file(f, title=None):
"""
Some REG files have multiple "sections" with different data.
This parses each chunk out of such a file (e.g. LCDIAG.REG)
"""
f.seek(0x26)
nparts = struct.unpack('<H', f.read(2))[0]
foff = 0x2D
if title is None:
data = []
for _ in range(nparts):
d = read_reg_file(f, foff)
data.append(d)
foff = f.tell() + 1
else:
for _ in range(nparts):
d = read_reg_file(f, foff)
if d.get('Title') == title:
data = d
break
foff = f.tell() + 1
else:
data = {}
return data
def read_reg_file(f, foff=0x2D):
"""
Given a file handle for an old-style Agilent *.REG file, this
will parse that file into a dictonary of key/value pairs
(including any tables that are in the *.REG file, which will
be parsed into lists of lists).
"""
# convenience function for reading in data
def rd(st):
return struct.unpack(st, f.read(struct.calcsize(st)))
f.seek(0x19)
if f.read(1) != b'A':
# raise TypeError("Version of REG file is too new.")
return {}
f.seek(foff)
nrecs = rd('<I')[0] # TODO: should be '<H'
rec_tab = [rd('<HHIII') for n in range(nrecs)]
names = {}
f.seek(foff + 20 * nrecs + 4)
for r in rec_tab:
d = f.read(r[2])
if r[1] == 1539: # '0306'
# this is part of the linked list too, but contains a
# reference to a table
cd = struct.unpack('<HIII21sI', d)
names[cd[5]] = cd[4].decode('iso8859').strip('\x00')
# except:
# pass
elif r[1] == 32769 or r[1] == 32771: # b'0180' or b'0380'
names[r[4]] = d[:-1].decode('iso8859')
elif r[1] == 32774: # b'0680'
# this is a string that is referenced elsewhere (in a table)
names[r[4]] = d[2:-1].decode('iso8859')
elif r[1] == 32770: # b'0280'
# this is just a flattened numeric array
names[r[4]] = np.frombuffer(d, dtype=np.uint32, offset=4)
data = {}
f.seek(foff + 20 * nrecs + 4)
for r in rec_tab:
d = f.read(r[2])
if r[1] == 1538: # '0206'
# this is part of a linked list
if len(d) == 43:
cd = struct.unpack('<HIII21sd', d)
data[cd[4].decode('iso8859').strip('\x00')] = cd[5]
else:
pass
elif r[1] == 1537: # b'0106'
# name of property
n = d[14:30].split(b'\x00')[0].decode('iso8859')
# with value from names
data[n] = names.get(struct.unpack('<I', d[35:39])[0], '')
elif r[1] == 1793: # b'0107'
# this is a table of values
nrow = struct.unpack('<H', d[4:6])[0]
ncol = struct.unpack('<H', d[16:18])[0]
if ncol != 0:
cols = [struct.unpack('<16sHHHHHI', d[20 + 30 * i:50 + 30 * i])
for i in range(ncol)]
colnames = [c[0].split(b'\x00')[0].decode('iso8859')
for c in cols]
# TODO: type 2 is not a constant size? 31, 17
rty2sty = {1: 'H', 3: 'I', 4: 'f', 5: 'H',
7: 'H', 8: 'd', 11: 'H', 12: 'H',
13: 'I', 14: 'I', 16: 'H'}
coltype = '<' + ''.join([rty2sty.get(c[3], str(c[2]) + 's')
for c in cols])
lencol = struct.calcsize(coltype)
tab = []
for i in reversed(range(2, nrow + 2)):
rawrow = struct.unpack(coltype,
d[-i * lencol: (1 - i) * lencol])
row = []
for j, p in enumerate(rawrow):
if cols[j][3] == 3:
row.append(names.get(p, str(p)))
else:
row.append(p)
tab.append(row)
data[names[r[4]]] = [colnames, tab]
elif r[1] == 1281 or r[1] == 1283: # b'0105' or b'0305'
fm = '<HHBIIhIdII12shIddQQB8sII12shIddQQB8s'
m = struct.unpack(fm, d)
nrecs = m[4] # number of points in table
# x_units = names.get(m[8], '')
x_arr = m[14] * names.get(m[9], np.arange(nrecs - 1))
y_arr = m[25] * names.get(m[20])
y_units = names.get(m[19], '')
if y_units == 'bar':
y_arr *= 0.1 # convert to MPa
# TODO: what to call this?
data['Trace'] = Trace(y_arr, x_arr, name='')
# elif r[1] == 1025: # b'0104'
# # lots of zeros? maybe one or two numbers?
# # only found in REG entries that have long 0280 records
# fm = '<HQQQIHHHHIIHB'
# m = struct.unpack(fm, d)
# print(m)
# #print(r[1], len(d), binascii.hexlify(d))
# pass
# elif r[1] == 512: # b'0002'
# # either points to two null pointers or two other pointers
# # (indicates start of linked list?)
# print(r[1], len(d), binascii.hexlify(d))
# elif r[1] == 769 or r[1] == 772: # b'0103' or b'0403'
# # points to 2nd, 3rd & 4th records (two 0002 records and a 0180)
# b = binascii.hexlify
# print(b(d[10:14]), b(d[14:18]), b(d[18:22]))
return data
def read_regb_file(f):
pass
# CObArray
# either 2 bytes long (0100) or 8 (0000 0380 0580 XXXX)
# CHPAnnText
# [j[36:36+2*struct.unpack('<H', j[34:36])[0]].decode('utf-16') \
# for i, j in a if i == b'CHPAnnText']
# slen = struct.unpack('<H', struct_dat[34:36])
# struct_dat[36:36 + slen * 2].decode('utf-16')
# CHPDatLongRow
# starts at byte 14?
# CHPNdrString
# [j[12:12 + 2 * struct.unpack('<H', j[10:12])[0]].decode('utf-16') \
# for i, j in a if i == b'CHPNdrString']
# CHPNdrObject
# CHPTable