def test_random_mb_run(): rid = 'RGI60-15.03473' gdir = oggm_compat.single_flowline_glacier_directory(rid, prepro_border=80) mbmod = oggm_compat.RandomLinearMassBalance(gdir, seed=1, sigma_ela=300, h_perc=55) from oggm.core.flowline import robust_model_run flmodel = robust_model_run(gdir, mb_model=mbmod, ys=0, ye=700) # Check that "something" is computed import xarray as xr ds = xr.open_dataset(gdir.get_filepath('model_diagnostics')) assert ds.isel(time=-1).volume_m3 > 0 if do_plot: import matplotlib.pyplot as plt from oggm import graphics graphics.plot_modeloutput_section(flmodel) f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4)) (ds.volume_m3 * 1e-9).plot(ax=ax1) ax1.set_ylabel('Glacier volume (km$^{3}$)') (ds.area_m2 * 1e-6).plot(ax=ax2) ax2.set_ylabel('Glacier area (km$^{2}$)') (ds.length_m * 1e3).plot(ax=ax3) ax3.set_ylabel('Glacier length (km)') plt.tight_layout() plt.show()
def test_random_mb_run(): rid = 'RGI60-15.03473' gdir = oggm_compat.single_flowline_glacier_directory(rid, prepro_border=80) # This initializes the mass balance model, but does not run it mbmod = oggm_compat.RandomLinearMassBalance(gdir, seed=1, sigma_ela=300, h_perc=55) # HERE CAN BE THE LOOP SUCH THAT EVERYTHING IS ALREADY LOADED for i in [1, 2, 3, 4]: # Change the model parameter mbmod.param1 = i # Run the mass balance model with fixed geometry ts_mb = mbmod.get_specific_mb(years=[2000, 2001, 2002]) # Run the glacier flowline model with a mass balance model from oggm.core.flowline import robust_model_run flmodel = robust_model_run(gdir, mb_model=mbmod, ys=0, ye=700) # Check that "something" is computed import xarray as xr ds = xr.open_dataset(gdir.get_filepath('model_diagnostics')) assert ds.isel(time=-1).volume_m3 > 0 if do_plot: import matplotlib.pyplot as plt from oggm import graphics graphics.plot_modeloutput_section(flmodel) f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4)) (ds.volume_m3 * 1e-9).plot(ax=ax1) ax1.set_ylabel('Glacier volume (km$^{3}$)') (ds.area_m2 * 1e-6).plot(ax=ax2) ax2.set_ylabel('Glacier area (km$^{2}$)') (ds.length_m * 1e3).plot(ax=ax3) ax3.set_ylabel('Glacier length (km)') plt.tight_layout() plt.show()
def run_uncertain_random_climate(gdir, nyears=700, output_filesuffix='', sigma_t=None, sigma_p=None, sigma_smb=None, rdn_temp_bias_seed=None, rdn_prcp_bias_seed=None, rdn_bias_seed=None, orig_mb_tbias=0, orig_mb_pbias=1, orig_mb_sbias=0, **kwargs): """Test stuff Parameters ---------- gdir nyears output_filesuffix rdn_temp_bias_seed rdn_prcp_bias_seed rdn_bias_seed kwargs Returns ------- """ # Find the optimal bias for a balanced MB mbref = mbmods.PastMassBalance(gdir) h, w = gdir.get_inversion_flowline_hw() cyrs = np.unique(mbref.years) def to_minimize(x): mbref.bias = x return np.mean(mbref.get_specific_mb(h, w, cyrs)) opti_bias = optimization.brentq(to_minimize, -10000, 10000, xtol=0.01) mbref.bias = opti_bias assert np.allclose(np.mean(mbref.get_specific_mb(h, w, cyrs)), 0, atol=0.01) rdf = pd.DataFrame(index=cyrs) for y in cyrs: rdf.loc[y, 'SMB'] = mbref.get_specific_mb(h, w, year=y) t, _, p, _ = mbref.get_annual_climate([np.mean(h)], year=y) rdf.loc[y, 'TEMP'] = t rdf.loc[y, 'PRCP'] = p if sigma_smb is None: sigma_smb = rdf.std()['SMB'] / 2 if sigma_t is None: sigma_t = rdf.std()['TEMP'] / 2 if sigma_p is None: sigma_p = (rdf.std() / rdf.mean())['PRCP'] / 2 mb = mbmods.RandomMassBalance(gdir, all_years=True, seed=0) mb.mbmod.temp_bias = orig_mb_tbias mb.mbmod.prcp_bias = orig_mb_pbias mb.mbmod.bias = orig_mb_sbias + opti_bias rmb = mbmods.UncertainMassBalance(mb, rdn_temp_bias_seed=rdn_temp_bias_seed, rdn_temp_bias_sigma=sigma_t, rdn_prcp_bias_seed=rdn_prcp_bias_seed, rdn_prcp_bias_sigma=sigma_p, rdn_bias_seed=rdn_bias_seed, rdn_bias_sigma=sigma_smb) return robust_model_run(gdir, output_filesuffix=output_filesuffix, mb_model=rmb, ys=0, ye=nyears, **kwargs)
def run_and_store_from_disk(rgi, histalp_storage, commit_storage, y0=1999, years=300, seed=None, unique_samples=False, halfsize=15): for i in np.arange(999): # Local working directory (where OGGM will write its output) storage_dir = os.path.join(histalp_storage, rgi, '{:02d}'.format(i), rgi[:8], rgi[:11], rgi) new_dir = os.path.join(cfg.PATHS['working_dir'], 'per_glacier', rgi[:8], rgi[:11], rgi) # make sure directory is empty: try: shutil.rmtree(new_dir) except FileNotFoundError: pass # if path does not exist, we handled all ensemble members: try: shutil.copytree(storage_dir, new_dir) except FileNotFoundError: log.info('processed {:02d} ensemble members'.format(i)) break gdir = GlacierDirectory(rgi) pdict = gdir.get_climate_info()['ensemble_calibration'] cfg.PARAMS['prcp_scaling_factor'] = pdict['prcp_scaling_factor'] default_glena = 2.4e-24 cfg.PARAMS['glen_a'] = pdict['glena_factor'] * default_glena cfg.PARAMS['inversion_glen_a'] = pdict['glena_factor'] * default_glena mbbias = pdict['mbbias'] tmp_mod = FileModel( gdir.get_filepath('model_run', filesuffix='_histalp_{:02d}'.format(i))) tmp_mod.run_until(tmp_mod.last_yr) mb = MultipleFlowlineMassBalance(gdir, mb_model_class=RandomMassBalance, filename='climate_monthly', bias=mbbias, y0=y0, seed=seed, unique_samples=unique_samples, halfsize=halfsize) robust_model_run(gdir, output_filesuffix='commitment{:04d}_{:02d}'.format( y0, i), mb_model=mb, ys=0, ye=years, init_model_fls=tmp_mod.fls) fn1 = 'model_diagnostics_commitment{:04d}_{:02d}.nc'.format(y0, i) shutil.copyfile( gdir.get_filepath('model_diagnostics', filesuffix='commitment{:04d}_{:02d}'.format( y0, i)), os.path.join(commit_storage, fn1)) fn4 = 'model_run_commitment{:04d}_{:02d}.nc'.format(y0, i) shutil.copyfile( gdir.get_filepath('model_run', filesuffix='commitment{:04d}_{:02d}'.format( y0, i)), os.path.join(commit_storage, fn4))