def get_mean_temps_2k(rgi, return_prcp): from oggm import cfg, utils, workflow, tasks from oggm.core.massbalance import PastMassBalance # Initialize OGGM cfg.initialize() wd = utils.gettempdir(reset=True) cfg.PATHS['working_dir'] = wd utils.mkdir(wd, reset=True) cfg.PARAMS['baseline_climate'] = 'HISTALP' # and set standard histalp values cfg.PARAMS['temp_melt'] = -1.75 cfg.PARAMS['prcp_scaling_factor'] = 1.75 gdir = workflow.init_glacier_regions(rgidf=rgi.split('_')[0], from_prepro_level=3, prepro_border=10)[0] # run histalp climate on glacier! tasks.process_histalp_data(gdir) f = gdir.get_filepath('climate_historical') with utils.ncDataset(f) as nc: refhgt = nc.ref_hgt mb = PastMassBalance(gdir, check_calib_params=False) df = pd.DataFrame() df2 = pd.DataFrame() for y in np.arange(1870, 2015): for i in np.arange(9, 12): flyear = utils.date_to_floatyear(y, i) tmp = mb.get_monthly_climate([refhgt], flyear)[0] df.loc[y, i] = tmp.mean() if return_prcp: for i in np.arange(3, 6): flyear = utils.date_to_floatyear(y, i) pcp = mb.get_monthly_climate([refhgt], flyear)[3] df2.loc[y, i] = tmp.mean() t99 = df.loc[1984:2014, :].mean().mean() t85 = df.loc[1870:1900, :].mean().mean() t2k = df.loc[1900:2000, :].mean().mean() if return_prcp: p99 = df2.loc[1984:2014, :].mean().mean() p85 = df2.loc[1870:1900, :].mean().mean() p2k = df2.loc[1900:2000, :].mean().mean() return t85, t99, t2k, p85, p99, p2k return t85, t99, t2k
def gdir(): cfg.initialize() test_dir = '/home/lilianschuster/Schreibtisch/PhD/oggm_files/MBsandbox_tests' if not os.path.exists(test_dir): test_dir = utils.gettempdir(dirname='OGGM_MBsandbox_test', reset=True) cfg.PATHS['working_dir'] = test_dir base_url = ('https://cluster.klima.uni-bremen.de/~oggm/gdirs/oggm_v1.4/' 'L1-L2_files/elev_bands') df = ['RGI60-11.00897'] gdirs = workflow.init_glacier_directories(df, from_prepro_level=2, prepro_border=10, prepro_base_url=base_url, prepro_rgi_version='62') return gdirs[0]
def test_geodetic_glaciologicalMB_HEF(): cfg.initialize() cfg.PARAMS['use_multiprocessing'] = False working_dir = '/home/lilianschuster/Schreibtisch/PhD/oggm_files/oneFlowline' # this needs to be changed if working on another computer if not os.path.exists(working_dir): working_dir = utils.gettempdir(dirname='OGGM_mb_type_intercomparison', reset=True) cfg.PATHS['working_dir'] = working_dir # use Huss flowlines base_url = ('https://cluster.klima.uni-bremen.de/~oggm/gdirs/oggm_v1.4/' 'L1-L2_files/elev_bands') # get HEF glacier df = utils.get_rgi_glacier_entities(['RGI60-11.00897']) gdirs = workflow.init_glacier_directories(df, from_prepro_level=2, prepro_border=10, prepro_base_url=base_url, prepro_rgi_version='62') gd = gdirs[0] h, w = gd.get_inversion_flowline_hw() fls = gd.read_pickle('inversion_flowlines') cfg.PARAMS['baseline_climate'] = 'ERA5dr' oggm.shop.ecmwf.process_ecmwf_data(gd, dataset='ERA5dr') ###################### # test area is similar to OGGM gdir HEF area np.testing.assert_allclose(pd_geodetic_alps.loc['RGI60-11.00897'].area, gd.rgi_area_km2, rtol=0.01) glaciological_mb_data_HEF_mean = gd.get_ref_mb_data()['ANNUAL_BALANCE'].loc[2000:].mean() #.sel(2000,2020) # test if mass balance in period 2000 and 2020 of glaciological method is similar to the geodetic one np.testing.assert_allclose(pd_geodetic_alps.loc['RGI60-11.00897'].dmdtda*1000, glaciological_mb_data_HEF_mean, rtol = 0.05 )
def gdir(): """ Provides a copy of the base Hintereisenferner glacier directory in a case directory specific to the current test class using the single elev_bands as flowlines. All cases in the test class will use the same copy of this glacier directory. """ cfg.initialize() cfg.PARAMS['use_multiprocessing'] = False test_dir = '/home/lilianschuster/Schreibtisch/PhD/oggm_files/MBsandbox_tests' if not os.path.exists(test_dir): test_dir = utils.gettempdir(dirname='OGGM_MBsandbox_test', reset=True) cfg.PATHS['working_dir'] = test_dir base_url = ('https://cluster.klima.uni-bremen.de/~oggm/gdirs/oggm_v1.4/' 'L1-L2_files/elev_bands') df = ['RGI60-11.00897'] gdirs = workflow.init_glacier_directories(df, from_prepro_level=2, prepro_border=10, prepro_base_url=base_url, prepro_rgi_version='62') return gdirs[0]
def test_ssp585_problem(): from oggm import cfg, utils, workflow, tasks, graphics from oggm.core import massbalance, flowline, climate import logging log = logging.getLogger(__name__) cfg.initialize() cfg.PATHS['working_dir'] = utils.gettempdir(dirname='test', reset=False) gdirs = workflow.init_glacier_directories( ['RGI60-11.03238'], #'RGI60-11.03677'], #'RGI60-11.03471'], from_prepro_level=2, prepro_border=10, prepro_base_url=base_url, prepro_rgi_version='62', ) gdir = gdirs[0] cfg.PARAMS['hydro_month_nh'] = 1 ssp = 'ssp585' process_w5e5_data(gdir, temporal_resol='monthly', climate_type='WFDE5_CRU') process_isimip_data(gdir, ensemble=ensemble, ssp=ssp, climate_historical_filesuffix='_monthly_WFDE5_CRU', temporal_resol='monthly')
'entity_intersects.shp') new_intersects.to_file(entity_intersects_path) # set intersect file to use cfg.set_intersects_db(new_intersects) # year where outlines are valid outline_year = os.path.basename(fp)[:4] # check geometries #for idx, row in entity.iterrows(): # if entity.geometry.iloc[idx].type != 'Polygon': # print(row['RGIId'] + row.geometry.type) # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('User_past_mb', reset=True) utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR #cfg.PATHS['working_dir'] = utils.gettempdir('user', reset=True) #cfg.PATHS['working_dir'] = utils.gettempdir(dirname='OGGM-GettingStarted', reset=True) # glacier directory gdirs = workflow.init_glacier_directories( entity ) #, prepro_base_url=base_url) # <- from prepro_level uses preprocessed files #rgi_id = gdir.rgi_id #gdir.rgi_date = outline_year # update gdir outline year for gdir in gdirs:
from oggm import utils, workflow, tasks # For timing the run import time start = time.time() # Module logger log = logging.getLogger(__name__) # Initialize OGGM and set up the default run parameters cfg.initialize(logging_level='WORKFLOW') rgi_version = '61' rgi_region = '11' # Region Central Europe # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('OGGM_Inversion') utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR # RGI file path = utils.get_rgi_region_file(rgi_region, version=rgi_version) rgidf = gpd.read_file(path) # Select the glaciers in the Pyrenees rgidf = rgidf.loc[rgidf['O2Region'] == '2'] # Sort for more efficient parallel computing rgidf = rgidf.sort_values('Area', ascending=False) log.workflow('Starting OGGM inversion run') log.workflow('Number of glaciers: {}'.format(len(rgidf)))
# Locals import oggm.cfg as cfg from oggm import tasks, utils, workflow from oggm.workflow import execute_entity_task # Module logger log = logging.getLogger(__name__) # For timing the run start = time.time() # Initialize OGGM and set up the run parameters cfg.initialize() # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('OGGM_precalibrated_run') cfg.PATHS['working_dir'] = WORKING_DIR # Use multiprocessing? cfg.PARAMS['use_multiprocessing'] = True # Initialize from existing directories, no need for shapefiles gdirs = workflow.init_glacier_regions() log.info('Starting OGGM run') log.info('Number of glaciers: {}'.format(len(gdirs))) # We can step directly to a new experiment! # Random climate representative for the recent climate (1985-2015) # This is a kind of "commitment" run execute_entity_task(tasks.run_random_climate,
import geopandas as gpd import oggm from oggm import cfg, tasks from oggm.utils import get_demo_file, gettempdir cfg.initialize() cfg.set_intersects_db(get_demo_file('rgi_intersect_oetztal.shp')) cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') base_dir = gettempdir('Flowlines_Docs') entity = gpd.read_file(get_demo_file('HEF_MajDivide.shp')).iloc[0] gdir = oggm.GlacierDirectory(entity, base_dir=base_dir) tasks.define_glacier_region(gdir, entity=entity) tasks.glacier_masks(gdir) tasks.compute_centerlines(gdir) tasks.initialize_flowlines(gdir) tasks.compute_downstream_line(gdir)
# Imports from os import path import geopandas as gpd import matplotlib.pyplot as plt from oggm.utils import get_demo_file, gettempdir # Local working directory (where OGGM wrote its output) WORKING_DIR = gettempdir('OGGM_Rofental') # Plot: the basin, the outlines and the centerlines basin = gpd.read_file(get_demo_file('rofental_hydrosheds.shp')) rgi = gpd.read_file(path.join(WORKING_DIR, 'rgi_rofental.shp')) centerlines = gpd.read_file(path.join(WORKING_DIR, 'glacier_centerlines.shp')) f, ax = plt.subplots() basin.plot(ax=ax, color='k', alpha=0.2) rgi.plot(ax=ax, color='C0') centerlines.plot(ax=ax, color='C3') plt.title('Rofental glaciers and centerlines') plt.tight_layout() plt.show()
# Locals from oggm import cfg, utils, workflow from oggm.core import climate, gis import oggm_vas as vascaling # Initialize OGGM and set up the default run parameters vascaling.initialize() rgi_version = '62' # 10 is only for OGGM-VAS, OGGM needs 80 to run cfg.PARAMS['border'] = 80 # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('moritz') utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR # RGI glaciers: her you usually pick a region or yours rgi_ids = ['RGI60-11.00897', 'RGI60-11.00892'] rgi_region = (rgi_ids[0].split('-')[-1]).split('.')[0] # The important bit: update with the folder with CRU files in it when available base_url = 'https://cluster.klima.uni-bremen.de/~oggm/gdirs/oggm_v1.4/' \ 'L3-L5_files/RGIV62_fleb_qc3_CRU_pcp2.5' # Go - get the pre-processed glacier directories gdirs = workflow.init_glacier_directories(rgi_ids, from_prepro_level=3, prepro_base_url=base_url, prepro_rgi_version=rgi_version)
gdf = gpd.read_file(fp53) # read one of the outline files to use as 'default' # exclude glaciers smaller than 0.1 km2 #gdf = entity[entity['Area'] >= 0.1] # select or deselect ids excludeIDs = [ 'RGI60-05.01920', 'RGI60-05.01987_1', 'RGI60-05.02213', 'RGI60-05.02328_1', 'RGI60-05.02280_1' ] gdf = gdf[gdf.RGIId.isin(excludeIDs)] #outline_year = os.path.basename(fp)[:4] # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('User_surf_change', reset=True) utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR # empty GeoDataFrame to save flowlines flowlines = gpd.GeoDataFrame() for rgiid in gdf['RGIId']: # select glacier entity = gdf[gdf['RGIId'] == rgiid] # glacier directory gdirs = workflow.init_glacier_directories( entity ) #, prepro_base_url=base_url) # <- from prepro_level uses preprocessed files
def run_and_plot_merged_montmine(pout): # Set-up cfg.initialize(logging_level='WORKFLOW') cfg.PATHS['working_dir'] = utils.gettempdir(dirname='OGGM-merging', reset=True) # Use a suitable border size for your domain cfg.PARAMS['border'] = 80 cfg.PARAMS['use_intersects'] = False montmine = workflow.init_glacier_directories(['RGI60-11.02709'], from_prepro_level=3)[0] gdirs = workflow.init_glacier_directories(['RGI60-11.02709', 'RGI60-11.02715'], from_prepro_level=3) workflow.execute_entity_task(tasks.init_present_time_glacier, gdirs) gdirs_merged = workflow.merge_glacier_tasks(gdirs, 'RGI60-11.02709', return_all=False, filename='climate_monthly', buffer=2.5) # plot centerlines fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[20, 10]) plot_centerlines(montmine, ax=ax1, use_flowlines=True) xt = ax1.get_xticks() ax1.set_xticks(xt[::2]) ax1.tick_params(axis='both', which='major', labelsize=20) ax1.set_title('entity glacier', fontsize=24) plot_centerlines(gdirs_merged, ax=ax2, use_model_flowlines=True) ax2.tick_params(axis='both', which='major', labelsize=20) ax2.set_title('merged with Glacier de Ferpecle', fontsize=24) axs = fig.get_axes() axs[3].remove() axs[2].tick_params(axis='y', labelsize=16) axs[2].set_ylabel('Altitude [m]', fontsize=18) fig.suptitle('Glacier du Mont Mine', fontsize=24) fig.subplots_adjust(left=0.04, right=0.99, bottom=0.08, top=0.89, wspace=0.3) fn = os.path.join(pout, 'merged_montmine.png') fig.savefig(fn) # run glaciers with negative t bias # some model settings years = 125 tbias = -1.5 # model Mont Mine glacier as entity and complile the output tasks.run_constant_climate(montmine, nyears=years, output_filesuffix='_entity', temperature_bias=tbias) ds_entity = utils.compile_run_output([montmine], path=False, filesuffix='_entity') # model the merged glacier and complile the output tasks.run_constant_climate(gdirs_merged, nyears=years, output_filesuffix='_merged', temperature_bias=tbias, climate_filename='climate_monthly') ds_merged = utils.compile_run_output([gdirs_merged], path=False, filesuffix='_merged') # # bring them to same size again tbias = -2.2 years = 125 tasks.run_constant_climate(montmine, nyears=years, output_filesuffix='_entity1', temperature_bias=tbias) ds_entity1 = utils.compile_run_output([montmine], path=False, filesuffix='_entity1') # and let them shrink again # some model settings tbias = -0.5 years = 100 # load the previous entity run tmp_mine = FileModel( montmine.get_filepath('model_run', filesuffix='_entity1')) tmp_mine.run_until(years) tasks.run_constant_climate(montmine, nyears=years, output_filesuffix='_entity2', init_model_fls=tmp_mine.fls, temperature_bias=tbias) ds_entity2 = utils.compile_run_output([montmine], path=False, filesuffix='_entity2') # model the merged glacier and complile the output tmp_merged = FileModel( gdirs_merged.get_filepath('model_run', filesuffix='_merged')) tmp_merged.run_until(years) tasks.run_constant_climate(gdirs_merged, nyears=years, output_filesuffix='_merged2', init_model_fls=tmp_merged.fls, temperature_bias=tbias, climate_filename='climate_monthly') ds_merged2 = utils.compile_run_output([gdirs_merged], path=False, filesuffix='_merged2') fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[20, 7]) dse = ds_entity.length.to_series().rolling(5, center=True).mean() dsm = ds_merged.length.to_series().rolling(5, center=True).mean() ax1.plot(dse.values, 'C1', label='Entity glacier', linewidth=3) ax1.plot(dsm.values, 'C2', label='Merged glacier', linewidth=3) ax1.set_xlabel('Simulation time [yr]', fontsize=20) ax1.set_ylabel('Glacier length[m]', fontsize=20) ax1.grid(True) ax1.legend(loc=2, fontsize=18) dse2 = ds_entity2.length.to_series().rolling(5, center=True).mean() dsm2 = ds_merged2.length.to_series().rolling(5, center=True).mean() ax2.plot(dse2.values, 'C1', label='Entity glacier', linewidth=3) ax2.plot(dsm2.values, 'C2', label='Merged glacier', linewidth=3) ax2.set_xlabel('Simulation time [yr]', fontsize=22) ax2.set_ylabel('Glacier length [m]', fontsize=22) ax2.grid(True) ax2.legend(loc=1, fontsize=18) ax1.set_xlim([0, 120]) ax2.set_xlim([0, 100]) ax1.set_ylim([7500, 12000]) ax2.set_ylim([7500, 12000]) ax1.tick_params(axis='both', which='major', labelsize=20) ax2.tick_params(axis='both', which='major', labelsize=20) fig.subplots_adjust(left=0.08, right=0.96, bottom=0.11, top=0.93, wspace=0.3) fn = os.path.join(pout, 'merged_montmine_timeseries.png') fig.savefig(fn)
def get_mean_temps_eq(rgi, histalp_storage, comit_storage, ensmembers): from oggm import cfg, utils, GlacierDirectory from oggm.core.massbalance import MultipleFlowlineMassBalance from oggm.core.flowline import FileModel import shutil # 1. get mean surface heights df85 = pd.DataFrame([]) df99 = pd.DataFrame([]) for i in range(ensmembers): fnc1 = os.path.join(comit_storage, rgi, 'model_run_commitment1885_{:02d}.nc'.format(i)) fnc2 = os.path.join(comit_storage, rgi, 'model_run_commitment1999_{:02d}.nc'.format(i)) tmpmod1 = FileModel(fnc1) tmpmod2 = FileModel(fnc2) for j in np.arange(270, 301): tmpmod1.run_until(j) df85.loc[:, '{}{}'.format(i, j)] = tmpmod1.fls[-1].surface_h tmpmod2.run_until(j) df99.loc[:, '{}{}'.format(i, j)] = tmpmod2.fls[-1].surface_h meanhgt99 = df99.mean(axis=1).values meanhgt85 = df85.mean(axis=1).values # 2. get the climate # Initialize OGGM cfg.initialize() wd = utils.gettempdir(reset=True) cfg.PATHS['working_dir'] = wd utils.mkdir(wd, reset=True) cfg.PARAMS['baseline_climate'] = 'HISTALP' # and set standard histalp values cfg.PARAMS['temp_melt'] = -1.75 i = 0 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) shutil.copytree(storage_dir, new_dir) gdir = GlacierDirectory(rgi) mb = MultipleFlowlineMassBalance(gdir, filename='climate_monthly', check_calib_params=False) # need to do the above for every ensemble member if I consider PRECIP! # and set cfg.PARAMS['prcp_scaling_factor'] = pdict['prcp_scaling_factor'] df99_2 = pd.DataFrame() df85_2 = pd.DataFrame() for i in np.arange(9, 12): for y in np.arange(1870, 1901): flyear = utils.date_to_floatyear(y, i) tmp = mb.flowline_mb_models[-1].get_monthly_climate(meanhgt85, flyear)[0] df85_2.loc[y, i] = tmp.mean() for y in np.arange(1984, 2015): tmp = mb.flowline_mb_models[-1].get_monthly_climate(meanhgt99, flyear)[0] df99_2.loc[y, i] = tmp.mean() t99 = df99_2.mean().mean() t85 = df85_2.mean().mean() return t85, t99
# CRU, HISTALP, ERA5, ERA5L, CERA, CERA+ERA5, CERA+ERA5L? baseline = 'CERA+ERA5L' # Initialize OGGM and set up the run parameters cfg.initialize() if baseline == 'HISTALP': # Other params: see https://oggm.org/2018/08/10/histalp-parameters/ cfg.PARAMS['prcp_scaling_factor'] = 1.75 cfg.PARAMS['temp_melt'] = -1.75 # Local paths (where to find the OGGM run output) dirname = 'OGGM_ref_mb_{}_RGIV{}_OGGM{}'.format(baseline, rgi_version, oggm.__version__) WORKING_DIR = utils.gettempdir(dirname, home=True) cfg.PATHS['working_dir'] = WORKING_DIR # Read the rgi ids of the reference glaciers rids = pd.read_csv(os.path.join(WORKING_DIR, 'mb_ref_glaciers.csv'), index_col=0, squeeze=True) # Go - initialize glacier directories gdirs = workflow.init_glacier_directories(rids) # Cross-validation file = os.path.join(cfg.PATHS['working_dir'], 'ref_tstars.csv') ref_df = pd.read_csv(file, index_col=0) for i, gdir in enumerate(gdirs): print('Cross-validation iteration {} of {}'.format(i + 1, len(ref_df)))
dsc = xr.open_dataset(smb_path, decode_times=False)#.chunk({'time':20}) dsc.attrs['pyproj_srs'] = proj.srs dsc['time'] = np.append(pd.period_range(start='2018.01.01', end='2018.12.01', freq='M').to_timestamp(), pd.period_range(start='1958.01.01', end='2017.12.01', freq='M').to_timestamp()) ds_smb_two = dsc.isel(time=slice(48,12*34)) see = ds_smb_two.chunk({'time':2}) avg = see.SMB_rec.mean(dim='time').compute() # OGGM run cfg.initialize() cfg.initialize(logging_level='WORKFLOW') cfg.PATHS['working_dir'] = utils.gettempdir(dirname='OGGM-k-plots', reset=True) cfg.PARAMS['border'] = 10 # Find a glacier with good coverage of both data gdirs = workflow.init_glacier_regions(['RGI60-05.00800'], from_prepro_level=3) gdir=gdirs[0] utils_vel.write_flowlines_to_shape(gdir, path=gdir.dir) shp_path = os.path.join(gdir.dir, 'RGI60-05.shp') shp = gpd.read_file(shp_path) # Crop velocity raster dvel_sel, derr_sel = utils_vel.crop_vel_data_to_glacier_grid(gdir, dvel, derr)
import pandas as pd import xarray as xr import oggm from oggm import cfg, tasks from oggm.core.climate import (mb_yearly_climate_on_glacier, t_star_from_refmb, local_t_star, mu_star_calibration) from oggm.core.massbalance import (ConstantMassBalance) from oggm.utils import get_demo_file, gettempdir cfg.initialize() cfg.set_intersects_db(get_demo_file('rgi_intersect_oetztal.shp')) cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') base_dir = gettempdir('Climate_docs') entity = gpd.read_file(get_demo_file('HEF_MajDivide.shp')).iloc[0] gdir = oggm.GlacierDirectory(entity, base_dir=base_dir) tasks.define_glacier_region(gdir, entity=entity) tasks.glacier_masks(gdir) tasks.compute_centerlines(gdir) tasks.initialize_flowlines(gdir) tasks.compute_downstream_line(gdir) tasks.catchment_area(gdir) tasks.catchment_width_geom(gdir) tasks.catchment_width_correction(gdir) data_dir = get_demo_file('HISTALP_precipitation_all_abs_1801-2014.nc') cfg.PATHS['cru_dir'] = os.path.dirname(data_dir) cfg.PARAMS['baseline_climate'] = 'HISTALP' cfg.PARAMS['baseline_y0'] = 1850
# Module logger log = logging.getLogger(__name__) # Initialize OGGM and set up the default run parameters cfg.initialize(logging_level='WORKFLOW') rgi_version = '61' rgi_region = '11' # Region Central Europe # Here we override some of the default parameters # How many grid points around the glacier? # Make it large if you expect your glaciers to grow large: # here, 80 is more than enough cfg.PARAMS['border'] = 80 # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('OGGM_Rofental') utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR # RGI file path = utils.get_rgi_region_file(rgi_region, version=rgi_version) rgidf = gpd.read_file(path) # Get the Rofental Basin file path = utils.get_demo_file('rofental_hydrosheds.shp') basin = gpd.read_file(path) # Take all glaciers in the Rofental Basin in_bas = [basin.geometry.contains(shpg.Point(x, y))[0] for (x, y) in zip(rgidf.CenLon, rgidf.CenLat)] rgidf = rgidf.loc[in_bas]
# set intersect file to use #cfg.set_intersects_db(new_intersects) # select subset entity = entity[~entity.RGIId.isin(sel)] # year where outlines are valid outline_year = os.path.basename(fp)[:4] # check geometries #for idx, row in entity.iterrows(): # if entity.geometry.iloc[idx].type != 'Polygon': # print(row['RGIId'] + row.geometry.type) # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('User_gcm_data', reset=True) utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR #cfg.PATHS['working_dir'] = utils.gettempdir('user', reset=True) #cfg.PATHS['working_dir'] = utils.gettempdir(dirname='OGGM-GettingStarted', reset=True) # test with one glacier #entity = entity[entity['RGIId'] == 'RGI60-05.02114'] # rgi_ids for default data #rgi_ids = ['RGI60-05.01916'] # glacier directory #base_url = 'https://cluster.klima.uni-bremen.de/~oggm/gdirs/oggm_v1.4/L3-L5_files/CRU/centerlines/qc3/pcp2.5/no_match/' gdirs = workflow.init_glacier_directories(
import oggm.cfg as cfg from oggm import tasks, utils, workflow from oggm.workflow import execute_entity_task from oggm.utils import get_demo_file # Module logger log = logging.getLogger(__name__) # For timing the run start = time.time() # Initialize OGGM and set up the default run parameters cfg.initialize() # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('OGGM_spinup_run') utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR # Use multiprocessing? cfg.PARAMS['use_multiprocessing'] = True # How many grid points around the glacier? # Make it large if you expect your glaciers to grow large cfg.PARAMS['border'] = 80 # Go - initialize glacier directories gdirs = workflow.init_glacier_regions(['RGI60-11.00897'], from_prepro_level=4) # Additional climate file (CESM) cfg.PATHS['cesm_temp_file'] = get_demo_file('cesm.TREFHT.160001-200512'
# Initialize OGGM and set up the default run parameters cfg.initialize(logging_level='WORKFLOW') # Here we override some of the default parameters # How many grid points around the glacier? # We make it small because we want the model to error because # of flowing out of the domain cfg.PARAMS['border'] = 80 # This is the important bit! # We tell OGGM to continue despite of errors cfg.PARAMS['continue_on_error'] = True # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('OGGM_Errors') utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR rgi_ids = ['RGI60-11.00897', 'RGI60-11.01450', 'RGI60-11.03295'] log.workflow('Starting OGGM run') log.workflow('Number of glaciers: {}'.format(len(rgi_ids))) # Go - get the pre-processed glacier directories gdirs = workflow.init_glacier_directories(rgi_ids, from_prepro_level=4) # We can step directly to the experiment! # Random climate representative for the recent climate (1985-2015) # with a negative bias added to the random temperature series workflow.execute_entity_task(tasks.run_random_climate, gdirs,
def mb_calibration(rgi_version, baseline): """Run the mass balance calibration for the VAS model. RGI version and baseline climate must be given. Parameters ---------- rgi_version : str Version (and subversion) of the RGI, e.g., '62' baseline : str 'HISTALP' or 'CRU', name of the baseline climate """ # initialize OGGM and set up the run parameters vascaling.initialize(logging_level='WORKFLOW') # LOCAL paths (where to write the OGGM run output) dirname = 'VAS_ref_mb_{}_RGIV{}'.format(baseline, rgi_version) wdir = utils.gettempdir(dirname, home=True, reset=True) utils.mkdir(wdir, reset=True) cfg.PATHS['working_dir'] = wdir # # CLUSTER paths # wdir = os.environ.get('WORKDIR', '') # cfg.PATHS['working_dir'] = wdir # we are running the calibration ourselves cfg.PARAMS['run_mb_calibration'] = True # we are using which baseline data? cfg.PARAMS['baseline_climate'] = baseline # no need for intersects since this has an effect on the inversion only cfg.PARAMS['use_intersects'] = False # use multiprocessing? cfg.PARAMS['use_multiprocessing'] = True # set to True for operational runs cfg.PARAMS['continue_on_error'] = True # 10 is only for OGGM-VAS, OGGM needs 80 to run cfg.PARAMS['border'] = 80 if baseline == 'HISTALP': # OGGM HISTALP PARAMETERS from Matthias Dusch # see https://oggm.org/2018/08/10/histalp-parameters/ # cfg.PARAMS['prcp_scaling_factor'] = 1.75 # cfg.PARAMS['temp_melt'] = -1.75 # cfg.PARAMS['temp_all_solid'] = 0 # cfg.PARAMS['prcp_default_gradient'] = 0 # VAS HISTALP PARAMETERS from x-validation cfg.PARAMS['prcp_scaling_factor'] = 2.5 cfg.PARAMS['temp_melt'] = -0.5 cfg.PARAMS['temp_all_solid'] = 0 cfg.PARAMS['prcp_default_gradient'] = 0 elif baseline == 'CRU': # using the parameters from Marzeion et al. (2012) # cfg.PARAMS['prcp_scaling_factor'] = 2.5 # cfg.PARAMS['temp_melt'] = 1 # cfg.PARAMS['temp_all_solid'] = 3 # cfg.PARAMS['prcp_default_gradient'] = 3e-4 # using the parameters from Malles and Marzeion 2020 cfg.PARAMS['prcp_scaling_factor'] = 3 cfg.PARAMS['temp_melt'] = 0 cfg.PARAMS['temp_all_solid'] = 4 cfg.PARAMS['prcp_default_gradient'] = 4e-4 # the next step is to get all the reference glaciers, # i.e. glaciers with mass balance measurements. # get the reference glacier ids (they are different for each RGI version) df, _ = utils.get_wgms_files() rids = df['RGI{}0_ID'.format(rgi_version[0])] # we can't do Antarctica rids = [rid for rid in rids if not ('-19.' in rid)] # For HISTALP only RGI reg 11.01 (ALPS) if baseline == 'HISTALP': rids = [rid for rid in rids if '-11' in rid] # initialize the glacier regions base_url = "https://cluster.klima.uni-bremen.de/~oggm/gdirs/oggm_v1.4/" \ "L3-L5_files/CRU/elev_bands/qc3/pcp2.5/match_geod" # Go - get the pre-processed glacier directories gdirs = workflow.init_glacier_directories(rids, from_prepro_level=3, prepro_base_url=base_url, prepro_rgi_version=rgi_version) # Some glaciers in RGI Region 11 are not inside the HISTALP domain if baseline == 'HISTALP': gdirs = [gdir for gdir in gdirs if gdir.rgi_subregion == '11-01'] # get reference glaciers with mass balance measurements gdirs = utils.get_ref_mb_glaciers(gdirs) # make a new dataframe with those (this takes a while) print('For RGIV{} we have {} candidate reference ' 'glaciers.'.format(rgi_version, len(gdirs))) # run climate tasks vascaling.compute_ref_t_stars(gdirs) execute_entity_task(vascaling.local_t_star, gdirs) # we store the associated params mb_calib = gdirs[0].read_pickle('climate_info')['mb_calib_params'] with open(os.path.join(wdir, 'mb_calib_params.json'), 'w') as fp: json.dump(mb_calib, fp)
import matplotlib.pyplot as plt import numpy as np from matplotlib import cm as colormap import xarray as xr try: import salemf except ImportError: pass # Read the default parameter file and make them available to other OGGM tools via cfg.PARAMS cfg.initialize(logging_level='WORKFLOW') cfg.PARAMS['prcp_scaling_factor'], cfg.PARAMS['ice_density'], cfg.PARAMS['continue_on_error'] cfg.PATHS['working_dir'] = utils.gettempdir(dirname='/home/nirab/OGGM/OGGM-GettingStarted', reset=True) rgi_ids = ['RGI60-11.01328', 'RGI60-11.00897'] #graphics.plot_domain(gdirs, figsize=(8, 7)) #gdir = rgi_ids[1] # Plotting the glacier over the DEM file for rgi in rgi_ids: f, axs = plt.subplots(2, 2, figsize=(8, 6)) for ax, border in zip(np.array(axs).flatten(), [10, 80, 160, 250]): gdir = workflow.init_glacier_directories(rgi, from_prepro_level=1,prepro_border=border) graphics.plot_domain(gdir, ax=ax, title='Border: {}'.format(border),
def test_process_era5_daily_data(): cfg.initialize() test_dir = '/home/lilianschuster/Schreibtisch/PhD/oggm_files/tests' if not os.path.exists(test_dir): test_dir = utils.gettempdir(dirname='OGGM_era5_daily_test', reset=True) cfg.PATHS['working_dir'] = test_dir b_url_ = 'https://cluster.klima.uni-bremen.de/~fmaussion' base_url = b_url_ + '/gdirs/prepro_l2_202010/elevbands_fl_with_consensus' df = utils.get_rgi_glacier_entities(['RGI60-11.00897']) gdirs = workflow.init_glacier_directories(df, from_prepro_level=2, prepro_border=40, prepro_base_url=base_url, prepro_rgi_version='62') gdir = gdirs[0] process_era5_daily_data(gdir, y0=1979, y1=2018) filename = 'climate_historical_daily' fpath = gdir.get_filepath(filename) # check the climate files of an individual glacier (Hintereisferner) xr_nc = xr.open_dataset(fpath) assert np.all(xr_nc.prcp) > 0 # to be sure that there are no erroneaous filling values inside assert np.all(xr_nc.prcp) < 10000 # temperature values are in °C and in the right range assert np.all(xr_nc.temp) > -100 assert np.all(xr_nc.temp) < 100 # temperature gradient should be in the following range assert np.all(xr_nc.gradient > -0.015) assert np.all(xr_nc.gradient < -0.002) # all lapse rates/ precipitation values in one month should be equal # because only temperature is on daily basis np.testing.assert_allclose(xr_nc.resample(time='1M').std().prcp, 0, atol=1e-3) np.testing.assert_allclose(xr_nc.resample(time='1M').std().gradient, 0, atol=1e-3) # summed up monthly precipitation from daily dataset xr_nc_prcp_m = xr_nc.prcp.resample(time='1M').sum() oggm.shop.ecmwf.process_ecmwf_data(gdir, dataset="ERA5", y0=1979, y1=2018) filename = 'climate_historical' fpath = gdir.get_filepath(filename) xr_nc_monthly = xr.open_dataset(fpath) # check if summed up monthly precipitation from daily # dataset equals approx. to the ERA5 monthly prcp np.testing.assert_allclose(xr_nc_prcp_m.values, xr_nc_monthly.prcp.values, rtol=1e-4) xr_nc_temp_m = xr_nc.temp.resample(time='1M').mean() # check if mean temperature from daily dataset equals # approx. to the ERA5 monthly temp. np.testing.assert_allclose(xr_nc_temp_m.values, xr_nc_monthly.temp.values, atol=0.05) with pytest.raises(InvalidParamsError): # dataset only goes from 1979--2018 process_era5_daily_data(gdir, y0=1979, y1=2019) # in cfg.PARAMS that is initiated during testing, # cfg.PARAMS[hydro_month_nh = 10], this is in conflict with 8 process_era5_daily_data(gdir, y0=1979, y1=2018, hydro_month_nh=8)
# Locals import oggm.cfg as cfg from oggm import utils, workflow # For timing the run import time start = time.time() # Initialize OGGM and set up the default run parameters cfg.initialize() rgi_version = '61' rgi_region = '11' # Alps # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('OGGM_Rofental') utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR # We use intersects path = utils.get_rgi_intersects_region_file(rgi_region, version=rgi_version) cfg.set_intersects_db(path) # RGI file path = utils.get_rgi_region_file(rgi_region, version=rgi_version) rgidf = gpd.read_file(path) # Get the Rofental Basin file path = utils.get_demo_file('rofental_hydrosheds.shp') basin = gpd.read_file(path)
# Imports import os import xarray as xr import matplotlib.pyplot as plt from oggm.utils import get_demo_file, gettempdir # Local working directory (where OGGM wrote its output) WORKING_DIR = gettempdir('OGGM_Rofental') # Read the files using xarray ds = xr.open_dataset(os.path.join(WORKING_DIR, 'run_output_commitment.nc')) dsp = xr.open_dataset(os.path.join(WORKING_DIR, 'run_output_bias_p.nc')) dsm = xr.open_dataset(os.path.join(WORKING_DIR, 'run_output_bias_m.nc')) # Compute and plot the regional sums f, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4)) # Volume (ds.volume.sum(dim='rgi_id') * 1e-9).plot(ax=ax1, label='[1985-2015]') (dsp.volume.sum(dim='rgi_id') * 1e-9).plot(ax=ax1, label='+0.5°C') (dsm.volume.sum(dim='rgi_id') * 1e-9).plot(ax=ax1, label='-0.5°C') ax1.legend(loc='best') # Area (ds.area.sum(dim='rgi_id') * 1e-6).plot(ax=ax2, label='[1985-2015]') (dsp.area.sum(dim='rgi_id') * 1e-6).plot(ax=ax2, label='+0.5°C') (dsm.area.sum(dim='rgi_id') * 1e-6).plot(ax=ax2, label='-0.5°C') plt.tight_layout() # Pick a specific glacier (Hintereisferner) rid = 'RGI60-11.00897' f, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))
import logging log = logging.getLogger(__name__) # RGI Version rgi_version = '61' # CRU or HISTALP? baseline = 'CRU' # Initialize OGGM and set up the run parameters cfg.initialize() # Local paths (where to write the OGGM run output) dirname = 'OGGM_ref_mb_{}_RGIV{}_OGGM{}'.format(baseline, rgi_version, oggm.__version__) WORKING_DIR = utils.gettempdir(dirname, home=True) utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR # We are running the calibration ourselves cfg.PARAMS['run_mb_calibration'] = True # We are using which baseline data? cfg.PARAMS['baseline_climate'] = baseline # Use multiprocessing? cfg.PARAMS['use_multiprocessing'] = True # Set to True for operational runs - here we want all glaciers to run cfg.PARAMS['continue_on_error'] = False
# This script will plot the workflow for the columbia glacier and the LeConte # glacier. """Useful plotting functions""" import os import geopandas as gpd import shutil from oggm import cfg, graphics, utils, workflow, tasks from oggm.workflow import execute_entity_task from matplotlib import rcParams import matplotlib.pyplot as plt cfg.initialize(logging_level='WORKFLOW') WORKING_DIR = utils.gettempdir(dirname='OGGM-workflow', reset=False) cfg.PATHS['working_dir'] = WORKING_DIR MAIN_PATH = os.path.expanduser('~/cryo_calving_2019/') plot_path = os.path.join(MAIN_PATH, 'plots/') RGI_FILE = os.path.join(MAIN_PATH, 'input_data/01_rgi60_Alaska_modify/01_rgi60_Alaska.shp') #This path needs to be change Columbia_prepro = os.path.join(MAIN_PATH, 'output_data/1_Columbia_Glacier_runs/1_preprocessing/per_glacier/') Columbia_dir = os.path.join(Columbia_prepro, 'RGI60-01/RGI60-01.10/RGI60-01.10689')
import oggm from oggm import cfg, tasks from oggm.core.climate import (mb_yearly_climate_on_glacier, t_star_from_refmb, local_t_star, mu_star_calibration) from oggm.core.massbalance import (ConstantMassBalance) from oggm.utils import get_demo_file, gettempdir from oggm.shop import histalp cfg.initialize() cfg.set_intersects_db(get_demo_file('rgi_intersect_oetztal.shp')) cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') histalp.set_histalp_url('https://cluster.klima.uni-bremen.de/~oggm/' 'test_climate/histalp/') base_dir = gettempdir('Climate_docs') cfg.PATHS['working_dir'] = base_dir entity = gpd.read_file(get_demo_file('HEF_MajDivide.shp')).iloc[0] gdir = oggm.GlacierDirectory(entity, base_dir=base_dir, reset=True) tasks.define_glacier_region(gdir) tasks.glacier_masks(gdir) tasks.compute_centerlines(gdir) tasks.initialize_flowlines(gdir) tasks.compute_downstream_line(gdir) tasks.catchment_area(gdir) tasks.catchment_width_geom(gdir) tasks.catchment_width_correction(gdir) cfg.PARAMS['baseline_climate'] = 'HISTALP' cfg.PARAMS['baseline_y0'] = 1850 tasks.process_histalp_data(gdir)
def single_flowline_glacier_directory(rgi_id, reset=False, prepro_border=80): """Prepare a GlacierDirectory for PyGEM (single flowline to start with) Parameters ---------- rgi_id : str the rgi id of the glacier reset : bool set to true to delete any pre-existing files. If false (the default), the directory won't be re-downloaded if already available locally in order to spare time. prepro_border : int the size of the glacier map: 10, 80, 160, 250 Returns ------- a GlacierDirectory object """ if type(rgi_id) != str: raise ValueError('We expect rgi_id to be a string') if 'RGI60-' not in rgi_id: raise ValueError('OGGM currently expects IDs to start with RGI60-') cfg.initialize() wd = utils.gettempdir(dirname='pygem-{}-b{}'.format(rgi_id, prepro_border), reset=reset) cfg.PATHS['working_dir'] = wd cfg.PARAMS['use_multiple_flowlines'] = False # Check if folder is already processed try: gdir = utils.GlacierDirectory(rgi_id) gdir.read_pickle('model_flowlines') # If the above works the directory is already processed, return return gdir except OSError: pass # If not ready, we download the preprocessed data for this glacier gdirs = workflow.init_glacier_regions([rgi_id], from_prepro_level=2, prepro_border=prepro_border) # Compute all the stuff list_talks = [ tasks.glacier_masks, tasks.compute_centerlines, tasks.initialize_flowlines, tasks.compute_downstream_line, tasks.catchment_area, tasks.catchment_width_geom, tasks.catchment_width_correction, tasks.compute_downstream_bedshape, tasks.local_t_star, tasks.mu_star_calibration, tasks.prepare_for_inversion, tasks.mass_conservation_inversion, tasks.filter_inversion_output, tasks.init_present_time_glacier, ] for task in list_talks: # The order matters! workflow.execute_entity_task(task, gdirs) return gdirs[0]
# Initialize OGGM and set up the default run parameters cfg.initialize(logging_level='WORKFLOW') # Here we override some of the default parameters # How many grid points around the glacier? # We make it small because we want the model to error because # of flowing out of the domain cfg.PARAMS['border'] = 80 # This is the important bit! # We tell OGGM to continue despite of errors cfg.PARAMS['continue_on_error'] = True # Local working directory (where OGGM will write its output) WORKING_DIR = utils.gettempdir('OGGM_Errors') utils.mkdir(WORKING_DIR, reset=True) cfg.PATHS['working_dir'] = WORKING_DIR rgi_ids = ['RGI60-11.00897', 'RGI60-11.01450', 'RGI60-11.03295'] log.workflow('Starting OGGM run') log.workflow('Number of glaciers: {}'.format(len(rgi_ids))) # Go - get the pre-processed glacier directories gdirs = workflow.init_glacier_regions(rgi_ids, from_prepro_level=4) # We can step directly to the experiment! # Random climate representative for the recent climate (1985-2015) # with a negative bias added to the random temperature series workflow.execute_entity_task(tasks.run_random_climate, gdirs,