-
Notifications
You must be signed in to change notification settings - Fork 903
/
MN.py
150 lines (119 loc) · 4.59 KB
/
MN.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
#!/usr/bin/env python3
from datetime import datetime
from logging import Logger, getLogger
from typing import Any
from zoneinfo import ZoneInfo
from requests import Response, Session
from electricitymap.contrib.config import ZoneKey
from electricitymap.contrib.lib.models.event_lists import (
ProductionBreakdownList,
TotalConsumptionList,
)
from electricitymap.contrib.lib.models.events import ProductionMix
from parsers.lib.exceptions import ParserException
NDC_GENERATION = "https://disnews.energy.mn/convertt.php"
TZ = ZoneInfo("Asia/Ulaanbaatar") # UTC+8
# Query fields to web API fields
JSON_QUERY_TO_SRC = {
"time": "date",
"consumptionMW": "syssum",
"solarMW": "sumnar",
"windMW": "sums",
"importMW": "energyimport", # positive = import
"temperatureC": "t", # current temperature
}
def parse_json(web_json: dict, logger: Logger, zone_key: ZoneKey) -> dict[str, Any]:
"""
Parse the fetched JSON data to our query format according to JSON_QUERY_TO_SRC.
Example of expected JSON format present at URL:
{"date":"2023-06-27 18:00:00","syssum":"869.37","sumnar":42.34,"sums":119.79,"energyimport":"49.58"}
"""
# Validate first if keys in fetched dict match expected keys
if set(JSON_QUERY_TO_SRC.values()) != set(web_json.keys()):
logger.error(
msg=f"Fetched keys from source {web_json.keys()} do not match expected keys {JSON_QUERY_TO_SRC.values()}.",
extra={"zone_key": zone_key, "parser": "MN.py"},
)
if None in web_json.values():
raise ParserException(
parser="MN.py",
message=f"Fetched values contain null. Fetched data: {web_json}.",
)
# Then we can safely parse them
query_data = {}
for query_key, src_key in JSON_QUERY_TO_SRC.items():
if "time" in query_key:
# convert to datetime
query_data[query_key] = datetime.fromisoformat(web_json[src_key]).replace(
tzinfo=TZ
)
else:
# or convert to float, might also be string
query_data[query_key] = float(web_json[src_key])
return query_data
def query(session: Session, logger: Logger, zone_key: ZoneKey) -> dict[str, Any]:
"""
Query the JSON endpoint and parse it.
"""
target_response: Response = session.get(NDC_GENERATION)
if not target_response.ok:
raise ParserException(
parser="MN.py",
message=f"Data request did not succeed: {target_response.status_code}",
)
# Read as JSON
response_json = target_response.json()
query_result = parse_json(response_json, logger, zone_key)
return query_result
def fetch_production(
zone_key: ZoneKey,
session: Session = Session(),
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
):
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates.")
query_data = query(session, logger, zone_key)
# Calculated 'unknown' production from available data (consumption, import, solar, wind, tpp).
# 'unknown' consists of 92.8% coal, 5.8% oil and 1.4% hydro as per 2020; sources: IEA and IRENA statistics.
query_data["leftoverMW"] = round(
query_data["consumptionMW"]
- query_data["importMW"]
- query_data["solarMW"]
- query_data["windMW"],
13,
)
prod_mix = ProductionMix()
prod_mix.add_value("solar", query_data["solarMW"])
prod_mix.add_value("wind", query_data["windMW"])
prod_mix.add_value("unknown", query_data["leftoverMW"])
prod_breakdown_list = ProductionBreakdownList(logger)
prod_breakdown_list.append(
datetime=query_data["time"],
zoneKey=zone_key,
source="https://ndc.energy.mn/",
production=prod_mix,
)
return prod_breakdown_list.to_list()
def fetch_consumption(
zone_key: ZoneKey,
session: Session = Session(),
target_datetime: datetime | None = None,
logger: Logger = getLogger(__name__),
):
if target_datetime:
raise NotImplementedError("This parser is not yet able to parse past dates.")
query_data = query(session, logger, zone_key)
consumption_list = TotalConsumptionList(logger)
consumption_list.append(
datetime=query_data["time"],
zoneKey=zone_key,
consumption=query_data["consumptionMW"],
source="https://ndc.energy.mn/",
)
return consumption_list.to_list()
if __name__ == "__main__":
print("fetch_production() ->")
print(fetch_production(ZoneKey("MN")))
print("fetch_consumption() ->")
print(fetch_consumption(ZoneKey("MN")))