-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinReader.py
94 lines (83 loc) · 3.34 KB
/
binReader.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
import struct
import glob
import os
import time
import datetime
with open('timestamps.csv','w') as fout:
for fname in glob.glob('*.bin'):
#fname='sample_16field_50Hz 2014-0517-1206-33.bin'
print fname
print >>fout, fname, ',',
print >>fout, os.stat(fname).st_ctime, ',',
with open(fname,'rb') as f:
# read a string looks like version number
fmt1 = 'B'
st1 = struct.Struct(fmt1)
i1 = st1.unpack(f.read(st1.size))[0]
print 'len=',i1
fmt2 = '<%ds'%i1
st2 = struct.Struct(fmt2)
s2 = st2.unpack(f.read(st2.size))[0]
print 'version=',s2
# looks like another string
fmt3 = 'B'
st3 = struct.Struct(fmt3)
i3 = st3.unpack(f.read(st3.size))[0]
print 'len=',i3
fmt4 = '<%ds'%i3
st4 = struct.Struct(fmt4)
s4 = st4.unpack(f.read(st4.size))[0]
print 'unknown=',s4
# read the number of data fields
fmt5 = '4B'
st5 = struct.Struct(fmt5)
i5 = st5.unpack(f.read(st5.size))[0]
print '#channels=',i5
for chan in range(i5):
fmt6 = 'B'
st6 = struct.Struct(fmt6)
i6 = st6.unpack(f.read(st6.size))[0]
fmt7 = '<%ds'%i6
st7 = struct.Struct(fmt7)
s7 = st7.unpack(f.read(st7.size))[0]
print 'channel=',s7,
fmt6 = 'B'
st6 = struct.Struct(fmt6)
i6 = st6.unpack(f.read(st6.size))[0]
fmt7 = '<%ds'%i6
st7 = struct.Struct(fmt7)
s7 = st7.unpack(f.read(st7.size))[0]
print ' name=',s7,
fmt6 = 'B'
st6 = struct.Struct(fmt6)
i6 = st6.unpack(f.read(st6.size))[0]
fmt7 = '<%ds'%i6
st7 = struct.Struct(fmt7)
s7 = st7.unpack(f.read(st7.size))[0]
print ' unit=',s7
fmt12 = 'Q'
#fmt12 = '2L'
st12 = struct.Struct(fmt12)
d12 = st12.unpack(f.read(st12.size))
print 'timestamp = ', d12
print >>fout, d12[0], ',',
# Fix this. I am manually adjusting for some time zone error.
# The value is stored by Msoft's System::DateTime::ToBinary.
# You can run a program to get the value for 1970-01-01 0:0:0.
ts = (d12[0] - 621355968000000000L) * 1.0E-07
ts = ts + 7 * 60 * 60
dt = datetime.datetime.fromtimestamp(ts)
#print >>fout, ts, ',', dt.isoformat()
#print >>fout, ts, ',', dt.strftime('%Y-%m-%d %H:%M:%S.%f')
print >>fout, ts, ',', dt.strftime('%Y-%m-%d %H:%M:%S')
#print >>fout, d12[0], ',', d12[1]
fmt13 = '<d'
st13 = struct.Struct(fmt13)
d13 = st13.unpack(f.read(st13.size))[0]
print 'deltat = ', d13, 's'
fmt = '<%dd'%i5
st = struct.Struct(fmt)
#while True:
for row in range(1):
data = st.unpack(f.read(st.size))
print data