From 13e1bbcf9bc1fbdc7da5b5556b632b7526f9d1cb Mon Sep 17 00:00:00 2001 From: Fabien Maussion Date: Tue, 6 Sep 2022 18:25:10 +0200 Subject: [PATCH] Make sure sh cannot be set without nh --- oggm/cfg.py | 12 +++++++++++- oggm/core/climate.py | 7 +++++++ oggm/tests/test_utils.py | 7 +++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/oggm/cfg.py b/oggm/cfg.py index e2a56d0be..5181f4131 100644 --- a/oggm/cfg.py +++ b/oggm/cfg.py @@ -28,7 +28,7 @@ except ImportError: pass -from oggm.exceptions import InvalidParamsError +from oggm.exceptions import InvalidParamsError, InvalidWorkflowError # Local logger log = logging.getLogger(__name__) @@ -114,6 +114,7 @@ class ParamsLoggingDict(ResettingOrderedDict): def __setitem__(self, key, value): # Overrides the original dic to log the change + self._check_input(key, value) if self.do_log: self._log_param_change(key, value) ResettingOrderedDict.__setitem__(self, key, value) @@ -159,6 +160,15 @@ def _log_param_change(self, key, value): prev, value)) + def _check_input(self, key, value): + + if key == 'hydro_month_sh' and value == 1: + nh = self.get('hydro_month_nh') + if nh is not None and nh != 1: + msg = ("When setting PARAMS['hydro_month_sh'] to 1, please set " + "PARAMS['hydro_month_nh'] to 1 first.") + raise InvalidWorkflowError(msg) + # Globals IS_INITIALIZED = False diff --git a/oggm/core/climate.py b/oggm/core/climate.py index 6051b0f19..63ad9460f 100644 --- a/oggm/core/climate.py +++ b/oggm/core/climate.py @@ -1416,6 +1416,13 @@ def mu_star_calibration_from_geodetic_mb(gdir, max_mu_star = cfg.PARAMS['max_mu_star'] sm = cfg.PARAMS['hydro_month_' + gdir.hemisphere] + if sm == 1: + # Check that the other hemisphere is set to 1 as well to avoid surprises + oh = 'sh' if gdir.hemisphere == 'nh' else 'nh' + if cfg.PARAMS['hydro_month_' + oh] != 1: + raise InvalidParamsError('Please set both hydro_month_nh and ' + 'hydro_month_sh to 1 for geodetic ' + 'calibration.') if sm != 1 and not ignore_hydro_months: raise InvalidParamsError('mu_star_calibration_from_geodetic_mb makes ' 'more sense when applied on calendar years ' diff --git a/oggm/tests/test_utils.py b/oggm/tests/test_utils.py index a09e4ddfb..68da1a460 100644 --- a/oggm/tests/test_utils.py +++ b/oggm/tests/test_utils.py @@ -26,6 +26,7 @@ characs_apply_func) from oggm.utils import shape_factor_adhikari from oggm.exceptions import (InvalidParamsError, InvalidDEMError, + InvalidWorkflowError, DownloadVerificationFailedException) @@ -393,6 +394,12 @@ def test_defaults(self): else: self.assertFalse(cfg.PATHS['working_dir']) + def test_params_warn(self): + with pytest.raises(InvalidWorkflowError): + cfg.PARAMS['hydro_month_sh'] = 1 + cfg.PARAMS['hydro_month_nh'] = 1 + cfg.PARAMS['hydro_month_sh'] = 1 + def test_pathsetter(self): cfg.PATHS['working_dir'] = os.path.join('~', 'my_OGGM_wd') expected = os.path.join(self.homedir, 'my_OGGM_wd')