-
Notifications
You must be signed in to change notification settings - Fork 11
/
importAgilentBin.py
74 lines (64 loc) · 2.58 KB
/
importAgilentBin.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
import sys
import numpy as np
import struct
from collections import namedtuple
def readfile(binFile, arg):
"""read binFile and return data as numpy array
:binFile: path to bin file
:arg: an int, indicate the nth waveform to read
"""
selWav = 0
headerFmt = "5if3d2i16s16s24s16sdI"
headerSiz = struct.calcsize(headerFmt)
waveformHeader = namedtuple("waveformHeader",
"headerSize waveformType nWaveformBuffers nPoints \
count xDisplayRange xDisplayOrigin xIncrement \
xOrigin xUnits yUnits dateString timeString \
frameString waveformString timeTag segmentIndex")
bufHeaderFmt = "ihhi"
bufHeaderSiz = struct.calcsize(bufHeaderFmt)
bufHeader = namedtuple("bufHeader",
"headerSize bufferType bytesPerPoint bufferSize")
data = None
time = None
try:
fd = open(binFile, 'rb')
magic, fileVer, fileSize, nWav = struct.unpack(
'2s2sii', fd.read(12))
if magic.decode('ascii') != "AG":
sys.stderr.write("Unrecognized file format\n")
return None, None
if arg and arg <= nWav:
selWav = arg
for idx in range(nWav):
# read waveform header
header = waveformHeader._make(
struct.unpack(headerFmt, fd.read(headerSiz)))
# skip remaining data in the header
fd.seek(header.headerSize - headerSiz, 1)
if idx == selWav:
stop = header.xIncrement * header.nPoints + header.xOrigin
time = np.linspace(header.xOrigin, stop, header.nPoints)
for bufIdx in range(header.nWaveformBuffers):
bufferHeader = bufHeader._make(
struct.unpack(bufHeaderFmt, fd.read(bufHeaderSiz)))
if idx == selWav:
if bufferHeader.bufferType in [1, 2, 3]:
fmt = "%df" % (header.nPoints)
elif bufferHeader.bufferType == 4:
fmt = "%di" % (header.nPoints)
elif bufferHeader.bufferType == 5:
fmt = "%dB" % (header.nPoints)
else:
fmt = "%dB" % (header.bufferSize)
data = np.asarray(struct.unpack(
fmt, fd.read(struct.calcsize(fmt))))
else:
# skip waveform
fd.seek(bufferHeader.bufferSize, 1)
return time, data
except IOError:
sys.stderr.write("%s open error\n" & (binFile))
return None, None
finally:
fd.close()