Skip to content

Commit 6717484

Browse files
pesapstaadecker
authored andcommitted
Merge pull request #78 from staadecker/get_inputs_post
Improve how get_inputs post-processing is handled
2 parents 0e197f1 + d532577 commit 6717484

File tree

10 files changed

+432
-367
lines changed

10 files changed

+432
-367
lines changed

docs/Database.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ their strong points. DBVisualizer can also create a graph of all the relationshi
2121
tables.
2222

2323
Further, it is often useful to read the comments on tables (PGAdmin: right-click table -> Properties)
24-
as they sometimes give details on the tables role. Finally, if the table is used in [`get_inputs.py`](/switch_model/wecc/get_inputs.py)
24+
as they sometimes give details on the tables role. Finally, if the table is used in [`get_inputs.py`](/switch_model/wecc/get_inputs/get_inputs.py)
2525
one can discover what it does by looking at how get_inputs.py uses the table to generate the SWITCH inputs.
2626

2727
## Connecting to the database
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
Script to retrieve the input data from the switch-wecc database and apply post-processing steps.
3+
"""
4+
import argparse
5+
import os
6+
7+
from switch_model.utilities import query_yes_no, StepTimer
8+
from switch_model.wecc.get_inputs.get_inputs import query_db
9+
from switch_model.wecc.get_inputs.register_post_process import run_post_process
10+
from switch_model.wecc.utilities import load_config
11+
from switch_model.wecc.get_inputs.post_process_steps import *
12+
13+
14+
def main():
15+
timer = StepTimer()
16+
17+
# Create command line tool, just provides help information
18+
parser = argparse.ArgumentParser(
19+
description="Write SWITCH input files from database tables.",
20+
epilog="""
21+
This tool will populate the inputs folder with the data from the PostgreSQL database.
22+
config.yaml specifies the scenario parameters.
23+
The environment variable DB_URL specifies the url to connect to the database. """,
24+
)
25+
parser.add_argument(
26+
"--skip-cf",
27+
default=False,
28+
action="store_true",
29+
help="Skip creation variable_capacity_factors.csv. Useful when debugging and one doesn't"
30+
"want to wait for the command.",
31+
)
32+
parser.add_argument(
33+
"--post-only",
34+
default=False,
35+
action="store_true",
36+
help="Only run the post solve functions (don't query db)",
37+
)
38+
parser.add_argument(
39+
"--overwrite",
40+
default=False,
41+
action="store_true",
42+
help="Overwrite previous input files without prompting to confirm.",
43+
)
44+
args = parser.parse_args() # Makes switch get_inputs --help works
45+
46+
# Load values from config.yaml
47+
full_config = load_config()
48+
switch_to_input_dir(full_config, overwrite=args.overwrite)
49+
50+
if not args.post_only:
51+
query_db(full_config, skip_cf=args.skip_cf)
52+
run_post_process()
53+
print(f"\nScript took {timer.step_time_as_str()} seconds to build input tables.")
54+
55+
56+
def switch_to_input_dir(config, overwrite):
57+
inputs_dir = config["inputs_dir"]
58+
59+
# Create inputs_dir if it doesn't exist
60+
if not os.path.exists(inputs_dir):
61+
os.makedirs(inputs_dir)
62+
print("Inputs directory created.")
63+
else:
64+
if not overwrite and not query_yes_no(
65+
"Inputs directory already exists. Allow contents to be overwritten?"
66+
):
67+
raise SystemExit("User cancelled run.")
68+
69+
os.chdir(inputs_dir)
70+
return inputs_dir
71+
72+
73+
if __name__ == "__main__":
74+
main()

0 commit comments

Comments
 (0)