-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper_davis.py
More file actions
81 lines (71 loc) · 2.16 KB
/
wrapper_davis.py
File metadata and controls
81 lines (71 loc) · 2.16 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
79
80
81
from datetime import datetime
import sys
import csv
import MySQLdb
estacion_id = 1
# IDs de cada sensor en la BD para cada sensor de esta estacion
sensor_ids = {
"barometer": 3,
"pressure": 11,
"temperature": 4,
"humidity": 10,
"wind_speed": 1,
"wind_direction":6,
"wind_gust": 16,
"wind_gust_dir": 15,
"rain_rate": 13,
"rain": 9,
"dewpoint": 5,
"windchill": 2,
"heat_index": 12,
"et": 14,
"radiation": 8,
"uv": 7
}
# en que columnas del csv de entrada se encuentra cada variable
csv_columns = {
"barometer": 3,
"pressure": 4,
"temperature": 7,
"humidity": 9,
"wind_speed": 10,
"wind_direction": 11,
"wind_gust": 12,
"wind_gust_dir": 13,
"rain_rate": 14,
"rain": 15,
"dewpoint": 16,
"windchill": 17,
"heat_index": 18,
"et": 19,
"radiation": 20,
"uv": 21,
}
def parse_date(s):
return datetime.strptime(s[:-6], "%Y-%m-%dT%H:%M:%S")
def process(filename):
r = csv.reader(open(filename))
r.next() # descartar el header
dbrows = dict((k, []) for k in sensor_ids)
for row in r:
assert len(row) == 22
date = parse_date(row[0])
for name,index in csv_columns.iteritems():
try:
dbrows[name].append((date, float(row[index])))
except ValueError:
# print "hubo un error parseando '%s' (%s) como float en el dia %s" % (row[index],
# name,
# date)
pass
conn = MySQLdb.connect(host="localhost",
user="emisiones",
passwd="emisiones",
db="emisiones")
cur = conn.cursor()
for table, values in dbrows.iteritems():
cur.executemany("INSERT INTO %s (measureDate, value, sensorId) VALUES (%%s, %%s, %%s)" % table,
[(date, value, sensor_ids[table]) for date, value in values])
conn.commit()
if __name__ == "__main__":
process(sys.argv[1])