-
Notifications
You must be signed in to change notification settings - Fork 903
/
US_NEISO.py
277 lines (220 loc) · 8.29 KB
/
US_NEISO.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
#!/usr/bin/env python3
"""Real time parser for the New England ISO (NEISO) area."""
import time
from datetime import datetime, timedelta, timezone
from logging import Logger, getLogger
from typing import Any
from zoneinfo import ZoneInfo
from requests import Session
from electricitymap.contrib.lib.models.event_lists import (
ExchangeList,
ProductionBreakdownList,
)
from electricitymap.contrib.lib.models.events import ProductionMix, StorageMix
from electricitymap.contrib.lib.types import ZoneKey
from parsers.lib.config import refetch_frequency
US_NEISO_KEY = ZoneKey("US-NE-ISNE")
SOURCE = "iso-ne.com"
url = "https://www.iso-ne.com/ws/wsclient"
generation_mapping = {
"Coal": "coal",
"NaturalGas": "gas",
"Wind": "wind",
"Hydro": "hydro",
"Nuclear": "nuclear",
"Wood": "biomass",
"Oil": "oil",
"Refuse": "biomass",
"LandfillGas": "biomass",
"Solar": "solar",
}
def get_json_data(
target_datetime: datetime | None, params, session: Session | None = None
) -> dict:
"""Fetches json data for requested params and target_datetime using a post request."""
epoch_time = str(int(time.time()))
target_datetime = target_datetime or datetime.now(tz=timezone.utc)
target_ne = target_datetime.astimezone(tz=ZoneInfo("America/New_York"))
target_ne_day = target_ne.strftime("%m/%d/%Y")
postdata = {
"_nstmp_formDate": epoch_time,
"_nstmp_startDate": target_ne_day,
"_nstmp_endDate": target_ne_day,
"_nstmp_twodays": "false",
"_nstmp_showtwodays": "false",
}
postdata.update(params)
s = session or Session()
req = s.post(url, data=postdata)
json_data = req.json()
raw_data = json_data[0]["data"]
return raw_data
def production_data_processer(
zone_key: ZoneKey, raw_data: dict | list, logger: Logger
) -> ProductionBreakdownList:
"""
Takes raw json data and removes unnecessary keys.
Separates datetime key and converts to a datetime object.
"""
other_keys = {"BeginDateMs", "Renewables", "BeginDate", "Other"}
known_keys = generation_mapping.keys() | other_keys
unmapped = set()
production_breakdowns = ProductionBreakdownList(logger)
counter = 0
for datapoint in raw_data:
current_keys = datapoint.keys() | set()
unknown_keys = current_keys - known_keys
unmapped = unmapped | unknown_keys
keys_to_remove = {"BeginDateMs", "Renewables"} | unknown_keys
for k in keys_to_remove:
datapoint.pop(k, None)
time_string = datapoint.pop("BeginDate", None)
if time_string:
dt = datetime.fromisoformat(time_string)
else:
counter += 1
logger.warning(
f"Skipping {zone_key} datapoint missing timestamp.",
extra={"zone_key": zone_key},
)
continue
# neiso storage flow signs are opposite to EM
battery_storage = -1 * datapoint.pop("Other", 0.0)
storage_mix = StorageMix(battery=battery_storage, hydro=None)
production_mix = ProductionMix()
for k, v in datapoint.items():
production_mix.add_value(
mode=generation_mapping[k], value=v, correct_negative_with_zero=v > -5
)
production_breakdowns.append(
zoneKey=zone_key,
datetime=dt,
source=SOURCE,
production=production_mix,
storage=storage_mix,
)
for key in unmapped:
logger.warning(
f"Key '{key}' in {zone_key} is not mapped to type.",
extra={"zone_key": zone_key},
)
if counter > 0:
logger.warning(
f"Skipped {counter} {zone_key} datapoints that were missing timestamps.",
extra={"zone_key": zone_key},
)
return production_breakdowns
@refetch_frequency(timedelta(days=1))
def fetch_production(
zone_key: ZoneKey = US_NEISO_KEY,
session: Session | None = None,
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
) -> list[dict[str, Any]]:
"""Requests the last known production mix (in MW) of a given country."""
postdata = {
"_nstmp_chartTitle": "Fuel+Mix+Graph",
"_nstmp_requestType": "genfuelmix",
"_nstmp_fuelType": "all",
"_nstmp_height": "250",
}
production_json = get_json_data(target_datetime, postdata, session)
production_breakdowns = production_data_processer(zone_key, production_json, logger)
return production_breakdowns.to_list()
def fetch_exchange(
zone_key1: ZoneKey,
zone_key2: ZoneKey,
session: Session | None = None,
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
) -> list[dict[str, Any]]:
"""Requests the last known power exchange (in MW) between two zones."""
sorted_zone_keys = ZoneKey("->".join(sorted([zone_key1, zone_key2])))
# For directions, note that ISO-NE always reports its import as negative
if sorted_zone_keys == "CA-NB->US-NE-ISNE":
# CA-NB->US-NEISO means import to NEISO should be positive
multiplier = -1
postdata = {"_nstmp_zone0": "4010"} # ".I.SALBRYNB345 1"
elif sorted_zone_keys == "CA-QC->US-NE-ISNE":
# CA-QC->US-NEISO means import to NEISO should be positive
multiplier = -1
postdata = {
"_nstmp_zone0": "4012", # ".I.HQ_P1_P2345 5"
"_nstmp_zone1": "4013", # ".I.HQHIGATE120 2"
}
elif sorted_zone_keys == "US-NE-ISNE->US-NY-NYIS":
# US-NEISO->US-NY means import to NEISO should be negative
multiplier = 1
postdata = {
"_nstmp_zone0": "4014", # ".I.SHOREHAM138 99"
"_nstmp_zone1": "4017", # ".I.NRTHPORT138 5"
"_nstmp_zone2": "4011", # ".I.ROSETON 345 1"
}
else:
raise Exception(f"Exchange pair not supported: {sorted_zone_keys}")
postdata["_nstmp_requestType"] = "externalflow"
exchanges = ExchangeList(logger)
exchange_data = get_json_data(target_datetime, postdata, session)
for _, exchange_values in exchange_data.items():
for datapoint in exchange_values:
time_string = datapoint.pop("BeginDate", None)
if time_string:
dt = datetime.fromisoformat(time_string)
else:
logger.warning(
f"Skipping {sorted_zone_keys} datapoint missing timestamp.",
extra={"zone_key": sorted_zone_keys},
)
continue
exchanges.append(
zoneKey=sorted_zone_keys,
datetime=dt,
netFlow=datapoint["Actual"] * multiplier,
source=SOURCE,
)
# This will merge exchanges with the same datetime
exchanges = ExchangeList.merge_exchanges([exchanges], logger)
return exchanges.to_list()
if __name__ == "__main__":
"""Main method, never used by the Electricity Map backend, but handy for testing."""
from pprint import pprint
print("fetch_production() ->")
pprint(fetch_production())
print(
'fetch_production(target_datetime=datetime.fromisoformat("2017-12-31T12:00:00+00:00")) ->'
)
pprint(
fetch_production(
target_datetime=datetime.fromisoformat("2017-12-31T12:00:00+00:00")
)
)
print(
'fetch_production(target_datetime=datetime.fromisoformat("2007-03-13T12:00:00+00:00")) ->'
)
pprint(
fetch_production(
target_datetime=datetime.fromisoformat("2007-03-13T12:00:00+00:00")
)
)
print(f'fetch_exchange("{US_NEISO_KEY}", "CA-QC") ->')
pprint(fetch_exchange(US_NEISO_KEY, ZoneKey("CA-QC")))
print(
f'fetch_exchange("{US_NEISO_KEY}", "CA-QC", target_datetime=datetime.fromisoformat("2017-12-31T12:00:00+00:00")) ->'
)
pprint(
fetch_exchange(
US_NEISO_KEY,
ZoneKey("CA-QC"),
target_datetime=datetime.fromisoformat("2017-12-31T12:00:00+00:00"),
)
)
print(
f'fetch_exchange("{US_NEISO_KEY}", "CA-QC", target_datetime=datetime.fromisoformat("2007-03-13T12:00:00+00:00")) ->'
)
pprint(
fetch_exchange(
US_NEISO_KEY,
ZoneKey("CA-QC"),
target_datetime=datetime.fromisoformat("2007-03-13T12:00:00+00:00"),
)
)