Dealing with errors after a run

In this example, we run the model on a list of three glaciers: two of them will end with errors: one because it already failed at preprocessing (i.e. prior to this run), and one during the run. We show how to analyze theses erros and solve (some) of them, as described in the OGGM documentation under troubleshooting.

Run with cfg.PARAMS['continue_on_error'] = True

# Locals
import oggm.cfg as cfg
from oggm import utils, workflow, tasks

# Libs
import os
import xarray as xr
import pandas as pd

# Initialize OGGM and set up the default run parameters
cfg.initialize(logging_level='WARNING')

# 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 useful since we have three glaciers
cfg.PARAMS['use_multiprocessing'] = True

# 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', reset=True)
cfg.PATHS['working_dir'] = WORKING_DIR

rgi_ids = ['RGI60-11.00897', 'RGI60-11.01450', 'RGI60-11.03295']

# 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,
                             nyears=150, seed=0,
                             temperature_bias=-1)
2022-10-07 12:45:42: oggm.cfg: Reading default parameters from the OGGM `params.cfg` configuration file.
2022-10-07 12:45:42: oggm.cfg: Multiprocessing switched OFF according to the parameter file.
2022-10-07 12:45:42: oggm.cfg: Multiprocessing: using all available processors (N=2)
2022-10-07 12:45:43: oggm.cfg: PARAMS['border'] changed from `40` to `80`.
2022-10-07 12:45:43: oggm.cfg: Multiprocessing switched ON after user settings.
2022-10-07 12:45:43: oggm.cfg: PARAMS['continue_on_error'] changed from `False` to `True`.
---------------------------------------------------------------------------
InvalidParamsError                        Traceback (most recent call last)
Cell In [1], line 33
     30 rgi_ids = ['RGI60-11.00897', 'RGI60-11.01450', 'RGI60-11.03295']
     32 # Go - get the pre-processed glacier directories
---> 33 gdirs = workflow.init_glacier_directories(rgi_ids, from_prepro_level=4)
     35 # We can step directly to the experiment!
     36 # Random climate representative for the recent climate (1985-2015)
     37 # with a negative bias added to the random temperature series
     38 workflow.execute_entity_task(tasks.run_random_climate, gdirs,
     39                              nyears=150, seed=0,
     40                              temperature_bias=-1)

File /usr/local/pyenv/versions/3.10.7/lib/python3.10/site-packages/oggm/workflow.py:351, in init_glacier_directories(rgidf, reset, force, from_prepro_level, prepro_border, prepro_rgi_version, prepro_base_url, from_tar, delete_tar)
    348     reset = utils.query_yes_no('Delete all glacier directories?')
    350 if from_prepro_level:
--> 351     url = utils.get_prepro_base_url(base_url=prepro_base_url,
    352                                     border=prepro_border,
    353                                     prepro_level=from_prepro_level,
    354                                     rgi_version=prepro_rgi_version)
    355     if cfg.PARAMS['has_internet'] and not utils.url_exists(url):
    356         raise InvalidParamsError("base url seems unreachable with these "
    357                                  "parameters: {}".format(url))

File /usr/local/pyenv/versions/3.10.7/lib/python3.10/site-packages/oggm/utils/_downloads.py:1278, in get_prepro_base_url(base_url, rgi_version, border, prepro_level)
   1275 """Extended base url where to find the desired gdirs."""
   1277 if base_url is None:
-> 1278     raise InvalidParamsError('Starting with v1.6, users now have to '
   1279                              'explicitly indicate the url they want '
   1280                              'to start from.')
   1282 if not base_url.endswith('/'):
   1283     base_url += '/'

InvalidParamsError: Starting with v1.6, users now have to explicitly indicate the url they want to start from.

Error diagnostics

# Write the compiled output
utils.compile_glacier_statistics(gdirs);  # saved as glacier_statistics.csv in the WORKING_DIR folder
utils.compile_run_output(gdirs);  # saved as run_output.nc in the WORKING_DIR folder
# Read it
with xr.open_dataset(os.path.join(WORKING_DIR, 'run_output.nc')) as ds:
    ds = ds.load()
df_stats = pd.read_csv(os.path.join(WORKING_DIR, 'glacier_statistics.csv'), index_col=0)
# all possible statistics about the glaciers
df_stats
  • in the column error_task, we can see whether an error occurred, and if yes during which task

  • error_msg describes the actual error message

df_stats[['error_task', 'error_msg']]

We can also check which glacier failed at which task by using compile_task_log.

# also saved as task_log.csv in the WORKING_DIR folder - "append=False" replaces the existing one
utils.compile_task_log(gdirs, task_names=['glacier_masks', 'compute_centerlines', 'flowline_model_run'], append=False)

Error solving

RuntimeError: Glacier exceeds domain boundaries, at year: 98.08333333333333

To remove this error just increase the domain boundary before running init_glacier_directories ! Attention, this means that more data has to be downloaded and the run takes more time. The available options for cfg.PARAMS['border'] are 10, 40, 80 or 160 at the moment; the unit is number of grid points outside the glacier boundaries. More about that in the OGGM documentation under preprocessed files.

# reset to recompute statistics
# Beware! If you use `reset=True` in `utils.mkdir`, ALL DATA in this folder will be deleted!
utils.mkdir(WORKING_DIR, reset=True)

# increase the amount of gridpoints outside the glacier
cfg.PARAMS['border'] = 160
gdirs = workflow.init_glacier_directories(rgi_ids, from_prepro_level=4)
workflow.execute_entity_task(tasks.run_random_climate, gdirs,
                             nyears=150, seed=0,
                             temperature_bias=-1);

# recompute the output
# we can also get the run output directly from the methods
df_stats = utils.compile_glacier_statistics(gdirs)
ds = utils.compile_run_output(gdirs)
# check again
df_stats[['error_task', 'error_msg']]

Now RGI60-11.00897 runs without errors!

Error: Need a valid model_flowlines file.

This error message in the log is misleading: it does not really describe the source of the error, which happened earlier in the processing chain. Therefore we can look instead into the glacier_statistics via compile_glacier_statistics or into the log output via compile_task_log:

print('error_task: {}, error_msg: {}'.format(df_stats.loc['RGI60-11.03295']['error_task'],
                                             df_stats.loc['RGI60-11.03295']['error_msg']))

Now we have a better understanding of the error:

  • OGGM can not work with this geometry of this glacier and could therefore not make a gridded mask of the glacier outlines.

  • there is no way to prevent this except you find a better way to pre-process the geometry of this glacier

  • these glaciers have to be ignored! Less than 0.5% of glacier area globally have errors during the geometry processing or failures in computing certain topographical properties by e.g. invalid DEM, see Sect. 4.2 Invalid Glaciers of the OGGM paper (Maussion et al., 2019) and this tutorial for more up-to-date numbers

Ignoring those glaciers with errors that we can’t solve

In the run_output, you can for example just use *.dropna to remove these. For other applications (e.g. quantitative mass change evaluation), more will be needed (not available yet in the OGGM codebase):

ds.dropna(dim='rgi_id')  # here we can e.g. find the volume evolution

What’s next?