-
Notifications
You must be signed in to change notification settings - Fork 902
/
US_PJM.py
399 lines (325 loc) · 12 KB
/
US_PJM.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
#!/usr/bin/env python3
"""Parser for the PJM area of the United States."""
import re
from datetime import datetime, timedelta
from logging import Logger, getLogger
from typing import List, Optional, Union
import arrow
import demjson3 as demjson
import pandas as pd
import pytz
from bs4 import BeautifulSoup
from dateutil import parser, tz
from requests import Response, Session
from parsers.lib.config import refetch_frequency
from parsers.lib.exceptions import ParserException
# Used for consumption forecast data.
API_ENDPOINT = "https://api.pjm.com/api/v1/"
# Used for both production and price data.
url = "http://www.pjm.com/markets-and-operations.aspx"
mapping = {
"Coal": "coal",
"Gas": "gas",
"Hydro": "hydro",
"Multiple Fuels": "unknown",
"Nuclear": "nuclear",
"Oil": "oil",
"Other": "unknown",
"Other Renewables": "unknown",
"Solar": "solar",
"Wind": "wind",
}
exchange_mapping = {
"nyiso": "NYIS|NYIS",
"neptune": "NEPTUNE|SAYR",
"linden": "LINDENVFT|LINDEN",
"hudson": "HUDSONTP|HTP",
"miso": "miso",
"ohio valley": "DEOK|OVEC",
"louisville": "SOUTHIMP|LGEE",
"tennessee valley": "SOUTHIMP|TVA",
"cpl west": "SOUTHIMP|CPLW",
"duke": "SOUTHIMP|DUKE",
"cpl east": "SOUTHIMP|CPLE",
}
FUEL_MAPPING = {
"Coal": "coal",
"Gas": "gas",
"Hydro": "hydro",
"Multiple Fuels": "unknown",
"Nuclear": "nuclear",
"Oil": "oil",
"Other": "unknown",
"Other Renewables": "unknown",
"Solar": "solar",
"Storage": "battery",
"Wind": "wind",
}
def get_api_subscription_key(session: Session) -> str:
pjm_settings: Response = session.get(
"https://dataminer2.pjm.com/config/settings.json"
)
if pjm_settings.status_code == 200:
return pjm_settings.json()["subscriptionKey"]
raise ParserException(
parser="US_PJM.py",
message=f"Could not get API key",
)
def fetch_api_data(kind: str, params: dict, session: Session) -> list:
headers = {
"Host": "api.pjm.com",
"Ocp-Apim-Subscription-Key": get_api_subscription_key(session=session),
"Origin": "http://dataminer2.pjm.com",
"Referer": "http://dataminer2.pjm.com/",
}
url = API_ENDPOINT + kind
resp: Response = session.get(url=url, params=params, headers=headers)
if resp.status_code == 200:
data = resp.json()
return data
else:
raise ParserException(
parser="US_PJM.py",
message=f"{kind} data is not available in the API",
)
def fetch_consumption_forecast_7_days(
zone_key: str = "US-PJM",
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> list:
"""Gets consumption forecast for specified zone."""
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates")
if not session:
session = Session()
# startRow must be set if forecast_area is set.
# RTO_COMBINED is area for whole PJM zone.
params = {"download": True, "startRow": 1, "forecast_area": "RTO_COMBINED"}
# query API
data = fetch_api_data(kind="load_frcstd_7_day", params=params, session=session)
data_points = list()
for elem in data:
utc_datetime = elem["forecast_datetime_beginning_utc"]
data_point = {
"zoneKey": zone_key,
"datetime": arrow.get(utc_datetime).replace(tzinfo="UTC").datetime,
"value": elem["forecast_load_mw"],
"source": "pjm.com",
}
data_points.append(data_point)
return data_points
@refetch_frequency(timedelta(days=1))
def fetch_production(
zone_key: str = "US-PJM",
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> list:
"""uses PJM API to get generation by fuel. we assume that storage is battery storage (see https://learn.pjm.com/energy-innovations/energy-storage)"""
if target_datetime is None:
target_datetime = arrow.utcnow().datetime
params = {
"download": True,
"startRow": 1,
"fields": "datetime_beginning_ept,fuel_type,mw",
"datetime_beginning_ept": target_datetime.strftime("%Y-%m-%dT%H:00:00.0000000"),
}
resp_data = fetch_api_data(kind="gen_by_fuel", params=params, session=session)
data = pd.DataFrame(resp_data)
data.datetime_beginning_ept = pd.to_datetime(data.datetime_beginning_ept)
data = data.set_index("datetime_beginning_ept")
data.fuel_type = data.fuel_type.map(FUEL_MAPPING)
all_data_points = []
for dt in data.index.unique():
production = {}
storage = {}
data_dt = data.loc[data.index == dt]
for i in range(len(data_dt)):
row = data_dt.iloc[i]
if row["fuel_type"] == "battery":
storage["battery"] = row.get("mw")
else:
mode = row["fuel_type"]
production[mode] = row.get("mw")
data_point = {
"zoneKey": zone_key,
"datetime": arrow.get(dt).datetime.replace(
tzinfo=pytz.timezone("US/Eastern")
),
"production": production,
"storage": storage,
"source": "pjm.com",
}
all_data_points += [data_point]
return all_data_points
def add_default_tz(timestamp):
"""Adds EST timezone to datetime object if tz = None."""
EST = tz.gettz("America/New_York")
modified_timestamp = timestamp.replace(tzinfo=timestamp.tzinfo or EST)
return modified_timestamp
def get_miso_exchange(session: Session) -> tuple:
"""
Current exchange status between PJM and MISO.
:return: tuple containing flow and timestamp.
"""
map_url = "http://pjm.com/markets-and-operations/interregional-map.aspx"
res: Response = session.get(map_url)
soup = BeautifulSoup(res.text, "html.parser")
find_div = soup.find("div", {"id": "body_0_flow1", "class": "flow"})
miso_flow = find_div.text
miso_flow_no_ws = "".join(miso_flow.split())
miso_actual = miso_flow_no_ws.split("/")[0].replace(",", "")
direction_tag = find_div.find("img")
left_or_right = direction_tag["src"]
# The flow direction is determined by img arrows.
if left_or_right == "/assets/images/mapImages/black-L.png":
# left set negative
flow = -1 * float(miso_actual)
elif left_or_right == "/assets/images/mapImages/black-R.png":
# right set positive
flow = float(miso_actual)
else:
raise ValueError("US-MISO->US-PJM flow direction cannot be determined.")
find_timestamp = soup.find("div", {"id": "body_0_divTimeStamp"})
dt_naive = parser.parse(find_timestamp.text)
dt_aware = add_default_tz(dt_naive)
return flow, dt_aware
def get_exchange_data(interface, session: Session) -> list:
"""
This function can fetch 5min data for any PJM interface in the current day.
Extracts load and timestamp data from html source then joins them together.
"""
base_url = "http://www.pjm.com/Charts/InterfaceChart.aspx?open="
url = base_url + exchange_mapping[interface]
res: Response = session.get(url)
soup = BeautifulSoup(res.text, "html.parser")
scripts = soup.find(
"script",
{
"type": "text/javascript",
"src": "/assets/js/Highcharts/HighCharts/highcharts.js",
},
)
exchange_script = scripts.find_next_sibling("script")
load_pattern = r"var load = (\[(.*)\])"
load = re.search(load_pattern, str(exchange_script)).group(1)
load_vals = demjson.decode(load)[0]
# Occasionally load_vals contains a null at the end of the list which must be caught.
actual_load = [float(val) for val in load_vals if val is not None]
time_pattern = r"var timeArray = (\[(.*)\])"
time_array = re.search(time_pattern, str(exchange_script)).group(1)
time_vals = demjson.decode(time_array)
flows = zip(actual_load, time_vals)
arr_date = arrow.now("America/New_York").floor("day")
converted_flows = []
for flow in flows:
arr_time = arrow.get(flow[1], "h:mm A")
arr_dt = arr_date.replace(hour=arr_time.hour, minute=arr_time.minute).datetime
converted_flow = (flow[0], arr_dt)
converted_flows.append(converted_flow)
return converted_flows
def combine_NY_exchanges(session: Session) -> list:
"""
Combination function for the 4 New York interfaces.
Timestamps are checked to ensure correct combination.
"""
nyiso = get_exchange_data("nyiso", session)
neptune = get_exchange_data("neptune", session)
linden = get_exchange_data("linden", session)
hudson = get_exchange_data("hudson", session)
combined_flows = zip(nyiso, neptune, linden, hudson)
flows = []
for datapoint in combined_flows:
total = sum([n[0] for n in datapoint])
stamps = [n[1] for n in datapoint]
# Data quality check to make sure timestamps all match.
if len(set(stamps)) == 1:
dt = stamps[0]
else:
# Drop bad datapoint and move to next.
continue
flows.append((total, dt))
return flows
def fetch_exchange(
zone_key1: str,
zone_key2: str,
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> Union[List[dict], dict]:
"""Requests the last known power exchange (in MW) between two zones."""
if target_datetime is not None:
raise NotImplementedError("This parser is not yet able to parse past dates")
# PJM reports exports as negative.
sortedcodes = "->".join(sorted([zone_key1, zone_key2]))
if sortedcodes == "US-NY->US-PJM":
flows = combine_NY_exchanges(session)
elif sortedcodes == "US-MIDA-PJM->US-NY-NYIS":
flows = combine_NY_exchanges(session)
flows = [(-total, dt) for total, dt in flows]
elif sortedcodes == "US-MISO->US-PJM":
flow = get_miso_exchange(session)
exchange = {
"sortedZoneKeys": sortedcodes,
"datetime": flow[1],
"netFlow": flow[0],
"source": "pjm.com",
}
return exchange
elif sortedcodes == "US-MIDA-PJM->US-MIDW-MISO":
flow = get_miso_exchange(session)
exchange = {
"sortedZoneKeys": sortedcodes,
"datetime": flow[1],
"netFlow": -flow[0],
"source": "pjm.com",
}
return exchange
else:
raise NotImplementedError("This exchange pair is not implemented")
exchanges = []
for flow in flows:
exchange = {
"sortedZoneKeys": sortedcodes,
"datetime": flow[1],
"netFlow": flow[0],
"source": "pjm.com",
}
exchanges.append(exchange)
return exchanges
def fetch_price(
zone_key: str = "US-PJM",
session: Session = Session(),
target_datetime: Optional[datetime] = None,
logger: Logger = getLogger(__name__),
) -> dict:
"""Requests the last known power price of a given country."""
if target_datetime is not None:
raise NotImplementedError("This parser is not yet able to parse past dates")
res: Response = session.get(url)
soup = BeautifulSoup(res.text, "html.parser")
price_tag = soup.find("span", class_="rtolmpico")
price_data = price_tag.find_next("h2")
price_string = price_data.text.split("$")[1]
price = float(price_string)
dt = arrow.now("America/New_York").floor("second").datetime
data = {
"zoneKey": zone_key,
"currency": "USD",
"datetime": dt,
"price": price,
"source": "pjm.com",
}
return data
if __name__ == "__main__":
print("fetch_consumption_forecast_7_days() ->")
print(fetch_consumption_forecast_7_days())
print("fetch_production() ->")
print(fetch_production())
print("fetch_exchange(US-NY, US-PJM) ->")
print(fetch_exchange("US-NY", "US-PJM"))
print("fetch_exchange(US-MISO, US-PJM)")
print(fetch_exchange("US-MISO", "US-PJM"))
print("fetch_price() ->")
print(fetch_price())