def test_distribute(self): hef_file = get_demo_file("Hintereisferner.shp") entity = gpd.GeoDataFrame.from_file(hef_file).iloc[0] 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.process_histalp_nonparallel([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, prcp_fac = 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, prcp_fac=prcp_fac) # OK. Values from Fischer and Kuhn 2013 # Area: 8.55 # meanH = 67+-7 # Volume = 0.573+-0.063 # maxH = 242+-13 inversion.prepare_for_inversion(gdir) ref_v = 0.573 * 1e9 def to_optimize(x): glen_a = cfg.A * x[0] fs = cfg.FS * x[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a) return (v - ref_v) ** 2 import scipy.optimize as optimization out = optimization.minimize(to_optimize, [1, 1], bounds=((0.01, 10), (0.01, 10)), tol=1e-1)["x"] glen_a = cfg.A * out[0] fs = cfg.FS * out[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) np.testing.assert_allclose(ref_v, v) inversion.distribute_thickness(gdir, how="per_altitude", add_nc_name=True) inversion.distribute_thickness(gdir, how="per_interpolation", add_slope=False, add_nc_name=True) grids_file = gdir.get_filepath("gridded_data") with netCDF4.Dataset(grids_file) as nc: t1 = nc.variables["thickness_per_altitude"][:] t2 = nc.variables["thickness_per_interpolation"][:] np.testing.assert_allclose(np.sum(t1), np.sum(t2)) if not HAS_NEW_GDAL: np.testing.assert_allclose(np.max(t1), np.max(t2), atol=30)
def init_hef(reset=False, border=40, invert_with_sliding=True, invert_with_rectangular=True): from oggm.core.preprocessing import gis, centerlines, geometry from oggm.core.preprocessing import climate, inversion import oggm import oggm.cfg as cfg from oggm.utils import get_demo_file # test directory testdir = os.path.join(cfg.PATHS['test_dir'], 'tmp_border{}'.format(border)) if not invert_with_sliding: testdir += '_withoutslide' if not invert_with_rectangular: testdir += '_withoutrectangular' if not os.path.exists(testdir): os.makedirs(testdir) reset = True # Init cfg.initialize() cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') cfg.PATHS['climate_file'] = get_demo_file('histalp_merged_hef.nc') cfg.PARAMS['border'] = border cfg.PARAMS['use_optimized_inversion_params'] = True hef_file = get_demo_file('Hintereisferner_RGI5.shp') entity = gpd.GeoDataFrame.from_file(hef_file).iloc[0] gdir = oggm.GlacierDirectory(entity, base_dir=testdir, reset=reset) if not gdir.has_file('inversion_params'): reset = True gdir = oggm.GlacierDirectory(entity, base_dir=testdir, reset=reset) if not reset: return gdir gis.define_glacier_region(gdir, entity=entity) gis.glacier_masks(gdir) centerlines.compute_centerlines(gdir) centerlines.compute_downstream_lines(gdir) geometry.initialize_flowlines(gdir) centerlines.compute_downstream_bedshape(gdir) geometry.catchment_area(gdir) geometry.catchment_intersections(gdir) geometry.catchment_width_geom(gdir) geometry.catchment_width_correction(gdir) climate.process_histalp_nonparallel([gdir]) climate.mu_candidates(gdir, div_id=0) mbdf = gdir.get_ref_mb_data()['ANNUAL_BALANCE'] res = climate.t_star_from_refmb(gdir, mbdf) climate.local_mustar_apparent_mb(gdir, tstar=res['t_star'][-1], bias=res['bias'][-1], prcp_fac=res['prcp_fac']) inversion.prepare_for_inversion(gdir, add_debug_var=True, invert_with_rectangular=invert_with_rectangular) ref_v = 0.573 * 1e9 if invert_with_sliding: def to_optimize(x): # For backwards compat _fd = 1.9e-24 * x[0] glen_a = (cfg.N+2) * _fd / 2. fs = 5.7e-20 * x[1] v, _ = inversion.mass_conservation_inversion(gdir, fs=fs, glen_a=glen_a) return (v - ref_v)**2 out = optimization.minimize(to_optimize, [1, 1], bounds=((0.01, 10), (0.01, 10)), tol=1e-4)['x'] _fd = 1.9e-24 * out[0] glen_a = (cfg.N+2) * _fd / 2. fs = 5.7e-20 * out[1] v, _ = inversion.mass_conservation_inversion(gdir, fs=fs, glen_a=glen_a, write=True) else: def to_optimize(x): glen_a = cfg.A * x[0] v, _ = inversion.mass_conservation_inversion(gdir, fs=0., glen_a=glen_a) return (v - ref_v)**2 out = optimization.minimize(to_optimize, [1], bounds=((0.01, 10),), tol=1e-4)['x'] glen_a = cfg.A * out[0] fs = 0. v, _ = inversion.mass_conservation_inversion(gdir, fs=fs, glen_a=glen_a, write=True) d = dict(fs=fs, glen_a=glen_a) d['factor_glen_a'] = out[0] try: d['factor_fs'] = out[1] except IndexError: d['factor_fs'] = 0. gdir.write_pickle(d, 'inversion_params') # filter inversion.filter_inversion_output(gdir) inversion.distribute_thickness(gdir, how='per_altitude', add_nc_name=True) inversion.distribute_thickness(gdir, how='per_interpolation', add_slope=False, smooth=False, add_nc_name=True) return gdir
def init_hef(reset=False, border=40, invert_with_sliding=True, invert_with_rectangular=True): from oggm.core.preprocessing import gis, centerlines, geometry from oggm.core.preprocessing import climate, inversion import oggm import oggm.cfg as cfg from oggm.utils import get_demo_file # test directory testdir = os.path.join(cfg.PATHS['test_dir'], 'tmp_border{}'.format(border)) if not invert_with_sliding: testdir += '_withoutslide' if not invert_with_rectangular: testdir += '_withoutrectangular' if not os.path.exists(testdir): os.makedirs(testdir) reset = True # Init cfg.initialize() cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') cfg.PATHS['climate_file'] = get_demo_file('histalp_merged_hef.nc') cfg.PARAMS['border'] = border hef_file = get_demo_file('Hintereisferner_RGI5.shp') entity = gpd.GeoDataFrame.from_file(hef_file).iloc[0] gdir = oggm.GlacierDirectory(entity, base_dir=testdir, reset=reset) if not gdir.has_file('inversion_params'): reset = True gdir = oggm.GlacierDirectory(entity, base_dir=testdir, reset=reset) if not reset: return gdir gis.define_glacier_region(gdir, entity=entity) gis.glacier_masks(gdir) centerlines.compute_centerlines(gdir) centerlines.compute_downstream_lines(gdir) geometry.initialize_flowlines(gdir) geometry.catchment_area(gdir) geometry.catchment_intersections(gdir) geometry.catchment_width_geom(gdir) geometry.catchment_width_correction(gdir) climate.process_histalp_nonparallel([gdir]) climate.mu_candidates(gdir, div_id=0) mbdf = gdir.get_ref_mb_data()['ANNUAL_BALANCE'] res = climate.t_star_from_refmb(gdir, mbdf) climate.local_mustar_apparent_mb(gdir, tstar=res['t_star'][-1], bias=res['bias'][-1], prcp_fac=res['prcp_fac']) inversion.prepare_for_inversion( gdir, add_debug_var=True, invert_with_rectangular=invert_with_rectangular) ref_v = 0.573 * 1e9 if invert_with_sliding: def to_optimize(x): # For backwards compat _fd = 1.9e-24 * x[0] glen_a = (cfg.N + 2) * _fd / 2. fs = 5.7e-20 * x[1] v, _ = inversion.mass_conservation_inversion(gdir, fs=fs, glen_a=glen_a) return (v - ref_v)**2 out = optimization.minimize(to_optimize, [1, 1], bounds=((0.01, 10), (0.01, 10)), tol=1e-4)['x'] _fd = 1.9e-24 * out[0] glen_a = (cfg.N + 2) * _fd / 2. fs = 5.7e-20 * out[1] v, _ = inversion.mass_conservation_inversion(gdir, fs=fs, glen_a=glen_a, write=True) else: def to_optimize(x): glen_a = cfg.A * x[0] v, _ = inversion.mass_conservation_inversion(gdir, fs=0., glen_a=glen_a) return (v - ref_v)**2 out = optimization.minimize(to_optimize, [1], bounds=((0.01, 10), ), tol=1e-4)['x'] glen_a = cfg.A * out[0] fs = 0. v, _ = inversion.mass_conservation_inversion(gdir, fs=fs, glen_a=glen_a, write=True) d = dict(fs=fs, glen_a=glen_a) d['factor_glen_a'] = out[0] try: d['factor_fs'] = out[1] except IndexError: d['factor_fs'] = 0. gdir.write_pickle(d, 'inversion_params') # filter inversion.filter_inversion_output(gdir) inversion.distribute_thickness(gdir, how='per_altitude', add_nc_name=True) inversion.distribute_thickness(gdir, how='per_interpolation', add_slope=False, smooth=False, add_nc_name=True) return gdir
def init_hef(reset=False, border=40, invert_with_sliding=True): # test directory testdir = TESTDIR_BASE + '_border{}'.format(border) if not invert_with_sliding: testdir += '_withoutslide' if not os.path.exists(testdir): os.makedirs(testdir) reset = True if not os.path.exists(os.path.join(testdir, 'RGI40-11.00897')): reset = True if not os.path.exists(os.path.join(testdir, 'RGI40-11.00897', 'inversion_params.pkl')): reset = True # Init cfg.initialize() cfg.set_divides_db(get_demo_file('HEF_divided.shp')) cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') cfg.PATHS['climate_file'] = get_demo_file('histalp_merged_hef.nc') cfg.PARAMS['border'] = border # loop because for some reason indexing wont work hef_file = get_demo_file('Hintereisferner.shp') rgidf = gpd.GeoDataFrame.from_file(hef_file) for index, entity in rgidf.iterrows(): gdir = oggm.GlacierDirectory(entity, base_dir=testdir, reset=reset) if not reset: return gdir gis.define_glacier_region(gdir, entity=entity) gis.glacier_masks(gdir) centerlines.compute_centerlines(gdir) centerlines.compute_downstream_lines(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']) climate.local_mustar_apparent_mb(gdir, tstar=t_star[-1], bias=bias[-1]) inversion.prepare_for_inversion(gdir) ref_v = 0.573 * 1e9 if invert_with_sliding: def to_optimize(x): # For backwards compat _fd = 1.9e-24 * x[0] glen_a = (cfg.N+2) * _fd / 2. fs = 5.7e-20 * x[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a) return (v - ref_v)**2 import scipy.optimize as optimization out = optimization.minimize(to_optimize, [1, 1], bounds=((0.01, 10), (0.01, 10)), tol=1e-4)['x'] _fd = 1.9e-24 * out[0] glen_a = (cfg.N+2) * _fd / 2. fs = 5.7e-20 * out[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) else: def to_optimize(x): glen_a = cfg.A * x[0] v, _ = inversion.invert_parabolic_bed(gdir, fs=0., glen_a=glen_a) return (v - ref_v)**2 import scipy.optimize as optimization out = optimization.minimize(to_optimize, [1], bounds=((0.01, 10),), tol=1e-4)['x'] glen_a = cfg.A * out[0] fs = 0. v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) d = dict(fs=fs, glen_a=glen_a) d['factor_glen_a'] = out[0] try: d['factor_fs'] = out[1] except IndexError: d['factor_fs'] = 0. gdir.write_pickle(d, 'inversion_params') inversion.distribute_thickness(gdir, how='per_altitude', add_nc_name=True) inversion.distribute_thickness(gdir, how='per_interpolation', add_slope=False, smooth=False, add_nc_name=True) return gdir
def init_hef(reset=False, border=40, invert_with_sliding=True): from oggm.core.preprocessing import gis, centerlines, geometry from oggm.core.preprocessing import climate, inversion import oggm import oggm.cfg as cfg from oggm.utils import get_demo_file # test directory testdir = TESTDIR_BASE + "_border{}".format(border) if not invert_with_sliding: testdir += "_withoutslide" if not os.path.exists(testdir): os.makedirs(testdir) reset = True if not os.path.exists(os.path.join(testdir, "RGI40-11.00897")): reset = True if not os.path.exists(os.path.join(testdir, "RGI40-11.00897", "inversion_params.pkl")): reset = True # Init cfg.initialize() cfg.PATHS["dem_file"] = get_demo_file("hef_srtm.tif") cfg.PATHS["climate_file"] = get_demo_file("histalp_merged_hef.nc") cfg.PARAMS["border"] = border hef_file = get_demo_file("Hintereisferner.shp") entity = gpd.GeoDataFrame.from_file(hef_file).iloc[0] gdir = oggm.GlacierDirectory(entity, base_dir=testdir, reset=reset) if not reset: return gdir gis.define_glacier_region(gdir, entity=entity) gis.glacier_masks(gdir) centerlines.compute_centerlines(gdir) centerlines.compute_downstream_lines(gdir) geometry.initialize_flowlines(gdir) geometry.catchment_area(gdir) geometry.catchment_width_geom(gdir) geometry.catchment_width_correction(gdir) climate.process_histalp_nonparallel([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, prcp_fac = climate.t_star_from_refmb(gdir, mbdf["ANNUAL_BALANCE"]) climate.local_mustar_apparent_mb(gdir, tstar=t_star[-1], bias=bias[-1], prcp_fac=prcp_fac) inversion.prepare_for_inversion(gdir) ref_v = 0.573 * 1e9 if invert_with_sliding: def to_optimize(x): # For backwards compat _fd = 1.9e-24 * x[0] glen_a = (cfg.N + 2) * _fd / 2.0 fs = 5.7e-20 * x[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a) return (v - ref_v) ** 2 out = optimization.minimize(to_optimize, [1, 1], bounds=((0.01, 10), (0.01, 10)), tol=1e-4)["x"] _fd = 1.9e-24 * out[0] glen_a = (cfg.N + 2) * _fd / 2.0 fs = 5.7e-20 * out[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) else: def to_optimize(x): glen_a = cfg.A * x[0] v, _ = inversion.invert_parabolic_bed(gdir, fs=0.0, glen_a=glen_a) return (v - ref_v) ** 2 out = optimization.minimize(to_optimize, [1], bounds=((0.01, 10),), tol=1e-4)["x"] glen_a = cfg.A * out[0] fs = 0.0 v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) d = dict(fs=fs, glen_a=glen_a) d["factor_glen_a"] = out[0] try: d["factor_fs"] = out[1] except IndexError: d["factor_fs"] = 0.0 gdir.write_pickle(d, "inversion_params") inversion.distribute_thickness(gdir, how="per_altitude", add_nc_name=True) inversion.distribute_thickness(gdir, how="per_interpolation", add_slope=False, smooth=False, add_nc_name=True) return gdir
def init_hef(reset=False, border=40, invert_with_sliding=True): # test directory testdir = TESTDIR_BASE + '_border{}'.format(border) if not invert_with_sliding: testdir += '_withoutslide' if not os.path.exists(testdir): os.makedirs(testdir) reset = True if not os.path.exists(os.path.join(testdir, 'RGI40-11.00897')): reset = True if not os.path.exists(os.path.join(testdir, 'RGI40-11.00897', 'inversion_params.pkl')): reset = True # Init cfg.initialize() cfg.set_divides_db(get_demo_file('divides_workflow.shp')) cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') cfg.PATHS['climate_file'] = get_demo_file('histalp_merged_hef.nc') cfg.PARAMS['border'] = border hef_file = get_demo_file('Hintereisferner.shp') entity = gpd.GeoDataFrame.from_file(hef_file).iloc[0] gdir = oggm.GlacierDirectory(entity, base_dir=testdir, reset=reset) if not reset: return gdir gis.define_glacier_region(gdir, entity=entity) gis.glacier_masks(gdir) centerlines.compute_centerlines(gdir) centerlines.compute_downstream_lines(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']) climate.local_mustar_apparent_mb(gdir, tstar=t_star[-1], bias=bias[-1]) inversion.prepare_for_inversion(gdir) ref_v = 0.573 * 1e9 if invert_with_sliding: def to_optimize(x): # For backwards compat _fd = 1.9e-24 * x[0] glen_a = (cfg.N+2) * _fd / 2. fs = 5.7e-20 * x[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a) return (v - ref_v)**2 out = optimization.minimize(to_optimize, [1, 1], bounds=((0.01, 10), (0.01, 10)), tol=1e-4)['x'] _fd = 1.9e-24 * out[0] glen_a = (cfg.N+2) * _fd / 2. fs = 5.7e-20 * out[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) else: def to_optimize(x): glen_a = cfg.A * x[0] v, _ = inversion.invert_parabolic_bed(gdir, fs=0., glen_a=glen_a) return (v - ref_v)**2 out = optimization.minimize(to_optimize, [1], bounds=((0.01, 10),), tol=1e-4)['x'] glen_a = cfg.A * out[0] fs = 0. v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) d = dict(fs=fs, glen_a=glen_a) d['factor_glen_a'] = out[0] try: d['factor_fs'] = out[1] except IndexError: d['factor_fs'] = 0. gdir.write_pickle(d, 'inversion_params') inversion.distribute_thickness(gdir, how='per_altitude', add_nc_name=True) inversion.distribute_thickness(gdir, how='per_interpolation', add_slope=False, smooth=False, add_nc_name=True) return gdir
def test_distribute(self): hef_file = get_demo_file('Hintereisferner.shp') entity = gpd.GeoDataFrame.from_file(hef_file).iloc[0] 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) # OK. Values from Fischer and Kuhn 2013 # Area: 8.55 # meanH = 67+-7 # Volume = 0.573+-0.063 # maxH = 242+-13 inversion.prepare_for_inversion(gdir) ref_v = 0.573 * 1e9 def to_optimize(x): glen_a = cfg.A * x[0] fs = cfg.FS * x[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a) return (v - ref_v)**2 import scipy.optimize as optimization out = optimization.minimize(to_optimize, [1, 1], bounds=((0.01, 10), (0.01, 10)), tol=1e-1)['x'] glen_a = cfg.A * out[0] fs = cfg.FS * out[1] v, _ = inversion.invert_parabolic_bed(gdir, fs=fs, glen_a=glen_a, write=True) np.testing.assert_allclose(ref_v, v) inversion.distribute_thickness(gdir, how='per_altitude', add_nc_name=True) inversion.distribute_thickness(gdir, how='per_interpolation', add_slope=False, add_nc_name=True) grids_file = gdir.get_filepath('gridded_data') with netCDF4.Dataset(grids_file) as nc: t1 = nc.variables['thickness_per_altitude'][:] t2 = nc.variables['thickness_per_interpolation'][:] np.testing.assert_allclose(np.sum(t1), np.sum(t2)) if not HAS_NEW_GDAL: np.testing.assert_allclose(np.max(t1), np.max(t2), atol=30)