-
Notifications
You must be signed in to change notification settings - Fork 0
/
datahandler.py
70 lines (53 loc) · 2.1 KB
/
datahandler.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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 18 14:41:53 2024
@author: laqm
"""
import csv
import datetime
class DataHandler:
def __init__(self, file_path=None):
self.current_time = datetime.datetime.now().strftime("%m-%d-%Y %I.%M%p")
if file_path:
self.csv_file_path = file_path
else:
self.csv_file_path = f"C:\\Users\\ppms\\Documents\\CSV Data Outputs\\{self.current_time}.csv"
self.fieldnames = ['Time', 'Harmonic', 'Voltage', 'Frequency', 'Channel1(X)', 'Channel2(Y)', 'Temperature', 'Field']
self.data = {
'Time': [],
'Harmonic': [],
'Voltage': [],
'Frequency': [],
'Channel1(X)': [],
'Channel2(Y)': [],
'Temperature': [],
'Field': []
}
def write_to_csv(self, info):
with open(self.csv_file_path, mode='a', newline="") as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=self.fieldnames)
csv_writer.writerow(info)
def write_header(self):
with open(self.csv_file_path, mode='w', newline='') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=self.fieldnames)
csv_writer.writeheader()
def append_data(self, time, harmonic, voltage, freq, channel1, channel2, temp, field):
info = {
"Time": time,
"Harmonic": harmonic,
"Voltage": voltage,
"Frequency": freq,
"Channel1(X)": float(channel1),
"Channel2(Y)": float(channel2),
'Temperature': temp,
'Field': field
}
self.data['Time'].append(time)
self.data['Harmonic'].append(harmonic)
self.data['Voltage'].append(voltage)
self.data['Frequency'].append(freq)
self.data['Channel1(X)'].append(float(channel1))
self.data['Channel2(Y)'].append(float(channel2))
self.data['Temperature'].append(temp)
self.data['Field'].append(field)
self.write_to_csv(info)