-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass commandline arguments instead of using CIME
generate_cupid_config_file.py now relies on argparse to get a handful of commandline arguments instead of importing some CIME functions and determining these values on the fly (it'll be easy to set all these arguments when creating the template)
- Loading branch information
1 parent
2b36874
commit 97fd4d4
Showing
1 changed file
with
61 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import os | ||
|
||
import yaml | ||
from CIME.case import Case | ||
from standard_script_setup import * # noqa: F401,F403 | ||
|
||
# Create variable for the caseroot environment variable | ||
caseroot = os.getcwd() | ||
# Open the config.yml file and create a dictionary with safe_load from the yaml package | ||
with open("config_template.yml") as f: | ||
my_dict = yaml.safe_load(f) | ||
|
||
# get environment cesm case variables | ||
with Case(caseroot, read_only=False, record=True) as case: | ||
cime_case = case.get_value("CASE") | ||
# create variable to access cesm_output_dir | ||
outdir = case.get_value("DOUT_S_ROOT") | ||
|
||
my_dict["global_params"]["case_name"] = cime_case | ||
my_dict["timeseries"]["case_name"] = cime_case | ||
|
||
# create variable user to access the user environment variable | ||
user = os.environ["USER"] | ||
# replace USER with the environment variable | ||
# my_dict['data_sources']['nb_path_root'] = f'/glade/u/home/{user}/CUPiD/examples/nblibrary' | ||
|
||
# replace with environment variable | ||
my_dict["global_params"]["CESM_output_dir"] = outdir | ||
|
||
# create new file, make it writeable | ||
with open("config.yml", "w") as f: | ||
# write a comment | ||
f.write("# sample comment\n") | ||
# enter in each element of the dictionary into the new file | ||
yaml.dump(my_dict, f) | ||
|
||
|
||
def _parse_args(): | ||
"""Parse command line arguments""" | ||
|
||
parser = argparse.ArgumentParser( | ||
description="Generate config.yml based on an existing CUPID example", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
# Command line argument for name of case (required) | ||
parser.add_argument("--case", action="store", dest="case", help="Name of CESM case") | ||
|
||
# Command line argument for name of case (required) | ||
parser.add_argument( | ||
"--cesm-output-dir", | ||
action="store", | ||
dest="cesm_output_dir", | ||
help="Location of CESM history files (short-term archive)", | ||
) | ||
|
||
parser.add_argument( | ||
"--cupid-root", | ||
action="store", | ||
dest="cupid_root", | ||
help="Location of CUPiD in file system", | ||
) | ||
|
||
parser.add_argument( | ||
"--cupid-example", | ||
action="store", | ||
dest="cupid_example", | ||
default="key_metrics", | ||
help="CUPiD example to use as template for config.yml", | ||
) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def generate_cupid_config(case, cesm_output_dir, cupid_root, cupid_example): | ||
with open(os.path.join(cupid_root, "examples", cupid_example, "config.yml")) as f: | ||
my_dict = yaml.safe_load(f) | ||
|
||
my_dict["global_params"]["case_name"] = case | ||
my_dict["timeseries"]["case_name"] = case | ||
|
||
# replace with environment variable | ||
my_dict["global_params"]["CESM_output_dir"] = cesm_output_dir | ||
|
||
# create new file, make it writeable | ||
with open("config.yml", "w") as f: | ||
# write a comment | ||
f.write("# sample comment\n") | ||
# enter in each element of the dictionary into the new file | ||
yaml.dump(my_dict, f, sort_keys=False) | ||
|
||
|
||
if __name__ == "__main__": | ||
args = vars(_parse_args()) | ||
generate_cupid_config(**args) |