-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
337 lines (305 loc) · 14.7 KB
/
convert.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import os
####################################
# idf_file: path of idf (e.g. /home/user/eplus/model)
####################################
def idfUpdate(idf_file):
# read idf file
f=open(idf_file+'.idf','r')
lines=f.readlines()
f.close()
# identify the zones
sch=[]# get 'ZoneControl:Thermostat,' !- Control 1 Name
zone=[]# get 'ZoneControl:Thermostat,' !- Zone or ZoneList Name
for i in range(len(lines)):
if lines[i].find('ZoneControl:Thermostat,')!=-1:
index=i+1
num=0
while lines[index].find(';')==-1:
index=index+1
num=num+1
if num>5:
print "complicated control detected"+lines[i+5].split(' ')[0]
else:
sch.append(lines[i+5].split(';')[0].replace(' ',''))
zone.append(lines[i+2].split(',')[0].replace(' ',''))
# identify the existing schedules
sch_adjl=[]# !- Control 1 Name
line_sch=[]# line number of !- Cooling Setpoint Temperature Schedule Name
sch_al=[]# !- Cooling Setpoint Temperature Schedule Name
zone_al=[]# !- Zone or ZoneList Name
for i in range(len(lines)):
for j in range(len(sch)):
if lines[i].find(' '+sch[j]+',')!=-1:
if lines[i-1].find('Dual')!=-1:
line_sch.append(i+2)
sch_temp=lines[i+2].split(';')[0].replace(' ','')
sch_al.append(sch_temp)
zone_al.append(zone[j])
sch_adjl.append(sch[j])
elif lines[i-1].find('Cooling')!=-1:
line_sch.append(i+1)
sch_temp=lines[i+1].split(';')[0].replace(' ','')
sch_al.append(sch_temp)
zone_al.append(zone[j])
sch_adjl.append(sch[j])
# divide sch
# line with 'Actuated' or '! ...' (Use EMS)
# line without 'Actuated' or '! ...' (Do not use EMS)
sch_av=[]# !- Cooling Setpoint Temperature Schedule
sch_adj=[]# !- Control 1 Name
zone_av=[]# !- Zone or ZoneList Name
sch_av_ems=[]# !- Cooling Setpoint Temperature Schedule Name
sch_adj_ems=[]# !- Control 1 Name
zone_av_ems=[]# !- Zone or ZoneList Name
for i in range(len(sch_al)):
for j in range(len(lines)):
if lines[j].lower().find(sch_al[i].lower()+',')!=-1 and (lines[j].find('Actuated')!=-1 or lines[j][0]=='!'):
temp=True
break
temp=False
if temp==False:
sch_av.append(sch_al[i])
sch_adj.append(sch_adjl[i])
zone_av.append(zone_al[i])
if temp==True:
sch_av_ems.append(sch_al[i])
sch_adj_ems.append(sch_adjl[i])
zone_av_ems.append(zone_al[i])
# collect existing cooling setpoint schedules
sch_record=[]
for i in range(len(lines)):
for j in range(len(sch_av)):
temp=[]
if lines[i].split(',')[0].replace(' ','')==sch_av[j] and lines[i].split(',')[1].replace(' ','').replace('\n','')=='!-Name':
for k in range(i+1,len(lines)):
temp.append(lines[k])
if ';' in lines[k]:
break
sch_record.append(temp)
sch_record_ems=[]
for i in range(len(lines)):
for j in range(len(sch_av_ems)):
temp=[]
if lines[i].split(',')[0].replace(' ','')==sch_av_ems[j] and lines[i].split(',')[1].replace(' ','').replace('\n','')=='!-Name':
for k in range(i+1,len(lines)):
temp.append(lines[k])
if ';' in lines[k]:
break
sch_record_ems.append(temp)
# collect the minimum values of stpt
default=[]
for j in range(len(sch_av)):
for i in range(len(lines)):
if lines[i].lower().find(' '+sch_av[j].lower()+',')!=-1:
index=i+1
while lines[index].find(';')==-1:
index=index+1
stpt=[]
for k in range(i+1,index+1):
if lines[k].find(',')!=-1 and lines[k].find(';')==-1:
if len(lines[k].split(','))>2:
text=lines[k].split(',')[1].replace(' ','')
else:
text=lines[k].split(',')[0].replace(' ','').replace('!','')
elif lines[k].find(';')!=-1 and lines[k].find(',')==-1:
text=lines[k].split(';')[0].replace(' ','').replace('!','').replace(' ','')
elif lines[k].find(';')!=-1 and lines[k].find(',')!=-1:
text=lines[k].split(',')[1].split(';')[0].replace(' ','')
if text.replace(".", "", 1).isdigit():
stpt.append(float(text))
default.append(min(stpt))
break
# disable the existing schedules
heat_sch=[]
sch_type=[]
control_type=[]
heat_sch_ems=[]
sch_type_ems=[]
control_type_ems=[]
for i in range(len(lines)):
for j in range(len(sch_adj)):
if lines[i].find(' '+sch_adj[j]+';')!=-1:
if lines[i].find('! ')==-1:
lines[i]='!'+lines[i]
index=i-1
while lines[index].find('ZoneControl:Thermostat')==-1:
if lines[index].find('! ')==-1:
lines[index]='!'+lines[index]
index=index-1
if lines[index].find('ZoneControl:Thermostat')!=-1 and lines[index].find('!')==-1:
lines[index]='!'+lines[index]
control_type.append(lines[i-2])
if lines[i].find(' '+sch_adj[j]+',')!=-1:
if lines[i].find('! ')==-1:
lines[i-1]='!'+lines[i-1]
lines[i]='!'+lines[i]
index=i+1
while lines[index].find(';')==-1:
if lines[index].find('! ')==-1:
lines[index]='!'+lines[index]
index=index+1
if lines[index].find(';')!=-1 and lines[index].find('! ')==-1:
lines[index]='!'+lines[index]
if index>i+1:
heat_sch.append(lines[i+1].replace('! ',' '))
else:
heat_sch.append(None)
sch_type.append(lines[i-1].replace('!',''))
for j in range(len(sch_adj_ems)):
if lines[i].find(' '+sch_adj_ems[j]+';')!=-1:
if lines[i].find('! ')==-1:
lines[i]='!'+lines[i]
index=i-1
while lines[index].find('ZoneControl:Thermostat')==-1:
if lines[index].find('! ')==-1:
lines[index]='!'+lines[index]
index=index-1
if lines[index].find('ZoneControl:Thermostat')!=-1 and lines[index].find('!')==-1:
lines[index]='!'+lines[index]
control_type_ems.append(lines[i-2])
if lines[i].find(' '+sch_adj_ems[j]+',')!=-1:
if lines[i].find('! ')==-1:
lines[i-1]='!'+lines[i-1]
lines[i]='!'+lines[i]
index=i+1
while lines[index].find(';')==-1:
if lines[index].find('! ')==-1:
lines[index]='!'+lines[index]
index=index+1
if lines[index].find(';')!=-1 and lines[index].find('! ')==-1:
lines[index]='!'+lines[index]
if index>i+1:
heat_sch_ems.append(lines[i+1].replace('! ',' '))
else:
heat_sch_ems.append(None)
sch_type_ems.append(lines[i-1].replace('!',''))
# add new zone control
line_new=[]
# check whether there are duplicate items
# w/o EMS
dup_index=[]
for i in range(len(sch_av)):
for j in range(len(sch_av)):
if i!=j and zone_av[i]==zone_av[j]:
if len(dup_index)>1:
for k in range(len(dup_index)):
if dup_index[k]==i:
include=True
break
include=False
else:
include=False
if not include:
dup_index.append(i)
dup_index_ems=[]
for i in range(len(sch_av_ems)):
for j in range(len(sch_av_ems)):
if i!=j and zone_av_ems[i]==zone_av_ems[j]:
if len(dup_index_ems)>1:
for k in range(len(dup_index_ems)):
if dup_index_ems[k]==i:
include=True
break
include=False
else:
include=False
if not include:
dup_index_ems.append(i)
line_record = []
for i in range(len(sch_av)):
included=False
for j in range(len(dup_index)):
if i == dup_index[j]:
included=True
break
included=False
if not included:
line_new.append('Schedule:Compact,'+'\n')
line_new.append(' '+zone_av[i].replace('\n','')+'_'+sch_av[i].replace('\n','')+', !- Name'+'\n')
#line_new.append(' Temperature, !- Schedule Type Limits Name'+'\n')
#line_new.append(' Through: 12/31, !- Field 1'+'\n')
#line_new.append(' For: AllDays, !- Field 2'+'\n')
#line_new.append(' Until: 24:00,'+str(default[i])+'; !- Field 3'+'\n')
for x in sch_record[i]:
line_new.append(x)
line_new.append('\n')
line_record.append(' '+zone_av[i].replace('\n','')+'_'+sch_av[i].replace('\n','')+', !- Name'+'\n')
line_new.append(sch_type[i])
line_new.append(' '+zone_av[i].replace('\n','')+'_setpoint'+', !- Name'+'\n')
if heat_sch[i] is not None:
line_new.append(heat_sch[i])
line_new.append(' '+zone_av[i].replace('\n','')+'_'+sch_av[i].replace('\n','')+'; !- Cooling Setpoint Temperature Schedule Name'+'\n')
line_new.append('\n')
line_new.append('ZoneControl:Thermostat,'+'\n')
line_new.append(' '+zone_av[i].replace('\n','')+'_Thermostat, !- Name'+'\n')
line_new.append(' '+zone_av[i].replace('\n','')+', !- Name'+'\n')
line_new.append(control_type[i].replace('! ',' '))
line_new.append(' '+sch_type[i].replace('\n','')+' !- Control 1 Object Type'+'\n')
line_new.append(' '+zone_av[i].replace('\n','')+'_setpoint'+'; !- Control Name'+'\n')
line_new.append('\n')
for i in range(len(sch_av_ems)):
included=False
for j in range(len(dup_index_ems)):
if i == dup_index_ems[j]:
included=True
break
included=False
if not included:
line_new.append('Schedule:Compact,'+'\n')
line_new.append(' '+zone_av_ems[i].replace('\n','')+'_'+sch_av_ems[i].replace('\n','')+', !- Name'+'\n')
#line_new.append(' Temperature, !- Schedule Type Limits Name'+'\n')
#line_new.append(' Through: 12/31, !- Field 1'+'\n')
#line_new.append(' For: AllDays, !- Field 2'+'\n')
#line_new.append(' Until: 24:00,'+str('26.7')+'; !- Field 3'+'\n')
#line_new.append(' Until: 24:00,'+str(default[i])+'; !- Field 3'+'\n')
for x in sch_record_ems[i]:
line_new.append(x)
line_new.append('\n')
line_record.append(' '+zone_av_ems[i].replace('\n','')+'_'+sch_av_ems[i].replace('\n','')+', !- Name'+'\n')
line_new.append(sch_type_ems[i])
line_new.append(' '+zone_av_ems[i].replace('\n','')+'_setpoint'+', !- Name'+'\n')
if heat_sch_ems[i] is not None:
line_new.append(heat_sch_ems[i])
line_new.append(' '+zone_av_ems[i].replace('\n','')+'_'+sch_av_ems[i].replace('\n','')+'; !- Cooling Setpoint Temperature Schedule Name'+'\n')
line_new.append('\n')
line_new.append('ZoneControl:Thermostat,'+'\n')
line_new.append(' '+zone_av_ems[i].replace('\n','')+'_Thermostat, !- Name'+'\n')
line_new.append(' '+zone_av_ems[i].replace('\n','')+', !- Name'+'\n')
line_new.append(control_type_ems[i].replace('! ',' '))
line_new.append(' '+sch_type_ems[i].replace('\n','')+' !- Control 1 Object Type'+'\n')
line_new.append(' '+zone_av_ems[i].replace('\n','')+'_setpoint'+'; !- Control Name'+'\n')
# Output:Variable
# EnergyManagementSystem:OutputVariable
# EnergyManagementSystem:ProgramCallingManager
# EnergyManagementSystem:Program
# EnergyManagementSystem:GlobalVariable
f=open('template/out_temp.txt','r')
lines_output=f.readlines()
f.close()
# combine the lines, new lines, and output lines together
for i in range(len(lines)):
if lines[i].lower().find('simulationcontrol,')!=-1:
if lines[i+5].lower().find('no')!=-1:
lines[i+5]=lines[i+5].replace('No','Yes')
lines[i+5]=lines[i+5].replace('no','Yes')
lines=lines+line_new+lines_output
for i in range(len(lines)):
if lines[i].lower().find('runperiod,')!=-1:
lines[i+2]=' 8, !- Begin Month'+'\n'
lines[i+3]=' 1, !- Begin Day of Month'+'\n'
lines[i+4]=' 8, !- End Month'+'\n'
lines[i+5]=' 1, !- End Day of Month'+'\n'
lines[i+6]=' Monday, !- Day of Week for Start Day'+'\n'
for i in range(len(lines)):
if lines[i].lower().find('timestep,')!=-1 and lines[i].lower().find('update frequency')==-1:
if lines[i].lower().find(';')!=-1:
lines[i]=' Timestep,60;'+'\n'
else:
lines[i+1]=' 60;'+'\n'
os.rename(idf_file+'.idf', idf_file+'.idf-bak')# change the original idf file into .idf-bak
# generate the new idf file
f=open(idf_file+'.idf','w')
for i in range(len(lines)):
f.writelines(lines[i])
f.close()
return line_record