forked from openego/eGon-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Features/#325 import status2019
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from egon.data.datasets import Dataset | ||
from egon.data.datasets.scenario_path.import_status2019 import ( | ||
download_status2019, | ||
import_scn_status2019, | ||
) | ||
|
||
|
||
class CreateIntermediateScenarios(Dataset): | ||
def __init__(self, dependencies): | ||
super().__init__( | ||
name="scenario_path", | ||
version="0.0.1", | ||
dependencies=dependencies, | ||
tasks=( | ||
download_status2019, | ||
import_scn_status2019, | ||
), | ||
) |
102 changes: 102 additions & 0 deletions
102
src/egon/data/datasets/scenario_path/import_status2019.py
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,102 @@ | ||
""" | ||
Read eTraGo tables for the status2019 and import it to db | ||
""" | ||
|
||
from pathlib import Path | ||
from urllib.request import urlretrieve | ||
import os | ||
import subprocess | ||
|
||
import pandas as pd | ||
|
||
from egon.data import config, db | ||
import egon.data.config | ||
|
||
sources = egon.data.config.datasets()["scenario_path"]["sources"] | ||
|
||
|
||
def download_status2019(): | ||
""" | ||
Download the status2019 etrago tables from Zenodo | ||
Returns | ||
------- | ||
None. | ||
""" | ||
# Get parameters from config and set download URL | ||
url = sources["url_status2019"] | ||
status2019_path = Path(".") / "PoWerD_status2019.backup" | ||
|
||
# Retrieve files | ||
urlretrieve(url, status2019_path) | ||
|
||
return | ||
|
||
|
||
def import_scn_status2019(): | ||
""" | ||
Read and import the scenario status2019 and import it into db | ||
Parameters | ||
---------- | ||
*No parameters required | ||
""" | ||
# Connect to the data base | ||
con = db.engine() | ||
|
||
# Clean existing data for status2019 | ||
tables = pd.read_sql( | ||
""" | ||
SELECT tablename FROM pg_catalog.pg_tables | ||
WHERE schemaname = 'grid' | ||
""", | ||
con, | ||
) | ||
|
||
tables = tables[ | ||
~tables["tablename"].isin( | ||
[ | ||
"egon_etrago_carrier", | ||
"egon_etrago_temp_resolution", | ||
] | ||
) | ||
] | ||
|
||
for table in tables["tablename"]: | ||
db.execute_sql( | ||
f""" | ||
DELETE FROM grid.{table} WHERE scn_name = 'status2019'; | ||
""" | ||
) | ||
|
||
config_data = config.settings()["egon-data"] | ||
database = config_data["--database-name"] | ||
host = config_data["--database-host"] | ||
port = config_data["--database-port"] | ||
user = config_data["--database-user"] | ||
|
||
my_env = os.environ.copy() | ||
my_env["PGPASSWORD"] = config_data["--database-password"] | ||
|
||
for table in tables["tablename"]: | ||
subprocess.Popen( | ||
[ | ||
"pg_restore", | ||
"-d", | ||
database, | ||
"--host", | ||
host, | ||
"--port", | ||
port, | ||
"-U", | ||
user, | ||
"-a", | ||
"--single-transaction", | ||
f"--table={table}", | ||
"PoWerD_status2019.backup", | ||
], | ||
env=my_env, | ||
) | ||
return |