-
Notifications
You must be signed in to change notification settings - Fork 4
/
grid.py
165 lines (131 loc) · 4.46 KB
/
grid.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : grid.py
# Author : tzhang
# Date : 18.11.2019
# Last Modified Date: 30.07.2020
# Last Modified By : tzhang
import csv
from datetime import datetime as dtCal
import numpy as np
from matplotlib import pyplot as plt
"""
a module to read grid demand data (in csv format)
- to be done: generate a nomalized daily grid data
"""
class grid:
def __init__(self,dataMode,inFile=None,multiplier=1.0):
self.dataMode = dataMode # mode for grid data, 0 for read from input file,
self.multiplier = float(multiplier)
self.inFile = inFile
# grid data
self.date = []
self.time = []
self.demand = []
# read grid data from csv file
def _readData_(self):
date = []
demand = []
with open(self.inFile) as csv_file:
csv_data = csv.reader(csv_file,delimiter=',')
for row in csv_data:
date.append(row[1])
demand.append(row[2])
del date[0]
del demand[0]
# print (date[-1])
# print (demand[-1])
return date, demand
# convert date file into time array, in minute
def _date_time_converter_(self,date):
time = []
date0 = date[0].split()[0]
clock0 = date[0].split()[1]
year0 = int(date0.split('-')[0])
month0 = int(date0.split('-')[1])
day0 = int(date0.split('-')[2])
hour0 = int(clock0.split(':')[0])
minute0 = int(clock0.split(':')[1])
second0 = 0
date_ini = dtCal(year0,month0,day0,hour0,minute0,second0)
for data in date:
date_curr = data.split()[0]
clock_curr = data.split()[1]
year = int(date_curr.split('-')[0])
month = int(date_curr.split('-')[1])
day = int(date_curr.split('-')[2])
hour = int(clock_curr.split(':')[0])
minute = int(clock_curr.split(':')[1])
second = 0
date_curr = dtCal(year,month,day,hour,minute,second)
dTime = date_curr - date_ini
# calculate current time in unit minute
t_curr = dTime.days*1440 + dTime.seconds/60
time.append(t_curr)
return time
# scale the demand
def _demand_scale_(self,demand):
demand = np.asarray(demand,dtype = float)
demand = demand*self.multiplier
demand = list(demand)
return demand
# generate grid data
def gen(self):
if self.dataMode == 0:
date,demand = grid._readData_(self)
time = grid._date_time_converter_(self,date)
if self.multiplier != 1.0:
demand = grid._demand_scale_(self,demand)
else:
# to be imporved
pass
self.date = self.date + date
self.time = self.time + time
self.demand = self.demand + demand
# plot grid demand data
def demand_plot(self):
plt.figure(figsize = (12,8))
plt.plot(self.time,self.demand, color = 'g')
plt.xlabel('Time (min)',fontsize = '16')
plt.xlim(left = 0.0)
plt.ylabel('Grid Demand (MW)', fontsize = '16')
plt.grid(linestyle='--',linewidth = '1')
pltName = 'grid_demand.png'
# plt.show()
# plt.close()
plt.savefig(pltName,dpi = 100)
# aquire time data
def aquire_time(self):
return self.time
# aquire demand data
def aquire_demand(self):
return self.demand
# aquire demand data as numpy array
def aquire_demand_array(self):
P_demand = np.asarray(self.demand,dtype = float)
return P_demand
# plot user defined grid demand data
def user_demand_plot(self,time,demand):
plt.figure(figsize = (12,8))
plt.plot(time,demand, color = 'g')
plt.xlabel('Time (min)',fontsize = '16')
plt.xlim(left = 0.0)
plt.ylabel('Grid Demand (MW)', fontsize = '16')
plt.grid(linestyle='--',linewidth = '1')
pltName = 'user_grid_demand.png'
# plt.show()
# plt.close()
plt.savefig(pltName,dpi = 100)
"""
a class test
the test data comes from: G.B. National Grid Status, https://www.gridwatch.templar.co.uk/
dataMode = 0
inFile = 'UK_gridwatch_year2018_Jan.csv'
multiplier = 0.005
uk_grid = grid(dataMode,inFile,multiplier)
uk_grid.gen()
uk_grid.demand_plot()
print (uk_grid.date)
print (uk_grid.time)
print (uk_grid.demand)
"""