Skip to content

Commit

Permalink
Include assign weather_data
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosEpia committed Oct 11, 2021
2 parents 1d65d82 + 7fba1bc commit eb7d939
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,17 @@ Added
* Extend zensus by a combined table with all cells where
there's either building, apartment or population data
`#359 <https://github.com/openego/eGon-data/issues/359>`_
<<<<<<< HEAD
* Add example metadata for OSM, VG250 and Zensus VG250.
Add metadata templates for licences, context and some helper
functions. Extend docs on how to create metadata for tables.
`#139 <https://github.com/openego/eGon-data/issues/139>`_
* Integrate DSM potentials for CTS and industry
`#259 <https://github.com/openego/eGon-data/issues/259>`_
=======
* Assign weather cell id to weather dependant power plants
`#330 <https://github.com/openego/eGon-data/issues/330>`_
>>>>>>> features/#330-assign-weather-cell-id-to-power-plants

.. _PR #159: https://github.com/openego/eGon-data/pull/159

Expand Down
11 changes: 6 additions & 5 deletions src/egon/data/datasets/power_plants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from egon.data.datasets.power_plants.pv_rooftop import pv_rooftop_per_mv_grid
import egon.data.config
import egon.data.datasets.power_plants.pv_ground_mounted as pv_ground_mounted

import egon.data.datasets.power_plants.assign_weather_data as assign_weather_data
import egon.data.datasets.power_plants.wind_farms as wind_onshore

Base = declarative_base()
Expand Down Expand Up @@ -53,11 +55,10 @@ def __init__(self, dependencies):
tasks=(
create_tables,
insert_hydro_biomass,
{
wind_onshore.insert,
pv_ground_mounted.insert,
pv_rooftop_per_mv_grid,
},
wind_onshore.insert,
pv_ground_mounted.insert,
pv_rooftop_per_mv_grid,
assign_weather_data.weather_id,
),
)

Expand Down
101 changes: 101 additions & 0 deletions src/egon/data/datasets/power_plants/assign_weather_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import pandas as pd
import geopandas as gpd
import numpy as np
from egon.data import db
from shapely.geometry import Point
from pathlib import Path
import egon.data.config


def weather_id():
"""
Assign weather data to the weather dependant generators (wind and solar)
Parameters
----------
*No parameters required
"""

# Connect to the data base
con = db.engine()

cfg = egon.data.config.datasets()["power_plants"]

# Import table with power plants
sql = "SELECT * FROM supply.egon_power_plants"
power_plants = gpd.GeoDataFrame.from_postgis(
sql, con, crs="EPSG:4326", geom_col="geom"
)

# select the power_plants that are weather dependant
power_plants = power_plants[
(power_plants["carrier"] == "solar")
| (power_plants["carrier"] == "wind_onshore")
| (power_plants["carrier"] == "wind_offshore")
]
power_plants.set_index("id", inplace=True)

# Import table with weather data for each technology
sql = "SELECT * FROM supply.egon_era5_renewable_feedin"
weather_data = pd.read_sql_query(sql, con)
weather_data.set_index("w_id", inplace=True)

# Import weather cells with Id to match with the weather data
sql = "SELECT * FROM supply.egon_era5_weather_cells"
weather_cells = gpd.GeoDataFrame.from_postgis(
sql, con, crs="EPSG:4326", geom_col="geom"
)

# import Germany borders to speed up the matching process
sql = "SELECT * FROM boundaries.vg250_sta"
boundaries = gpd.GeoDataFrame.from_postgis(
sql, con, crs="EPSG:4326", geom_col="geometry"
)

# Clip weater data cells using the German boundaries
weather_cells = gpd.clip(weather_cells, boundaries)

for weather_id in weather_cells["w_id"]:
df = gpd.clip(
power_plants, weather_cells[weather_cells["w_id"] == weather_id]
)
power_plant_list = df.index.to_list()
power_plants.loc[power_plant_list, "weather_cell_id"] = weather_id

# delete weather dependent power_plants from supply.egon_power_plants
db.execute_sql(
f"""
DELETE FROM {cfg['target']['schema']}.{cfg['target']['table']}
WHERE carrier IN ('wind_onshore', 'solar', 'wind_offshore')
"""
)

# Look for the maximum id in the table egon_power_plants
sql = (
"SELECT MAX(id) FROM "
+ cfg["target"]["schema"]
+ "."
+ cfg["target"]["table"]
)
max_id = pd.read_sql(sql, con)
max_id = max_id["max"].iat[0]
if max_id == None:
ini_id = 1
else:
ini_id = int(max_id + 1)

# write_table in egon-data database:
# Reset index
power_plants.index = pd.RangeIndex(
start=ini_id, stop=ini_id + len(power_plants), name="id"
)

# Insert into database
power_plants.reset_index().to_postgis(
cfg["target"]["table"],
schema=cfg["target"]["schema"],
con=db.engine(),
if_exists="append",
)

return 0
4 changes: 3 additions & 1 deletion src/egon/data/datasets/power_plants/pv_ground_mounted.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ def run_methodology(
pv_per_distr_100RE["installed capacity in kW"] > 0
]


return (
pv_rora,
pv_agri,
Expand Down Expand Up @@ -1145,10 +1146,11 @@ def pv_parks(pv_rora, pv_agri, pv_per_distr, scenario_name):
insert_pv_parks["scenario"] = scenario_name

# change name and crs of geometry column
insert_pv_parks.set_crs(epsg= 3035, allow_override= True, inplace= True)
insert_pv_parks = (
insert_pv_parks.rename({"geometry": "geom"}, axis=1)
.set_geometry("geom")
.set_crs(4326)
.to_crs(4326)
)

# reset index
Expand Down
35 changes: 19 additions & 16 deletions src/egon/data/datasets/renewable_feedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class RenewableFeedin(Dataset):
def __init__(self, dependencies):
super().__init__(
name="RenewableFeedin",
version="0.0.0",
version="0.0.1",
dependencies=dependencies,
tasks={wind, pv, solar_thermal},
tasks=(wind, pv, solar_thermal),
)


def weather_cells_in_germany(geom_column="geom"):
""" Get weather cells which intersect with Germany
"""Get weather cells which intersect with Germany
Returns
-------
Expand All @@ -36,19 +36,18 @@ def weather_cells_in_germany(geom_column="geom"):
cfg = egon.data.config.datasets()["renewable_feedin"]["sources"]

return db.select_geodataframe(
f"""SELECT a.w_id, a.geom_point, a.geom
f"""SELECT w_id, geom_point, geom
FROM {cfg['weather_cells']['schema']}.
{cfg['weather_cells']['table']} a,
{cfg['vg250_sta_union']['schema']}.
{cfg['vg250_sta_union']['table']} b
WHERE ST_Intersects(ST_Transform(b.geometry, 4326), a.geom)""",
{cfg['weather_cells']['table']}
WHERE ST_Intersects('SRID=4326;
POLYGON((5 56, 15.5 56, 15.5 47, 5 47, 5 56))', geom)""",
geom_col=geom_column,
index_col="w_id",
)


def federal_states_per_weather_cell():
""" Assings a federal state to each weather cell in Germany.
"""Assings a federal state to each weather cell in Germany.
Sets the federal state to the weather celss using the centroid.
Weather cells at the borders whoes centroid is not inside Germany
Expand Down Expand Up @@ -80,9 +79,11 @@ def federal_states_per_weather_cell():
).index_right

# Assign a federal state to each cell inside Germany
buffer = 200
buffer = 1000

while len(weather_cells[weather_cells["federal_state"].isnull()]) > 0:
while (buffer < 30000) & (
len(weather_cells[weather_cells["federal_state"].isnull()]) > 0
):

cells = weather_cells[weather_cells["federal_state"].isnull()]

Expand All @@ -99,6 +100,8 @@ def federal_states_per_weather_cell():
.drop_duplicates(subset="w_id", keep="first")
.set_index("w_id")
)

weather_cells = weather_cells.dropna(axis=0, subset=["federal_state"])

return weather_cells.to_crs(4326)

Expand Down Expand Up @@ -145,7 +148,7 @@ def turbine_per_weather_cell():


def feedin_per_turbine():
""" Calculate feedin timeseries per turbine type and weather cell
"""Calculate feedin timeseries per turbine type and weather cell
Returns
-------
Expand Down Expand Up @@ -249,7 +252,7 @@ def feedin_per_turbine():


def wind():
""" Insert feed-in timeseries for wind onshore turbines to database
"""Insert feed-in timeseries for wind onshore turbines to database
Returns
-------
Expand Down Expand Up @@ -304,7 +307,7 @@ def wind():


def pv():
""" Insert feed-in timeseries for pv plants to database
"""Insert feed-in timeseries for pv plants to database
Returns
-------
Expand Down Expand Up @@ -334,7 +337,7 @@ def pv():


def solar_thermal():
""" Insert feed-in timeseries for pv plants to database
"""Insert feed-in timeseries for pv plants to database
Returns
-------
Expand Down Expand Up @@ -364,7 +367,7 @@ def solar_thermal():


def insert_feedin(data, carrier, weather_year):
""" Insert feedin data into database
"""Insert feedin data into database
Parameters
----------
Expand Down

0 comments on commit eb7d939

Please sign in to comment.