Skip to content

Commit

Permalink
[develop] Replace existing UW CLI with UW API calls to template (ufs-…
Browse files Browse the repository at this point in the history
…community#1078)

This work continues the integration of the uwtools package by replacing current use of the UW CLI with UW API calls in Python scripts. These changes are limited to the UW template tool.
  • Loading branch information
elcarpenterNOAA authored Apr 26, 2024
1 parent 527b242 commit 08537b0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 142 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ channels:
dependencies:
- pylint=2.17*
- pytest=7.2*
- uwtools=2.1*
- uwtools=2.2*
38 changes: 6 additions & 32 deletions ush/create_aqm_rc_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import argparse
import os
import sys
import tempfile
from subprocess import STDOUT, CalledProcessError, check_output
from textwrap import dedent
from uwtools.api.template import render

from python_utils import (
cfg_to_yaml_str,
Expand Down Expand Up @@ -124,36 +123,11 @@ def create_aqm_rc_file(cdate, run_dir, init_concentrations):
#
#-----------------------------------------------------------------------
#
with tempfile.NamedTemporaryFile(
dir="./",
mode="w+t",
prefix="aqm_rc_settings",
suffix=".yaml") as tmpfile:
tmpfile.write(settings_str)
tmpfile.seek(0)
cmd = " ".join(["uw template render",
"-i",
AQM_RC_TMPL_FP,
"-o",
aqm_rc_fp,
"-v",
"--values-file",
tmpfile.name,
]
)
indent = " "
output = ""
try:
output = check_output(cmd, encoding="utf=8", shell=True,
stderr=STDOUT, text=True)
except CalledProcessError as e:
output = e.output
print(f"Failed with status: {e.returncode}")
sys.exit(1)
finally:
print("Output:")
for line in output.split("\n"):
print(f"{indent * 2}{line}")
render(
input_file = AQM_RC_TMPL_FP,
output_file = aqm_rc_fp,
values_src = settings,
)
return True

def parse_args(argv):
Expand Down
32 changes: 5 additions & 27 deletions ush/create_diag_table_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import argparse
import os
import sys
import tempfile
from subprocess import STDOUT, CalledProcessError, check_output
from textwrap import dedent
from uwtools.api.template import render

from python_utils import (
cfg_to_yaml_str,
Expand Down Expand Up @@ -74,32 +73,11 @@ def create_diag_table_file(run_dir):
verbose=VERBOSE,
)

with tempfile.NamedTemporaryFile(dir="./",
mode="w+t",
prefix="aqm_rc_settings",
suffix=".yaml") as tmpfile:
tmpfile.write(settings_str)
tmpfile.seek(0)
cmd = " ".join(["uw template render",
"-i", DIAG_TABLE_TMPL_FP,
"-o", diag_table_fp,
"-v",
"--values-file", tmpfile.name,
]
render(
input_file = DIAG_TABLE_TMPL_FP,
output_file = diag_table_fp,
values_src = settings,
)
indent = " "
output = ""
try:
output = check_output(cmd, encoding="utf=8", shell=True,
stderr=STDOUT, text=True)
except CalledProcessError as e:
output = e.output
print(f"Failed with status: {e.returncode}")
sys.exit(1)
finally:
print("Output:")
for line in output.split("\n"):
print(f"{indent * 2}{line}")
return True


Expand Down
32 changes: 5 additions & 27 deletions ush/create_model_configure_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import argparse
import os
import sys
import tempfile
from textwrap import dedent
from subprocess import STDOUT, CalledProcessError, check_output
from uwtools.api.template import render

from python_utils import (
cfg_to_yaml_str,
Expand Down Expand Up @@ -220,32 +219,11 @@ def create_model_configure_file(
#
model_config_fp = os.path.join(run_dir, MODEL_CONFIG_FN)

with tempfile.NamedTemporaryFile(dir="./",
mode="w+t",
suffix=".yaml",
prefix="model_config_settings.") as tmpfile:
tmpfile.write(settings_str)
tmpfile.seek(0)
cmd = " ".join(["uw template render",
"-i", MODEL_CONFIG_TMPL_FP,
"-o", model_config_fp,
"-v",
"--values-file", tmpfile.name,
]
render(
input_file = MODEL_CONFIG_TMPL_FP,
output_file = model_config_fp,
values_src = settings
)
indent = " "
output = ""
try:
output = check_output(cmd, encoding="utf=8", shell=True,
stderr=STDOUT, text=True)
except CalledProcessError as e:
output = e.output
print(f"Failed with status: {e.returncode}")
sys.exit(1)
finally:
print("Output:")
for line in output.split("\n"):
print(f"{indent * 2}{line}")
return True


Expand Down
37 changes: 6 additions & 31 deletions ush/create_ufs_configure_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import argparse
import os
import sys
import tempfile
from subprocess import STDOUT, CalledProcessError, check_output
from textwrap import dedent
from uwtools.api.template import render

from python_utils import (
cfg_to_yaml_str,
Expand Down Expand Up @@ -46,7 +45,7 @@ def create_ufs_configure_file(run_dir):
#-----------------------------------------------------------------------
#
print_info_msg(f'''
Creating a ufs.configure file (\"{UFS_CONFIG_FN}\") in the specified
Creating a ufs.configure file (\"{UFS_CONFIG_FN}\") in the specified
run directory (run_dir):
run_dir = \"{run_dir}\"''', verbose=VERBOSE)
#
Expand Down Expand Up @@ -87,35 +86,11 @@ def create_ufs_configure_file(run_dir):
#
#-----------------------------------------------------------------------
#
# Store the settings in a temporary file
with tempfile.NamedTemporaryFile(dir="./",
mode="w+t",
prefix="ufs_config_settings",
suffix=".yaml") as tmpfile:
tmpfile.write(settings_str)
tmpfile.seek(0)

cmd = " ".join(["uw template render",
"-i", UFS_CONFIG_TMPL_FP,
"-o", ufs_config_fp,
"-v",
"--values-file", tmpfile.name,
]
render(
input_file = UFS_CONFIG_TMPL_FP,
output_file = ufs_config_fp,
values_src = settings,
)

indent = " "
output = ""
try:
output = check_output(cmd, encoding="utf=8", shell=True,
stderr=STDOUT, text=True)
except CalledProcessError as e:
output = e.output
print(f"Failed with status: {e.returncode}")
sys.exit(1)
finally:
print("Output:")
for line in output.split("\n"):
print(f"{indent * 2}{line}")
return True

def parse_args(argv):
Expand Down
30 changes: 6 additions & 24 deletions ush/generate_FV3LAM_wflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import logging
import os
import sys
from subprocess import STDOUT, CalledProcessError, check_output
from textwrap import dedent

from uwtools.api.config import get_nml_config, get_yaml_config, realize
from uwtools.api.template import render

from python_utils import (
log_info,
Expand Down Expand Up @@ -112,29 +112,11 @@ def generate_FV3LAM_wflow(
# Call the python script to generate the experiment's XML file
#
rocoto_yaml_fp = expt_config["workflow"]["ROCOTO_YAML_FP"]
cmd = " ".join(["uw template render",
"-i", template_xml_fp,
"-o", wflow_xml_fp,
"-v",
"--values-file", rocoto_yaml_fp,
]
)

indent = " "
output = ""
logfunc = logging.info
try:
output = check_output(cmd, encoding="utf=8", shell=True,
stderr=STDOUT, text=True)
except CalledProcessError as e:
logfunc = logging.error
output = e.output
logging.exception(("Failed with status: %s", e.returncode))
raise
finally:
logfunc("Output:")
for line in output.split("\n"):
logfunc("%s%s", indent * 2, line)
render(
input_file = template_xml_fp,
output_file = wflow_xml_fp,
values_src = rocoto_yaml_fp,
)
#
# -----------------------------------------------------------------------
#
Expand Down

0 comments on commit 08537b0

Please sign in to comment.