-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.py
More file actions
executable file
·122 lines (96 loc) · 4.36 KB
/
chart.py
File metadata and controls
executable file
·122 lines (96 loc) · 4.36 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
""" version: chart.py v1.2 """
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import DateFormatter
import os
import re
debug = True
data_path = '/scripts/TEMPer2.data'
timestamps = []
internal_temps = []
external_temps = []
# Iterate over all files in the directory
for filename in os.listdir(data_path):
file = os.path.join(data_path, filename)
if os.path.isfile(file): # Check if the current item is a file (not a subdirectory)
# open the data file
file = open(file)
# read the file as a list
lines = file.readlines()
# close the file
file.close()
length = len(lines)
for counter in range(length):
counter += 1
if debug:
if counter > 1000: break # currently only view first 100 lines for debugging
try:
line = lines[counter]
next_line = lines[counter+1]
next_next_line = lines[counter+2]
line = re.sub('\s+','',line) # remove all whitespace
next_line = re.sub('\s+','',next_line)
next_next_line = re.sub('\s+','',next_next_line)
if len(line) == 0: # ignore empty lines
continue
if "===" in line and "internaltemperature" in next_line and "externaltemperature" in next_next_line: # a time line was found
element_array = line.split("===") # try to detect 2024-01-24===11:16:5 lines
date_string = re.sub('\s+','',element_array[0])
time_string = re.sub('\s+','',element_array[1])
timestamps.append(date_string+" "+time_string)
element_array = next_line.split(":") # try to detect 2024-01-24===11:16:5 lines
internaltemperature = re.sub('\s+','',element_array[1])
internaltemperature = internaltemperature.replace(",", "")
internal_temps.append(internaltemperature)
element_array = next_next_line.split(":") # try to detect 2024-01-24===11:16:5 lines
externaltemperature = re.sub('\s+','',element_array[1])
externaltemperature = externaltemperature.replace(",", "")
external_temps.append(externaltemperature)
except IndexError:
status = "IndexError: list index out of range"
# Sample data for both temperature measurements
# timestamps = ['2024-01-24 11:16:57', '2024-01-24 12:30:00', '2024-01-24 14:45:30', '2024-01-24 16:00:00']
# print(internal_temps+"\n")
internal_temps_len = len(internal_temps)
# print(external_temps+"\n")
external_temps_len = len(external_temps)
# print(timestamps+"\n")
timestamps_len = len(timestamps)
"""
# output data to file
file_path = "./output.txt"
with open(file_path, 'a') as file:
# Iterate over the array and append each element to the file
for element in internal_temps:
file.write(element + '\n')
for element in external_temps:
file.write(element + '\n')
for element in timestamps:
file.write(element + '\n')
"""
# Convert timestamps to datetime objects
x_values = [datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') for ts in timestamps]
# Plotting the External temperature chart
plt.plot(x_values, external_temps, marker='o', linestyle='-', color='orange', label='External')
# to display the 12°C value at every dot uncomment next 2 lines:
# for x, y in zip(x_values, external_temps):
# plt.text(x, y, f'{y}', ha='center', va='bottom', fontsize=8)
# Plotting the Internal temperature chart
plt.plot(x_values, internal_temps, marker='o', linestyle='-', color='blue', label='Internal')
# to display the 12°C value at every dot uncomment next 2 lines:
#for x, y in zip(x_values, internal_temps):
# plt.text(x, y, f'{y}', ha='center', va='bottom', fontsize=8)
# Adding labels and title
plt.xlabel('Time')
plt.ylabel('Temperature (°C)')
plt.title('TEMPer2 Chart')
# Formatting the x-axis to display the date and time
plt.xticks(rotation=45, ha='right', rotation_mode='anchor', fontsize=8)
# Formatting the x-axis to display the date and time in YYYY-MM-DD hh:mm format
date_formatter = DateFormatter('%Y-%m-%d %H:%M')
plt.gca().xaxis.set_major_formatter(date_formatter)
plt.gcf().autofmt_xdate()
# Adding legend
plt.legend()
# Display the plot
plt.show()