-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.py
224 lines (170 loc) · 5.6 KB
/
parsers.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
'''
Name: parsers.py
Author: Bertfried Fauser
Contributor: Timur Kasimov
Created: 2017?
Updated: July 2024
Purpose:
internal script that parses through XML and time series data from
ENTSO-E API and outputs it as a pandas data frame
'''
import logging
import bs4
import pandas as pd
import mappings
logger = logging.getLogger(__name__ +'-api')
logger.addHandler(logging.NullHandler())
# logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.INFO)
def _extract_timeseries(xml_text):
"""
Parameters
----------
xml_text : str
Yields
-------
bs4.element.tag
"""
if not xml_text:
print("NOT XML TEXT")
return
soup = bs4.BeautifulSoup(xml_text, 'xml')
for timeseries in soup.find_all('TimeSeries'):
yield timeseries # returns an iterable object of timeseries
def parse_prices(xml_text):
"""
Parameters
----------
xml_text : str
Returns
-------
pd.Series
"""
series = pd.Series()
for soup in _extract_timeseries(xml_text):
series = series._append(_parse_price_timeseries(soup))
series = series.sort_index() # can potentially get away without sorting as everything should be sorted already
return series
def parse_generation(xml_text):
"""
Parameters
----------
xml_text : str
Returns
-------
pd.DataFrame
"""
all_series = {} # initialize all series as a dictionary
for soup in _extract_timeseries(xml_text):
#produce a single ts for one production type and the specified interval
ts = _parse_generation_forecast_timeseries(soup)
# name the data frame
series = all_series.get(ts.name)
if series is None:
all_series[ts.name] = ts
else:
series = series._append(ts)
series.sort_index()
all_series[series.name] = series
# put together all series into a single dataframe
df = pd.DataFrame.from_dict(all_series)
return df
def _parse_price_timeseries(soup):
"""
Parameters
----------
soup : bs4.element.tag
Returns
-------
pd.Series
"""
positions = []
prices = []
for point in soup.find_all('Point'):
positions.append(int(point.find('position').text))
prices.append(float(point.find('price.amount').text))
series = pd.Series(index=positions, data=prices)
series = series.sort_index()
series.index = _parse_datetimeindex(soup)
return series
def _parse_generation_forecast_timeseries(soup):
"""
Parameters
----------
# soup : bs4.element.tag
Returns
-------
pd.Series
"""
# soup is just one timeseries
psrtype = soup.find('psrType').text
production_type = 'Generation'
# consumption case
is_out = soup.find('outBiddingZone_Domain.mRID')
if is_out is not None:
production_type = 'Consumption'
positions = []
quantities = []
# record each quantity with corresponding position
for point in soup.find_all('Point'):
position = point.find('position')
quantity = point.find('quantity')
if (position is not None):
positions.append(int(position.text))
if (quantity is not None):
quantities.append(float(quantity.text))
series = pd.Series(index=positions, data=quantities)
series = series.sort_index() # is this line even necessary?
# negate consumption values for plotting
if (production_type == 'Consumption'):
series *= -1
# replaces df's series' indices with time stamps and obtain energy units scaling factor
series.index, Wh_converting_factor = _parse_datetimeindex(soup)
series = series * Wh_converting_factor # scale series's power (MW) values into energy values (MWh)
# name the series according to production type
series.name = mappings.PSRTYPE_MAPPINGS[psrtype] + ' ' + production_type
return series
def _parse_datetimeindex(soup):
"""
Create a datetimeindex from a parsed beautifulsoup,
given that it contains the elements 'start', 'end'
and 'resolution'
Parameters
----------
soup : bs4.element.tag
Returns
-------
pd.DatetimeIndex
"""
start = pd.Timestamp(soup.find('start').text.rstrip("Z")) #manually remove UTC's timezone info
end = pd.Timestamp(soup.find('end').text.rstrip("Z"))
delta = _resolution_to_timedelta(res_text=soup.find('resolution').text) #specified time interval between points
logger.info('delta is %s', delta)
# index = pd.date_range(start=start, end=end, freq=delta, closed='left')
index = pd.date_range(start=start, end=end, freq=delta, inclusive='left') # replaces df's series' indices with time stamps in increments on delta
Wh_scaler = mappings.DELTA_TO_SCALER[delta]
return index, Wh_scaler
def _resolution_to_timedelta(res_text):
"""
Convert an Entsoe resolution to something that pandas can understand
Parameters
----------
res_text : str
Returns
-------
str
"""
if res_text == 'PT15M':
delta = '15min'
elif res_text == 'PT60M':
delta = '60min'
elif res_text == 'P1Y':
delta = '12M'
elif res_text == 'PT30M':
delta = '30min'
else:
raise NotImplementedError("Sorry, I don't know what to do with the "
"resolution '{}', because there was no "
"documentation to be found of this format "
"everything is hard coded. Please open an "
"issue.".format(res_text))
return delta