-
Notifications
You must be signed in to change notification settings - Fork 903
/
ONS.py
168 lines (130 loc) · 5.08 KB
/
ONS.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
from collections import defaultdict
from datetime import datetime
from logging import Logger, getLogger
from typing import Any
from requests import Session
from electricitymap.contrib.lib.models.event_lists import (
ExchangeList,
ProductionBreakdownList,
)
from electricitymap.contrib.lib.models.events import ProductionMix
from electricitymap.contrib.lib.types import ZoneKey
URL = "http://tr.ons.org.br/Content/GetBalancoEnergetico/null"
SOURCE = "ons.org.br"
GENERATION_MAPPING = {
"nuclear": "nuclear",
"eolica": "wind",
"termica": "unknown",
"solar": "solar",
"hidraulica": "hydro",
"itaipu50HzBrasil": "hydro", # BR_CS contains the Itaipu Dam.
# We merge the hydro keys into one.
"itaipu60Hz": "hydro",
}
# Those modes report self consumption, therefore they can be negative.
CORRECTED_NEGATIVE_PRODUCTION = {"solar"}
REGIONS = {
"BR-NE": "nordeste",
"BR-N": "norte",
"BR-CS": "sudesteECentroOeste",
"BR-S": "sul",
}
EXCHANGES = {
"BR-CS->BR-S": {"name": "sul_sudeste", "flow": -1, "level": "intercambio"},
"BR-CS->BR-NE": {"name": "sudeste_nordeste", "flow": 1, "level": "intercambio"},
"BR-CS->BR-N": {"name": "sudeste_norteFic", "flow": 1, "level": "intercambio"},
"BR-N->BR-NE": {"name": "norteFic_nordeste", "flow": 1, "level": "intercambio"},
"BR-S->UY": {"name": "uruguai", "flow": 1, "level": "internacional"},
"AR->BR-S": {"name": "argentina", "flow": -1, "level": "internacional"},
"BR-S->PY": {"name": "paraguai", "flow": -1, "level": "internacional"},
}
def get_data(session: Session | None):
"""Requests generation data in json format."""
s = session or Session()
json_data = s.get(URL).json()
return json_data
def production_processor(
json_data: dict, zone_key: str
) -> tuple[datetime, ProductionMix]:
"""Extracts data timestamp and sums regional data into totals by key."""
dt = datetime.fromisoformat(json_data["Data"])
totals = defaultdict(lambda: 0.0)
region = REGIONS[zone_key]
breakdown = json_data[region]["geracao"]
for generation, val in breakdown.items():
totals[generation] += val
del totals["total"]
production = ProductionMix()
for mode, value in totals.items():
mode_name = GENERATION_MAPPING.get(mode, "unknown")
production.add_value(
mode_name, value, mode_name in CORRECTED_NEGATIVE_PRODUCTION
)
return dt, production
def fetch_production(
zone_key: ZoneKey,
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."""
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates")
data = get_data(session)
date, production = production_processor(data, zone_key)
productions = ProductionBreakdownList(logger)
productions.append(
zoneKey=zone_key,
datetime=date,
source=SOURCE,
production=production,
)
return productions.to_list()
def get_exchange_flow(sorted_zone_keys: ZoneKey, raw_data: dict) -> float:
"""Returns the flow of the exchange between two regions."""
name, flow, level = EXCHANGES[sorted_zone_keys].values()
return raw_data[level][name] * flow
def fetch_exchange(
zone_key1: str,
zone_key2: str,
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 regions."""
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates")
data = get_data(session)
dt = datetime.fromisoformat(data["Data"])
sorted_zone_keys = ZoneKey("->".join(sorted([zone_key1, zone_key2])))
exchanges = ExchangeList(logger)
net_flow = get_exchange_flow(sorted_zone_keys, data)
exchanges.append(
zoneKey=sorted_zone_keys,
datetime=dt,
source=SOURCE,
netFlow=net_flow,
)
return exchanges.to_list()
if __name__ == "__main__":
"""Main method, never used by the Electricity Map backend, but handy for testing."""
print("fetch_production(BR-NE) ->")
print(fetch_production(ZoneKey("BR-NE")))
print("fetch_production(BR-N) ->")
print(fetch_production(ZoneKey("BR-N")))
print("fetch_production(BR-CS) ->")
print(fetch_production(ZoneKey("BR-CS")))
print("fetch_production(BR-S) ->")
print(fetch_production(ZoneKey("BR-S")))
print("fetch_exchange(BR-S, UY) ->")
print(fetch_exchange("BR-S", "UY"))
print("fetch_exchange(BR-S, AR) ->")
print(fetch_exchange("BR-S", "AR"))
print("fetch_region_exchange(BR-CS->BR-S)")
print(fetch_exchange("BR-CS", "BR-S"))
print("fetch_region_exchange(BR-CS->BR-NE)")
print(fetch_exchange("BR-CS", "BR-NE"))
print("fetch_region_exchange(BR-CS->BR-N)")
print(fetch_exchange("BR-CS", "BR-N"))
print("fetch_region_exchange(BR-N->BR-NE)")
print(fetch_exchange("BR-N", "BR-NE"))