Skip to content

Commit

Permalink
clm5nl-gen - CLM5 namelist generator script (ESCOMP#3)
Browse files Browse the repository at this point in the history
* 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
kvrigor authored Mar 22, 2021
1 parent b61d710 commit d0e7e3b
Show file tree
Hide file tree
Showing 15 changed files with 6,343 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ src/externals/mct/Makefile.conf
src/externals/mct/config.h
src/externals/mct/config.log
src/externals/mct/config.status

# Python byte-compiled / optimized / DLL files
*.py[cod]
65 changes: 65 additions & 0 deletions namelist_generator/clm5nl-gen
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.
Loading

0 comments on commit d0e7e3b

Please sign in to comment.