Skip to content

Commit 9adf624

Browse files
authored
Allow for monthly temp and precip biases / facs (#1247)
* WIP * Allow for monthly temp and precip biases / facs * Whats new and better handling of scalars
1 parent 7b65cb7 commit 9adf624

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

docs/whats-new.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Version history
55
===============
66

7-
v1.4.1 (unreleased)
7+
v1.5.0 (unreleased)
88
-------------------
99

1010
Breaking changes
@@ -44,6 +44,9 @@ Enhancements
4444
- Added new diagnostic variables (`terminus_thick_0` ...) to better track
4545
the thickness at the terminus (:pull:`1230`).
4646
By `Fabien Maussion <https://github.com/fmaussion>`_
47+
- Temperature and precipitation corrections can now also be applied on a
48+
monthly basis for the PastMassBalanceModel (:pull:`1247`).
49+
By `Fabien Maussion <https://github.com/fmaussion>`_
4750

4851

4952
Bug fixes
@@ -52,8 +55,8 @@ Bug fixes
5255
- Fixed bug in hydro date / calendar date conversions with month=1 (i.e.
5356
no conversion) (:pull:`1220`).
5457
By `Lilian Schuster <https://github.com/lilianschuster>`_
55-
- Fixed bug in ``graphics.plot_distributed_thickness`` which encounter Error under
56-
the elevation bands flowline (:pull: `1241`).
58+
- Fixed bug in ``graphics.plot_distributed_thickness`` which led to an error with
59+
elevation bands flowlines (:pull:`1241`).
5760
By `Li Fei <https://github.com/Keeptg>`_
5861

5962
v1.4.0 (17.02.2021)

oggm/core/massbalance.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,18 @@ def prcp_fac(self):
428428
@prcp_fac.setter
429429
def prcp_fac(self, new_prcp_fac):
430430
# just to check that no invalid prcp_factors are used
431-
if new_prcp_fac <= 0:
431+
if np.any(np.asarray(new_prcp_fac) <= 0):
432432
raise InvalidParamsError('prcp_fac has to be above zero!')
433+
434+
if len(np.atleast_1d(new_prcp_fac)) == 12:
435+
# OK so that's monthly stuff
436+
# We dirtily assume that user just used calendar month
437+
sm = cfg.PARAMS['hydro_month_' + self.hemisphere]
438+
new_prcp_fac = np.roll(new_prcp_fac, 13 - sm)
439+
new_prcp_fac = np.tile(new_prcp_fac, len(self.prcp) // 12)
440+
433441
self.prcp *= new_prcp_fac / self._prcp_fac
442+
434443
# update old prcp_fac in order that it can be updated again ...
435444
self._prcp_fac = new_prcp_fac
436445

@@ -441,7 +450,16 @@ def temp_bias(self):
441450

442451
@temp_bias.setter
443452
def temp_bias(self, new_temp_bias):
453+
454+
if len(np.atleast_1d(new_temp_bias)) == 12:
455+
# OK so that's monthly stuff
456+
# We dirtily assume that user just used calendar month
457+
sm = cfg.PARAMS['hydro_month_' + self.hemisphere]
458+
new_temp_bias = np.roll(new_temp_bias, 13 - sm)
459+
new_temp_bias = np.tile(new_temp_bias, len(self.temp) // 12)
460+
444461
self.temp += new_temp_bias - self._temp_bias
462+
445463
# update old temp_bias in order that it can be updated again ...
446464
self._temp_bias = new_temp_bias
447465

oggm/tests/test_models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ def test_prcp_fac_temp_bias_update(self, hef_gdir):
393393
assert mb_mod._prcp_fac == prcp_fac_old
394394
assert mb_mod.temp_bias == temp_bias_old
395395

396+
# Now monthly stuff
397+
mb_mod.temp_bias = [0] * 12
398+
np.testing.assert_allclose(mb_mod.temp_bias, temp_bias_old)
399+
mb_mod.prcp_fac = [prcp_fac_old] * 12
400+
np.testing.assert_allclose(mb_mod.prcp_fac, prcp_fac_old)
401+
396402
# Deprecated attrs
397403
with pytest.raises(AttributeError):
398404
mb_mod.prcp_bias = 2
@@ -560,6 +566,7 @@ def test_constant_mb_model(self, hef_gdir):
560566
months = np.arange(12)
561567
monthly_1 = months * 0.
562568
monthly_2 = months * 0.
569+
monthly_3 = months * 0.
563570
for m in months:
564571
yr = utils.date_to_floatyear(0, m + 1)
565572
cmb_mod.temp_bias = 0
@@ -568,14 +575,23 @@ def test_constant_mb_model(self, hef_gdir):
568575
cmb_mod.temp_bias = 1
569576
tmp = cmb_mod.get_monthly_mb(h, yr) * SEC_IN_MONTH * rho
570577
monthly_2[m] = np.average(tmp, weights=w)
578+
cmb_mod.temp_bias = [0] * 6 + [1] + [0] * 5
579+
cmb_mod.prcp_fac = [10] * 3 + [2.5] * 9
580+
tmp = cmb_mod.get_monthly_mb(h, yr) * SEC_IN_MONTH * rho
581+
monthly_3[m] = np.average(tmp, weights=w)
582+
cmb_mod.prcp_fac = 2.5
571583

572584
# check that the winter months are close but summer months no
573585
np.testing.assert_allclose(monthly_1[1: 5], monthly_2[1: 5], atol=1)
586+
np.testing.assert_allclose(monthly_1[1: 2], monthly_3[1: 2], atol=1)
574587
assert np.mean(monthly_1[5:]) > (np.mean(monthly_2[5:]) + 100)
588+
assert np.mean(monthly_3[3:5]) > (np.mean(monthly_1[3:5]) + 100)
589+
assert monthly_3[9] == monthly_2[9]
575590

576591
if do_plot: # pragma: no cover
577592
plt.plot(monthly_1, '-', label='Normal')
578593
plt.plot(monthly_2, '-', label='Temp bias')
594+
plt.plot(monthly_3, '-', label='Temp bias monthly')
579595
plt.legend()
580596
plt.show()
581597

0 commit comments

Comments
 (0)