-
Notifications
You must be signed in to change notification settings - Fork 916
/
OPENNEM.py
409 lines (354 loc) · 12.9 KB
/
OPENNEM.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
from collections.abc import Mapping
from datetime import datetime, timedelta
from logging import Logger, getLogger
import pandas as pd
import requests
from requests import Session
from parsers.lib.config import refetch_frequency
REFETCH_FREQUENCY = timedelta(days=21)
ZONE_KEY_TO_REGION = {
"AU-NSW": "NSW1",
"AU-QLD": "QLD1",
"AU-SA": "SA1",
"AU-TAS": "TAS1",
"AU-VIC": "VIC1",
"AU-WA": "WEM",
}
ZONE_KEY_TO_NETWORK = {
"AU-NSW": "NEM",
"AU-QLD": "NEM",
"AU-SA": "NEM",
"AU-TAS": "NEM",
"AU-VIC": "NEM",
"AU-WA": "WEM",
}
EXCHANGE_MAPPING_DICTIONARY = {
"AU-NSW->AU-QLD": {
"region_id": "NSW1->QLD1",
"direction": 1,
},
"AU-NSW->AU-VIC": {
"region_id": "NSW1->VIC1",
"direction": 1,
},
"AU-SA->AU-VIC": {
"region_id": "SA1->VIC1",
"direction": 1,
},
"AU-TAS->AU-VIC": {
"region_id": "TAS1->VIC1",
"direction": 1,
},
}
OPENNEM_PRODUCTION_CATEGORIES = {
"coal": ["COAL_BLACK", "COAL_BROWN"],
"gas": ["GAS_CCGT", "GAS_OCGT", "GAS_RECIP", "GAS_STEAM"],
"oil": ["DISTILLATE"],
"hydro": ["HYDRO"],
"wind": ["WIND"],
"biomass": ["BIOENERGY_BIOGAS", "BIOENERGY_BIOMASS"],
"solar": ["SOLAR_UTILITY", "SOLAR_ROOFTOP"],
}
OPENNEM_STORAGE_CATEGORIES = {
# Storage
"battery": ["BATTERY_DISCHARGING", "BATTERY_CHARGING"],
"hydro": ["PUMPS"],
}
SOURCE = "opennem.org.au"
def dataset_to_df(dataset):
series = dataset["history"]
interval = series["interval"]
dt_start = datetime.fromisoformat(series["start"])
dt_end = datetime.fromisoformat(series["last"])
data_type = dataset["data_type"]
_id = dataset.get("id")
# When `power` is given, the multiple power sources will be given
# we therefore set `name` to the power source
name = data_type.upper() if data_type != "power" else _id.split(".")[-2].upper()
# Turn into minutes
if interval[-1] == "m":
interval += "in"
index = pd.date_range(start=dt_start, end=dt_end, freq=interval)
assert len(index) == len(series["data"])
df = pd.DataFrame(index=index, data=series["data"], columns=[name])
return df
def process_solar_rooftop(df: pd.DataFrame) -> pd.DataFrame:
if "SOLAR_ROOFTOP" in df:
# at present, solar rooftop data comes in each 30 mins.
# Resample data to not require interpolation
return df.resample("30T").mean()
return df
def get_capacities(filtered_datasets: list[Mapping], region: str) -> pd.Series:
# Parse capacity data
capacities = {
obj["id"].split(".")[-2].upper(): obj.get("x_capacity_at_present")
for obj in filtered_datasets
if obj["region"] == region
}
return pd.Series(capacities)
def sum_vector(pd_series, keys, ignore_nans=False):
# Only consider keys that are in the pd_series
filtered_keys = pd_series.index.intersection(keys)
# Require all present keys to be non-null
pd_series_filtered = pd_series.loc[filtered_keys]
nan_filter = pd_series_filtered.notnull().all() | ignore_nans
if filtered_keys.size and nan_filter:
return pd_series_filtered.fillna(0).sum()
else:
return None
def filter_production_objs(
objs: list[dict], logger: Logger = getLogger(__name__)
) -> list[dict]:
def filter_solar_production(obj: dict) -> bool:
if (
"solar" in obj.get("production", {})
and obj["production"]["solar"] is not None
):
return True
return False
all_filters = [filter_solar_production]
filtered_objs = []
for obj in objs:
_valid = True
for f in all_filters:
_valid &= f(obj)
if _valid:
filtered_objs.append(obj)
else:
logger.warning(
f"Entry for {obj['datetime']} is dropped because it does not pass the production filter."
)
return filtered_objs
def generate_url(
zone_key: str, is_flow, target_datetime: datetime | None, logger: Logger
) -> str:
if target_datetime:
network = ZONE_KEY_TO_NETWORK[zone_key]
# We will fetch since the beginning of the current month
month = target_datetime.strftime("%Y-%m-%d")
if is_flow:
url = (
f"http://api.opennem.org.au/stats/flow/network/{network}?month={month}"
)
else:
region = ZONE_KEY_TO_REGION.get(zone_key)
url = f"http://api.opennem.org.au/stats/power/network/fueltech/{network}/{region}?month={month}"
else:
# Contains flows and production combined
url = "https://data.opennem.org.au/v3/clients/em/latest.json"
return url
def fetch_main_price_df(
zone_key: str | None = None,
sorted_zone_keys: str | None = None,
session: Session | None = None,
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
) -> pd.DataFrame:
return _fetch_main_df(
"price",
zone_key=zone_key,
sorted_zone_keys=sorted_zone_keys,
session=session,
target_datetime=target_datetime,
logger=logger,
)[0]
def fetch_main_power_df(
zone_key: str | None = None,
sorted_zone_keys: list[str] | None = None,
session: Session | None = None,
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
) -> tuple[pd.DataFrame, list]:
df, filtered_datasets = _fetch_main_df(
"power",
zone_key=zone_key,
sorted_zone_keys=sorted_zone_keys,
session=session,
target_datetime=target_datetime,
logger=logger,
)
# Solar rooftop is a special case
df = process_solar_rooftop(df)
return df, filtered_datasets
def _fetch_main_df(
data_type,
zone_key: str,
sorted_zone_keys: str,
session: Session,
target_datetime: datetime,
logger: Logger,
) -> tuple[pd.DataFrame, list]:
region = ZONE_KEY_TO_REGION.get(zone_key)
url = generate_url(
zone_key=zone_key or sorted_zone_keys[0],
is_flow=sorted_zone_keys is not None,
target_datetime=target_datetime,
logger=logger,
)
# Fetches the last week of data
logger.info(f"Requesting {url}..")
r = (session or requests).get(url)
r.raise_for_status()
logger.debug("Parsing JSON..")
datasets = r.json()["data"]
logger.debug("Filtering datasets..")
def filter_dataset(ds: dict) -> bool:
filter_data_type = ds["type"] == data_type
filter_region = False
if zone_key:
filter_region |= ds.get("region") == region
if sorted_zone_keys:
filter_region |= (
ds.get("id").split(".")[-2]
== EXCHANGE_MAPPING_DICTIONARY["->".join(sorted_zone_keys)]["region_id"]
)
return filter_data_type and filter_region
filtered_datasets = [ds for ds in datasets if filter_dataset(ds)]
logger.debug("Concatenating datasets..")
df = pd.concat([dataset_to_df(ds) for ds in filtered_datasets], axis=1)
# Sometimes we get twice the columns. In that case, only return the first one
is_duplicated_column = df.columns.duplicated(keep="last")
if is_duplicated_column.sum():
logger.warning(
f"Dropping columns {df.columns[is_duplicated_column]} that appear more than once"
)
df = df.loc[:, is_duplicated_column]
return df, filtered_datasets
@refetch_frequency(REFETCH_FREQUENCY)
def fetch_production(
zone_key: str | None = None,
session: Session | None = None,
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
):
df, filtered_datasets = fetch_main_power_df(
zone_key=zone_key,
session=session,
target_datetime=target_datetime,
logger=logger,
)
region = ZONE_KEY_TO_REGION.get(zone_key)
capacities = get_capacities(filtered_datasets, region) if region else pd.Series()
# Drop interconnectors
df = df.drop([x for x in df.columns if "->" in x], axis=1)
# Make sure charging is counted positively
# and discharging negetively
if "BATTERY_DISCHARGING" in df.columns:
df["BATTERY_DISCHARGING"] = df["BATTERY_DISCHARGING"] * -1
logger.debug("Preparing final objects..")
objs = [
{
"datetime": dt.to_pydatetime(),
"production": { # Unit is MW
"coal": sum_vector(row, OPENNEM_PRODUCTION_CATEGORIES["coal"]),
"gas": sum_vector(row, OPENNEM_PRODUCTION_CATEGORIES["gas"]),
"oil": sum_vector(row, OPENNEM_PRODUCTION_CATEGORIES["oil"]),
"hydro": sum_vector(row, OPENNEM_PRODUCTION_CATEGORIES["hydro"]),
"wind": sum_vector(row, OPENNEM_PRODUCTION_CATEGORIES["wind"]),
"biomass": sum_vector(row, OPENNEM_PRODUCTION_CATEGORIES["biomass"]),
# We here assume all rooftop solar is fed to the grid
# This assumption should be checked and we should here only report
# grid-level generation
"solar": sum_vector(row, OPENNEM_PRODUCTION_CATEGORIES["solar"]),
},
"storage": {
# opennem reports charging as negative, we here should report as positive
# Note: we made the sign switch before, so we can just sum them up
"battery": sum_vector(row, OPENNEM_STORAGE_CATEGORIES["battery"]),
# opennem reports pumping as positive, we here should report as positive
"hydro": sum_vector(row, OPENNEM_STORAGE_CATEGORIES["hydro"]),
},
"capacity": {
"coal": sum_vector(capacities, OPENNEM_PRODUCTION_CATEGORIES["coal"]),
"gas": sum_vector(capacities, OPENNEM_PRODUCTION_CATEGORIES["gas"]),
"oil": sum_vector(capacities, OPENNEM_PRODUCTION_CATEGORIES["oil"]),
"hydro": sum_vector(capacities, OPENNEM_PRODUCTION_CATEGORIES["hydro"]),
"wind": sum_vector(capacities, OPENNEM_PRODUCTION_CATEGORIES["wind"]),
"biomass": sum_vector(
capacities, OPENNEM_PRODUCTION_CATEGORIES["biomass"]
),
"solar": sum_vector(capacities, OPENNEM_PRODUCTION_CATEGORIES["solar"]),
"hydro storage": capacities.get(OPENNEM_STORAGE_CATEGORIES["hydro"][0]),
"battery storage": capacities.get(
OPENNEM_STORAGE_CATEGORIES["battery"][0]
),
},
"source": SOURCE,
"zoneKey": zone_key,
}
for dt, row in df.iterrows()
]
objs = filter_production_objs(objs)
# Validation
logger.debug("Validating..")
for obj in objs:
for k, v in obj["production"].items():
if v is None:
continue
if v < 0 and v > -50:
# Set small negative values to 0
logger.warning(
f"Setting small value of {k} ({v}) to 0.", extra={"key": zone_key}
)
obj["production"][k] = 0
return objs
@refetch_frequency(REFETCH_FREQUENCY)
def fetch_price(
zone_key: str,
session: Session | None = None,
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
) -> list:
df = fetch_main_price_df(
zone_key=zone_key,
session=session,
target_datetime=target_datetime,
logger=logger,
)
df = df.loc[~df["PRICE"].isna()] # Only keep prices that are defined
return [
{
"datetime": dt.to_pydatetime(),
"price": sum_vector(row, ["PRICE"]), # currency / MWh
"currency": "AUD",
"source": SOURCE,
"zoneKey": zone_key,
}
for dt, row in df.iterrows()
]
@refetch_frequency(REFETCH_FREQUENCY)
def fetch_exchange(
zone_key1: str,
zone_key2: str,
session: Session | None = None,
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
) -> list:
sorted_zone_keys = sorted([zone_key1, zone_key2])
key = "->".join(sorted_zone_keys)
df, _ = fetch_main_power_df(
sorted_zone_keys=sorted_zone_keys,
session=session,
target_datetime=target_datetime,
logger=logger,
)
direction = EXCHANGE_MAPPING_DICTIONARY[key]["direction"]
# Take the first column (there's only one)
series = df.iloc[:, 0]
return [
{
"datetime": dt.to_pydatetime(),
"netFlow": value * direction,
"source": SOURCE,
"sortedZoneKeys": key,
}
for dt, value in series.iteritems()
]
if __name__ == "__main__":
"""Main method, never used by the electricityMap backend, but handy for testing."""
print(fetch_price("AU-SA"))
print(fetch_production("AU-WA"))
print(fetch_production("AU-NSW"))
target_datetime = datetime.fromisoformat("2020-01-01T00:00:00+00:00")
print(fetch_production("AU-SA", target_datetime=target_datetime))
print(fetch_exchange("AU-SA", "AU-VIC"))