-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlread.py
More file actions
74 lines (54 loc) · 1.76 KB
/
sqlread.py
File metadata and controls
74 lines (54 loc) · 1.76 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
import sqlite3
from sqlite3 import Error
import matplotlib.pyplot as plt
import numpy as np
from datetime import timedelta
#function which connects to the database
def create_wal_connection(db_file):
""" create a database connection to a SQLite database """
try:
conn = sqlite3.connect(db_file)
conn.cursor().execute("PRAGMA journal_mode=WAL")
return conn
except Error as e:
print(e)
return None
#makes each row an element of a list
#each row is represented by a tuple
#table is either 'TPH' or 'PC'
def db_to_list(conn, table):
cur = conn.cursor()
cur.execute("SELECT * FROM {}".format(table))
rows = cur.fetchall()
return rows
#accepts a weather tuple and returns the datetime string
def datestr(tup):
out = tup[0][0:4]+tup[0][5:7]+tup[0][8:10]+tup[0][11:13]+tup[0][14:16]
return out
#makes a plot of the temperature for the entire rawlist
def tempplot(rawlist):
xlist = []
ylist = []
labels = []
for entry in rawlist:
datetime = int(datestr(entry))
xlist.append(datetime)
ylist.append(entry[2])
label = entry[0][0:16]
labels.append(label)
sparselabels = ['']*len(labels)
sparselabels[0::15] = labels[0::15]
nat = np.arange(len(ylist))
plt.figure(num=None, figsize=(3*6.4,3*4.8), dpi=300)
plt.plot(nat,ylist)
plt.xticks(nat, sparselabels, rotation='vertical')
ax = plt.gca()
ax.set_title('Temperature ID: 106')
ax.set_xlabel('date/time')
ax.set_ylabel('Temperature (C)')
plt.savefig('tempplot.pdf')
database = "/home/teststand/SQLite/database/weather.db"
conn = create_wal_connection(database)
tphlist = db_to_list(conn, 'TPH')
#print(tphlist[-1])
tempplot(tphlist)