-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtractAccREST.py
87 lines (72 loc) · 2.85 KB
/
ExtractAccREST.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
from __future__ import division
from numpy.fft import fft
import web
import csv
#Spit it out as a .csv
urls = (
'/extract', 'extract'
)
app = web.application(urls, globals())
class extract:
def GET(self):
return "extract Acc"
def POST(self):
i = web.data()
infile = open('ExtractinFile.csv', 'w+')#create infile, write (i), close it
infile.write(i)
infile.close()
infile = open('ExtractinFile.csv')
#infile = open('DataWithDFT.csv')
#print i
outfile = open('ExtractedAccDFTData10.csv', 'w+')
def avg_str(l):
return str(int(round((sum(l) / len(l)))))
tsIdx = -1 # row index of timestamp
magIdx = -1 # row index of acceleration magnitude
uid = -1 # row index of unique id added by addDFT.py
e1Idx = -1 # row index of DFT energy co-efficient at 1Hz
e2Idx = -1 # row index of DFT energy co-efficient at 2Hz
e3Idx = -1 # row index of DFT energy co-efficient at 3Hz
newData = [] # to hold the discretized data
# Reading the contents of input file and discretizing it on the go:
with infile:
contents = csv.reader(infile, delimiter=',')
first = True
for row in contents:
if first:
first = False
tsIdx = row.index('timestamp')
magIdx = row.index('magnitude')
uid = row.index('uid')
e1Idx = row.index('DFT_E1')
e2Idx = row.index('DFT_E2')
e3Idx = row.index('DFT_E3')
else:
ruid = int(row[uid])
# ruid -> unique identifier for the group of rows over which
# a particular set of DFT values was calculated.
# The discretization step:
if len(newData) <= ruid:
newData.append([row])
else:
newData[ruid].append(row)
# this is used to find average timestamp value below
# Writing the discretized data to file:
with outfile:
outfile.write('timestamp,magnitude,DFT_E1,DFT_E2,DFT_E3\n')
for x in newData:
ts = avg_str([float(y[tsIdx]) for y in x])
new_row = ts + ',' + x[0][magIdx] + ',' + x[0][e1Idx] + ',' + x[0][e2Idx] + ',' + x[0][e3Idx] + '\n'
outfile.write(new_row)
d = '' #this is return logic
outfile.close()
outfile = open('ExtractedAccDFTData10.csv', 'r')
a = 0
for curline in outfile:
d = d+curline
web.header('Content-Type','text/csv') #this makes browser autodownload
#outfile.close()
#infile.close()
return d
if __name__ == "__main__":
app.run()