Skip to content

Commit

Permalink
Merge branch 'features/#844-biogas_gen_eGon100RE' into features/#779-…
Browse files Browse the repository at this point in the history
…eGon100RE-gas-stores
  • Loading branch information
AmeliaNadal committed Sep 13, 2022
2 parents 5babb72 + c4f59d2 commit 760bf94
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 150 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ Changed
`#787 <https://github.com/openego/eGon-data/issues/787>`_
* Update pypsa-eur-sec fork and store national demand time series
`#402 <https://github.com/openego/eGon-data/issues/402>`_
* Add biogas generators ton eGon100RE
`#402 <https://github.com/openego/eGon-data/issues/844>`_
* Move and merge the two assign_gas_bus_id functions to a central place
`#797 <https://github.com/openego/eGon-data/issues/797>`_
* Add coordinates to non AC buses abroad in eGon100RE
Expand Down
6 changes: 6 additions & 0 deletions src/egon/data/datasets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,9 @@ gas_prod:
stores:
schema: 'grid'
table: 'egon_etrago_generator'
scenario_parameters:
schema: 'scenario'
table: 'egon_scenario_parameters'

weather_BusID:
sources:
Expand Down Expand Up @@ -1032,6 +1035,9 @@ gas_neighbours:
links:
schema: 'grid'
table: 'egon_etrago_link'
stores:
schema: 'grid'
table: 'egon_etrago_store'
targets:
generators:
schema: 'grid'
Expand Down
121 changes: 103 additions & 18 deletions src/egon/data/datasets/ch4_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from urllib.request import urlretrieve
import ast
import json

import geopandas as gpd
import numpy as np
Expand All @@ -13,20 +14,21 @@
from egon.data import config, db
from egon.data.config import settings
from egon.data.datasets import Dataset
from egon.data.datasets.pypsaeursec import read_network
from egon.data.datasets.scenario_parameters import get_sector_parameters


class CH4Production(Dataset):
def __init__(self, dependencies):
super().__init__(
name="CH4Production",
version="0.0.7",
version="0.0.8",
dependencies=dependencies,
tasks=(import_gas_generators),
tasks=(insert_ch4_generators),
)


def load_NG_generators(scn_name):
def load_NG_generators(scn_name="eGon2035"):
"""Define the natural CH4 production units in Germany
Parameters
Expand Down Expand Up @@ -126,13 +128,36 @@ def load_NG_generators(scn_name):
return NG_generators_list


def download_biogas_data():
"""Download the biogas production units data in Germany
Parameters
----------
None
Returns
-------
None
"""
basename = "Biogaspartner_Einspeiseatlas_Deutschland_2021.xlsx"
url = (
"https://www.biogaspartner.de/fileadmin/Biogaspartner/Dokumente/Einspeiseatlas/"
+ basename
)
target_file = Path(".") / "datasets" / "gas_data" / basename

urlretrieve(url, target_file)


def load_biogas_generators(scn_name):
"""Define the biogas production units in Germany
Parameters
----------
scn_name : str
Name of the scenario.
Returns
-------
CH4_generators_list :
Expand All @@ -142,16 +167,9 @@ def load_biogas_generators(scn_name):
# read carrier information from scnario parameter data
scn_params = get_sector_parameters("gas", scn_name)

# Download file
basename = "Biogaspartner_Einspeiseatlas_Deutschland_2021.xlsx"
url = (
"https://www.biogaspartner.de/fileadmin/Biogaspartner/Dokumente/Einspeiseatlas/"
+ basename
)
target_file = Path(".") / "datasets" / "gas_data" / basename

urlretrieve(url, target_file)

# Read-in data from csv-file
biogas_generators_list = pd.read_excel(
target_file,
Expand Down Expand Up @@ -197,7 +215,7 @@ def load_biogas_generators(scn_name):
)

sql = """SELECT *
FROM grid.egon_biogas_generator, boundaries.vg250_sta_union as vg
FROM grid.egon_biogas_generator, boundaries.vg250_sta_union as vg
WHERE ST_Transform(vg.geometry,4326) && egon_biogas_generator.geom
AND ST_Contains(ST_Transform(vg.geometry,4326), egon_biogas_generator.geom)"""

Expand Down Expand Up @@ -232,14 +250,16 @@ def load_biogas_generators(scn_name):
return biogas_generators_list


def import_gas_generators(scn_name="eGon2035"):
def import_gas_generators(scn_name):
"""Insert list of gas production units in database
Parameters
----------
scn_name : str
Name of the scenario.
"""
carrier = "CH4"

# Connect to local database
engine = db.engine()

Expand All @@ -251,25 +271,30 @@ def import_gas_generators(scn_name="eGon2035"):
db.execute_sql(
f"""
DELETE FROM {target['stores']['schema']}.{target['stores']['table']}
WHERE "carrier" = 'CH4' AND
WHERE "carrier" = '{carrier}' AND
scn_name = '{scn_name}' AND bus not IN (
SELECT bus_id FROM {source['buses']['schema']}.{source['buses']['table']}
WHERE scn_name = '{scn_name}' AND country != 'DE'
);
"""
)

CH4_generators_list = pd.concat(
[load_NG_generators(scn_name), load_biogas_generators(scn_name)]
)
if scn_name == "eGon2035":
CH4_generators_list = pd.concat(
[load_NG_generators(scn_name), load_biogas_generators(scn_name)]
)

elif scn_name == "eGon100RE":
CH4_generators_list = load_biogas_generators(scn_name)
overwrite_max_gas_generation_overtheyear(scn_name)

# Add missing columns
c = {"scn_name": scn_name, "carrier": "CH4"}
c = {"scn_name": scn_name, "carrier": carrier}
CH4_generators_list = CH4_generators_list.assign(**c)

# Match to associated CH4 bus
CH4_generators_list = db.assign_gas_bus_id(
CH4_generators_list, scn_name, "CH4"
CH4_generators_list, scn_name, carrier
)

# Remove useless columns
Expand Down Expand Up @@ -297,3 +322,63 @@ def import_gas_generators(scn_name="eGon2035"):
index=False,
if_exists="append",
)


def overwrite_max_gas_generation_overtheyear(scn_name):
"""Overright max_gas_generation_overtheyear in scenario parameter table
Overright max_gas_generation_overtheyear in scenario parameter
table if the value of this parameter has changed in the p-e-s run.
Parameters
----------
scn_name : str
Name of the scenario
"""
execute_pypsa_eur_sec = True # False

# Select source and target from dataset configuration
target = config.datasets()["gas_prod"]["target"]

if execute_pypsa_eur_sec:
n = read_network()
max_value = n.stores[n.stores["carrier"] == "biogas"].loc[
"DE0 0 biogas", "e_initial"
]

parameters = db.select_dataframe(
f"""
SELECT *
FROM {target['scenario_parameters']['schema']}.{target['scenario_parameters']['table']}
WHERE name = '{scn_name}'
"""
)

gas_param = parameters.loc[0, "gas_parameters"]
gas_param["max_gas_generation_overtheyear"] = {"biogas": max_value}
gas_param = json.dumps(gas_param)

# Update data in db
db.execute_sql(
f"""
UPDATE {target['scenario_parameters']['schema']}.{target['scenario_parameters']['table']}
SET gas_parameters = '{gas_param}'
WHERE name = '{scn_name}';
"""
)


def insert_ch4_generators():
"""Insert gas production units in database for both scenarios
Parameters
----------
None
Returns
-------
None
"""
import_gas_generators("eGon2035")
import_gas_generators("eGon100RE")
6 changes: 4 additions & 2 deletions src/egon/data/datasets/ch4_storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,17 @@ def import_ch4_grid_capacity(scn_name):

def insert_ch4_stores(scn_name):
"""Insert gas stores for specific scenario
Parameters
----------
scn_name : str
Name of the scenario.
Returns
----
None"""
-------
None
"""
# Connect to local database
engine = db.engine()

Expand Down
2 changes: 1 addition & 1 deletion src/egon/data/datasets/gas_neighbours/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GasNeighbours(Dataset):
def __init__(self, dependencies):
super().__init__(
name="GasNeighbours",
version="0.0.1",
version="0.0.2",
dependencies=dependencies,
tasks=(
{tyndp_gas_generation, tyndp_gas_demand, grid},
Expand Down
67 changes: 63 additions & 4 deletions src/egon/data/datasets/gas_neighbours/eGon100RE.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Module containing code dealing with crossbording gas pipelines for eGon100RE
"""Module containing code dealing with gas components abroad for eGon100RE
In this module the crossbordering pipelines for H2 and CH4, exclusively
between Germany and its neighbouring countries, in eGon100RE are
defined and inserted in the database.
In this module the missing gas components abroad for eGon100RE are
defined and inserted in the database:
* missing crossbording pipelines: the missing crossbordering
pipelines for H2 and CH4,are exclusively between Germany and
its neighbouring countries
* biogas generators
Dependecies (pipeline)
======================
Expand All @@ -12,6 +15,8 @@
Resulting tables
================
* grid.egon_etrago_link is completed
* grid.egon_etrago_store is completed
* grid.egon_etrago_generator is modified
"""

Expand All @@ -20,6 +25,7 @@
from egon.data import config, db
from egon.data.datasets.gas_neighbours.gas_abroad import (
insert_gas_grid_capacities,
insert_generators,
)
from egon.data.datasets.pypsaeursec import read_network

Expand All @@ -38,6 +44,59 @@


def insert_gas_neigbours_eGon100RE():
"""Insert gas components abroad for eGon100RE
Insert the missing gas crossbordering grid capacities and the
biogas generators for eGon100RE
"""
insert_gas_neigbours_eGon100RE_pipes()
insert_generators(insert_biogas_generators_abroad(), scn_name="eGon100RE")


def insert_biogas_generators_abroad():
"""Insert biogas generators abroad for eGon100RE
This function defines the biogas generators in the neighbouring
countries for the scenario eGon100RE. The capacities arrise from
the pypsa-eur-sec run where the biogas available is modelled as
stores. Therefore, the corresponding stores are deleted and the
capacities inserted as biogas generation potentials.
Returns
-------
gen : pandas.DataFrame
Gas production capacities per foreign node
"""
sources = config.datasets()["gas_neighbours"]["sources"]
scn_name = "eGon100RE"
carrier = "biogas"

gen = db.select_dataframe(
f"""
SELECT scn_name, bus, e_initial, marginal_cost
FROM {sources['stores']['schema']}.{sources['stores']['table']}
WHERE scn_name = '{scn_name}'
AND carrier = '{carrier}'
"""
)
gen = gen.rename(columns={"e_initial": "p_nom"})
gen["e_nom_max"] = gen["p_nom"]

db.execute_sql(
f"""
DELETE FROM
{sources['stores']['schema']}.{sources['stores']['table']}
WHERE scn_name = '{scn_name}'
AND carrier = '{carrier}';
"""
)

return gen


def insert_gas_neigbours_eGon100RE_pipes():
"""Insert missing gas crossbordering grid capacities for eGon100RE
This function insert the crossbordering pipelines for H2 and CH4,
Expand Down
Loading

0 comments on commit 760bf94

Please sign in to comment.