forked from ESCOMP/CTSM
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clm5nl-gen - CLM5 namelist generator script (ESCOMP#3)
* Imported CLM5 namelist generator scripts * Made namelist generator installable with pip * Used PEP517 approach to packaging, with setuptools as build backend. More info: https://setuptools.readthedocs.io/en/latest/userguide/quickstart.html * Renamed 'build-clm5nl' to 'clm5nl-gen'. 'clm5nl-gen' sounds more unique which makes it easier to be "tab-completed" by the shell.
- Loading branch information
Showing
15 changed files
with
6,343 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env python3 | ||
|
||
"""clm5nl-gen - CLM5 namelist generator | ||
Generates CLM5 namelists from model parameters file and saves | ||
the outputs to a specified directory. If no directory is | ||
specified, the namelists are saved to the current directory. | ||
Usage: | ||
clm5nl-gen [--out DIR] PARAMFILE | ||
clm5nl-gen (-h | --help) | ||
clm5nl-gen (-v | --version) | ||
Arguments: | ||
PARAMFILE model parameters file (.yaml) | ||
Options: | ||
-o DIR --out DIR Save generated namelists to this directory. | ||
-h --help Show this screen. | ||
-v --version Show version. | ||
""" | ||
import sys | ||
from pathlib import Path | ||
from docopt import docopt | ||
from ruamel.yaml import YAML | ||
from clm5nl.generators.gen_lnd_in import build_lnd_in | ||
|
||
__version__ = "0.1" | ||
args = docopt(__doc__, version="clm5nl-gen v" + __version__) | ||
invalid_args = False | ||
params_file = Path(args["PARAMFILE"]).absolute() | ||
out_nl_file = "lnd_in" | ||
|
||
# Validate input args | ||
if (not params_file.exists()): | ||
print("clm5nl-gen error: Model parameter file '{}' not found.".format(params_file), file=sys.stderr) | ||
invalid_args = True | ||
|
||
if (args["--out"] is None): | ||
out_dir = Path.cwd() | ||
else: | ||
out_dir = Path(args["--out"]).absolute() | ||
if (not out_dir.is_dir()): | ||
print("clm5nl-gen error: Directory '{}' does not exist.".format(out_dir), file=sys.stderr) | ||
invalid_args = True | ||
|
||
if invalid_args: sys.exit(-1) | ||
|
||
# Parse model parameters file | ||
yaml = YAML(typ='safe') | ||
print("Reading model parameters file from '{}'".format(params_file)) | ||
config = yaml.load(open(params_file, "r")) | ||
|
||
opts, nl, drv_flds = {}, {}, {} | ||
for category in config: | ||
if category == "general_options": | ||
opts = {k:(v if str(v).lower() != "none" else None) for k, v in config[category].items()} | ||
elif category == "lnd_in": | ||
nl = {k:v for k, v in config[category].items()} | ||
elif category == "drv_flds_in": | ||
drv_flds = {k:v for k, v in config[category].items()} | ||
|
||
# Build namelists | ||
build_lnd_in(opts, nl, drv_flds, Path(out_dir, out_nl_file)) | ||
print("Successfully generated '{}'".format(Path(out_dir, out_nl_file))) |
Empty file.
Oops, something went wrong.