def get_apparent_climate(gdir): # Climate period mu_hp = int(cfg.PARAMS['mu_star_halfperiod']) df = pd.read_csv(gdir.get_filepath('local_mustar')) tstar = df['tstar'] yr = [tstar-mu_hp, tstar+mu_hp] # Ok. Looping over divides for div_id in list(gdir.divide_ids): # For each flowline compute the apparent MB # For div 0 it is kind of artificial but this is for validation df = pd.read_csv(gdir.get_filepath('local_mustar'), div_id=div_id) fls = [] if div_id == 0: for i in gdir.divide_ids: fls.extend(gdir.read_pickle('inversion_flowlines', div_id=i)) else: fls = gdir.read_pickle('inversion_flowlines', div_id=div_id) # Reset flux for fl in fls: fl.flux = np.zeros(len(fl.surface_h)) # Flowlines in order to be sure for fl in fls: y, t, p = mb_yearly_climate_on_height(gdir, fl.surface_h, year_range=yr, flatten=False) fl.set_apparent_mb(np.mean(p, axis=1) - df['mustar']*np.mean(t, axis=1))
def __init__(self, gdir, bias=0.): """ Instanciate.""" super(TstarMassBalanceModel, self).__init__(bias) df = pd.read_csv(gdir.get_filepath('local_mustar', div_id=0)) mu_star = df['mu_star'][0] t_star = df['t_star'][0] # Climate period mu_hp = int(cfg.PARAMS['mu_star_halfperiod']) yr = [t_star - mu_hp, t_star + mu_hp] fls = gdir.read_pickle('model_flowlines') h = np.array([]) for fl in fls: h = np.append(h, fl.surface_h) h = np.linspace(np.min(h) - 200, np.max(h) + 1200, 1000) y, t, p = climate.mb_yearly_climate_on_height(gdir, h, year_range=yr) t = np.mean(t, axis=1) p = np.mean(p, axis=1) mb_on_h = p - mu_star * t self.interp = interp1d(h, mb_on_h) self.t_star = t_star
def __init__(self, gdir, bias=0.): """ Instanciate.""" super(TodayMassBalanceModel, self).__init__(bias) df = pd.read_csv(gdir.get_filepath('local_mustar', div_id=0)) mu_star = df['mu_star'][0] t_star = df['t_star'][0] # Climate period # TODO temporary solution until https://github.com/OGGM/oggm/issues/88 # is implemented fpath = gdir.get_filepath('climate_monthly') with netCDF4.Dataset(fpath, mode='r') as nc: # time time = nc.variables['time'] time = netCDF4.num2date(time[:], time.units) last_yr = time[-1].year yr = [last_yr - 30, last_yr] fls = gdir.read_pickle('model_flowlines') h = np.array([]) for fl in fls: h = np.append(h, fl.surface_h) h = np.linspace(np.min(h) - 100, np.max(h) + 200, 1000) y, t, p = climate.mb_yearly_climate_on_height(gdir, h, year_range=yr) t = np.mean(t, axis=1) p = np.mean(p, axis=1) mb_on_h = p - mu_star * t self.interp = interp1d(h, mb_on_h)
def test_local_mustar(self): hef_file = get_demo_file('Hintereisferner.shp') rgidf = gpd.GeoDataFrame.from_file(hef_file) # loop because for some reason indexing wont work for index, entity in rgidf.iterrows(): gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir) gis.define_glacier_region(gdir, entity=entity) gis.glacier_masks(gdir) centerlines.compute_centerlines(gdir) geometry.initialize_flowlines(gdir) geometry.catchment_area(gdir) geometry.catchment_width_geom(gdir) geometry.catchment_width_correction(gdir) climate.distribute_climate_data([gdir]) climate.mu_candidates(gdir, div_id=0) hef_file = get_demo_file('mbdata_RGI40-11.00897.csv') mbdf = pd.read_csv(hef_file).set_index('YEAR') t_star, bias = climate.t_star_from_refmb(gdir, mbdf['ANNUAL_BALANCE']) t_star = t_star[-1] bias = bias[-1] climate.local_mustar_apparent_mb(gdir, tstar=t_star, bias=bias) df = pd.read_csv(gdir.get_filepath('local_mustar', div_id=0)) mu_ref = gdir.read_pickle('mu_candidates', div_id=0).loc[t_star] np.testing.assert_allclose(mu_ref, df['mu_star'][0], atol=1e-3) # Check for apparent mb to be zeros for i in [0] + list(gdir.divide_ids): fls = gdir.read_pickle('inversion_flowlines', div_id=i) tmb = 0. for fl in fls: self.assertTrue(fl.apparent_mb.shape == fl.widths.shape) tmb += np.sum(fl.apparent_mb * fl.widths) np.testing.assert_allclose(tmb, 0., atol=0.01) if i == 0: continue np.testing.assert_allclose(fls[-1].flux[-1], 0., atol=0.01) # ------ Look for gradient # which years to look at fls = gdir.read_pickle('inversion_flowlines', div_id=0) mb_on_h = np.array([]) h = np.array([]) for fl in fls: y, t, p = climate.mb_yearly_climate_on_height(gdir, fl.surface_h) selind = np.searchsorted(y, mbdf.index) t = np.mean(t[:, selind], axis=1) p = np.mean(p[:, selind], axis=1) mb_on_h = np.append(mb_on_h, p - mu_ref * t) h = np.append(h, fl.surface_h) dfg = pd.read_csv(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(h < 3100) from scipy.stats import linregress slope_obs, _, _, _, _ = linregress(dfg.index, dfg.values) slope_our, _, _, _, _ = linregress(h[pok], mb_on_h[pok]) np.testing.assert_allclose(slope_obs, slope_our, rtol=0.1)
def test_yearly_mb_climate(self): hef_file = get_demo_file('Hintereisferner.shp') rgidf = gpd.GeoDataFrame.from_file(hef_file) # loop because for some reason indexing wont work gdirs = [] for index, entity in rgidf.iterrows(): gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir) gis.define_glacier_region(gdir, entity=entity) gdirs.append(gdir) climate.distribute_climate_data(gdirs) nc_r = netCDF4.Dataset(get_demo_file('histalp_merged_hef.nc')) ref_h = nc_r.variables['hgt'][1, 1] ref_p = nc_r.variables['prcp'][:, 1, 1] ref_p *= cfg.PARAMS['prcp_scaling_factor'] ref_t = nc_r.variables['temp'][:, 1, 1] ref_t = np.where(ref_t < 0, 0, ref_t) nc_r.close() # NORMAL -------------------------------------------------------------- hgts = np.array([ref_h, ref_h, -8000, 8000]) years, temp, prcp = climate.mb_yearly_climate_on_height(gdir, hgts) ref_nt = 202 self.assertTrue(len(years) == ref_nt) self.assertTrue(temp.shape == (4, ref_nt)) self.assertTrue(prcp.shape == (4, ref_nt)) yr = [1802, 1802] years, temp, prcp = climate.mb_yearly_climate_on_height(gdir, hgts, year_range=yr) ref_nt = 1 self.assertTrue(len(years) == ref_nt) self.assertTrue(years == 1802) self.assertTrue(temp.shape == (4, ref_nt)) self.assertTrue(prcp.shape == (4, ref_nt)) np.testing.assert_allclose(temp[0, :], np.sum(ref_t[0:12])) np.testing.assert_allclose(temp[0, :], temp[1, :]) np.testing.assert_allclose(prcp[0, :], prcp[1, :]) np.testing.assert_allclose(prcp[3, :], np.sum(ref_p[0:12])) np.testing.assert_allclose(prcp[2, :], np.sum(ref_p[0:12]) * 0) np.testing.assert_allclose(temp[3, :], np.sum(ref_p[0:12]) * 0) yr = [1803, 1804] years, temp, prcp = climate.mb_yearly_climate_on_height(gdir, hgts, year_range=yr) ref_nt = 2 self.assertTrue(len(years) == ref_nt) np.testing.assert_allclose(years, yr) self.assertTrue(temp.shape == (4, ref_nt)) self.assertTrue(prcp.shape == (4, ref_nt)) np.testing.assert_allclose(prcp[2, :], [0, 0]) np.testing.assert_allclose(temp[3, :], [0, 0]) # FLATTEN ------------------------------------------------------------- hgts = np.array([ref_h, ref_h, -8000, 8000]) years, temp, prcp = climate.mb_yearly_climate_on_height(gdir, hgts, flatten=True) ref_nt = 202 self.assertTrue(len(years) == ref_nt) self.assertTrue(temp.shape == (ref_nt, )) self.assertTrue(prcp.shape == (ref_nt, )) yr = [1802, 1802] hgts = np.array([ref_h]) years, temp, prcp = climate.mb_yearly_climate_on_height(gdir, hgts, year_range=yr, flatten=True) ref_nt = 1 self.assertTrue(len(years) == ref_nt) self.assertTrue(years == 1802) self.assertTrue(temp.shape == (ref_nt, )) self.assertTrue(prcp.shape == (ref_nt, )) np.testing.assert_allclose(temp[:], np.sum(ref_t[0:12])) yr = [1802, 1802] hgts = np.array([8000]) years, temp, prcp = climate.mb_yearly_climate_on_height(gdir, hgts, year_range=yr, flatten=True) np.testing.assert_allclose(prcp[:], np.sum(ref_p[0:12]))