Skip to content

Commit

Permalink
Make sure sh cannot be set without nh
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaussion committed Sep 6, 2022
1 parent 57ff957 commit 13e1bbc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion oggm/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
except ImportError:
pass

from oggm.exceptions import InvalidParamsError
from oggm.exceptions import InvalidParamsError, InvalidWorkflowError

# Local logger
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions oggm/core/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down
7 changes: 7 additions & 0 deletions oggm/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
characs_apply_func)
from oggm.utils import shape_factor_adhikari
from oggm.exceptions import (InvalidParamsError, InvalidDEMError,
InvalidWorkflowError,
DownloadVerificationFailedException)


Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 13e1bbc

Please sign in to comment.