Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/#328 demands to etra go table #379

Merged
merged 7 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge load curves and export data to db
  • Loading branch information
IlkaCu committed Aug 10, 2021
commit 4b7c39cd12da557d07656b8bf94bb71c1b75c2ad
4 changes: 2 additions & 2 deletions src/egon/data/datasets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,10 @@ etrago_electricity:
table: 'egon_etrago_electricity_cts'
targets:
etrago_load:
schema: 'demand'
schema: 'grid'
table: 'egon_etrago_load'
etrago_load_curves:
schema: 'demand'
schema: 'grid'
table: 'egon_etrago_load_timeseries'


Expand Down
125 changes: 114 additions & 11 deletions src/egon/data/datasets/electricity_demand_etrago.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@


import egon.data.config
import geopandas as gpd
import numpy as np
import pandas as pd
from egon.data import db
from egon.data.datasets import Dataset
from egon.data.datasets.industry.temporal import (
insert_osm_ind_load,
insert_sites_ind_load,
)
from sqlalchemy import Column, String, Float, Integer, ARRAY
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declarative_base and Base are not needed in the script later on. So lines 11 and 14 could be deleted.



def merge_electricity_demand():
def demands_per_bus(scenario):
"""Sum all electricity demand curves up per bus

Parameters
----------
scenario : str
Scenario name.

Returns
-------
None.
pandas.DataFrame
Aggregated electrical demand timeseries per bus
"""

# Read information from configuration file
sources = egon.data.config.datasets()["etrago_electricity"][
"sources"
]

scenario = 'eGon2035'

# Select data on CTS electricity demands per bus
cts_curves = db.select_dataframe(
Expand Down Expand Up @@ -72,7 +71,111 @@ def merge_electricity_demand():

demand_curves = cts_curves.append([ind_curves_osm, ind_curves_sites])

# Split array to single columns in the dataframe
demand_curves_split = demand_curves

demand_curves_split = pd.DataFrame(demand_curves.p_set.tolist(), index=demand_curves_split.index)

# Group all rows with the same bus
demand_curves_bus = demand_curves_split.groupby(demand_curves_split.index).sum()

# Initzialize and fill resulsting dataframe
curves = pd.DataFrame(columns=["bus", "p_set"])
curves["bus"] = demand_curves_bus.index
curves["p_set"] = demand_curves_bus.values.tolist()

return curves


def export_to_db():
"""Prepare and export eTraGo-ready information of loads per bus and their
time series to the database

Returns
-------
None.

"""

targets = egon.data.config.datasets()["etrago_electricity"][
"targets"
]

for scenario in ["eGon2035", "eGon100RE"]:

demand_curves_bus = demand_curves.groupby(demand_curves.index).p_set.sum()
# Delete existing data from database
db.execute_sql(
f"""
DELETE FROM
{targets['etrago_load']['schema']}.{targets['etrago_load']['table']}
WHERE scn_name = '{scenario}'
"""
)

db.execute_sql(
f"""
DELETE FROM
{targets['etrago_load_curves']['schema']}.{targets['etrago_load_curves']['table']}
WHERE scn_name = '{scenario}'
"""
)

curves = demands_per_bus(scenario)

# Initialize dataframes equivalent to database tables

load = pd.DataFrame(columns=["version", "scn_name", "load_id", "bus", "type", "carrier", "p_set_fixed", "q_set_fixed", "sign", "p_set"])
load_timeseries = pd.DataFrame(columns=["version", "scn_name", "load_id", "temp_id", "p_set", "q_set"])

# Choose next unused load_id
next_load_id= db.next_etrago_id('load')

# Insert values into load df
load.bus = curves.bus
load.version = '0.0.0'
load.scn_name = scenario
load.sign = -1
load.carrier = 'AC'
load.load_id = range(next_load_id, next_load_id+len(load))
load.p_set = curves.p_set

# Insert values into load timeseries df
load_timeseries[['load_id', 'p_set']] = load[['load_id', 'p_set']]
load_timeseries.version = '0.0.0'
load_timeseries.scn_name = scenario
load_timeseries.temp_id = 1

# Delete p_set column from load df
load.drop(columns=['p_set'], inplace=True)

# Set index

load = load.set_index(["version", "scn_name", "load_id"])
load_timeseries = load_timeseries.set_index(["version", "scn_name", "load_id", "temp_id"])

# Insert data into database
load.to_sql(
targets["etrago_load"]["table"],
schema=targets["etrago_load"]["schema"],
con=db.engine(),
if_exists="append",
)

load_timeseries.to_sql(
targets["etrago_load_curves"]["table"],
schema=targets["etrago_load_curves"]["schema"],
con=db.engine(),
if_exists="append",
)


class ElectricalLoadEtrago(Dataset):
def __init__(self, dependencies):
super().__init__(
name="Electrical_load_etrago",
version="0.0.0",
dependencies=dependencies,
tasks=(
export_to_db,
),
)