def setUp(self, verbose=0): util_mdtf.check_required_dirs( already_exist=[], create_if_nec=[self.MODEL_WK_DIR, self.MODEL_DATA_DIR], verbose=verbose) self.envvars.update({ "DATADIR": self.MODEL_DATA_DIR, "variab_dir": self.MODEL_WK_DIR, "CASENAME": self.case_name, "model": self.model_name, "FIRSTYR": self.firstyr.format(precision=1), "LASTYR": self.lastyr.format(precision=1) }) # set env vars for unit conversion factors (TODO: honest unit conversion) translate = util_mdtf.VariableTranslator() if self.convention not in translate.units: raise AssertionError( ("Variable name translation doesn't recognize " "{}.").format(self.convention)) temp = translate.variables[self.convention].to_dict() for key, val in iter(temp.items()): util_mdtf.setenv(key, val, self.envvars, verbose=verbose) temp = translate.units[self.convention].to_dict() for key, val in iter(temp.items()): util_mdtf.setenv(key, val, self.envvars, verbose=verbose) for pod in self.iter_pods(): self._setup_pod(pod) self._build_data_dicts()
def test_setenv_bool(self): test_d = {} util.setenv('TEST_TRUE', True, test_d) self.assertEqual(test_d['TEST_TRUE'], True) self.assertEqual(os.environ['TEST_TRUE'], '1') util.setenv('TEST_FALSE', False, test_d) self.assertEqual(test_d['TEST_FALSE'], False) self.assertEqual(os.environ['TEST_FALSE'], '0')
def _set_pod_env_vars(self, verbose=0): """Private method called by :meth:`~shared_diagnostic.Diagnostic.setUp`. Sets all environment variables for POD. Args: verbose (:py:obj:`int`, optional): Logging verbosity level. Default 0. """ self.pod_env_vars.update({ "POD_HOME": self.POD_CODE_DIR, # location of POD's code "OBS_DATA": self.POD_OBS_DATA, # POD's observational data "WK_DIR": self.POD_WK_DIR, # POD's subdir within working directory }) # Set env vars POD has inherited globally and from current case # (set in DataManager._setup_pod). for key, val in iter(self.pod_env_vars.items()): util_mdtf.setenv(key, val, self.pod_env_vars, verbose=verbose, overwrite=True) # Set env vars for variable and axis names: axes = dict() ax_bnds = dict() ax_status = dict() for var in self.iter_vars_and_alts(): # util_mdtf.setenv(var.original_name, var.name_in_model, # self.pod_env_vars, verbose=verbose) # make sure axes found for different vars are consistent for ax_name, ax_attrs in iter(var.axes.items()): if 'MDTF_envvar' not in ax_attrs: print(("\tWarning: don't know env var to set" "for axis name {}").format(ax_name)) envvar_name = ax_name + '_coord' else: envvar_name = ax_attrs['MDTF_envvar'] set_from_axis = ax_attrs.get('MDTF_set_from_axis', None) if envvar_name not in axes: # populate dict axes[envvar_name] = ax_name ax_status[envvar_name] = set_from_axis elif axes[envvar_name] != ax_name and ax_status[ envvar_name] is None: # populated with defaults, but now overwrite with name that # was confirmed from file axes[envvar_name] = ax_name ax_status[envvar_name] = set_from_axis elif axes[envvar_name] != ax_name \ and ax_status[envvar_name] == set_from_axis: # names found in two different files disagree - raise error raise PodRequirementFailure( self, ("Two variables have conflicting axis names {}:" "({}!={})").format(envvar_name, axes[envvar_name], ax_name)) for key, val in axes.items(): # Define ax bounds variables; TODO do this more honestly ax_bnds[key + '_bnds'] = val + '_bnds' for key, val in axes.items(): util_mdtf.setenv(key, val, self.pod_env_vars, verbose=verbose) for key, val in ax_bnds.items(): util_mdtf.setenv(key, val, self.pod_env_vars, verbose=verbose)
def test_setenv_int(self): test_d = {} util.setenv('TEST_INT', 2019, test_d) self.assertEqual(test_d['TEST_INT'], 2019) self.assertEqual(os.environ['TEST_INT'], '2019')
def test_setenv_str(self): test_d = {} util.setenv('TEST_STR', 'B', test_d) self.assertEqual(test_d['TEST_STR'], 'B') self.assertEqual(os.environ['TEST_STR'], 'B')
def test_setenv_overwrite(self): test_d = {'TEST_OVERWRITE': 'A'} util.setenv('TEST_OVERWRITE', 'B', test_d, overwrite=False) self.assertEqual(test_d['TEST_OVERWRITE'], 'A') self.assertEqual(os.environ['TEST_OVERWRITE'], 'A')