Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import oggm.cfg as cfg
from oggm import workflow
from oggm import tasks
from oggm.workflow import execute_entity_task
from oggm import graphics, utils
# Initialize OGGM
cfg.initialize()
# Local paths (where to write output and where to download input)
DATA_DIR = '/home/mowglie/disk/OGGM_INPUT'
WORKING_DIR = '/home/mowglie/disk/OGGM_RUNS/TEST_DEMS'
PLOTS_DIR = os.path.join(WORKING_DIR, 'plots')
cfg.PATHS['working_dir'] = WORKING_DIR
cfg.PATHS['topo_dir'] = os.path.join(DATA_DIR, 'topo')
cfg.PATHS['rgi_dir'] = os.path.join(DATA_DIR, 'rgi')
utils.mkdir(WORKING_DIR)
utils.mkdir(cfg.PATHS['topo_dir'])
utils.mkdir(cfg.PATHS['rgi_dir'])
# Use multiprocessing?
cfg.PARAMS['use_multiprocessing'] = False
cfg.PARAMS['border'] = 20
cfg.PARAMS['continue_on_error'] = False
# Read in the RGI file
rgisel = os.path.join(WORKING_DIR, 'rgi_selection.shp')
if not os.path.exists(rgisel):
rgi_dir = utils.get_rgi_dir()
regions = ['{:02d}'.format(int(p)) for p in range(1, 20)]
files = [glob.glob(os.path.join(rgi_dir, '*', r + '_rgi50_*.shp'))[0] for r in regions]
import matplotlib.pyplot as plt
# Locals
import oggm.cfg as cfg
from oggm import workflow, graphics, utils
from oggm.core import flowline
# Module logger
log = logging.getLogger(__name__)
# Initialize OGGM
cfg.initialize()
# Local working directory (where OGGM will write its output)
WORKING_DIR = utils.gettempdir('OGGM_precalibrated_run')
cfg.PATHS['working_dir'] = WORKING_DIR
# Initialize from existing directories
# (note that we don't need the RGI file:
# this can be slow sometimes but it works)
gdirs = workflow.init_glacier_regions()
# Plot: we will show the state of all four glaciers at the beginning and at
# the end of the commitment simulation
f, axs = plt.subplots(2, 3, figsize=(14, 6))
for i in range(3):
ax = axs[0, i]
gdir = gdirs[i]
# Use the model ouptut file to simulate the glacier evolution
model = flowline.FileModel(gdir.get_filepath('model_run',
filesuffix='_commitment'))
# South Asia East 15
# Low Latitudes 16
# Southern Andes 17
# New Zealand 18
# Antarctic and Subantarctic 19
rgi_reg = '07'
# Initialize OGGM and set up the run parameters
# ---------------------------------------------
cfg.initialize()
# Local paths (where to write output and where to download input)
WORKING_DIR = '/home/mowglie/disk/OGGM_Runs/TESTS'
utils.mkdir(WORKING_DIR)
cfg.PATHS['working_dir'] = WORKING_DIR
PLOTS_DIR = os.path.join(WORKING_DIR, 'plots')
utils.mkdir(PLOTS_DIR)
# Use multiprocessing?
cfg.PARAMS['use_multiprocessing'] = False
# How many grid points around the glacier?
# Make it large if you expect your glaciers to grow large
cfg.PARAMS['border'] = 100
# Set to True for operational runs
cfg.PARAMS['continue_on_error'] = False
cfg.PARAMS['auto_skip_task'] = True
# Don't use divides for now
# Compare their standard deviation
std_ref = refmb.ANNUAL_BALANCE.std()
rcor = np.corrcoef(refmb.OGGM, refmb.ANNUAL_BALANCE)[0, 1]
if std_ref == 0:
# I think that such a thing happens with some geodetic values
std_ref = refmb.OGGM.std()
rcor = 1
# Store the scores
ref_df.loc[gdir.rgi_id, 'CV_MB_BIAS'] = (refmb.OGGM.mean() -
refmb.ANNUAL_BALANCE.mean())
ref_df.loc[gdir.rgi_id, 'CV_MB_SIGMA_BIAS'] = (refmb.OGGM.std() / std_ref)
ref_df.loc[gdir.rgi_id, 'CV_MB_COR'] = rcor
# Write out
ref_df.to_csv(os.path.join(cfg.PATHS['working_dir'], 'crossval_tstars.csv'))
# Marzeion et al Figure 3
f, ax = plt.subplots(1, 1)
bins = np.arange(20) * 400 - 3800
ref_df['CV_MB_BIAS'].plot(ax=ax, kind='hist', bins=bins, color='C3', label='')
ax.vlines(ref_df['CV_MB_BIAS'].mean(), 0, 120, linestyles='--', label='Mean')
ax.vlines(ref_df['CV_MB_BIAS'].quantile(), 0, 120, label='Median')
ax.vlines(ref_df['CV_MB_BIAS'].quantile([0.05, 0.95]), 0, 120, color='grey',
label='5% and 95%\npercentiles')
ax.text(0.01, 0.99, 'N = {}'.format(len(gdirs)),
horizontalalignment='left',
verticalalignment='top',
transform=ax.transAxes)
ax.set_ylim(0, 120)
ax.set_ylabel('N Glaciers')
# 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]
# Sort for more efficient parallel computing
rgidf = rgidf.sort_values('Area', ascending=False)
# major RGI version relevant
v = gdir.rgi_version[0]
# baseline climate
str_s = 'cru4' if 'CRU' in source else 'histalp'
vn = 'vas_ref_tstars_rgi{}_{}_calib_params'.format(v, str_s)
for k in params:
if cfg.PARAMS[k] != cfg.PARAMS[vn][k]:
msg = ('The reference t* you are trying to use was '
'calibrated with different MB parameters. You '
'might have to run the calibration manually.')
raise MassBalanceCalibrationError(msg)
ref_df = cfg.PARAMS['vas_ref_tstars_rgi{}_{}'.format(v, str_s)]
else:
# Use the the local calibration
fp = os.path.join(cfg.PATHS['working_dir'], 'ref_tstars.csv')
ref_df = pd.read_csv(fp)
# Compute the distance to each glacier
distances = utils.haversine(gdir.cenlon, gdir.cenlat,
ref_df.lon, ref_df.lat)
# Take the 10 closest
aso = np.argsort(distances)[0:9]
amin = ref_df.iloc[aso]
distances = distances[aso]**2
# If really close no need to divide, else weighted average
if distances.iloc[0] <= 0.1:
tstar = amin.tstar.iloc[0]
bias = amin.bias.iloc[0]
else:
def _cached_download_helper(cache_obj_name, dl_func, reset=False):
"""Helper function for downloads.
Takes care of checking if the file is already cached.
Only calls the actual download function when no cached version exists.
"""
cache_dir = cfg.PATHS['dl_cache_dir']
cache_ro = cfg.PARAMS['dl_cache_readonly']
try:
# this is for real runs
fb_cache_dir = os.path.join(cfg.PATHS['working_dir'], 'cache')
check_fb_dir = False
except KeyError:
# Nothing have been set up yet, this is bad - use tmp
# This should happen on RO cluster only but still
fb_cache_dir = os.path.join(cfg.CACHE_DIR, 'cache')
check_fb_dir = True
if not cache_dir:
# Defaults to working directory: it must be set!
if not cfg.PATHS['working_dir']:
raise InvalidParamsError("Need a valid PATHS['working_dir']!")
cache_dir = fb_cache_dir
path : str, bool
Set to "True" in order to store the info in the working directory
Set to a path to store the file to your chosen location
add_climate_period : int or list of ints
compile climate statistics for the 30 yrs period around the selected
date.
"""
from oggm.workflow import execute_entity_task
out_df = execute_entity_task(climate_statistics, gdirs,
add_climate_period=add_climate_period)
out = pd.DataFrame(out_df).set_index('rgi_id')
if path:
if path is True:
out.to_csv(os.path.join(cfg.PATHS['working_dir'],
('climate_statistics' +
filesuffix + '.csv')))
else:
out.to_csv(path)
return out
from oggm import cfg, tasks, graphics
from oggm import utils
from oggm.core.flowline import (FileModel)
from oggm.sandbox.gmd_paper import PLOT_DIR
from oggm.utils import get_demo_file, mkdir
cfg.initialize()
reset = False
cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif')
cfg.PARAMS['border'] = 60
cfg.PARAMS['auto_skip_task'] = True
cfg.PARAMS['run_mb_calibration'] = True
base_dir = os.path.join(os.path.expanduser('~/tmp'), 'OGGM_GMD', 'scenarios')
cfg.PATHS['working_dir'] = base_dir
mkdir(base_dir, reset=reset)
entity = gpd.read_file(get_demo_file('Hintereisferner_RGI5.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.compute_downstream_bedshape(gdir)
tasks.catchment_area(gdir)
tasks.catchment_intersections(gdir)
tasks.catchment_width_geom(gdir)
tasks.catchment_width_correction(gdir)
tasks.process_cru_data(gdir)
cfg.set_intersects_db(test_intersects_file)
if is_test:
# Just for fun
rgidf = rgidf.sample(test_nr)
# Sort for more efficient parallel computing
rgidf = rgidf.sort_values('Area', ascending=False)
log.workflow('Starting prepro run for RGI reg: {} '
'and border: {}'.format(rgi_reg, border))
log.workflow('Number of glaciers: {}'.format(len(rgidf)))
# Input
if test_topofile:
cfg.PATHS['dem_file'] = test_topofile
# L1 - initialize working directories
# Which DEM source?
if dem_source.upper() == 'ALL':
# This is the complex one, just do the job an leave
log.workflow('Running prepro on ALL sources')
for i, s in enumerate(utils.DEM_SOURCES):
rs = i == 0
rgidf['DEM_SOURCE'] = s
log.workflow('Running prepro on sources: {}'.format(s))
gdirs = workflow.init_glacier_regions(rgidf, reset=rs, force=rs)
workflow.execute_entity_task(_rename_dem_folder, gdirs, source=s)
# make a GeoTiff mask of the glacier, choose any source
workflow.execute_entity_task(gis.rasterio_glacier_mask,
gdirs, source='ALL')