-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_reader.py
1286 lines (904 loc) · 43.9 KB
/
profile_reader.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 29 09:37:01 2018
@author: user
"""
import os, sys
import numpy as np
import pandas as pd
import itertools
import re
import grimsel.auxiliary.sqlutils.aux_sql_func as aql
import grimsel.auxiliary.maps as maps
import grimsel.auxiliary.timemap as timemap
from xlrd import open_workbook
from grimsel.auxiliary.aux_general import read_xlsx_table
class ProfileReader():
def __init__(self, **kwargs):
'''
Instantiate and manage basic properties of the ProfileReader class.
Input arguments:
base_dir -- the base data directory
exclude_substrings -- list, ignores file names containing any of the
substrings in this list
ext -- string, e.g. 'csv'. file extension
to_sql -- boolean; if True, appends to sql table for each read file
'''
base_dir = kwargs.pop('base_dir')
defaults = dict(ext='csv',
exclude_substrings=[],
tm_filt={},
dict_sql={},
col_filt=tuple(),
skip_table_init=False)
for key, val in defaults.items():
setattr(self, key, val)
if key in kwargs.keys():
setattr(self, key, kwargs[key])
kwargs.pop(key)
self._dir = os.path.join(base_dir, self.data_dir)
# update sql dict using child defaults
self.dict_sql.update({kk: vv for kk, vv
in self.dict_sql_default.items()
if not kk in self.dict_sql.keys()})
print('self.dict_sql', self.dict_sql)
print('self.__dict__', self.__dict__.keys())
print('type(self)', type(self))
self.init_schema()
# init table is defined in the child classes since this depends on
# on the table structure; alternatively, define appropriate
# child attributes in analogy to dict_sql
if not self.skip_table_init:
self.init_table()
def init_tm(self, yr_list):
self.tm = timemap.TimeMap(keep_datetime=True)
self.tm.gen_hoy_timemap(start='{}-1-1 00:00'.format(min(yr_list)),
stop='{}-12-31 23:59'.format(max(yr_list)))
def get_fn_list(self):
self.fn_list = [os.path.join(self._dir, f)
for f in os.listdir(self._dir) if
os.path.isfile(os.path.join(self._dir, f))]
# exclude files whose filenames include excluded substrings
self.fn_list = [fn for fn in self.fn_list
if not any(ss in fn for ss
in self.exclude_substrings)]
_ext = self.ext if type(self.ext) is list else [self.ext]
# exclude files with wrong file extension
self.fn_list = [fn for fn in self.fn_list
if fn.split('.')[-1] in _ext]
def read(self):
'''
Main data reading method defined in the children.
Returns a DataFrame with the required columns
(DateTime, value) as well as all columns referred to in
self.col_filt and (probably) defined in the init_table (not stringent)
'''
pass
def time_resample(self, df, res='H'):
list_ind = [c for c in df.columns if not c in ['DateTime', 'value']]
df = (df.groupby(list_ind)[['DateTime', 'value']]
.apply(lambda x: (x.set_index('DateTime').astype(float)
.resample(res).mean())).reset_index())
return df
def get_hour_of_the_year(self, df):
'''
df must not contain any columns containing time indices < year
'''
if not 'year' in df.columns:
df['year'] = df.DateTime.dt.year
if not hasattr(self, 'tm'):
self.tm = timemap.TimeMap(keep_datetime=True)
self.tm.gen_hoy_timemap(start=df.DateTime.min(),
stop=df.DateTime.max())
# explicitly reset UTC
self.tm.df_time_map['DateTime'] = (self.tm.df_time_map.DateTime
.dt.tz_convert('UTC'))
# explicitly make datetime
df['DateTime'] = pd.to_datetime(df.DateTime)
df = pd.merge(self.tm.df_time_map[['DateTime', 'hy']],
df, on=['DateTime'])
return df
def single_post_processing(self, df_add):
df_add = self.filter_tm(df_add)
df_add = self.filter_col(df_add)
df_add = self.filter_leap_day(df_add)
return df_add
def read_all(self, skip_sql=False):
self.df_tot = pd.DataFrame()
fn = self.fn_list[0]
for fn in self.fn_list:
print('Reading {}'.format(fn))
self._fn = fn
df_add = self.read(fn)
if df_add is None:
# This happens for the Terna data if one of the automatically
# downloaded pdf files is not a valid data file.
continue # nothing to do here
df_add = self.single_post_processing(df_add)
if not skip_sql:
self.df_add = df_add.copy()
df_add = self.get_hour_of_the_year(df_add)
df_add = self.post_processing(df_add)
self.append_to_sql(df_add.copy())
self.df_tot = pd.concat([self.df_tot, df_add])
def append_to_sql(self, df):
if 'DateTime' in df.columns:
df['DateTime'] = df.DateTime.dt.strftime("%Y-%m-%d %H:%M:%S%z")
aql.write_sql(df, **self.dict_sql, if_exists='append',
chunksize=10000)
def post_processing(self, df):
'''
Operations on the otherwise completed table to avoid clashes
(e.g. month column addition clashing with hour of the year generation)
'''
return df
def filter_tm(self, df):
for kk, vv in self.tm_filt.items():
df = df.loc[getattr(df.DateTime.dt, 'year').isin(vv)]
return df
def filter_col(self, df):
for col, val, sgn in self.col_filt:
df = df.loc[sgn == df[col].isin(val)]
return df
def init_table(self):
'''
Initialize output table based on attributes of child classes:
self.tb_cols and self.tb_pk
'''
tb_name = self.dict_sql['tb']
aql.init_table(tb_name=tb_name, cols=self.tb_cols,
schema=self.dict_sql['sc'],
db=self.dict_sql['db'],
pk=self.tb_pk)
def init_schema(self):
exec_str = '''
CREATE SCHEMA IF NOT EXISTS {sc};
'''.format(**self.dict_sql)
aql.exec_sql(exec_str, db=self.dict_sql['db'])
def filter_leap_day(self, df):
mask_ly = (df.DateTime.dt.day == 29) & (df.DateTime.dt.month == 2)
df = df[-mask_ly]
df = df.reset_index(drop=True)
return df
def tz_localize_convert(self, tz='Europe/Zurich', df=None):
_df = self.df_tot if df is None else df
_df['DateTime'] = _df['DateTime'].dt.tz_localize(tz, ambiguous='infer')
_df['DateTime'] = _df['DateTime'].dt.tz_convert('UTC')
if df is not None:
return _df['DateTime']
def filter_years_by_data_length(self, df):
# filter incomplete years
list_years = (df.loc[df.value > 0].pivot_table(values='value', index='year',
aggfunc=len) < 7000)
list_years = (list_years.loc[list_years.value]
.index
.get_level_values(0).values)
return df.loc[-df.DateTime.dt.year.isin(list_years)]
def fix_double_dates(self):
'''
Terna data has multiple versions of some days.
'''
self.df_tot = self.df_tot.sort_values('DateTime')
self.df_tot = self.df_tot.pivot_table(values='value',
index=['DateTime', 'nd_id',
'year'],
aggfunc=np.mean)
self.df_tot = self.df_tot.reset_index()
def fix_missing_dst(self):
'''
Some countries (RTE/Terna) report copied 2am values as 3am values on the last
Sunday of March, and skip the second 2am values on the last Sunday in
October.
'''
self.df_tot['hour'] = self.df_tot.DateTime.dt.hour
self.df_tot['doy'] = self.df_tot.DateTime.dt.dayofyear
self.df_tot['year'] = self.df_tot.DateTime.dt.year
# remove double march dst hours
dict_dst = self.tm.get_dst_days(['MAR'])
list_dst = [(kk[0], vv) for kk, vv in dict_dst.items()]
mask_double_march = (self.df_tot[['year', 'doy']].apply(tuple, axis=1)
.isin(list_dst))
mask_double_march &= self.df_tot.hour.isin([2])
self.df_tot = self.df_tot.loc[-mask_double_march]
# double missing dst hours in october
dict_dst = self.tm.get_dst_days(['OCT'])
list_dst = [(kk[0], vv) for kk, vv in dict_dst.items()]
mask_missing_october = (self.df_tot[['year', 'doy']].apply(tuple, axis=1)
.isin(list_dst))
mask_missing_october_0 = (mask_missing_october
& self.df_tot.hour.isin([2])
& self.df_tot.DateTime.dt.minute.isin([0]))
mask_missing_october_1 = (mask_missing_october
& self.df_tot.hour.isin([2])
& self.df_tot.DateTime.dt.minute.isin([30]))
mask_missing_october = mask_missing_october_0 | mask_missing_october_1
self.df_tot_oct = pd.concat([self.df_tot.loc[mask_missing_october_0]]
+ [self.df_tot.loc[mask_missing_october_1]]
+ [self.df_tot.loc[mask_missing_october_0]]
+ [self.df_tot.loc[mask_missing_october_1]])
self.df_tot_oct = self.df_tot_oct.reset_index(drop=True).reset_index()
self.df_tot_oct.loc[self.df_tot_oct.index.isin([1,2]), 'value'] = np.nan
self.df_tot = self.df_tot.loc[-(mask_missing_october)]
self.df_tot = pd.concat([self.df_tot_oct.drop('index', axis=1),
self.df_tot], axis=0)
self.df_tot = self.df_tot.sort_values(['year', 'doy', 'hour']).reset_index(drop=True)
self.df_tot['value'] = self.df_tot['value'].interpolate('linear')
self.df_tot = self.df_tot.reset_index(drop=True)
# self.df_tot.loc[self.df_tot.doy.isin(dict_dst.values())][['value']].plot(marker='.')
# %%
class EpexPriceVolume(ProfileReader):
'''
Reads EPEX price profiles and volumes from csv files.
It would be better to include the EPEX web parser here.
'''
dict_sql_default = dict(sc='profiles_raw', tb='epex_price_volumes')
data_dir = os.path.normpath('EPEX/MORE')
# sql table columns
tb_cols = [('"DateTime"', 'TIMESTAMP'),
('nd_id', 'VARCHAR'),
('quantity', 'VARCHAR'),
('value', 'DOUBLE PRECISION'),
('year', 'SMALLINT'),
('mt_id', 'SMALLINT'),
# ('day', 'SMALLINT'),
# ('hod', 'SMALLINT'),
('hy', 'SMALLINT')
]
# sql table primary key
tb_pk = ['quantity', 'nd_id', 'year', 'hy']
def __init__(self, kw_dict):
super().__init__(**kw_dict)
def post_processing(self, df):
df['mt_id'] = df['DateTime'].dt.month - 1
return df
def read(self, fn):
df_add = pd.read_csv(fn)
df_add.columns = [c.replace('/', '_').lower() for c in df_add.columns]
df_add = df_add.loc[-df_add.year.isin(['year'])]
# data type conversion
int_cols = ['year', 'month', 'day', 'hod', 'hoy']
float_cols = ['price_eur_mwh', 'volume_mwh']
df_add[int_cols] = df_add[int_cols].applymap(int)
df_add[float_cols] = df_add[float_cols].applymap(float)
#
df_add = df_add.set_index(['region', 'year', 'month', 'day', 'hod', 'hoy']).stack()
df_add = df_add.reset_index()
df_add = df_add.rename(columns={'region': 'nd_id', 'level_6': 'quantity', 0: 'value'})
df_add['nd_id'] = df_add['nd_id'] + '0'
df_add['DateTime'] = pd.to_datetime(df_add[['year', 'month', 'day', 'hod']].rename(columns={'hod': 'hour'}))
df_add['DateTime'] = df_add.DateTime.dt.tz_localize('UTC')
return df_add[['DateTime', 'nd_id', 'year', 'quantity', 'value']]
if __name__ == '__main__':
kw_dict = dict(dict_sql=dict(db='storage2'),
tm_filt={'year': range(2005, 2018)},
col_filt=[], ext='csv', exclude_substrings=[])
op = EpexPriceVolume(kw_dict)
op.get_fn_list()
self = op
fn = op.fn_list[1]
op.read_all()
sys.exit()
# %%
class RTEProduction(ProfileReader):
dict_sql_default = dict(sc='profiles_raw', tb='rte_production_eco2mix')
data_dir = os.path.normpath('SUPPLY_PROFILES/FRANCE_RTE')
# sql table columns
tb_cols = [('"DateTime"', 'TIMESTAMP'), ('fl_id', 'VARCHAR'),
('value', 'DOUBLE PRECISION'), ('hy', 'SMALLINT'),
('year', 'SMALLINT'),
('mt_id', 'SMALLINT'),
('nd_id', 'VARCHAR(7)')]
# sql table primary key
tb_pk = ['nd_id', 'fl_id', 'year', 'hy']
def __init__(self, kw_dict):
super().__init__(**kw_dict)
def post_processing(self):
self.df_tot['mt_id'] = self.df_tot['DateTime'].dt.month - 1
self.init_tm(self.tm_filt['year'])
self.fix_missing_dst()
self.df_tot['DateTime'] = self.df_tot.groupby('fl_id')['DateTime'].apply(lambda x: x.dt.tz_localize('Europe/Zurich', ambiguous ='infer'))
self.df_tot['DateTime'] = self.df_tot.DateTime.dt.tz_convert('UTC')
self.df_tot = self.get_hour_of_the_year(self.df_tot)
self.df_tot = self.filter_tm(self.df_tot)
#
#self.df_tot.loc[(self.df_tot.fl_id == 'photovoltaics')
# & (self.df_tot.DateTime.dt.month == 3)
# & (self.df_tot.DateTime.dt.year == 2014)
# & (self.df_tot.DateTime.dt.day == 30)]
self.df_tot = self.df_tot[['DateTime', 'fl_id', 'value', 'hy',
'year', 'mt_id', 'nd_id']]
self.append_to_sql(self.df_tot)
#
#self.df_tot.loc[(self.df_tot.year == 2014)
# & (self.df_tot.fl_id == 'bio_all')
# & (self.df_tot.hy == 0)
# ]
#
# self.df_tot = self.filter_tm(self.df_tot)
def single_post_processing(self, df_add):
df_add = self.filter_col(df_add)
df_add = self.filter_leap_day(df_add)
return df_add
def read(self, fn):
df_add = pd.read_excel(fn, header=0, encoding='utf-8')
df_add = df_add.loc[(-df_add.Date.isnull())
& (-df_add.Consommation.isnull())]
df_add['DateTime'] = pd.to_datetime(df_add['Date'].dt.date.apply(str)
+ ' ' + df_add['Heures'].apply(str))
df_add['DateTime'] = df_add['DateTime'].dt.tz_localize('UTC')
skip_cols = ['DateTime', 'Date', 'Heures', 'Nature', 'Périmètre',
'year', 'hour']
df_add = df_add.set_index(['DateTime'])[[c for c in df_add.columns
if not c in skip_cols]]
dict_sf = {'Consommation': 'load',
'Prévision J-1': 'load_prediction_d-1',
'Prévision J': 'load_prediction_d',
'Fioul': 'mineral_oil_heavy',
'Charbon': 'hard_coal',
'Gaz': 'natural_gas',
'Nucléaire': 'nuclear_fuel',
'Eolien': 'wind_onshore',
'Solaire': 'photovoltaics',
'Hydraulique': 'hydro_total',
'Pompage': 'pumped_hydro_pumping',
'Bioénergies': 'bio_all',
'Ech. physiques': 'import_export',
'Taux de Co2': 'co2_intensity',
'Ech. comm. Angleterre': 'imex_UK',
'Ech. comm. Espagne': 'imex_ES',
'Ech. comm. Italie': 'imex_IT',
'Ech. comm. Suisse': 'imex_CH',
'Ech. comm. Allemagne-Belgique': 'imex_DE_BE',
'Fioul - TAC': 'mineral_oil_heavy_turbines',
'Fioul - Cogén': 'mineral_oil_heavy_chp',
'Fioul - Autres': 'mineral_oil_heavy_others',
'Gaz - TAC': 'natural_gas_turbines',
'Gaz - Cogén.': 'natural_gas_chp',
'Gaz - CCG': 'natural_gas_cc',
'Gaz - Autres': 'natural_gas_others',
'Hydraulique - Fil de l?eau + éclusée': 'run_of_river',
'Hydraulique - Lacs': 'reservoirs',
'Hydraulique - STEP turbinage': 'pumped_hydro',
'Bioénergies - Déchets': 'waste_mix',
'Bioénergies - Biomasse': 'biomass',
'Bioénergies - Biogaz': 'biogas'}
col_dict = {'level_1': 'fl_id', 0: 'value'}
df_add = df_add.stack().reset_index().rename(columns=col_dict)
df_add = df_add.loc[df_add.fl_id.isin(list(dict_sf.keys()))]
df_add['fl_id'] = df_add.fl_id.replace(dict_sf)
df_add['year'] = df_add['DateTime'].dt.year
df_add['nd_id'] = 'FR0'
df_add.loc[df_add.value.astype(str) == 'ND'] = np.nan
df_add['value'] = df_add['value'].fillna(method='ffill')
df_add['hour'] = df_add.DateTime.dt.hour
df_add['date'] = df_add.DateTime.dt.date
df_add['day'] = df_add.DateTime.dt.day
df_add['month'] = df_add.DateTime.dt.month
df_add['year'] = df_add.DateTime.dt.year
df_add = df_add.pivot_table(values='value',
index=['year', 'month', 'day', 'hour',
'fl_id', 'nd_id'], aggfunc=np.mean)
df_add = df_add.reset_index()
df_add['DateTime'] = pd.to_datetime(df_add[['year', 'month',
'day', 'hour']])
# df_add['DateTime'] = df_add.DateTime.dt.tz_localize('UTC')
#df_add.loc[df_add.DateTime.dt.month.isin([10])
# & df_add.DateTime.dt.day.isin([26])
# & df_add.fl_id.isin(['bio_all'])
# ]
return df_add
if __name__ == '__main__':
dict_sql = dict(db='storage2')
kw_dict = dict(dict_sql=dict_sql,
tm_filt={'year': [2015, 2014, 2016]},
col_filt=[], ext=['xlsx'], exclude_substrings=['Real'])
pr = RTEProduction(kw_dict)
pr.get_fn_list()
pr.fn_list = [f for f in pr.fn_list if '2015' in f or '2016' in f or '2014' in f]
fn = pr.fn_list[0]
pr.read_all(skip_sql=True)
pr.post_processing()
# %%
class RTEProductionRealisProd(ProfileReader):
'''
Read production data from RTE RealisationDonneesProduction_*.xls files,
obtained from http://clients.rte-france.com/lang/fr/visiteurs/vie/prod/realisation_production.jsp
'''
dict_sql_default = dict(sc='profiles_raw', tb='rte_production_realis_prod')
data_dir = os.path.normpath('SUPPLY_PROFILES/FRANCE_RTE')
# sql table columns
tb_cols = [('"DateTime"', 'TIMESTAMP'), ('fl_id', 'VARCHAR'),
('value', 'DOUBLE PRECISION'), ('hy', 'SMALLINT'),
('year', 'SMALLINT'),
('mt_id', 'SMALLINT'),
('nd_id', 'VARCHAR(7)')]
# sql table primary key
tb_pk = ['nd_id', 'fl_id', 'year', 'hy']
def __init__(self, kw_dict):
super().__init__(**kw_dict)
def post_processing(self, df):
df['mt_id'] = df['DateTime'].dt.month - 1
return df
def read(self, fn):
#
#df_add = pd.read_csv(fn, encoding="ISO-8859-1")
#df_add.apply(lambda x: x.split('\t'), axis=1)
#
## df_add = df_add.loc[(-df_add.Date.isnull())
## & (-df_add.Consommation.isnull())]
#
# df_add['DateTime'] = pd.to_datetime(df_add['Date'].dt.date.apply(str)
# + ' ' + df_add['Heures'].apply(str))
#
# df_add['DateTime'] = df_add['DateTime'].dt.tz_localize('UTC')
#
# skip_cols = ['DateTime', 'Date', 'Heures', 'Nature', 'Périmètre',
# 'year', 'hour']
# df_add = df_add.set_index(['DateTime'])[[c for c in df_add.columns
# if not c in skip_cols]]
#
# dict_sf = {'Consommation': 'load',
# 'Prévision J-1': 'load_prediction_d-1',
# 'Prévision J': 'load_prediction_d',
# 'Fioul': 'mineral_oil_heavy',
# 'Charbon': 'hard_coal',
# 'Gaz': 'natural_gas',
# 'Nucléaire': 'nuclear_fuel',
# 'Eolien': 'wind_onshore',
# 'Solaire': 'photovoltaics',
# 'Hydraulique': 'hydro_total',
# 'Pompage': 'pumped_hydro',
# 'Bioénergies': 'bio_all',
# 'Ech. physiques': 'import_export',
# 'Taux de Co2': 'co2_intensity',
## 'Ech. comm. Angleterre': 'import/export_UK',
## 'Ech. comm. Espagne': 'import/export_ES',
## 'Ech. comm. Italie': 'import/export_IT',
## 'Ech. comm. Suisse': 'import/export_CH',
## 'Ech. comm. Allemagne-Belgique': 'import/export_DE/BE',
# }
#
#
# col_dict = {'level_1': 'fl_id', 0: 'value'}
# df_add = df_add.stack().reset_index().rename(columns=col_dict)
#
# df_add = df_add.loc[df_add.fl_id.isin(list(dict_sf.keys()))]
#
# df_add['fl_id'] = df_add.fl_id.replace(dict_sf)
# df_add['year'] = df_add['DateTime'].dt.year
# df_add['nd_id'] = 'FR0'
#
# df_add['value'] = df_add.groupby([df_add.fl_id,
# df_add.DateTime.dt.hour,
# df_add.DateTime.dt.date])['value'].transform(lambda x: x.mean())
#
# df_add = df_add.loc[df_add.DateTime.dt.minute == 0]
return df_add
if __name__ == '__main__':
dict_sql = dict(db='storage2')
kw_dict = dict(dict_sql=dict_sql,
tm_filt={'year': range(2005, 2018)},
col_filt=[], ext=['xls'], exclude_substrings=['eCO2'] + ['%d'%yr for yr in range(2006,2015)])
pr = RTEProductionRealisProd(kw_dict)
pr.get_fn_list()
self = pr
fn = pr.fn_list[0]
# pr.read_all()
# df = aql.read_sql('storage2', 'profiles_raw', 'rte_production',
# filt=[('fl_id', ['hard_coal'])])
#
# df.pivot_table(columns='year', index='mt_id', values='value', aggfunc=np.sum)
#
# df.set_index('DateTime')['value'].plot()
# %%
class EntsoeGenerationReader(ProfileReader):
dict_sql_default = dict(sc='profiles_raw', tb='entsoe_generation')
data_dir = os.path.normpath('ENTSOE/ACTUAL_GENERATION_PER_PRODUCTION_TYPE')
# sql table columns
tb_cols = [('"DateTime"', 'TIMESTAMP'), ('fl_id', 'VARCHAR'),
('value', 'DOUBLE PRECISION'), ('hy', 'SMALLINT'),
('year', 'SMALLINT'), ('nd_id', 'VARCHAR(7)')]
# sql table primary key
tb_pk = ['nd_id', 'fl_id', 'year', 'hy']
def __init__(self, kw_dict):
super().__init__(**kw_dict)
def read(self, fn):
''' Read data from Entsoe UTC csv files. '''
df_add = pd.read_csv(fn).fillna(0).replace({'n/e': 0})
df_add['nd_id'] = df_add['Area'].apply(lambda x:
x.split('(')[-1]
.replace(')', '') + '0')
# get center datetime column
separate_dt = lambda x: x.split(' - ')[ind].replace(' (UTC)', '')
dt_format = '%d.%m.%Y %H:%M'
for dtcol, ind in [('beg', 0)]:
df_add[dtcol] = df_add['MTU'].apply(separate_dt)
df_add[dtcol] = pd.to_datetime(df_add[dtcol], format=dt_format)
df_add[dtcol] = df_add[dtcol].dt.tz_localize('UTC')
time_res = 0.5 * pd.Timedelta(df_add['beg'].iloc[1]
- df_add['beg'].iloc[0])
df_add['DateTime'] = df_add['beg'].map(lambda x: x + time_res)
# drop unnecessary columns
df_add = df_add.drop(['MTU', 'Area', 'beg'], axis=1)
# stack
df_add = (df_add.set_index(['nd_id', 'DateTime'])
.stack()
.reset_index()
.rename(columns={'level_2': 'fl_id', 0: 'value'}))
# translate technologies to our sub_fuel names
dict_sf = {'Biomass': 'bio_all',
'Fossil Brown coal/Lignite': 'lignite',
'Fossil Coal-derived gas': 'natural_gas',
'Fossil Gas': 'natural_gas',
'Fossil Hard coal': 'hard_coal',
'Fossil Oil': 'mineral_oil_heavy',
'Fossil Oil shale': 'mineral_oil_heavy',
'Fossil Peat': 'lignite',
'Geothermal': 'geothermal',
'Pumped': 'pumped_hydro',
'Pumped(.+)\.1': 'pumped_hydro_pumping',
'Run-of-river': 'run_of_river',
'Run-of-river(.+)\.1': 'run_of_river_negative',
'Reservoir(.+)\.1': 'reservoir_negative',
'Reservoir': 'reservoir',
'Marine': 'marine',
'Nuclear': 'nuclear_fuel',
'Other': 'other',
'Other(.+)\.1': 'other_negative',
'Other renewable': 'other_ren',
'Solar': 'photovoltaics',
'Waste': 'waste_mix',
'Wind Offshore': 'wind_offshore',
'Wind Onshore': 'wind_onshore'}
# remove sf which are zero
sf_non_zero = df_add.loc[df_add.value != 0, 'fl_id'].drop_duplicates()
df_add = df_add.loc[df_add.fl_id.isin(sf_non_zero)]
# translate sf either based on contains or on regex
replace_sf = lambda x: x.replace({x.iloc[0]: vv for kk, vv in
dict_sf.items()
if re.match(re.compile(kk),
x.iloc[0])
or kk in x.iloc[0]})
df_add['fl_id'] = df_add.groupby('fl_id')['fl_id'].transform(replace_sf)
#check if any raw entsoe column names remain
lst_raw = (df_add.loc[df_add.fl_id.str.contains('ggreg'), 'fl_id']
.drop_duplicates().tolist())
if lst_raw:
raise ValueError('Couldn\'t translate columns {} while reading {}.'
.format(', '.join(lst_raw), fn))
df_add = self.time_resample(df_add, res='H')
return df_add
if __name__ == '__main__':
dict_sql = dict(db='storage2')
kw_dict = dict(dict_sql=dict_sql,
tm_filt={'year': range(2005, 2018)},
col_filt=[])
nr = EntsoeGenerationReader(kw_dict)
nr.get_fn_list()
self = nr
fn = nr.fn_list[0]
nr.read_all()
# %%
class OpenPowerSystemReader(ProfileReader):
''' Read data from https://data.open-power-system-data.org/. '''
dict_sql_default = dict(sc='profiles_raw', tb='open_power_system_data')
data_dir = os.path.normpath('OPEN_POWER_SYSTEM_DATA/TIME_SERIES')
tb_cols = [('"DateTime"', 'TIMESTAMP'),
('pp_id', 'VARCHAR'),
('value', 'DOUBLE PRECISION'),
('hy', 'SMALLINT'),
('val_type', 'VARCHAR'),
('tso', 'VARCHAR'),
('year', 'SMALLINT')]
tb_pk = ['pp_id', 'val_type', 'tso', 'year', 'hy']
def __init__(self, kw_dict):
super().__init__(**kw_dict)
self.get_fn_list()
def read(self, fn):
''' Reads input files and generates a dataframe. '''
df_add = pd.read_csv(fn)
lst_tso = ['amprion', 'tennet', 'transnetbw', '50hertz']
df_add = df_add[['utc_timestamp'] + [c for c in df_add.columns
if not 'entsoe' in c
and any([tso in c for tso in lst_tso])]]
df_add['utc_timestamp'] = pd.to_datetime(df_add['utc_timestamp'])
df_add['DateTime'] = df_add['utc_timestamp'].dt.tz_localize('UTC')
df_add = df_add.drop('utc_timestamp', axis=1)
df_add = df_add.set_index('DateTime')
df_add = df_add.resample('H').mean()
# split columns names
col_tuples = [tuple(c.replace('_o', 'o').split('_'))
for c in df_add.columns]
col_level_names = ['nd_id', 'tso', 'pp_id', 'generation', 'val_type']
df_add.columns = pd.MultiIndex.from_tuples(col_tuples,
names=col_level_names)
# stack
df_add = df_add.stack(level=list(range(5))).rename('value').reset_index()
df_add = df_add.drop(['generation', 'nd_id'], axis=1)
df_add.loc[df_add.value < 0] = 0
df_add = df_add.loc[df_add.pp_id != 0]
dict_pp_id = {'wind': 'DE_WIN_TOT',
'windoffshore': 'DE_WIN_OFF',
'windonshore': 'DE_WIN_ONS',
'solar': 'DE_SOL_PHO'}
df_add['pp_id'] = df_add['pp_id'].replace(dict_pp_id)
return df_add
if __name__ == '__main__':
dict_sql = dict(db='storage2')
kw_dict = dict(dict_sql=dict_sql,
tm_filt={'year': range(2005, 2018)},
col_filt=[],
exclude_substrings=['filtered', '30', '60'])
op = OpenPowerSystemReader(kw_dict)
self = op
fn = self.fn_list[0]
sys.exit()
op.read_all(skip_sql=True)
self.df_tot = self.get_hour_of_the_year(self.df_tot)
self.append_to_sql(self.df_tot)
# %%
class CHPProfileReader(ProfileReader):
''' '''
dict_sql_default = dict(sc='profiles_raw', tb='chp_profiles')
data_dir = os.path.normpath('HEAT_DEMAND_PROFILE')
tb_cols = [('nd_id', 'VARCHAR'),
('value', 'DOUBLE PRECISION'),
('hy', 'SMALLINT')]
tb_pk = ['nd_id', 'hy']
def __init__(self, kw_dict):
super().__init__(**kw_dict)
self.get_fn_list()
def single_post_processing(self, df):
'''
Overwriting parent method.
'''
return df
def read(self, fn):
''' Reads input files and generates a dataframe. '''
wb = open_workbook(fn)
profile_heat_cols = range(31)
df_profile_heat = read_xlsx_table(wb, ['TOT'], columns=profile_heat_cols)
years = df_profile_heat.loc[1]
ctrys = df_profile_heat.loc[0]
cols = [(years[i], ctrys[i]) for i in range(len(years))]
df_profile_heat.columns = ['hy'] + cols[1:]
df_profile_heat = df_profile_heat.drop([0,1], axis=0).reset_index(drop=True)
df_profile_heat = df_profile_heat.set_index('hy')
df_profile_heat.columns = pd.MultiIndex.from_tuples(df_profile_heat.columns,
names=['year','country'])
df_profile_heat = df_profile_heat.stack(['year', 'country']).reset_index()
df_profile_heat = df_profile_heat.loc[df_profile_heat.year == 2015]
df_profile_heat.columns = ([c for c in df_profile_heat.columns[:-1]]
+ ['power'])
df_profile_heat['value'] = df_profile_heat['power'].apply(float)
df_profile_heat = df_profile_heat.loc[df_profile_heat.year == 2015]
df_profile_heat['nd_id'] = df_profile_heat['country'] + '0'
df_profile_heat = df_profile_heat[['hy', 'nd_id', 'value']]
# normalize
df_profile_heat['value'] = df_profile_heat.groupby('nd_id')['value'].apply(lambda x: x/x.sum())
return df_profile_heat
if __name__ == '__main__':
dict_sql = dict(db='storage2')
kw_dict = dict(dict_sql=dict_sql,
tm_filt={'year': range(2005, 2018)},
col_filt=[],
exclude_substrings=[], ext='xlsx')
op = CHPProfileReader(kw_dict)
self = op
fn = self.fn_list[0]
# sys.exit()
op.read_all(skip_sql=True)
self.append_to_sql(self.df_tot)
# %%
class WeeklyRORReader(ProfileReader):
''' '''
dict_sql_default = dict(sc='profiles_raw', tb='weekly_ror_data')
data_dir = os.path.normpath('WEEKLY_ROR_DATA')