Skip to content

Commit

Permalink
Added user_env_vars conf functionality and FCST_TIME (lead) environme…
Browse files Browse the repository at this point in the history
…nt variable setting in grid_stat
  • Loading branch information
georgemccabe committed Nov 29, 2018
1 parent d68efe4 commit 5e35a01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 16 additions & 2 deletions ush/command_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, p, logger):
self.env = os.environ.copy()
self.set_verbose(self.p.getstr('config', 'LOG_MET_VERBOSITY', '2'))
self.cmdrunner = CommandRunner(self.p, logger=self.logger)
self.set_user_environment()
self.clear()

def clear(self):
Expand All @@ -57,7 +58,15 @@ def clear(self):
self.outdir = ""
self.outfile = ""
self.param = ""


def set_user_environment(self):
if 'user_env_vars' not in self.p.sections():
self.p.add_section('user_env_vars')
for env_var in self.p.keys('user_env_vars'):
# if env_var in self.env:
# self.logger.warning("{} is already set in the environment. Overwriting from conf file"
# .format(env_var))
self.add_env_var(env_var, self.p.getstr('user_env_vars', env_var))

def set_debug(self, debug):
self.debug = debug
Expand Down Expand Up @@ -132,7 +141,8 @@ def print_env_copy(self, vars):
copied into terminal
"""
out = ""
for v in vars:
all_vars = vars + self.p.keys('user_env_vars')
for v in all_vars:
if self.env[v].find('"') != -1:
next = 'export '+v+'="'+self.env[v].replace('"', '\\"')+'"'
else:
Expand All @@ -145,6 +155,10 @@ def print_env_item(self, item):
"""
self.logger.debug(item+"="+self.env[item])

def print_user_env_items(self):
for k in self.p.keys('user_env_vars'):
self.print_env_item(k)

def get_command(self):
"""! Builds the command to run the MET application
@rtype string
Expand Down
9 changes: 6 additions & 3 deletions ush/compare_gridded_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,12 @@ def run_at_time_once(self, ti, v):
self.add_env_var("OBS_FIELD", obs_field)
self.add_env_var("INPUT_BASE", input_base)
self.add_env_var("MET_VALID_HHMM", valid_time[4:8])
self.add_env_var("FCST_TIME", str(ti.lead).zfill(3))
cmd = self.get_command()

self.logger.debug("")
self.logger.debug("ENVIRONMENT FOR NEXT COMMAND: ")
self.print_user_env_items()
self.print_env_item("MODEL")
self.print_env_item("FCST_VAR")
self.print_env_item("OBS_VAR")
Expand All @@ -352,14 +354,15 @@ def run_at_time_once(self, ti, v):
self.print_env_item("FCST_FIELD")
self.print_env_item("OBS_FIELD")
self.print_env_item("INPUT_BASE")
self.print_env_item("MET_VALID_HHMM")
self.print_env_item("MET_VALID_HHMM")
self.print_env_item("FCST_TIME")
self.logger.debug("")
self.logger.debug("COPYABLE ENVIRONMENT FOR NEXT COMMAND: ")
self.print_env_copy(["MODEL", "FCST_VAR", "OBS_VAR",
"LEVEL", "OBTYPE", "CONFIG_DIR",
"FCST_FIELD", "OBS_FIELD",
"INPUT_BASE",
"MET_VALID_HHMM"])
"INPUT_BASE", "MET_VALID_HHMM",
"FCST_TIME"])
self.logger.debug("")
cmd = self.get_command()
if cmd is None:
Expand Down

0 comments on commit 5e35a01

Please sign in to comment.