-
Notifications
You must be signed in to change notification settings - Fork 902
/
IE.py
267 lines (234 loc) · 8.29 KB
/
IE.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
from datetime import datetime, timedelta
from logging import Logger, getLogger
from typing import Optional
import arrow
import pytz
from requests import Response, Session
from parsers.lib.config import refetch_frequency
from parsers.lib.exceptions import ParserException
IE_TZ = pytz.timezone("Europe/Dublin")
DEMAND_URL = "https://www.smartgriddashboard.com/DashboardService.svc/data?area=demandactual®ion={zone}&datefrom={dt}+00%3A00&dateto={dt}+23%3A59"
DEMAND_FORECAST_URL = "https://www.smartgriddashboard.com/DashboardService.svc/data?area=demandforecast®ion={zone}&datefrom={dt}+00%3A00&dateto={dt}+23%3A59"
WIND_URL = "https://www.smartgriddashboard.com/DashboardService.svc/data?area=windactual®ion={zone}&datefrom={dt}+00%3A00&dateto={dt}+23%3A59"
WIND_FORECAST_URL = "https://www.smartgriddashboard.com/DashboardService.svc/data?area=windforecast®ion={zone}&datefrom={dt}+00%3A00&dateto={dt}+23%3A59"
EXCHANGE_URL = "https://www.smartgriddashboard.com/DashboardService.svc/data?area=interconnection®ion={zone}&datefrom={dt}+00%3A00&dateto={dt}+23%3A59"
KINDS_URL = {
"demand": DEMAND_URL,
"demand_forecast": DEMAND_FORECAST_URL,
"wind": WIND_URL,
"wind_forecast": WIND_FORECAST_URL,
"exchange": EXCHANGE_URL,
}
ZONE_MAPPING = {
"IE": {"key": "ROI", "exchange": "INTER_EWIC"},
"GB-NIR": {"key": "NI", "exchange": "INTER_MOYLE"},
}
def fetch_data(
target_datetime: datetime,
zone_key: str,
kind: str,
session: Session,
) -> list:
"""
Gets values and corresponding datetimes for the specified data kind in ROI.
Removes any values that are in the future or don't have a datetime associated with them.
"""
assert type(target_datetime) == datetime
assert kind != ""
assert session is not None
resp: Response = session.get(
KINDS_URL[kind].format(
dt=target_datetime.strftime("%d-%b-%Y"), zone=ZONE_MAPPING[zone_key]["key"]
)
)
try:
data = resp.json().get("Rows", {})
except:
raise ParserException(
parser="IE.py",
message=f"{target_datetime}: {kind} data is not available for {zone_key}",
)
filtered_data = [item for item in data if item["Value"] is not None]
return filtered_data
@refetch_frequency(timedelta(days=1))
def fetch_production(
zone_key: str,
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> list:
"""Gets values for wind production and estimates unknwon production as demand - wind - exchange"""
if target_datetime is None:
target_datetime = datetime.now().replace(tzinfo=IE_TZ)
demand_data = fetch_data(
target_datetime=target_datetime,
zone_key=zone_key,
kind="demand",
session=session,
)
wind_data = fetch_data(
target_datetime=target_datetime, zone_key=zone_key, kind="wind", session=session
)
exchange_data = fetch_data(
target_datetime=target_datetime,
zone_key=zone_key,
kind="exchange",
session=session,
)
assert len(demand_data) > 0
assert len(wind_data) > 0
assert len(exchange_data) > 0
production = []
for item in demand_data:
dt = item["EffectiveTime"]
wind_dt = [item for item in wind_data if item["EffectiveTime"] == dt]
if len(wind_dt) == 1:
wind_prod = wind_dt[0]["Value"]
else:
wind_prod = 0
exchange_dt = [item for item in exchange_data if item["EffectiveTime"] == dt]
if len(exchange_dt) == 1:
exchange = exchange_dt[0]["Value"]
else:
exchange = 0
data_point = {
"zoneKey": zone_key,
"datetime": datetime.strptime(dt, "%d-%b-%Y %H:%M:%S").replace(
tzinfo=pytz.timezone("Europe/Dublin")
),
"production": {
"unknown": item["Value"] - exchange - wind_prod,
"wind": wind_prod,
},
"source": "eirgridgroup.com",
}
production += [data_point]
return production
@refetch_frequency(timedelta(days=1))
def fetch_exchange(
zone_key1: str,
zone_key2: str,
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> list:
"""
Fetches exchanges values for the East-West (GB->IE) and Moyle (GB->GB-NIR)
interconnectors.
"""
if target_datetime is None:
target_datetime = datetime.now().replace(tzinfo=IE_TZ)
sortedZoneKeys = "->".join(sorted([zone_key1, zone_key2]))
exchange_data = fetch_data(
target_datetime=target_datetime,
zone_key=zone_key2,
kind="exchange",
session=session,
)
assert len(exchange_data) > 0
filtered_exchanges = [
item
for item in exchange_data
if item["FieldName"] == ZONE_MAPPING[zone_key2]["exchange"]
]
exchange = []
for item in filtered_exchanges:
data_point = {
"netFlow": -item["Value"],
"sortedZoneKeys": sortedZoneKeys,
"datetime": datetime.strptime(
item["EffectiveTime"], "%d-%b-%Y %H:%M:%S"
).replace(tzinfo=pytz.timezone("Europe/Dublin")),
"source": "eirgridgroup.com",
}
exchange += [data_point]
return exchange
@refetch_frequency(timedelta(days=1))
def fetch_consumption(
zone_key: str,
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> list:
"""gets consumption values for ROI"""
if target_datetime is None:
target_datetime = datetime.now().replace(tzinfo=IE_TZ)
demand_data = fetch_data(
target_datetime=target_datetime,
zone_key=zone_key,
kind="demand",
session=session,
)
assert len(demand_data) > 0
demand = []
for item in demand_data:
data_point = {
"zone_key": zone_key,
"consumption": item["Value"],
"datetime": datetime.strptime(
item["EffectiveTime"], "%d-%b-%Y %H:%M:%S"
).replace(tzinfo=pytz.timezone("Europe/Dublin")),
"source": "eirgridgroup.com",
}
demand += [data_point]
return demand
@refetch_frequency(timedelta(days=1))
def fetch_consumption_forecast(
zone_key: str,
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> list:
"""gets forecasted consumption values for ROI"""
if target_datetime is None:
target_datetime = datetime.now().replace(tzinfo=IE_TZ)
demand_forecast_data = fetch_data(
target_datetime=target_datetime,
zone_key=zone_key,
kind="demand_forecast",
session=session,
)
assert len(demand_forecast_data) > 0
demand_forecast = []
for item in demand_forecast_data:
data_point = {
"zone_key": zone_key,
"consumption": item["Value"],
"datetime": datetime.strptime(
item["EffectiveTime"], "%d-%b-%Y %H:%M:%S"
).replace(tzinfo=pytz.timezone("Europe/Dublin")),
"source": "eirgridgroup.com",
}
demand_forecast += [data_point]
return demand_forecast
@refetch_frequency(timedelta(days=1))
def fetch_wind_forecasts(
zone_key: str,
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> list:
"""
Gets values and corresponding datetimes for forecasted wind produciton.
"""
if target_datetime is None:
target_datetime = datetime.now().replace(tzinfo=IE_TZ)
wind_forecast_data = fetch_data(
target_datetime=target_datetime,
zone_key=zone_key,
kind="wind_forecast",
session=session,
)
assert len(wind_forecast_data) > 0
wind_forecast = []
for item in wind_forecast_data:
data_point = {
"zone_key": zone_key,
"production": {"wind": item["Value"]},
"datetime": datetime.strptime(
item["EffectiveTime"], "%d-%b-%Y %H:%M:%S"
).replace(tzinfo=pytz.timezone("Europe/Dublin")),
"source": "eirgridgroup.com",
}
wind_forecast += [data_point]
return wind_forecast