def test_histalp_mb(self): gdir = init_hef(border=DOM_BORDER) flowline.init_present_time_glacier(gdir) mb_mod = massbalance.TodayMassBalanceModel(gdir) ofl = gdir.read_pickle('inversion_flowlines', div_id=0) h = np.array([]) w = np.array([]) for fl in ofl: h = np.append(h, fl.surface_h) w = np.append(w, fl.widths) ref_mbh = mb_mod.get_mb(h, None) * 365 * 24 * 3600 mb_mod = massbalance.HistalpMassBalanceModel(gdir) # Climate period yrs = np.arange(1983, 2003.1, 1) my_mb = ref_mbh * 0. for yr in yrs: my_mb += mb_mod.get_mb(h, yr) my_mb = my_mb / len(yrs) * 365 * 24 * 3600 np.testing.assert_allclose(ref_mbh, my_mb, rtol=0.01)
def test_tstar_mb(self): gdir = init_hef(border=DOM_BORDER) flowline.init_present_time_glacier(gdir) mb_mod = massbalance.TstarMassBalanceModel(gdir) ofl = gdir.read_pickle('inversion_flowlines', div_id=0) h = np.array([]) w = np.array([]) for fl in ofl: h = np.append(h, fl.surface_h) w = np.append(w, fl.widths) ombh = mb_mod.get_mb(h, None) otmb = np.sum(ombh * w) np.testing.assert_allclose(0., otmb, atol=0.26) mb_mod = massbalance.TodayMassBalanceModel(gdir) ofl = gdir.read_pickle('inversion_flowlines', div_id=0) h = np.array([]) w = np.array([]) for fl in ofl: h = np.append(h, fl.surface_h) w = np.append(w, fl.widths) mbh = mb_mod.get_mb(h, None) tmb = np.sum(mbh * w) self.assertTrue(tmb < otmb) if do_plot: # pragma: no cover plt.plot(h, ombh, 'o') plt.plot(h, mbh, 'o') plt.show()
def test_find_t0(self): gdir = init_hef(border=DOM_BORDER, invert_with_sliding=False) flowline.init_present_time_glacier(gdir) glacier = gdir.read_pickle('model_flowlines') df = pd.read_csv(utils.get_demo_file('hef_lengths.csv'), index_col=0) df.columns = ['Leclercq'] df = df.loc[1950:] vol_ref = flowline.FlowlineModel(glacier).volume_km3 init_bias = 100. # 100 so that "went too far" comes once on travis rtol = 0.005 flowline.find_inital_glacier(gdir, y0=df.index[0], init_bias=init_bias, rtol=rtol, write_steps=False) past_model = gdir.read_pickle('past_model') vol_start = past_model.volume_km3 bef_fls = copy.deepcopy(past_model.fls) mylen = [] for y in df.index: past_model.run_until(y) mylen.append(past_model.fls[-1].length_m) df['oggm'] = mylen df = df - df.iloc[-1] vol_end = past_model.volume_km3 np.testing.assert_allclose(vol_ref, vol_end, rtol=0.05) rmsd = utils.rmsd(df.Leclercq, df.oggm) self.assertTrue(rmsd < 1000.) if do_plot: # pragma: no cover df.plot() plt.ylabel('Glacier length (relative to 2003)') plt.show() fig = plt.figure() lab = 'ref (vol={:.2f}km3)'.format(vol_ref) plt.plot(glacier[-1].surface_h, 'k', label=lab) lab = 'oggm start (vol={:.2f}km3)'.format(vol_start) plt.plot(bef_fls[-1].surface_h, 'b', label=lab) lab = 'oggm end (vol={:.2f}km3)'.format(vol_end) plt.plot(past_model.fls[-1].surface_h, 'r', label=lab) plt.plot(glacier[-1].bed_h, 'gray', linewidth=2) plt.legend(loc='best') plt.show()
def test_backwards_mb(self): gdir = init_hef(border=DOM_BORDER) flowline.init_present_time_glacier(gdir) mb_mod_ref = massbalance.TstarMassBalanceModel(gdir) mb_mod = massbalance.BackwardsMassBalanceModel(gdir, use_tstar=True) ofl = gdir.read_pickle('inversion_flowlines', div_id=0) h = np.array([]) w = np.array([]) for fl in ofl: h = np.append(h, fl.surface_h) w = np.append(w, fl.widths) ombh = mb_mod_ref.get_mb(h, None) * SEC_IN_YEAR mbh = mb_mod.get_mb(h, None) * SEC_IN_YEAR mb_mod.set_bias(100.) mbhb = mb_mod.get_mb(h, None) * SEC_IN_YEAR np.testing.assert_allclose(ombh, mbh, rtol=0.001) self.assertTrue(np.mean(mbhb) > np.mean(mbh)) if do_plot: # pragma: no cover plt.plot(h, ombh, 'o') plt.plot(h, mbh, 'x') plt.plot(h, mbhb, 'x') plt.show() mb_mod_ref = massbalance.TodayMassBalanceModel(gdir) mb_mod = massbalance.BackwardsMassBalanceModel(gdir) ofl = gdir.read_pickle('inversion_flowlines', div_id=0) h = np.array([]) w = np.array([]) for fl in ofl: h = np.append(h, fl.surface_h) w = np.append(w, fl.widths) ombh = mb_mod_ref.get_mb(h, None) * SEC_IN_YEAR mbh = mb_mod.get_mb(h, None) * SEC_IN_YEAR mb_mod.set_bias(-100.) mbhb = mb_mod.get_mb(h, None) * SEC_IN_YEAR np.testing.assert_allclose(ombh, mbh, rtol=0.005) self.assertTrue(np.mean(mbhb) < np.mean(mbh)) if do_plot: # pragma: no cover plt.plot(h, ombh, 'o') plt.plot(h, mbh, 'x') plt.plot(h, mbhb, 'x') plt.show()
def test_init_present_time_glacier(self): gdir = init_hef(border=DOM_BORDER) flowline.init_present_time_glacier(gdir) fls = gdir.read_pickle('model_flowlines') lens = [ len(gdir.read_pickle('centerlines', div_id=i)) for i in [1, 2, 3] ] ofl = gdir.read_pickle('inversion_flowlines', div_id=np.argmax(lens) + 1)[-1] self.assertTrue(gdir.rgi_date.year == 2003) self.assertTrue(len(fls) == 5) vol = 0. area = 0. for fl in fls: refo = 1 if fl is fls[-1] else 0 self.assertTrue(fl.order == refo) ref = np.arange(len(fl.surface_h)) * fl.dx np.testing.assert_allclose(ref, fl.dis_on_line, rtol=0.001, atol=0.01) self.assertTrue( len(fl.surface_h) == len(fl.bed_h) == len(fl.bed_shape) == len( fl.dis_on_line) == len(fl.widths)) self.assertTrue(np.all(fl.bed_shape >= 0)) self.assertTrue(np.all(fl.widths >= 0)) vol += fl.volume_km3 area += fl.area_km2 if refo == 1: rtol = 0.07 if HAS_NEW_GDAL else 0.05 np.testing.assert_allclose(ofl.widths * gdir.grid.dx, fl.widths_m[0:len(ofl.widths)], rtol=rtol) np.testing.assert_allclose(0.573, vol, rtol=0.001) np.testing.assert_allclose(7350.0, fls[-1].length_m) rtol = 0.01 if HAS_NEW_GDAL else 0.001 np.testing.assert_allclose(gdir.rgi_area_km2, area, rtol=rtol) if do_plot: # pragma: no cover plt.plot(fls[-1].bed_h) plt.plot(fls[-1].surface_h) plt.show()
def test_init_present_time_glacier(self): gdir = init_hef(border=DOM_BORDER) flowline.init_present_time_glacier(gdir) fls = gdir.read_pickle('model_flowlines') lens = [len(gdir.read_pickle('centerlines', div_id=i)) for i in [1, 2, 3]] ofl = gdir.read_pickle('inversion_flowlines', div_id=np.argmax(lens)+1)[-1] self.assertTrue(gdir.rgi_date.year == 2003) self.assertTrue(len(fls) == 5) vol = 0. area = 0. for fl in fls: refo = 1 if fl is fls[-1] else 0 self.assertTrue(fl.order == refo) ref = np.arange(len(fl.surface_h)) * fl.dx np.testing.assert_allclose(ref, fl.dis_on_line, rtol=0.001, atol=0.01) self.assertTrue(len(fl.surface_h) == len(fl.bed_h) == len(fl.bed_shape) == len(fl.dis_on_line) == len(fl.widths)) self.assertTrue(np.all(fl.bed_shape >= 0)) self.assertTrue(np.all(fl.widths >= 0)) vol += fl.volume_km3 area += fl.area_km2 if refo == 1: rtol = 0.07 if HAS_NEW_GDAL else 0.05 np.testing.assert_allclose(ofl.widths * gdir.grid.dx, fl.widths_m[0:len(ofl.widths)], rtol=rtol) np.testing.assert_allclose(0.573, vol, rtol=0.001) np.testing.assert_allclose(7350.0, fls[-1].length_m) rtol = 0.01 if HAS_NEW_GDAL else 0.001 np.testing.assert_allclose(gdir.rgi_area_km2, area, rtol=rtol) if do_plot: # pragma: no cover plt.plot(fls[-1].bed_h) plt.plot(fls[-1].surface_h) plt.show()
def test_find_t0(self): gdir = init_hef(border=DOM_BORDER, invert_with_sliding=False) flowline.init_present_time_glacier(gdir) glacier = gdir.read_pickle('model_flowlines') df = pd.read_csv(utils.get_demo_file('hef_lengths.csv'), index_col=0) df.columns = ['Leclercq'] df = df.loc[1950:] vol_ref = flowline.FlowlineModel(glacier).volume_km3 init_bias = 100. # 100 so that "went too far" comes once on travis rtol = 0.005 flowline.find_inital_glacier(gdir, y0=df.index[0], init_bias=init_bias, rtol=rtol, write_steps=False) past_model = gdir.read_pickle('past_model') vol_start = past_model.volume_km3 bef_fls = copy.deepcopy(past_model.fls) mylen = [] for y in df.index: past_model.run_until(y) mylen.append(past_model.fls[-1].length_m) df['oggm'] = mylen df = df-df.iloc[-1] vol_end = past_model.volume_km3 np.testing.assert_allclose(vol_ref, vol_end, rtol=0.05) rmsd = utils.rmsd(df.Leclercq, df.oggm) self.assertTrue(rmsd < 1000.) if do_plot: # pragma: no cover df.plot() plt.ylabel('Glacier length (relative to 2003)') plt.show() fig = plt.figure() lab = 'ref (vol={:.2f}km3)'.format(vol_ref) plt.plot(glacier[-1].surface_h, 'k', label=lab) lab = 'oggm start (vol={:.2f}km3)'.format(vol_start) plt.plot(bef_fls[-1].surface_h, 'b', label=lab) lab = 'oggm end (vol={:.2f}km3)'.format(vol_end) plt.plot(past_model.fls[-1].surface_h, 'r', label=lab) plt.plot(glacier[-1].bed_h, 'gray', linewidth=2) plt.legend(loc='best') plt.show()
def test_present_time_glacier_massbalance(self): gdir = init_hef(border=DOM_BORDER) flowline.init_present_time_glacier(gdir) mb_mod = massbalance.HistalpMassBalanceModel(gdir) fls = gdir.read_pickle('model_flowlines') glacier = flowline.FlowlineModel(fls) hef_file = utils.get_demo_file('mbdata_RGI40-11.00897.csv') mbdf = pd.read_csv(hef_file).set_index('YEAR') hgts = np.array([]) widths = np.array([]) for fl in glacier.fls: hgts = np.concatenate((hgts, fl.surface_h)) widths = np.concatenate((widths, fl.widths_m)) tot_mb = [] refmb = [] grads = hgts * 0 for yr, mb in mbdf.iterrows(): refmb.append(mb['ANNUAL_BALANCE']) mbh = mb_mod.get_mb(hgts, yr) * SEC_IN_YEAR * cfg.RHO grads += mbh tot_mb.append(np.average(mbh, weights=widths)) grads /= len(tot_mb) # Bias self.assertTrue(np.abs(utils.md(tot_mb, refmb)) < 50) # Gradient dfg = pd.read_csv(utils.get_demo_file('mbgrads_RGI40-11.00897.csv'), index_col='ALTITUDE').mean(axis=1) # Take the altitudes below 3100 and fit a line dfg = dfg[dfg.index < 3100] pok = np.where(hgts < 3100) from scipy.stats import linregress slope_obs, _, _, _, _ = linregress(dfg.index, dfg.values) slope_our, _, _, _, _ = linregress(hgts[pok], grads[pok]) np.testing.assert_allclose(slope_obs, slope_our, rtol=0.1)
def setUp(self): self.gdir = init_hef(border=DOM_BORDER) d = self.gdir.read_pickle('inversion_params') self.fs = d['fs'] self.glen_a = d['glen_a']