/
total-robust-consumption.py
executable file
·134 lines (97 loc) · 4.47 KB
/
total-robust-consumption.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Author = Joseph Cameron
# total-robust-consumption.py
# Displays energy consumption for every hour, day, week and month.
# USAGE
# Hourly Line Graph = python total-robust-consumption.py -d Clean-Gas-Data/data.csv Clean-Electric-Data/data.csv
# Daily Line Graph = python total-robust-consumption.py -g Clean-Gas-Data/data.csv Clean-Electric-Data/data.csv
# Weekly Line Graph = python total-robust-consumption.py -w Clean-Gas-Data/data.csv Clean-Electric-Data/data.csv
# Monthly Line Graph = python total-robust-consumption.py -m Clean-Gas-Data/data.csv Clean-Electric-Data/data.csv
# --------------------------------------------------
# PARSE ARGUMENTS
import argparse
parser = argparse.ArgumentParser(description='Display a sum of energy consumption over every hour within a day, every day, every week or every month.')
group = parser.add_mutually_exclusive_group()
group.add_argument('-d', '--default', action='store_true', help='Display for every hour.')
group.add_argument('-g', '--giorno', action='store_true', help='Display for every day.')
group.add_argument('-w', '--week', action='store_true', help='Display for every week.')
group.add_argument('-m', '--month', action='store_true', help='Display for every month.')
parser.add_argument('gasData', metavar='N', type=str, nargs='+', help='The .csv file containing gas data.')
parser.add_argument('electricData', metavar='N', type=str, nargs='+', help='The .csv file containing electric data.')
args = parser.parse_args()
# --------------------------------------------------
# IMPORT STATEMENTS
import pandas as pd
from datetime import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pickle
import sys
# --------------------------------------------------
# DataFrame Headers
headers = ['Time', 'Sensor Value']
# --------------------------------------------------
# READ GAS DATA
# Read .csv while getting rid of IDEAL data strings surrounding the data on the first and/or last rows
# Be careful with skiprows and skipfooter, maybe remove them
gas_data = pd.read_csv(sys.argv[2], parse_dates = {"Datetime" : [0]}, names = headers, skiprows = 1, skipfooter = 1)
# Formatting dates
gas_data['Time'] = gas_data['Datetime'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S'))
# Change index to Time for proper resampling
gas_data = gas_data.set_index("Time")
# --------------------------------------------------
# READ ELECTRIC DATA
# Read .csv while getting rid of IDEAL data strings surrounding the data on the first and/or last rows
# Be careful with skiprows and skipfooter, maybe remove them
elec_data = pd.read_csv(sys.argv[3], parse_dates = {"Datetime" : [0]}, names = headers, skiprows = 1, skipfooter = 1)
# Formatting dates
elec_data['Time'] = elec_data['Datetime'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S'))
# Change index to Time for proper resampling
elec_data = elec_data.set_index("Time")
# --------------------------------------------------
# Variable for x-axis label
xlabel = "Default x-axis label"
# CREATE LINE GRAPH
# Resample by hour, day, week or month and plot the sum
# HOUR
if args.default:
gasConsumption = gas_data.resample('H').sum()
electricConsumption = elec_data.resample('H').sum()
xlabel = "hours"
# DAY
elif args.giorno:
gasConsumption = gas_data.resample('D').sum()
electricConsumption = elec_data.resample('D').sum()
xlabel = "days"
# WEEK
elif args.week:
gasConsumption = gas_data.resample('W').sum()
electricConsumption = elec_data.resample('W').sum()
xlabel = "weeks"
# MONTH
elif args.month:
gasConsumption = gas_data.resample('M').sum()
electricConsumption = elec_data.resample('M').sum()
xlabel = "months"
# DEFAULT = HOUR
else:
gasConsumption = gas_data.resample('H').sum()
electricConsumption = elec_data.resample('H').sum()
xlabel = "hours"
# --------------------------------------------------
# SHOW PLOT
plt.plot(gasConsumption.index.get_values(), gasConsumption['Sensor Value'], label='Gas Consumption')
plt.plot(gasConsumption.index.get_values(), (gasConsumption['Sensor Value'] / 2.5), label='ASHP Consumption (COP = 2.5)')
plt.plot(electricConsumption.index.get_values(), electricConsumption['Sensor Value'], label='Electricity Consumption')
# Properly format the x-labels
plt.gcf().autofmt_xdate()
# Y-Axis Label
plt.ylabel("Watt Hours")
# X-Axis Label
plt.xlabel("Time (in " + xlabel + ")")
# Title of Plot
plt.title("Energy Consumption Sum")
# Legend of Plot
plt.legend()
# Show final plot
plt.show()