Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Python scripts directly from rxcropmaturity.py #2603

Open
samsrabin opened this issue Jun 19, 2024 · 0 comments
Open

Call Python scripts directly from rxcropmaturity.py #2603

samsrabin opened this issue Jun 19, 2024 · 0 comments
Labels
type: code cleanup improving internal code structure type: tests additions or changes to tests

Comments

@samsrabin
Copy link
Contributor

There are several places in the RXCROPMATURITY test code (rxcropmaturity.py) where a call is made to subprocess. E.g.:

# It'd be much nicer to call generate_gdds.main(), but I can't import generate_gdds.
tool_path = os.path.join(
self._ctsm_root, "python", "ctsm", "crop_calendars", "generate_gdds.py"
)
command = " ".join(
[
f"python3 {tool_path}",
f"--input-dir {input_dir}",
f"--first-season {first_season}",
f"--last-season {last_season}",
f"--sdates-file {sdates_file}",
f"--hdates-file {hdates_file}",
f"--output-dir generate_gdds_out",
f"--skip-crops miscanthus,irrigated_miscanthus",
]
)
stu.run_python_script(
self._get_caseroot(),
self._this_conda_env,
command,
tool_path,
)

def run_python_script(caseroot, this_conda_env, command_in, tool_path):
# First, try with "conda run -n"
command = cmds_to_run_via_conda(caseroot, f"conda run -n {this_conda_env}", command_in)
# Run with logfile
tool_name = os.path.split(tool_path)[-1]
try:
with open(tool_name + ".log", "w") as f:
subprocess.run(
command, shell=True, check=True, text=True, stdout=f, stderr=subprocess.STDOUT
)

It would be more robust to import the relevant functions from the scripts in question, then call them directly from Python. I think I had problems with that before, though, which is why I did it this way. The problems might have been related to ctsm_pylib not being available, but I'm not sure.

@samsrabin samsrabin added type: code cleanup improving internal code structure type: tests additions or changes to tests labels Jun 19, 2024
samsrabin added a commit to samsrabin/CTSM that referenced this issue Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: code cleanup improving internal code structure type: tests additions or changes to tests
Projects
None yet
Development

No branches or pull requests

1 participant