-
Notifications
You must be signed in to change notification settings - Fork 7
/
read_data.py
1166 lines (959 loc) · 36.3 KB
/
read_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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Reading raw data
"""
import sys
import os
import csv
import json
import logging
from collections import defaultdict
import numpy as np
from energy_demand.technologies import tech_related
from energy_demand.profiles import load_profile
from energy_demand.scripts import init_scripts
class TechnologyData(object):
"""Class to store technology related
data
Arguments
---------
fuel_type : str
Fueltype of technology
eff_by : str
Efficiency of technology in base year
eff_ey : str
Efficiency of technology in future year
year_eff_ey : int
Future year when eff_ey is fully realised
eff_achieved : float
Factor of how much of the efficienc future
efficiency is achieved
diff_method : float
Differentiation method
market_entry : int,default=2015
Year when technology comes on the market
tech_list : list
List where technology is part of
tech_max_share : float
Maximum theoretical fraction of how much
this indivdual technology can contribute
to total energy service of its enduse
"""
def __init__(
self,
fuel_type=None,
eff_by=None,
eff_ey=None,
year_eff_ey=None,
eff_achieved=None,
diff_method=None,
market_entry=2015,
tech_list=None,
tech_max_share=None,
fueltypes=None
):
self.fuel_type_str = fuel_type
self.fuel_type_int = tech_related.get_fueltype_int(fueltypes, fuel_type)
self.eff_by = eff_by
self.eff_ey = eff_ey
self.year_eff_ey = year_eff_ey
self.eff_achieved = eff_achieved
self.diff_method = diff_method
self.market_entry = market_entry
self.tech_list = tech_list
self.tech_max_share = tech_max_share
class CapacitySwitch(object):
"""Capacity switch class for storing
switches
Arguments
---------
enduse : str
Enduse of affected switch
technology_install : str
Installed technology
switch_yr : int
Year until capacity installation is fully realised
installed_capacity : float
Installed capacity in GWh
"""
def __init__(
self,
enduse,
technology_install,
switch_yr,
installed_capacity
):
self.enduse = enduse
self.technology_install = technology_install
self.switch_yr = switch_yr
self.installed_capacity = installed_capacity
class FuelSwitch(object):
"""Fuel switch class for storing
switches
Arguments
---------
enduse : str
Enduse of affected switch
enduse_fueltype_replace : str
Fueltype which is beeing switched from
technology_install : str
Installed technology
switch_yr : int
Year until switch is fully realised
fuel_share_switched_ey : float
Switched fuel share
"""
def __init__(
self,
enduse=None,
enduse_fueltype_replace=None,
technology_install=None,
switch_yr=None,
fuel_share_switched_ey=None
):
self.enduse = enduse
self.enduse_fueltype_replace = enduse_fueltype_replace
self.technology_install = technology_install
self.switch_yr = switch_yr
self.fuel_share_switched_ey = fuel_share_switched_ey
class ServiceSwitch(object):
"""Service switch class for storing
switches
Arguments
---------
enduse : str
Enduse of affected switch
technology_install : str
Installed technology
service_share_ey : float
Service share of installed technology in future year
switch_yr : int
Year until switch is fully realised
"""
def __init__(
self,
enduse=None,
technology_install=None,
service_share_ey=None,
switch_yr=None
):
self.enduse = enduse
self.technology_install = technology_install
self.service_share_ey = service_share_ey
self.switch_yr = switch_yr
def read_in_results(path_runs, lookups, seasons, model_yeardays_daytype):
"""Read and post calculate
results from txt files
and store into container
"""
logging.info("... Reading in results")
results_container = {}
# -------------
# Fuels
# -------------
logging.info("... Reading in fuels")
results_container['results_every_year'] = read_results_yh(
lookups['fueltypes_nr'], path_runs)
results_container['results_enduse_every_year'] = read_enduse_specific_results_txt(
lookups['fueltypes_nr'], path_runs)
results_container['tot_peak_enduses_fueltype'] = read_max_results(
os.path.join(path_runs, "result_tot_peak_enduses_fueltype"))
# -------------
# Load factors
# -------------
logging.info("... Reading in load factors")
results_container['load_factors_y'] = read_lf_y(
os.path.join(path_runs, "result_reg_load_factor_y"))
results_container['load_factors_yd'] = read_lf_y(
os.path.join(path_runs, "result_reg_load_factor_yd"))
results_container['load_factor_seasons'] = {}
results_container['load_factor_seasons']['winter'] = read_lf_y(
os.path.join(path_runs, "result_reg_load_factor_winter"))
results_container['load_factor_seasons']['spring'] = read_lf_y(
os.path.join(path_runs, "result_reg_load_factor_spring"))
results_container['load_factor_seasons']['summer'] = read_lf_y(
os.path.join(path_runs, "result_reg_load_factor_summer"))
results_container['load_factor_seasons']['autumn'] = read_lf_y(
os.path.join(path_runs, "result_reg_load_factor_autumn"))
# -------------
# Post-calculations
# -------------
logging.info("... generating post calculations with read results")
# Calculate average per season and fueltype for every fueltype
av_season_daytype_cy = {}
season_daytype_cy = {}
for year, fueltypes_data in results_container['results_every_year'].items():
av_season_daytype_cy[year] = {}
season_daytype_cy[year] = {}
for fueltype, reg_fuels in fueltypes_data.items():
# Summarise across regions
tot_all_reg_fueltype = np.sum(reg_fuels, axis=0)
tot_all_reg_fueltype_reshape = tot_all_reg_fueltype.reshape((365, 24))
calc_av, calc_lp = load_profile.calc_av_lp(
tot_all_reg_fueltype_reshape,
seasons,
model_yeardays_daytype)
av_season_daytype_cy[year][fueltype] = calc_av
season_daytype_cy[year][fueltype] = calc_lp
results_container['av_season_daytype_cy'] = av_season_daytype_cy
results_container['season_daytype_cy'] = season_daytype_cy
logging.info("... Reading in results finished")
return results_container
def read_results_yh(fueltypes_nr, path_to_folder):
"""Read results
Arguments
---------
fueltypes_nr : int
Number of fueltypes
path_to_folder : str
Path to folder
Returns
-------
results = dict
Results
"""
results = defaultdict(dict)
all_txt_files_in_folder = os.listdir(path_to_folder)
# ------------------
# Get number of regions (search largest fueltype_array_position)
# ------------------
reg_nrs = 0
for file_path in all_txt_files_in_folder:
path_file_to_read = os.path.join(path_to_folder, file_path)
file_path_split = file_path.split("__")
try:
fueltype_array_position = int(file_path_split[2])
if fueltype_array_position > reg_nrs:
reg_nrs = fueltype_array_position
except IndexError:
pass #path is a folder and not a file
# Iterate files in folder
for file_path in all_txt_files_in_folder:
logging.info("... file_path: " + str(file_path))
try:
path_file_to_read = os.path.join(path_to_folder, file_path)
file_path_split = file_path.split("__")
year = int(file_path_split[1])
fueltype_array_position = int(file_path_split[2])
txt_data = np.loadtxt(path_file_to_read, delimiter=',')
try:
results[year]
except KeyError:
results[year] = np.zeros((fueltypes_nr, reg_nrs, 8760), dtype=float)
results[year][fueltype_array_position] = txt_data
except IndexError:
pass #path is a folder and not a file
return results
def read_max_results(path_enduse_specific_results):
"""Read max results
Arguments
---------
path_enduse_specific_results : str
Path to folder
"""
results = {}
all_txt_files_in_folder = os.listdir(path_enduse_specific_results)
# Iterate files
for file_path in all_txt_files_in_folder:
path_file_to_read = os.path.join(path_enduse_specific_results, file_path)
file_path_split = file_path.split("__")
year = int(file_path_split[1])
txt_data = np.loadtxt(path_file_to_read, delimiter=',')
# Add year if not already exists
results[year] = txt_data
return results
def read_enduse_specific_results_txt(fueltypes_nr, path_to_folder):
"""Read enduse specific results
Arguments
---------
fueltypes_nr : int
Number of fueltypes
path_to_folder : str
Folder path
"""
results = {}
path_enduse_specific_results = os.path.join(path_to_folder, "enduse_specific_results")
all_txt_files_in_folder = os.listdir(path_enduse_specific_results)
for file_path in all_txt_files_in_folder:
path_file_to_read = os.path.join(path_enduse_specific_results, file_path)
file_path_split = file_path.split("__")
enduse = file_path_split[1]
year = int(file_path_split[2])
fueltype_array_position = int(file_path_split[3])
txt_data = np.loadtxt(path_file_to_read, delimiter=',')
print("... reading file: {} {} {} {} ".format(year, enduse, fueltype_array_position, np.sum(txt_data)))
# Create year if not existing
try:
results[year]
except KeyError:
results[year] = {}
try:
results[year][enduse]
except KeyError:
results[year][enduse] = np.zeros((fueltypes_nr, 365, 24), dtype=float)
# Add year if not already exists
results[year][enduse][fueltype_array_position] = txt_data
return results
def load_script_data(data):
"""Load data generated by scripts
#SCRAP REMOVE
Arguments
---------
data : dict
Data container
"""
fts_cont, sgs_cont, sd_cont, switches_cont = init_scripts.scenario_initalisation(data['paths']['path_main'], data)
data['assumptions']['rs_service_tech_by_p'] = fts_cont['rs_service_tech_by_p']
data['assumptions']['ss_service_tech_by_p'] = fts_cont['ss_service_tech_by_p']
data['assumptions']['is_service_tech_by_p'] = fts_cont['is_service_tech_by_p']
data['assumptions']['rs_service_fueltype_by_p'] = fts_cont['rs_service_fueltype_by_p']
data['assumptions']['ss_service_fueltype_by_p'] = fts_cont['ss_service_fueltype_by_p']
data['assumptions']['is_service_fueltype_by_p'] = fts_cont['is_service_fueltype_by_p']
data['assumptions']['rs_service_fueltype_tech_by_p'] = fts_cont['rs_service_fueltype_tech_by_p']
data['assumptions']['ss_service_fueltype_tech_by_p'] = fts_cont['ss_service_fueltype_tech_by_p']
data['assumptions']['is_service_fueltype_tech_by_p'] = fts_cont['is_service_fueltype_tech_by_p']
data['assumptions']['rs_tech_increased_service'] = sgs_cont['rs_tech_increased_service']
data['assumptions']['ss_tech_increased_service'] = sgs_cont['ss_tech_increased_service']
data['assumptions']['is_tech_increased_service'] = sgs_cont['is_tech_increased_service']
data['assumptions']['rs_tech_decreased_share'] = sgs_cont['rs_tech_decreased_share']
data['assumptions']['ss_tech_decreased_share'] = sgs_cont['ss_tech_decreased_share']
data['assumptions']['is_tech_decreased_share'] = sgs_cont['is_tech_decreased_share']
data['assumptions']['rs_tech_constant_share'] = sgs_cont['rs_tech_constant_share']
data['assumptions']['ss_tech_constant_share'] = sgs_cont['ss_tech_constant_share']
data['assumptions']['is_tech_constant_share'] = sgs_cont['is_tech_constant_share']
data['assumptions']['rs_sig_param_tech'] = sgs_cont['rs_sig_param_tech']
data['assumptions']['ss_sig_param_tech'] = sgs_cont['ss_sig_param_tech']
data['assumptions']['is_sig_param_tech'] = sgs_cont['is_sig_param_tech']
data['rs_fuel_disagg'] = sd_cont['rs_fuel_disagg']
data['ss_fuel_disagg'] = sd_cont['ss_fuel_disagg']
data['is_fuel_disagg'] = sd_cont['is_fuel_disagg']
data['assumptions']['rs_service_switch'] = sgs_cont['rs_service_switch']
data['assumptions']['ss_service_switch'] = sgs_cont['ss_service_switch']
data['assumptions']['is_service_switch'] = sgs_cont['is_service_switch']
data['assumptions']['rs_service_switches'] = switches_cont['rs_service_switches']
data['assumptions']['ss_service_switches'] = switches_cont['ss_service_switches']
data['assumptions']['is_service_switches'] = switches_cont['is_service_switches']
return data
def read_csv_data_service(path_to_csv, fueltypes_nr):
"""This function reads in base_data_CSV all fuel types
Arguments
----------
path_to_csv : str
Path to csv file
fueltypes_nr : str
Nr of fueltypes
Returns
-------
elements_array : dict
Returns an dict with arrays
Notes
-----
the first row is the fuel_ID
The header is the sub_key
"""
lines = []
end_uses_dict = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip first row
_secondline = next(read_lines) # Skip first row
# All sectors
all_sectors = set([])
for sector in _secondline[1:]: #skip fuel ID:
all_sectors.add(sector)
# All enduses
all_enduses = set([])
for enduse in _headings[1:]: #skip fuel ID:
all_enduses.add(enduse)
# Initialise dict
for sector in all_sectors:
end_uses_dict[sector] = {}
for enduse in all_enduses:
end_uses_dict[sector][enduse] = np.zeros((fueltypes_nr), dtype=float)
for row in read_lines:
lines.append(row)
for cnt_fueltype, row in enumerate(lines):
for cnt, entry in enumerate(row[1:], 1):
enduse = _headings[cnt]
sector = _secondline[cnt]
end_uses_dict[sector][enduse][cnt_fueltype] += float(entry)
return end_uses_dict, list(all_sectors), list(all_enduses)
def read_csv_float(path_to_csv):
"""This function reads in CSV files and skips header row.
Arguments
----------
path_to_csv : str
Path to csv file
Returns
-------
elements_array : array_like
Returns an array `elements_array` with the read in csv files.
Notes
-----
The header row is always skipped.
"""
service_switches = []
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip first row
for row in read_lines:
service_switches.append(row)
return np.array(service_switches, float)
def read_load_shapes_tech(path_to_csv):
"""This function reads in csv technology shapes
Arguments
----------
path_to_csv : str
Path to csv file
"""
load_shapes_dh = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip first row
for row in read_lines:
dh_shape = np.zeros((24), dtype=float)
for cnt, row_entry in enumerate(row[1:], 1):
dh_shape[int(_headings[cnt])] = float(row_entry)
load_shapes_dh[str(row[0])] = dh_shape
return load_shapes_dh
def service_switch(path_to_csv, technologies):
"""This function reads in service assumptions from csv file,
tests whether the maximum defined switch is larger than
possible for a technology,
Arguments
----------
path_to_csv : str
Path to csv file
technologies : list
All technologies
Returns
-------
enduse_tech_ey_p : dict
Technologies per enduse for endyear in p
service_switches : dict
Service switches
Notes
-----
The base year service shares are generated from technology stock definition
"""
service_switches = []
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip first row
for row in read_lines:
try:
service_switches.append(
ServiceSwitch(
enduse=str(row[0]),
technology_install=str(row[1]),
service_share_ey=float(row[2]),
switch_yr=float(row[3])
)
)
except (KeyError, ValueError):
sys.exit("Check if provided data is complete (no empty csv entries)")
# Test if more service is provided as input than possible to maximum switch
for entry in service_switches:
if entry.service_share_ey > technologies[entry.technology_install].tech_max_share:
sys.exit(
"Input error: more service provided for tech '{}' in enduse '{}' than max possible".format(
entry['enduse'], entry['technology_install']))
return service_switches
def read_fuel_switches(path_to_csv, enduses, lu_fueltypes):
"""This function reads in from CSV file defined fuel
switch assumptions
Arguments
----------
path_to_csv : str
Path to csv file
enduses : dict
Endues per submodel
lu_fueltypes : dict
Look-ups
Returns
-------
dict_with_switches : dict
All assumptions about fuel switches provided as input
"""
fuel_switches = []
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines)
for row in read_lines:
try:
fuel_switches.append(
FuelSwitch(
enduse=str(row[0]),
enduse_fueltype_replace=lu_fueltypes[str(row[1])],
technology_install=str(row[2]),
switch_yr=float(row[3]),
fuel_share_switched_ey=float(row[4])))
except (KeyError, ValueError):
sys.exit("Check if provided data is complete (no emptly csv entries)")
# Testing wheter the provided inputs make sense
for obj in fuel_switches:
if obj.fuel_share_switched_ey == 0:
sys.exit(
"Input error: The share of switched fuel needs to be > 0. Delete {} from input".format(
obj.technology_install))
# Test if more than 100% per fueltype is switched
for obj in fuel_switches:
enduse = obj.enduse
fuel_type = obj.enduse_fueltype_replace
tot_share_fueltype_switched = 0
for obj_iter in fuel_switches:
if enduse == obj_iter.enduse and fuel_type == obj_iter.enduse_fueltype_replace:
tot_share_fueltype_switched += obj_iter.fuel_share_switched_ey
if tot_share_fueltype_switched > 1.0:
sys.exit(
"Input error: The fuel switches are > 1.0 for enduse {} and fueltype {}".format(
enduse, fuel_type))
# Test whether defined enduse exist
for obj in fuel_switches:
if obj.enduse in enduses['ss_all_enduses'] or obj.enduse in enduses['rs_all_enduses'] or obj.enduse in enduses['is_all_enduses']:
pass
else:
sys.exit(
"Input Error: The defined enduse '{}' to switch fuel from is not defined...".format(
obj.enduse))
return fuel_switches
def read_technologies(path_to_csv, fueltypes):
"""Read in technology definition csv file
Arguments
----------
path_to_csv : str
Path to csv file
Returns
-------
dict_technologies : dict
All technologies and their assumptions provided as input
dict_tech_lists : dict
List with technologies. The technology type
is defined in the technology input file
"""
dict_technologies = {}
dict_tech_lists = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip first row
for row in read_lines:
technology = row[0]
try:
dict_technologies[technology] = TechnologyData(
fuel_type=str(row[1]),
eff_by=float(row[2]),
eff_ey=float(row[3]),
year_eff_ey=float(row[4]), #MAYBE: ADD DICT WITH INTERMEDIARY POINTS
eff_achieved=float(row[5]),
diff_method=str(row[6]),
market_entry=float(row[7]),
tech_list=str.strip(row[8]),
tech_max_share=float(str.strip(row[9])),
fueltypes=fueltypes)
try:
dict_tech_lists[str.strip(row[8])].append(technology)
except KeyError:
dict_tech_lists[str.strip(row[8])] = [technology]
except Exception as e:
logging.error(e)
logging.error("Error in technology loading table. Check if e.g. empty field")
sys.exit()
return dict_technologies, dict_tech_lists
def read_base_data_resid(path_to_csv):
"""This function reads in base_data_CSV all fuel types
(first row is fueltype, subkey), header is appliances
Arguments
----------
path_to_csv : str
Path to csv file
_dt : str
Defines dtype of array to be read in (takes float)
Returns
-------
elements_array : dict
Returns an dict with arrays
Notes
-----
the first row is the fuel_ID
The header is the sub_key
"""
try:
lines = []
end_uses_dict = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip first row
for row in read_lines:
lines.append(row)
for i in _headings[1:]: # skip first
end_uses_dict[i] = np.zeros((len(lines)), dtype=float)
for cnt_fueltype, row in enumerate(lines):
cnt = 1 #skip first
for i in row[1:]:
end_use = _headings[cnt]
end_uses_dict[end_use][cnt_fueltype] = i
cnt += 1
except (KeyError, ValueError):
sys.exit("Check whether tehre any empty cells in the csv files for enduse '{}".format(end_use))
# Create list with all rs enduses
all_enduses = []
for enduse in end_uses_dict:
all_enduses.append(enduse)
return end_uses_dict, all_enduses
def read_csv_base_data_industry(path_to_csv, fueltypes_nr, lu_fueltypes):
"""This function reads in base_data_CSV all fuel types
Arguments
----------
path_to_csv : str
Path to csv file
_dt : str
Defines dtype of array to be read in (takes float)
Returns
-------
elements_array : dict
Returns an dict with arrays
Notes
-----
the first row is the fuel_ID
The header is the sub_key
"""
lines = []
end_uses_dict = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines)
_secondline = next(read_lines)
# All sectors
all_enduses = set([])
for enduse in _headings[1:]:
if enduse is not '':
all_enduses.add(enduse)
# All enduses
all_sectors = set([])
for line in read_lines:
lines.append(line)
all_sectors.add(line[0])
# Initialise dict
for sector in all_sectors:
end_uses_dict[sector] = {}
for enduse in all_enduses:
end_uses_dict[str(sector)][str(enduse)] = np.zeros((fueltypes_nr), dtype=float)
for row in lines:
sector = row[0]
for position, entry in enumerate(row[1:], 1): # Start with position 1
if entry != '':
enduse = str(_headings[position])
fueltype = _secondline[position]
fueltype_int = tech_related.get_fueltype_int(lu_fueltypes, fueltype)
end_uses_dict[sector][enduse][fueltype_int] += float(row[position])
return end_uses_dict, list(all_sectors), list(all_enduses)
def read_installed_tech(path_to_csv):
"""Read
Arguments
--------
path_to_csv : str
Path
"""
tech_installed = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip headers
for row in read_lines:
enduse = str.strip(row[0])
technology = str.strip(row[1])
try:
tech_installed[enduse]
except KeyError:
tech_installed[enduse] = []
# If no tech
if technology == "[]":
pass
else:
tech_installed[enduse].append(technology)
return tech_installed
def read_sig_param_tech(path_to_csv):
"""Read
"""
logging.debug("... read in sig parameters: %s", path_to_csv)
sig_param_tech = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip headers
for row in read_lines:
enduse = str.strip(row[0])
technology = str.strip(row[1])
midpoint = float(row[2])
steepness = float(row[3])
l_parameter = float(row[4])
try:
sig_param_tech[enduse]
except KeyError:
sig_param_tech[enduse] = {}
sig_param_tech[enduse][technology] = {}
sig_param_tech[enduse][technology]['midpoint'] = midpoint
sig_param_tech[enduse][technology]['steepness'] = steepness
sig_param_tech[enduse][technology]['l_parameter'] = l_parameter
return sig_param_tech
def read_service_fueltype_tech_by_p(path_to_csv):
"""Read in service data
Arguments
----------
path_to_csv : str
Path to csv
"""
service_fueltype_tech_by_p = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip headers
for row in read_lines:
service = str.strip(row[0])
fueltype = int(row[1])
tech = str.strip(row[2])
service_p = float(row[3])
try:
service_fueltype_tech_by_p[service]
except KeyError:
service_fueltype_tech_by_p[service] = {}
try:
service_fueltype_tech_by_p[service][fueltype]
except KeyError:
service_fueltype_tech_by_p[service][fueltype] = {}
if tech == 'None':
service_fueltype_tech_by_p[service][fueltype] = {}
else:
try:
service_fueltype_tech_by_p[service][fueltype][tech]
except KeyError:
service_fueltype_tech_by_p[service][fueltype][tech] = 0
service_fueltype_tech_by_p[service][fueltype][tech] += service_p
return service_fueltype_tech_by_p
def read_service_fueltype_by_p(path_to_csv):
"""Read
"""
logging.debug("... read in service data: %s", path_to_csv)
service_fueltype_by_p = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip headers
for row in read_lines:
service = str.strip(row[0])
fueltype = int(row[1])
service_p = float(row[2])
try:
service_fueltype_by_p[service]
except KeyError:
service_fueltype_by_p[service] = {}
try:
service_fueltype_by_p[service][fueltype]
except KeyError:
service_fueltype_by_p[service][fueltype] = 0
service_fueltype_by_p[service][fueltype] += service_p
return service_fueltype_by_p
def read_service_tech_by_p(path_to_csv):
"""Read
"""
logging.debug("... read in service data: %s", path_to_csv)
service_tech_by_p = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip headers
for row in read_lines:
service = str.strip(row[0])
tech = str.strip(row[1])
service_p = float(row[2])
try:
service_tech_by_p[service]
except KeyError:
service_tech_by_p[service] = {}
try:
service_tech_by_p[service][tech]
except KeyError:
service_tech_by_p[service][tech] = 0
service_tech_by_p[service][tech] += service_p
return service_tech_by_p
def read_disaggregated_fuel(path_to_csv, fueltypes_nr):
"""Read disaggregated fuel
Arguments
----------
path_to_csv : str
Path to csv file
fueltypes_nr : int
Nr of fueltypes
Returns
-------
fuel_sector_enduse : dict
Disaggregated fuel
"""
fuel_sector_enduse = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip headers
for row in read_lines:
region = str.strip(row[0])
enduse = str.strip(row[1])
fueltype = int(row[2])
fuel = float(row[3])
try:
fuel_sector_enduse[region]
except KeyError:
fuel_sector_enduse[region] = {}
try:
fuel_sector_enduse[region][enduse]
except KeyError:
fuel_sector_enduse[region][enduse] = np.zeros((fueltypes_nr), dtype=float)
fuel_sector_enduse[region][enduse][fueltype] = fuel
return fuel_sector_enduse
def read_disaggregated_fuel_sector(path_to_csv, fueltypes_nr):
"""Read disaggregated fuel
Arguments
----------
path_to_csv : str
Path to csv file
fueltypes_nr : int
Nr of fueltypes
Returns
-------
fuel_sector_enduse : dict
Disaggregated fuel
"""
fuel_sector_enduse = {}
with open(path_to_csv, 'r') as csvfile:
read_lines = csv.reader(csvfile, delimiter=',')
_headings = next(read_lines) # Skip headers
for row in read_lines:
region = str.strip(row[0])
enduse = str.strip(row[1])
sector = str.strip(row[2])
fueltype = int(row[3])
fuel = float(row[4])
try: