Skip to content

Commit

Permalink
Merge branch 'feature/#131-download-and-store-flow-foreign-countries'…
Browse files Browse the repository at this point in the history
… into workflow-run-20-10-23
  • Loading branch information
CarlosEpia committed Oct 20, 2023
2 parents 2dd55ef + 3044e43 commit fddc061
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
88 changes: 86 additions & 2 deletions src/egon/data/datasets/electrical_neighbours.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,84 @@ def insert_loads_sq(scn_name="status2019"):
session.commit()


def insert_crossborder_powerflow_sq(
start_time="20190101", end_time="20200101", scn_name="status2019"
):
"""
Parameters
----------
start_time : str, optional
define the starting timestep in format YYYYMMDD.
The default is "20190101".
end_time : str, optional
define the starting timestep in format YYYYMMDD.
The default is "20200101".
scn_name : str, optional
scenario name. The default is "status2019".
Returns
-------
None.
"""
# Connect to database
engine = db.engine()
session = sessionmaker(bind=engine)()

# Delete old entrances for the same scenario
engine.execute(
f"""DELETE FROM grid.egon_etrago_crossborder_flows
WHERE scn_name = '{scn_name}'"""
)

entsoe_token = open(
path.join(path.expanduser("~"), ".entsoe-token"), "r"
).read(36)
client = entsoe.EntsoePandasClient(api_key=entsoe_token)

start = pd.Timestamp(start_time, tz="Europe/Brussels")
end = pd.Timestamp(end_time, tz="Europe/Brussels")

foreign = [
"LU",
"AT",
"FR",
"NL",
"DK",
"PL",
"CH",
"SE",
"CZ",
]

pf = pd.DataFrame()

for country in foreign:
pf[country] = client.query_crossborder_flows(
country_code_from=country,
country_code_to="DE",
start=start,
end=end,
) - client.query_crossborder_flows(
country_code_from="DE",
country_code_to=country,
start=start,
end=end,
)

pf.index = pf.index.tz_convert("Europe/Brussels")

for foreign in pf.columns:
entry = etrago.EgonPfHvCrossborderFlows(
scn_name=scn_name, country=foreign, p=pf[foreign].to_list()
)

session.add(entry)
session.commit()

return pf


tasks = (grid,)

insert_per_scenario = set()
Expand All @@ -1733,7 +1811,13 @@ def insert_loads_sq(scn_name="status2019"):
insert_per_scenario.update([tyndp_generation, tyndp_demand])

if "status2019" in config.settings()["egon-data"]["--scenarios"]:
insert_per_scenario.update([insert_generators_sq, insert_loads_sq])
insert_per_scenario.update(
[
insert_generators_sq,
insert_loads_sq,
insert_crossborder_powerflow_sq,
]
)

tasks = tasks + (insert_per_scenario,)

Expand All @@ -1742,7 +1826,7 @@ class ElectricalNeighbours(Dataset):
def __init__(self, dependencies):
super().__init__(
name="ElectricalNeighbours",
version="0.0.10",
version="0.0.11",
dependencies=dependencies,
tasks=tasks,
)
11 changes: 11 additions & 0 deletions src/egon/data/datasets/etrago_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ class EgonPfHvBusmap(Base):
version = Column(Text, primary_key=True, nullable=False)


class EgonPfHvCrossborderFlows(Base):
__tablename__ = "egon_etrago_crossborder_flows"
__table_args__ = {"schema": "grid"}

scn_name = Column(String, primary_key=True, nullable=False)
country = Column(Text, primary_key=True, nullable=False)
p = Column(ARRAY(Float(precision=53)))


def create_tables():
"""Create tables for eTraGo input data.
Returns
Expand Down Expand Up @@ -495,6 +504,7 @@ def create_tables():
EgonPfHvTransformer.__table__.drop(bind=engine, checkfirst=True)
EgonPfHvTransformerTimeseries.__table__.drop(bind=engine, checkfirst=True)
EgonPfHvBusmap.__table__.drop(bind=engine, checkfirst=True)
EgonPfHvCrossborderFlows.__table__.drop(bind=engine, checkfirst=True)
# Create new tables
EgonPfHvBus.__table__.create(bind=engine, checkfirst=True)
EgonPfHvBusTimeseries.__table__.create(bind=engine, checkfirst=True)
Expand All @@ -517,6 +527,7 @@ def create_tables():
bind=engine, checkfirst=True
)
EgonPfHvBusmap.__table__.create(bind=engine, checkfirst=True)
EgonPfHvCrossborderFlows.__table__.create(bind=engine, checkfirst=True)


def temp_resolution():
Expand Down

0 comments on commit fddc061

Please sign in to comment.