-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathETAP.py
294 lines (273 loc) · 12.7 KB
/
ETAP.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Version 1.0.1 of ETAP Python library file
# Author lross2k
# Email [email protected]
import openpyxl # For Excel files (.xlsx)
import xlwt, xlrd # For old Excel files (.xls)
from datetime import datetime
import time, re, os # For utils
# Class with methods and fields related to generating and manipulating ETAP Lumped files
class Lump():
# Empty initialization
def __init__(self):
self._last_modified = time.localtime()
self._last_cache_xlwt = time.localtime()
self._last_cache_openpyxl = time.localtime()
self._wb = None
self._xlwt_wb = None
self._xlrd_wb = None
self.date_str = "%d/%m/%Y"
self._timestep = None # Y-d-m HH:MM:SS
self._rows = [] # row column syntax
self._gen_header_row()
# Calculate time step used in the current object
def _calc_step(self):
self._timestep = [self._rows[2][9] - self._rows[1][9],
self._rows[2][10] - self._rows[1][10],
self._rows[2][11] - self._rows[1][11]]
# Generate the HEADER of Lump file
def _gen_header_row(self):
self._rows = [['P(MW)','Q(Mvar)','PF%','V(p.u.)','Angle', 'Humidity','Temp C',
'Wind(m/s)','Irradiance(W/m^2)','Hour','Min','Seconds','Date']]
# Allow string conversion of the Lump structure for functions like str(), print()
def __str__(self):
longest = 0
for row in self._rows:
for col in row:
longest = len(str(col)) if len(str(col)) > longest else longest
string = ''
for row in self._rows:
for col in row:
if col != '':
string = string + str(col) + ' '*(longest - len(str(col)))
else:
string = string + ' '*longest
string = string + '\n'
return(string)
# Set the current values for all items in the rows
def set_values(self, P, Q, PF, Hour, Min, Sec, Date, V=None, Angle=None,
Humidity=None, Temp=None, Wind=None, Irradiance=None, date_str=None):
data = [P, Q, PF, V, Angle, Humidity, Temp, Wind,
Irradiance, Hour, Min, Sec, Date]
tmp_row = []
if date_str:
self.date_str = date_str
for i in range(0, len(P)):
for j in range(0, 13):
if data[j] != None:
if j == 12:
if type(data[j][i]) == str:
tmp_row.append(datetime.strptime(data[j][i], self.date_str) if data[j][i] != None else '')
else:
tmp_row.append(data[j][i] if data[j][i] != None else '')
else:
tmp_row.append(data[j][i] if data[j][i] != None else '')
else:
tmp_row.append('')
self._rows.append(tmp_row[::])
tmp_row = []
self._calc_step()
# Save the current data in an Excel file, either xlsx or xls
def save(self, filename, writer='openpyxl'):
if writer == 'openpyxl':
if self._wb != None and self._last_cache_openpyxl > self._last_modified:
self._wb.save(filename)
else:
time.sleep(.1)
self._write_openpyxl_wb()
try:
self.save(filename)
except PermissionError:
print("Couldn't write to file, close it before running the program")
elif writer == 'xlwt':
if self._xlwt_wb != None and self._last_cache_xlwt > self._last_modified:
self._xlwt_wb.save(filename)
else:
time.sleep(.1)
self._write_xlwt_wb()
try:
self.save(filename, writer='xlwt')
except PermissionError:
print("Couldn't write to file, close it before running the program")
# Generate data for an xls file using xlwt
def _write_xlwt_wb(self):
self._xlwt_wb = xlwt.Workbook()
ws = self._xlwt_wb.add_sheet('Sheet1')
date_format = xlwt.XFStyle()
date_format.num_format_str = 'dd/mm/yyyy'
for i in range(0, len(self._rows)):
for j in range(0, 13):
if j == 12:
ws.write(i,j,self._rows[i][j],date_format)
else:
ws.write(i,j,self._rows[i][j])
self._last_cache_xlwt = time.localtime()
# Generate data for an xlsx file using openpyxl
def _write_openpyxl_wb(self):
self._wb = openpyxl.Workbook()
ws = self._wb.active
for i in range(1, len(self._rows)+1):
for j in range(1, 14):
try:
ws.cell(row=i,column=j).value = self._rows[i-1][j-1]
except TypeError:
print(self._rows)
self._last_cache_openpyxl = time.localtime()
# Changes the step used in the Lump file, this doesn't support any value yet,
# it must be either 1h, 30min, 15min, or 1min. data values will be preserved
# to their corresponding step
def change_time_step(self, time_step):
if not self._timestep:
print("No data has been added yet")
return
_rows_copy = self._rows[::]
self._gen_header_row()
factor = (self._timestep[0]*60 + self._timestep[1]) / (time_step[0]*60 + time_step[1])
tmp_row = []
if time_step[1] == 1:
hour = 0
minute = 0
for i in range(1, len(_rows_copy)):
for k in range(0,int(factor)):
for j in range(0, 13):
if j == 10:
tmp_row.append(minute)
minute += time_step[1]
hour = hour if minute < 60 else hour + int(minute/60)
minute = minute if minute < 60 else 0
else:
tmp_row.append(_rows_copy[i][j])
self._rows.append(tmp_row[::])
tmp_row = []
elif type(factor) == float and factor < 1.0:
step = int(1/factor)
for i in range(1, len(_rows_copy), step):
for j in range(0, 13):
tmp_row.append(_rows_copy[i][j])
self._rows.append(tmp_row[::])
tmp_row = []
else:
factor = int(factor)
for i in range(1, len(_rows_copy)):
for k in range(0,factor):
for j in range(0, 13):
if j == 10:
tmp_row.append(time_step[1]*k)
else:
tmp_row.append(_rows_copy[i][j])
self._rows.append(tmp_row[::])
tmp_row = []
self._timestep = time_step
self._last_modified = time.localtime()
# Load lump data from an Excel file using openpyxl for xlsx files and xlrd for xls files
def load_lump(filename, reader='openpyxl', date_str=None):
_lump = Lump()
if reader == 'openpyxl':
_lump._wb = openpyxl.load_workbook(filename)
_ws = _lump._wb.active
P = []; Q = []; PF = []; V = []; Angle = []; Humidity = []; Temp = []
Wind = []; Irradiance = []; Hour = []; Min = []; Sec = []; Date = []
for row in range(2, _ws.max_row+1):
P.append( _ws.cell(row=row, column=1).value)
Q.append( _ws.cell(row=row, column=2).value)
PF.append( _ws.cell(row=row, column=3).value)
V.append( _ws.cell(row=row, column=4).value)
Angle.append( _ws.cell(row=row, column=5).value)
Humidity.append( _ws.cell(row=row, column=6).value)
Temp.append( _ws.cell(row=row, column=7).value)
Wind.append( _ws.cell(row=row, column=8).value)
Irradiance.append(_ws.cell(row=row, column=9).value)
Hour.append( _ws.cell(row=row, column=10).value)
Min.append( _ws.cell(row=row, column=11).value)
Sec.append( _ws.cell(row=row, column=12).value)
Date.append( _ws.cell(row=row, column=13).value)
_lump.set_values(P,Q,PF,Hour,Min,Sec,Date,V,Angle,Humidity,
Temp,Wind,Irradiance,date_str=date_str)
elif reader == 'xlrd':
_lump._xlrd_wb = xlrd.open_workbook(filename)
_ws = _lump._xlrd_wb.sheet_by_index(0)
P = []; Q = []; PF = []; V = []; Angle = []; Humidity = []; Temp = []
Wind = []; Irradiance = []; Hour = []; Min = []; Sec = []; Date = []
for row in range(1, _ws.nrows):
P.append( _ws.cell_value(rowx=row, colx=0))
Q.append( _ws.cell_value(rowx=row, colx=1))
PF.append( _ws.cell_value(rowx=row, colx=2))
V.append( _ws.cell_value(rowx=row, colx=3))
Angle.append( _ws.cell_value(rowx=row, colx=4))
Humidity.append( _ws.cell_value(rowx=row, colx=5))
Temp.append( _ws.cell_value(rowx=row, colx=6))
Wind.append( _ws.cell_value(rowx=row, colx=7))
Irradiance.append(_ws.cell_value(rowx=row, colx=8))
Hour.append( _ws.cell_value(rowx=row, colx=9))
Min.append( _ws.cell_value(rowx=row, colx=10))
Sec.append( _ws.cell_value(rowx=row, colx=11))
Date.append( _ws.cell_value(rowx=row, colx=12))
_lump.set_values(P,Q,PF,Hour,Min,Sec,Date,V,Angle,Humidity,
Temp,Wind,Irradiance,date_str=date_str)
else:
print('Unsupported reader, aborting')
return
return(_lump)
''' Higher level function, allows to change the timestep of a file while also managing
automatically most of the file format and folder structure dependent parameters '''
def _timeshift_generic(filename, folder='.', hour=0, minutes=0,
date_str="%Y-%d-%m %H:%M:%S", alt_date_str="%d/%m/%Y"):
if hour != 0 and minutes != 0:
print('Unsuported timestep operation, aborting')
return
if re.search('.xls$', filename):
reader = 'xlrd'
elif re.search('.xlsx$', filename):
reader = 'openpyxl'
else:
print('File:',filename,'should have .xls or .xlsx extension, aborting')
return
try:
_lump_ = load_lump(filename, reader=reader, date_str=date_str)
except ValueError:
try:
_lump_ = load_lump(filename, reader=reader, date_str=alt_date_str)
except ValueError:
print('File:',filename,'uses a different date format, specify it with date_str parameter, aborting')
return
if hour != 0:
if _lump_._timestep[0] == hour:
print('File:',filename,'already had given hour time step, aborting')
return
else:
_lump_.change_time_step([hour,0,0])
elif minutes != 0:
if _lump_._timestep[1] == minutes:
print('File:',filename,'already had given minutes time step, aborting')
return
else:
_lump_.change_time_step([0,minutes,0])
try:
os.mkdir(folder)
except FileExistsError:
pass
if reader == 'openpyxl':
filename = filename.replace('xlsx','xls')
_lump_.save(folder+'/'+filename, writer='xlwt')
def timeshift_to_60_minutes(filename, date_str="%Y-%d-%m %H:%M:%S"):
_timeshift_generic(filename, folder='60_MIN', hour=1, date_str=date_str)
def timeshift_to_15_minutes(filename, date_str="%Y-%d-%m %H:%M:%S"):
_timeshift_generic(filename, minutes=15, folder='15_MIN', date_str=date_str)
def timeshift_to_30_minutes(filename, date_str="%Y-%d-%m %H:%M:%S"):
_timeshift_generic(filename, minutes=30, folder='30_MIN', date_str=date_str)
def timeshift_to_1_minutes(filename, date_str="%Y-%d-%m %H:%M:%S"):
_timeshift_generic(filename, minutes=1, folder='1_MIN', date_str=date_str)
def _glob_timeshift_generic(func, date_str="%Y-%d-%m %H:%M:%S"):
files = []
for file in os.listdir():
if re.search('[.]xl.+$', file):
files.append(file)
for file in files:
func(file, date_str=date_str)
def glob_timeshift_to_60_minutes(date_str="%Y-%d-%m %H:%M:%S"):
_glob_timeshift_generic(timeshift_to_60_minutes, date_str=date_str)
def glob_timeshift_to_30_minutes(date_str="%Y-%d-%m %H:%M:%S"):
_glob_timeshift_generic(timeshift_to_30_minutes, date_str=date_str)
def glob_timeshift_to_15_minutes(date_str="%Y-%d-%m %H:%M:%S"):
_glob_timeshift_generic(timeshift_to_15_minutes, date_str=date_str)
def glob_timeshift_to_1_minutes(date_str="%Y-%d-%m %H:%M:%S"):
_glob_timeshift_generic(timeshift_to_1_minutes, date_str=date_str)