-
Notifications
You must be signed in to change notification settings - Fork 952
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ES_IB split parser ES-IB-MA: Mallorca (connected with ES, ES-IB-ME and ES-IB-IZ ES-IB-ME: Menorca (connected with ES-IB-MA, power cable currently broken ES-IB-IZ: Ibiza (connected with ES-IB-MA and ES-IB-FO) ES-IB-FO: Formentera (connected with ES-IB-IZ) * Update ES_IB uncommented "import ParserException" hoping checks won't fail afterwards * Enable verification, map waste to biomass, tidy up printing * Added compatibility with the old parser
- Loading branch information
Showing
3 changed files
with
234 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,209 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# The arrow library is used to handle datetimes | ||
import logging | ||
from arrow import get | ||
# The request library is used to fetch content through HTTP | ||
from requests import Session | ||
from ree import BalearicIslands | ||
from ree import (Formentera, Ibiza, | ||
Mallorca, Menorca, | ||
BalearicIslands) | ||
|
||
from .lib.exceptions import ParserException | ||
from .lib.validation import validate | ||
|
||
## Guess we'll need to figure these out later?! Adapted from ES-CN: | ||
|
||
# Minimum valid zone demand. This is used to eliminate some cases | ||
# where generation for one or more modes is obviously missing. | ||
FLOORS = { | ||
'ES-IB': 0, | ||
'ES-IB-FO': 0, | ||
'ES-IB-IZ': 0, | ||
'ES-IB-MA': 0, | ||
'ES-IB-ME': 0, | ||
} | ||
|
||
|
||
def fetch_island_data(zone_key, session): | ||
if zone_key == 'ES-IB-FO': | ||
formentera_data = Formentera(session, verify=False).get_all() | ||
if not formentera_data: | ||
raise ParserException(zone_key, "Formentera doesn't respond") | ||
else: | ||
return formentera_data | ||
elif zone_key == 'ES-IB-IZ': | ||
ibiza_data = Ibiza(session, verify=False).get_all() | ||
if not ibiza_data: | ||
raise ParserException(zone_key, "Party is over, Ibiza doesn't respond") | ||
else: | ||
return ibiza_data | ||
elif zone_key == 'ES-IB-MA': | ||
mallorca_data = Mallorca(session, verify=False).get_all() | ||
if not mallorca_data: | ||
raise ParserException(zone_key, "Mallorca doesn't respond") | ||
else: | ||
return mallorca_data | ||
elif zone_key == 'ES-IB-ME': | ||
menorca_data = Menorca(session, verify=False).get_all() | ||
if not menorca_data: | ||
raise ParserException(zone_key, "Menorca doesn't respond") | ||
else: | ||
return menorca_data | ||
elif zone_key == 'ES-IB': | ||
balearic_islands = BalearicIslands(session, verify=False).get_all() | ||
if not balearic_islands: | ||
raise ParserException(zone_key, "Balearic Islands doesn't respond") | ||
else: | ||
return balearic_islands | ||
else: | ||
raise ParserException(zone_key, 'Can\'t read this country code {0}'.format(zone_key)) | ||
|
||
|
||
def fetch_consumption(zone_key='ES-IB', session=None, target_datetime=None, logger=None): | ||
def fetch_consumption(zone_key, session=None, target_datetime=None, logger=None): | ||
if target_datetime: | ||
raise NotImplementedError('This parser is not yet able to parse past dates') | ||
|
||
ses = session or Session() | ||
|
||
# TODO: Remove verify SSL config when working without it. | ||
responses = BalearicIslands(ses, verify=False).get_all() | ||
if not responses: | ||
raise ParserException("ES-IB", "No response") | ||
else: | ||
data = [] | ||
|
||
for response in responses: | ||
response_data = { | ||
'zoneKey': zone_key, | ||
'datetime': get(response.timestamp).datetime, | ||
'consumption': response.demand, | ||
'source': 'demanda.ree.es' | ||
} | ||
ses = session or Session() | ||
island_data = fetch_island_data(zone_key, ses) | ||
data = [] | ||
for response in island_data: | ||
response_data = { | ||
'zoneKey': zone_key, | ||
'datetime': get(response.timestamp).datetime, | ||
'consumption': response.demand, | ||
'source': 'demanda.ree.es' | ||
} | ||
|
||
data.append(response_data) | ||
data.append(response_data) | ||
|
||
return data | ||
return data | ||
|
||
|
||
def fetch_production(zone_key='ES-IB', session=None, target_datetime=None, logger=None): | ||
def fetch_production(zone_key, session=None, target_datetime=None, logger=None): | ||
if target_datetime: | ||
raise NotImplementedError('This parser is not yet able to parse past dates') | ||
|
||
ses = session or Session() | ||
island_data = fetch_island_data(zone_key, ses) | ||
data = [] | ||
|
||
# TODO: Remove verify SSL config when working without it. | ||
responses = BalearicIslands(ses, verify=False).get_all() | ||
|
||
if not responses: | ||
raise ParserException("ES-IB", "No response") | ||
else: | ||
## biomass could probably be added as response.biomass in ree, including value['resid'] which I assume is for residuos=waste. Atm it seems unincluded. | ||
## I saw generation from "genAux" (generacion auxiliar) today on Formentera, which should probably be added to response.other in ree | ||
## Formentera mostly only has some solar generation during the day, importing from Ibiza all of the time, which probably has to be considered for response.production() > 0: ? | ||
|
||
data = [] | ||
|
||
for response in responses: | ||
for response in island_data: | ||
if response.production() > 0: | ||
response_data = { | ||
'zoneKey': zone_key, | ||
'datetime': get(response.timestamp).datetime, | ||
'production': { | ||
'coal': response.carbon, | ||
'gas': round(response.gas + response.combined, 2), | ||
'solar': response.solar, | ||
'oil': round(response.vapor + response.diesel, 2), | ||
'wind': response.wind, | ||
'hydro': response.hydraulic, | ||
'biomass': 0.0, | ||
'nuclear': 0.0, | ||
'geothermal': 0.0, | ||
'unknown': response.unknown() | ||
}, | ||
'storage': { | ||
'hydro': 0.0, | ||
'battery': 0.0 | ||
}, | ||
'source': 'demanda.ree.es', | ||
} | ||
|
||
data.append(response_data) | ||
|
||
return data | ||
|
||
|
||
def fetch_exchange(zone_key1='ES', zone_key2='ES-IB', session=None, target_datetime=None, logger=None): | ||
'coal': response.carbon, | ||
'gas': round(response.gas + response.combined, 2), | ||
'solar': response.solar, | ||
'oil': round(response.vapor + response.diesel, 2), | ||
'wind': response.wind, | ||
'hydro': response.hydraulic, | ||
'biomass': response.waste, | ||
'nuclear': 0.0, | ||
'geothermal': 0.0, | ||
'unknown': response.other | ||
}, | ||
'storage': { | ||
'hydro': 0.0, | ||
'battery': 0.0 | ||
}, | ||
'source': 'demanda.ree.es', | ||
} | ||
|
||
response_data = validate(response_data, logger, | ||
floor=FLOORS[zone_key]) | ||
|
||
if response_data: | ||
# append if valid | ||
data.append(response_data) | ||
|
||
return data | ||
|
||
|
||
def fetch_exchange(zone_key1, zone_key2, session=None, target_datetime=None, logger=None): | ||
|
||
if target_datetime: | ||
raise NotImplementedError('This parser is not yet able to parse past dates') | ||
|
||
sorted_zone_keys = '->'.join(sorted([zone_key1, zone_key2])) | ||
|
||
ses = session or Session() | ||
|
||
# TODO: Remove verify SSL config when working without it. | ||
responses = BalearicIslands(ses, verify=False).get_all() | ||
if not responses: | ||
raise ParserException("ES-IB", "No response") | ||
if sorted_zone_keys == 'ES->ES-IB': | ||
responses = BalearicIslands(ses, verify=False).get_all() | ||
if not responses: | ||
raise ParserException("ES-IB", "No responses") | ||
elif sorted_zone_keys == 'ES->ES-IB-MA' or sorted_zone_keys == 'ES-IB-MA->ES-IB-ME' or sorted_zone_keys == 'ES-IB-IZ->ES-IB-MA': | ||
responses = Mallorca(ses, verify=False).get_all() | ||
if not responses: | ||
raise ParserException("ES-IB-MA", "No responses") | ||
elif sorted_zone_keys == 'ES-IB-FO->ES-IB-IZ': | ||
responses = Formentera(ses, verify=False).get_all() | ||
if not responses: | ||
raise ParserException("ES-IB-FO", "No responses") | ||
else: | ||
|
||
data = [] | ||
for response in responses: | ||
|
||
sorted_zone_keys = sorted([zone_key1, zone_key2]) | ||
raise NotImplementedError('This exchange pair is not implemented') | ||
|
||
exchanges = [] | ||
for response in responses: | ||
|
||
if sorted_zone_keys == 'ES-IB-MA->ES-IB-ME': | ||
net_flow = response.link['ma_me'] | ||
elif sorted_zone_keys == 'ES-IB-IZ->ES-IB-MA': | ||
net_flow = response.link['ma_ib'] | ||
elif sorted_zone_keys == 'ES-IB-FO->ES-IB-IZ': | ||
net_flow = -1 * response.link['ib_fo'] | ||
else: | ||
net_flow = response.link['pe_ma'] | ||
|
||
response_data = { | ||
'sortedZoneKeys': '->'.join(sorted_zone_keys), | ||
'datetime': get(response.timestamp).datetime, | ||
'netFlow': net_flow if zone_key1 == sorted_zone_keys[0] else -1 * net_flow, | ||
'source': 'demanda.ree.es', | ||
} | ||
exchange = { | ||
'sortedZoneKeys': sorted_zone_keys, | ||
'datetime': get(response.timestamp).datetime, | ||
'netFlow': net_flow, | ||
'source': 'demanda.ree.es', | ||
} | ||
|
||
data.append(response_data) | ||
exchanges.append(exchange) | ||
|
||
return data | ||
return exchanges | ||
|
||
|
||
if __name__ == '__main__': | ||
session = Session | ||
print("fetch_consumption(ES-IB)") | ||
print(fetch_consumption('ES-IB', session)) | ||
|
||
print("fetch_production(ES-IB)") | ||
print(fetch_production('ES-IB', session)) | ||
|
||
print("fetch_exchange(ES, ES-IB)") | ||
print(fetch_exchange('ES', 'ES-IB', session)) | ||
|
||
print("fetch_consumption(ES-IB-FO)") | ||
print(fetch_consumption('ES-IB-FO')) | ||
print("fetch_production(ES-IB-FO)") | ||
print(fetch_production('ES-IB-FO')) | ||
print("fetch_consumption(ES-IB-IZ)") | ||
print(fetch_consumption('ES-IB-IZ')) | ||
print("fetch_production(ES-IB-IZ)") | ||
print(fetch_production('ES-IB-IZ')) | ||
print("fetch_consumption(ES-IB-MA)") | ||
print(fetch_consumption('ES-IB-MA')) | ||
print("fetch_production(ES-IB-MA)") | ||
print(fetch_production('ES-IB-MA')) | ||
print("fetch_consumption(ES-IB-ME)") | ||
print(fetch_consumption('ES-IB-ME')) | ||
print("fetch_production(ES-IB-ME)") | ||
print(fetch_production('ES-IB-ME')) | ||
print("fetch_exchange(ES, ES-IB-MA)") | ||
print(fetch_exchange('ES', 'ES-IB-MA')) | ||
print("fetch_exchange(ES-IB-MA, ES-IB-ME)") | ||
print(fetch_exchange('ES-IB-MA', 'ES-IB-ME')) | ||
print("fetch_exchange(ES-IB-MA, ES-IB-IZ)") | ||
print(fetch_exchange('ES-IB-MA', 'ES-IB-IZ')) | ||
print("fetch_exchange(ES-IB-IZ, ES-IB-FO)") | ||
print(fetch_exchange('ES-IB-IZ', 'ES-IB-FO')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.