Skip to content

Commit 10a08c1

Browse files
committed
Minor refactor to allow running with 'python -m ...'
1 parent 2e64717 commit 10a08c1

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

switch_model/__main__.py

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

0 commit comments

Comments
 (0)