-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctc_v2_new.py
100 lines (80 loc) · 3.08 KB
/
ctc_v2_new.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
88
89
90
91
92
93
94
95
96
97
98
99
100
import pandas as pd
import numpy as np
import os
import glob
from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS
from datetime import date
# Timezone:
timezone = 'CET'
# Influxdb parameters:
url = 'localhost:8086'
token = "YourToken"
org = "YourOrg"
bucket = "ctc/autogen"
client = InfluxDBClient(url=url, token=token, org=org)
def main(_tz=timezone, _client=client):
"""
Main function to be run
:return: prints success message
"""
df = pd.DataFrame()
files = glob.glob('*.csv')
for file in files:
# parse(file)
df = df.append(read_csv(file, _tz), sort=False)
df.sort_index(inplace=True)
influx(df, _client)
for file in files:
os.remove(file)
return print(len(files), 'csv file(s) have been parsed and pushed to the influxDB database')
def read_csv(file, tz):
"""
Reads the file into a pandas dataframe, cleans data and rename columns
:param file: file to be read
:param tz: timezone
:return: pandas dataframe
"""
df = pd.read_csv(file, index_col=0, parse_dates=True, header=1)
df.index = df.index.tz_localize(tz, ambiguous='NaT')
df = df.loc[df.index.notnull()]
df = df.loc[~df.index.duplicated(keep='first')]
return df
def influx(DataFrame, client):
"""
Instantiates influxdb and writes the dataframe to the database
:param DataFrame: DataFrame to be written to db
:param host: optional if other than localhost
:param port: optional if other than 8086
:return: Name of database that has been written to
"""
today = str(date.today())
measurements = {'temperature': ['Outdoor temp',
'TankUpperTemp',
'TankLowerTemp',
'RoomTemperature1',
'RoomTemperature2',
'HeatWater1Temp',
'Return temp',
'BrineInTemp',
'BrineOutTemp',
'PrimarySystemInTemp',
'PrimarySystemOutTemp',
'DischargeGasTemp',
'SuctionTemp',
'Superheat',
'HetgasOverheat'],
'electric power': ['ElBoilerUsedPwr',
'InverterMotorPower'],
'electric current': ['CurrentL1',
'CurrentL2',
'CurrentL3',
'Compressor L1']
}
write_api = client.write_api(write_options=SYNCHRONOUS)
for x,y in measurements.items():
data = DataFrame.filter(y, axis=1)
write_api.write(bucket, org, record=data, data_frame_measurement_name=x, record_tag_keys={'source': 'ctc_csv', 'date_read': today})
return
if __name__ == '__main__':
main()