1
|
|
|
""" |
2
|
|
|
Read eTraGo tables for the status2019 and import it to db |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from pathlib import Path |
6
|
|
|
from urllib.request import urlretrieve |
7
|
|
|
import os |
8
|
|
|
import subprocess |
9
|
|
|
|
10
|
|
|
import pandas as pd |
11
|
|
|
|
12
|
|
|
from egon.data import config, db |
13
|
|
|
import egon.data.config |
14
|
|
|
|
15
|
|
|
sources = egon.data.config.datasets()["scenario_path"]["sources"] |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def download_status2019(): |
19
|
|
|
""" |
20
|
|
|
Download the status2019 etrago tables from Zenodo |
21
|
|
|
|
22
|
|
|
Returns |
23
|
|
|
------- |
24
|
|
|
None. |
25
|
|
|
|
26
|
|
|
""" |
27
|
|
|
|
28
|
|
|
if "status2019" in config.settings()["egon-data"]["--scenarios"]: |
29
|
|
|
print("Scenario status2019 was created within the pipeline. " |
30
|
|
|
"It is not imported from zenodo.") |
31
|
|
|
return |
32
|
|
|
|
33
|
|
|
# Get parameters from config and set download URL |
34
|
|
|
url = sources["url_status2019"] |
35
|
|
|
status2019_path = Path(".") / "PoWerD_status2019.backup" |
36
|
|
|
|
37
|
|
|
# Retrieve files |
38
|
|
|
urlretrieve(url, status2019_path) |
39
|
|
|
|
40
|
|
|
return |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def import_scn_status2019(): |
44
|
|
|
""" |
45
|
|
|
Read and import the scenario status2019 and import it into db |
46
|
|
|
|
47
|
|
|
Parameters |
48
|
|
|
---------- |
49
|
|
|
*No parameters required |
50
|
|
|
|
51
|
|
|
""" |
52
|
|
|
|
53
|
|
|
if "status2019" in config.settings()["egon-data"]["--scenarios"]: |
54
|
|
|
print("Scenario status2019 was created within the pipeline. " |
55
|
|
|
"It is not imported from zenodo.") |
56
|
|
|
return |
57
|
|
|
|
58
|
|
|
# Connect to the data base |
59
|
|
|
con = db.engine() |
60
|
|
|
|
61
|
|
|
# Clean existing data for status2019 |
62
|
|
|
tables = pd.read_sql( |
63
|
|
|
""" |
64
|
|
|
SELECT tablename FROM pg_catalog.pg_tables |
65
|
|
|
WHERE schemaname = 'grid' |
66
|
|
|
""", |
67
|
|
|
con, |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
tables = tables[ |
71
|
|
|
~tables["tablename"].isin( |
72
|
|
|
[ |
73
|
|
|
"egon_etrago_carrier", |
74
|
|
|
"egon_etrago_temp_resolution", |
75
|
|
|
] |
76
|
|
|
) |
77
|
|
|
] |
78
|
|
|
|
79
|
|
|
for table in tables["tablename"]: |
80
|
|
|
db.execute_sql( |
81
|
|
|
f""" |
82
|
|
|
DELETE FROM grid.{table} WHERE scn_name = 'status2019'; |
83
|
|
|
""" |
84
|
|
|
) |
85
|
|
|
|
86
|
|
|
config_data = config.settings()["egon-data"] |
87
|
|
|
database = config_data["--database-name"] |
88
|
|
|
host = config_data["--database-host"] |
89
|
|
|
port = config_data["--database-port"] |
90
|
|
|
user = config_data["--database-user"] |
91
|
|
|
|
92
|
|
|
my_env = os.environ.copy() |
93
|
|
|
my_env["PGPASSWORD"] = config_data["--database-password"] |
94
|
|
|
|
95
|
|
|
for table in tables["tablename"]: |
96
|
|
|
subprocess.Popen( |
97
|
|
|
[ |
98
|
|
|
"pg_restore", |
99
|
|
|
"-d", |
100
|
|
|
database, |
101
|
|
|
"--host", |
102
|
|
|
host, |
103
|
|
|
"--port", |
104
|
|
|
port, |
105
|
|
|
"-U", |
106
|
|
|
user, |
107
|
|
|
"-a", |
108
|
|
|
"--single-transaction", |
109
|
|
|
f"--table={table}", |
110
|
|
|
"PoWerD_status2019.backup", |
111
|
|
|
], |
112
|
|
|
env=my_env, |
113
|
|
|
) |
114
|
|
|
return |
115
|
|
|
|