-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode.py
More file actions
78 lines (60 loc) · 2.18 KB
/
Copy pathdecode.py
File metadata and controls
78 lines (60 loc) · 2.18 KB
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
# Decodes binary files created by usb_omz.py
import csv
import json
##################################
#### Definition ####
##################################
def bin2dat(foldername,filename):
try:
fileobj = open(foldername + '/' + filename,'rb')
bin_data = fileobj.read()
finally:
fileobj.close()
sampleFreq = float(filename[filename.index('R') + 1 : filename.index('.bin')])
# mvolts = numpy.zeros(len(bin_data)//2)
# time = numpy.arange(len(mvolts)) / sampleFreq
mvolts = [0 for i in range(0,len(bin_data)//2)]
time = [i/sampleFreq for i in range(0,len(mvolts))]
for i in range(0, len(bin_data)-2, 2):
mvolts[i//2] = int.from_bytes(bin_data[i:i+2], byteorder='little', signed=True)
data = [ time, mvolts ]
data = list(map(list,zip(*data)))
with open(foldername + '/' + filename[0:filename.index('.bin')] + '.csv', 'w',newline='') as f:
writer = csv.writer(f)
writer.writerows(data)
print(foldername+'\t'+filename+'\t'+str(len(data)))
##################################
#### JSON File Saving ####
##################################
# dataset = {
# 'name':filename,
# 'data':data
# }
# chart = {
# 'status': True,
# 'data': {
# 'chart_title': foldername[ foldername.index('_') + 1 : ],
# 'colors': [
# '#e71d36'
# ],
# 'xaxis_title': 'sec',
# 'yaxis_title': 'mV',
# 'datasets': [dataset]
# }
# }
# with open(foldername + '/' + filename + '.json', 'w') as jsonfile:
# json.dump(chart, jsonfile, indent=4, sort_keys=True)
# jsonfile.close()
##################################
#### Main Loop ####
##################################
# Automate pick recent directory
import os
allfolders = [name for name in os.listdir() if os.path.isdir(name)]
datafolders = [name for name in allfolders if name.startswith('data_')]
print('foldername\t\tfilename\tdata')
for folder in datafolders:
datafiles = [name for name in os.listdir(folder) if name.endswith('.bin')]
for datafile in datafiles:
if not os.path.isfile(folder + '/' + datafile[0:-3]+'csv'):
bin2dat(folder,datafile)