-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into features/#162-district-heating-areas
- Loading branch information
Showing
8 changed files
with
689 additions
and
226 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
""" | ||
Central module containing all code dealing with importing era5 weather data. | ||
""" | ||
|
||
import atlite | ||
import os | ||
from pathlib import Path | ||
|
||
import geopandas as gpd | ||
import egon.data.config | ||
from egon.data import db | ||
from egon.data.importing.scenarios import get_sector_parameters | ||
from sqlalchemy import Column, String, Float, Integer, ARRAY | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from geoalchemy2 import Geometry | ||
# will be later imported from another file ### | ||
Base = declarative_base() | ||
|
||
class EgonEra5Cells(Base): | ||
__tablename__ = 'egon_era5_weather_cells' | ||
__table_args__ = {'schema': 'supply'} | ||
w_id = Column(Integer, primary_key=True) | ||
geom = Column(Geometry('POLYGON', 4326)) | ||
geom_point = Column(Geometry('POINT', 4326)) | ||
|
||
|
||
class EgonRenewableFeedIn(Base): | ||
__tablename__ = 'egon_era5_renewable_feedin' | ||
__table_args__ = {'schema': 'supply'} | ||
w_id = Column(Integer, primary_key=True) | ||
weather_year = Column(Integer, primary_key=True) | ||
carrier = Column(String, primary_key=True) | ||
feedin = Column(ARRAY(Float())) | ||
|
||
def create_tables(): | ||
|
||
db.execute_sql( | ||
"CREATE SCHEMA IF NOT EXISTS supply;") | ||
engine = db.engine() | ||
EgonEra5Cells.__table__.drop(bind=engine, checkfirst=True) | ||
EgonEra5Cells.__table__.create(bind=engine, checkfirst=True) | ||
EgonRenewableFeedIn.__table__.drop(bind=engine, checkfirst=True) | ||
EgonRenewableFeedIn.__table__.create(bind=engine, checkfirst=True) | ||
|
||
def import_cutout(boundary='Europe'): | ||
""" Import weather data from cutout | ||
Returns | ||
------- | ||
cutout : atlite.cutout.Cutout | ||
Weather data stored in cutout | ||
""" | ||
|
||
if boundary == 'Europe': | ||
xs = slice(-12., 35.1) | ||
ys = slice(72., 33.) | ||
|
||
elif boundary == 'Germany': | ||
geom_de = gpd.read_postgis( | ||
"SELECT geometry as geom FROM boundaries.vg250_sta_bbox", | ||
db.engine()).to_crs(4623).geom | ||
xs = slice(geom_de.bounds.minx[0], geom_de.bounds.maxx[0]) | ||
ys = slice(geom_de.bounds.maxy[0], geom_de.bounds.miny[0]) | ||
|
||
else: | ||
print( | ||
f"Boundary {boundary} not defined. " | ||
"Choose either 'Europe' or 'Germany'") | ||
|
||
directory = Path(".") / ( | ||
egon.data.config.datasets() | ||
['era5_weather_data']['targets']['weather_data']['path']) | ||
|
||
weather_year = get_sector_parameters('global', 'eGon2035')['weather_year'] | ||
|
||
cutout = atlite.Cutout( | ||
f"europe-{str(weather_year)}-era5", | ||
cutout_dir = directory.absolute(), | ||
module="era5", | ||
xs=xs, | ||
ys=ys, | ||
years=slice(weather_year, weather_year) | ||
) | ||
|
||
return cutout | ||
|
||
def download_era5(): | ||
""" Download weather data from era5 | ||
Returns | ||
------- | ||
None. | ||
""" | ||
|
||
directory = Path(".") / ( | ||
egon.data.config.datasets() | ||
['era5_weather_data']['targets']['weather_data']['path']) | ||
|
||
if not os.path.exists(directory): | ||
|
||
os.mkdir(directory) | ||
|
||
cutout = import_cutout() | ||
|
||
if not cutout.prepared: | ||
|
||
cutout.prepare(nprocesses=1) | ||
|
||
|
||
def insert_weather_cells(): | ||
""" Insert weather cells from era5 into database table | ||
Returns | ||
------- | ||
None. | ||
""" | ||
|
||
cfg = egon.data.config.datasets()['era5_weather_data'] | ||
|
||
cutout = import_cutout() | ||
|
||
df = gpd.GeoDataFrame( | ||
{'geom': cutout.grid_cells()}, geometry='geom', crs=4326) | ||
|
||
df.to_postgis(cfg['targets']['weather_cells']['table'], | ||
schema=cfg['targets']['weather_cells']['schema'], | ||
con=db.engine(), if_exists='append') | ||
|
||
db.execute_sql( | ||
f"""UPDATE {cfg['targets']['weather_cells']['schema']}. | ||
{cfg['targets']['weather_cells']['table']} | ||
SET geom_point=ST_Centroid(geom);""" | ||
) |
Oops, something went wrong.