-
Notifications
You must be signed in to change notification settings - Fork 7
/
write_data.py
557 lines (455 loc) · 15.2 KB
/
write_data.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""Functions which are writing data
"""
import os
import logging
import configparser
import csv
import yaml
from yaml import Loader, Dumper
import collections
import numpy as np
from energy_demand.basic import basic_functions, conversions
from energy_demand.geography import write_shp
from energy_demand.technologies import tech_related
class ExplicitDumper(yaml.Dumper):
"""
A dumper that will never emit aliases.
"""
def ignore_aliases(self, data):
return True
def tuple_representer(dumper, data):
return dumper.represent_scalar(tag=u'tag:yaml.org,2002:str', value='({}, {})'.format(data[0], data[1]))
def write_array_to_txt(path_result, array):
"""Write scenario population for a year to txt file
"""
np.savetxt(path_result, array, delimiter=',')
def write_list_to_txt(path_result, list):
"""Write scenario population for a year to txt file
"""
file = open(path_result, "w")
for entry in list:
file.write(entry + "\n")
return
def write_scenaric_population_data(sim_yr, path_result, pop_y):
"""Write scenario population for a year to '.npy' file
Parameters
----------
sim_yr : int
Simulation year
path_result : str
Path to resulting folder
pop_y : array
Population of simulation year
"""
path_file = os.path.join(
path_result,
"pop__{}__{}".format(sim_yr, ".npy"))
np.save(path_file, pop_y)
logging.info("... finished saving population")
def create_shp_results(data, results_container, paths, lookups, regions):
"""Create csv file and merge with shape
Arguments
---------
results_container : dict
Data container
paths : dict
Paths
lookups : dict
Lookups
regions : list
Region in a list with order how they are stored in result array
"""
logging.info("... create result shapefiles")
# ------------------------------------
# Create shapefile with load factors
# ------------------------------------
field_names, csv_results = [], []
# Iterate fueltpyes and years and add as attributes
for year in results_container['reg_load_factor_y'].keys():
for fueltype in range(lookups['fueltypes_nr']):
results = basic_functions.array_to_dict(
results_container['reg_load_factor_y'][year][fueltype], regions)
field_names.append('y_{}_{}'.format(year, fueltype))
csv_results.append(results)
# Add population
field_names.append('pop_{}'.format(year))
pop_dict = basic_functions.array_to_dict(
data['scenario_data']['population'][year], regions)
csv_results.append(pop_dict)
write_shp.write_result_shapefile(
paths['lad_shapefile'],
os.path.join(paths['data_results_shapefiles'], 'lf_max_y'),
field_names,
csv_results)
# ------------------------------------
# Create shapefile with yearly total fuel all enduses
# ------------------------------------
field_names, csv_results = [], []
# Iterate fueltpyes and years and add as attributes
for year in results_container['results_every_year'].keys():
for fueltype in range(lookups['fueltypes_nr']):
# Calculate yearly sum
yearly_sum = np.sum(results_container['results_every_year'][year][fueltype], axis=1)
yearly_sum_gw = yearly_sum
field_names.append('y_{}_{}'.format(year, fueltype))
csv_results.append(
basic_functions.array_to_dict(yearly_sum_gw, regions))
# Add population
field_names.append('pop_{}'.format(year))
csv_results.append(
basic_functions.array_to_dict(data['scenario_data']['population'][year], regions))
write_shp.write_result_shapefile(
paths['lad_shapefile'],
os.path.join(paths['data_results_shapefiles'], 'fuel_y'),
field_names,
csv_results)
# ------------------------------------
# Create shapefile with peak demand in gwh
# ------------------------------------
#
# ------------------------------------
# Create shapefile with
# ------------------------------------
logging.info("... finished generating shapefiles")
def dump(data, file_path):
"""Write plain data to a file as yaml
Parameters
----------
file_path : str
The path of the configuration file to write
data
Data to write (should be lists, dicts and simple values)
"""
yaml.add_representer(tuple, tuple_representer)
with open(file_path, 'w') as file_handle:
return yaml.dump(data, file_handle, Dumper=ExplicitDumper, default_flow_style=False)
def write_yaml_output_keynames(path_yaml, key_names):
"""Generate YAML file where the outputs
for the sector model can be easily copied
Arguments
----------
path_yaml : str
Path where yaml file is saved
key_names : dict
Names of keys of supply_out dict
"""
list_to_dump = []
for key_name in key_names:
dict_to_dump = {
'name': key_name,
'spatial_resolution': 'lad_uk_2016',
'temporal_resolution': 'hourly',
'units': 'GWh'}
list_to_dump.append(dict_to_dump)
dump(list_to_dump, path_yaml)
def write_yaml_param_scenario(path_yaml, dict_to_dump):
"""Write all strategy variables to YAML file
Arguments
----------
path_yaml : str
Path where yaml file is saved
dict_to_dump : dict
Dict which is written to YAML
"""
list_to_dump = [dict_to_dump]
dump(list_to_dump, path_yaml)
def write_yaml_param_complete(path_yaml, dict_to_dump):
"""Write all strategy variables to YAML file
Arguments
----------
path_yaml : str
Path where yaml file is saved
dict_to_dump : dict
Dict which is written to YAML
"""
list_to_dump = []
for paramter_info in dict_to_dump:
dump_dict = {}
dump_dict['suggested_range'] = paramter_info['suggested_range']
dump_dict['absolute_range'] = paramter_info['absolute_range']
dump_dict['description'] = paramter_info['description']
dump_dict['name'] = paramter_info['name']
dump_dict['default_value'] = paramter_info['default_value']
dump_dict['units'] = paramter_info['units']
list_to_dump.append(dump_dict)
# Dump list
dump(list_to_dump, path_yaml)
def write_simulation_inifile(path, enduses, assumptions, reg_nrs, regions):
"""Create .ini file with simulation parameters which ared
used to read in correctly the simulation results
Arguments
---------
path : str
Path to result foder
enduses : dict
Enduses
assumptions : dict
Assumptions
reg_nrs : int
Number of regions
regions : dict
Regions
"""
path_ini_file = os.path.join(
path, 'model_run_sim_param.ini')
config = configparser.ConfigParser()
config.add_section('SIM_PARAM')
config['SIM_PARAM']['reg_nrs'] = str(reg_nrs)
config['SIM_PARAM']['base_yr'] = str(assumptions.base_yr)
config['SIM_PARAM']['simulated_yrs'] = str(assumptions.simulated_yrs)
# ----------------------------
# Other information to pass to plotting and summing function
# ----------------------------
config.add_section('ENDUSES')
#convert list to strings
config['ENDUSES']['rs_enduses'] = str(enduses['rs_enduses'])
config['ENDUSES']['ss_enduses'] = str(enduses['ss_enduses'])
config['ENDUSES']['is_enduses'] = str(enduses['is_enduses'])
config.add_section('REGIONS')
config['REGIONS']['regions'] = str(regions)
with open(path_ini_file, 'w') as f:
config.write(f)
pass
def resilience_paper(
path_result_folder,
new_folder,
file_name,
results,
submodels,
regions,
fueltypes,
fueltype_str
):
"""Restuls for risk paper
results : array
ed_submodel_fueltype_regs_yh (3, 391, 7, 365, 24)
Get maximum and minimum h electricity for eversy submodel
for base year
"""
path_result_sub_folder = os.path.join(
path_result_folder, new_folder)
basic_functions.create_folder(path_result_sub_folder)
# Create file path
path_to_txt = os.path.join(
path_result_sub_folder,
"{}{}".format(
file_name,
".csv"))
# Write csv
file = open(path_to_txt, "w")
file.write("{}, {}, {}".format( #{}, {}, {}, {}
'lad_nr',
#'submodel',
'min_GW_elec',
'max_GW_elec') + '\n')
#'resid_min_GW_elec',
#'resid_max_GW_elec',
#'servi_min_GW_elec',
#'servi_max_GW_elec',
#'indus_min_GW_elec',
#'indus_max_GW_elec') + '\n')
fueltype_int = tech_related.get_fueltype_int(
fueltypes, fueltype_str)
for region_nr, region in enumerate(regions):
min_GW_elec = 0
max_GW_elec = 0
for submodel_nr, submodel in enumerate(submodels):
# Reshape
reshape_8760h = results[submodel_nr][region_nr][fueltype_int].reshape(8760)
min_GW_elec += np.min(reshape_8760h)
max_GW_elec += np.max(reshape_8760h)
# Min and max
'''if submodel_nr == 0:
resid_min_GW_elec = np.min(reshape_8760h)
resid_max_GW_elec = np.max(reshape_8760h)
elif submodel_nr == 1:
service_min_GW_elec = np.min(reshape_8760h)
service_max_GW_elec = np.max(reshape_8760h)
else:
industry_min_GW_elec = np.min(reshape_8760h)
industry_max_GW_elec = np.max(reshape_8760h)'''
file.write("{}, {}, {}".format(
str.strip(region),
float(min_GW_elec),
float(max_GW_elec)) + '\n')
'''file.write("{}, {}, {}, {}, {}, {}, {}".format(
str.strip(region),
float(resid_min_GW_elec),
float(resid_max_GW_elec),
float(service_min_GW_elec),
float(service_max_GW_elec),
float(industry_min_GW_elec),
float(industry_max_GW_elec)
) + '\n')'''
file.close()
# ----------------------
# Write out national average
# ----------------------
# Create file path
path_to_txt_flat = os.path.join(
path_result_sub_folder,
"{}{}".format(
'averge_nr',
".csv"))
file = open(path_to_txt_flat, "w")
file.write("{}".format(
'average_GW_UK') + '\n')
uk_av_gw_elec = 0
for submodel_nr, submodel in enumerate(submodels):
for region_nr, region in enumerate(regions):
reshape_8760h = results[submodel_nr][region_nr][fueltype_int].reshape(8760)
uk_av_gw_elec += np.average(reshape_8760h)
file.write("{}".format(uk_av_gw_elec))
file.close()
print("Finished writing out resilience .csv")
return
def write_lf(
path_result_folder,
path_new_folder,
parameters,
model_results,
file_name
):
"""Write numpy array to `.npy` file
path_result_folder,
path_new_folder,
parameters,
model_results,
file_name
"""
# Create folder and subolder
basic_functions.create_folder(
path_result_folder)
path_result_sub_folder = os.path.join(
path_result_folder, path_new_folder)
basic_functions.create_folder(path_result_sub_folder)
# Create full file_name
for name_param in parameters:
file_name += str("__") + str(name_param)
# Generate full path
path_file = os.path.join(path_result_sub_folder, file_name)
path_file_fueltype = path_file + "__" + ".npy"
np.save(path_file_fueltype, model_results)
def write_supply_results(
sim_yr,
name_new_folder,
path_result,
model_results,
file_name
):
"""Write model results to numpy file as follows:
name of file: name_year
array in file: np.array(region, fueltype, timesteps)
Arguments
---------
sim_yr : int
Simulation year
name_new_folder : str
Name of folder to create
path_result : str
Paths
model_results : array
Results to store to txt
file_name : str
File name
"""
# Create folder and subolder
path_result_sub_folder = os.path.join(
path_result, name_new_folder)
basic_functions.create_folder(
path_result_sub_folder)
path_file = os.path.join(
path_result_sub_folder,
"{}__{}__{}".format(
file_name,
sim_yr,
".npy"))
np.save(path_file, model_results)
def write_enduse_specific(sim_yr, path_result, model_results, filename):
"""Write out enduse specific results for every hour and store to
`.npy` file
Arguments
-----------
sim_yr : int
Simulation year
path_result : str
Path
model_results : dict
Modelling results
filename : str
File name
"""
# Create folder for model simulation year
basic_functions.create_folder(path_result)
basic_functions.create_folder(
path_result, "enduse_specific_results")
for enduse, fuel in model_results.items():
path_file = os.path.join(
os.path.join(path_result, "enduse_specific_results"),
"{}__{}__{}__{}".format(
filename,
enduse,
sim_yr,
".npy"))
np.save(path_file, fuel)
def write_max_results(sim_yr, path_result, result_foldername, model_results, filename):
"""Store yearly model resuls to numpy array '.npy'
Arguments
---------
sim_yr : int
Simulation year
path_result : str
Result path
result_foldername : str
Folder name
model_results : np.array
Model results
filename : str
File name
"""
# Create folder and subolder
basic_functions.create_folder(path_result)
basic_functions.create_folder(path_result, result_foldername)
# Write to txt
path_file = os.path.join(
os.path.join(path_result, result_foldername),
"{}__{}__{}".format(filename, sim_yr, ".npy"))
np.save(path_file, model_results)
return
def create_txt_shapes(
end_use,
path_txt_shapes,
shape_peak_dh,
shape_non_peak_y_dh,
shape_non_peak_yd
):
"""Function collecting functions to write out arrays
to txt files
"""
write_array_to_txt(
os.path.join(
path_txt_shapes,
str(end_use) + str("__") + str('shape_peak_dh') + str('.txt')),
shape_peak_dh)
write_array_to_txt(
os.path.join(
path_txt_shapes,
str(end_use) + str("__") + str('shape_non_peak_y_dh') + str('.txt')),
shape_non_peak_y_dh)
write_array_to_txt(
os.path.join(
path_txt_shapes,
str(end_use) + str("__") + str('shape_non_peak_yd') + str('.txt')),
shape_non_peak_yd)
return
def create_csv_file(path, rows):
"""
#filewriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
"""
with open(path, 'w', newline='') as csvfile: #newline=None
filewriter = csv.writer(
csvfile,
delimiter=',',
quotechar='|')
for row in rows:
filewriter.writerow(row)