{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 10 minutes to... OGGM as an accelerator for machine learning" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we want to showcase what OGGM does best: **preparing data for your modelling workflow**.\n", "\n", "We use preprocessed directories which contain most data available in [the OGGM shop](https://docs.oggm.org/en/stable/input-data.html) to illustrate how these could be used to inform data-based workflows. The data that is available in the shop and is show cased here, is more than is required for the regular OGGM workflow, which you will see in a bit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tags:** beginner, shop, workflow " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preprocessed directories with additional products" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to use the South Glacier example taken from the [ITMIX experiment](https://www.the-cryosphere.net/11/949/2017/). It is a small (5.6 km2) glacier in Alaska." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "## Libs\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "import salem\n", "\n", "# OGGM\n", "import oggm.cfg as cfg\n", "from oggm import utils, workflow, tasks, graphics" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Initialize OGGM and set up the default run parameters\n", "cfg.initialize(logging_level='WARNING')\n", "cfg.PARAMS['use_multiprocessing'] = False\n", "# Local working directory (where OGGM will write its output)\n", "cfg.PATHS['working_dir'] = utils.gettempdir('OGGM_Toy_Thickness_Model')\n", "# We use the preprocessed directories with additional data in it: \"W5E5_w_data\" \n", "base_url = 'https://cluster.klima.uni-bremen.de/~oggm/gdirs/oggm_v1.6/L3-L5_files/2023.3/elev_bands/W5E5_w_data/'\n", "gdirs = workflow.init_glacier_directories(['RGI60-01.16195'], from_prepro_level=3, prepro_base_url=base_url, prepro_border=10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Pick our glacier\n", "gdir = gdirs[0]\n", "gdir" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## OGGM-Shop datasets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are using here the preprocessed glacier directories that contain more data than the default ones:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "with xr.open_dataset(gdir.get_filepath('gridded_data')) as ds:\n", " ds = ds.load()\n", "# List all variables\n", "ds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's already quite a lot! We have access to a bunch of data for this glacier, lets have a look. We prepare the map first:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "smap = ds.salem.get_map(countries=False)\n", "smap.set_shapefile(gdir.read_shapefile('outlines'))\n", "smap.set_topography(ds.topo.data);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ITSLive velocity data " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets start with the [ITSLIVE](https://its-live.jpl.nasa.gov/#data) velocity data: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# get the velocity data\n", "u = ds.itslive_vx.where(ds.glacier_mask)\n", "v = ds.itslive_vy.where(ds.glacier_mask)\n", "ws = ds.itslive_v.where(ds.glacier_mask)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `.where(ds.glacier_mask)` command will remove the data outside of the glacier outline." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# get the axes ready\n", "f, ax = plt.subplots(figsize=(9, 9))\n", "\n", "# Quiver only every N grid point\n", "us = u[1::3, 1::3]\n", "vs = v[1::3, 1::3]\n", "\n", "smap.set_data(ws)\n", "smap.set_cmap('Blues')\n", "smap.plot(ax=ax)\n", "smap.append_colorbar(ax=ax, label='ice velocity (m yr$^{-1}$)')\n", "\n", "# transform their coordinates to the map reference system and plot the arrows\n", "xx, yy = smap.grid.transform(us.x.values, us.y.values, crs=gdir.grid.proj)\n", "xx, yy = np.meshgrid(xx, yy)\n", "qu = ax.quiver(xx, yy, us.values, vs.values)\n", "qk = ax.quiverkey(qu, 0.82, 0.97, 10, '10 m yr$^{-1}$',\n", " labelpos='E', coordinates='axes')\n", "ax.set_title('ITS-LIVE velocity');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Millan 2022 velocity data " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is more velocity data. Here follows the [Millan et al. (2022)](https://doi.org/10.1038/s41561-021-00885-z) dataset: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# get the velocity data\n", "u = ds.millan_vx.where(ds.glacier_mask)\n", "v = ds.millan_vy.where(ds.glacier_mask)\n", "ws = ds.millan_v.where(ds.glacier_mask)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# get the axes ready\n", "f, ax = plt.subplots(figsize=(9, 9))\n", "\n", "# Quiver only every N grid point\n", "us = u[1::3, 1::3]\n", "vs = v[1::3, 1::3]\n", "\n", "smap.set_data(ws)\n", "smap.set_cmap('Blues')\n", "smap.plot(ax=ax)\n", "smap.append_colorbar(ax=ax, label='ice velocity (m yr$^{-1}$)')\n", "\n", "# transform their coordinates to the map reference system and plot the arrows\n", "xx, yy = smap.grid.transform(us.x.values, us.y.values, crs=gdir.grid.proj)\n", "xx, yy = np.meshgrid(xx, yy)\n", "qu = ax.quiver(xx, yy, us.values, vs.values)\n", "qk = ax.quiverkey(qu, 0.82, 0.97, 10, '10 m yr$^{-1}$',\n", " labelpos='E', coordinates='axes')\n", "ax.set_title('Millan 2022 velocity');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Thickness data from Farinotti 2019 and Millan 2022 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also provide ice thickness data from [Farinotti et al. (2019)](https://doi.org/10.1038/s41561-019-0300-3) and [Millan et al. (2022)](https://doi.org/10.1038/s41561-021-00885-z) in the same gridded format. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# get the axes ready\n", "f, ax = plt.subplots(figsize=(9, 9))\n", "smap.set_cmap('viridis')\n", "smap.set_data(ds.consensus_ice_thickness)\n", "smap.plot(ax=ax)\n", "smap.append_colorbar(ax=ax, label='ice thickness (m)')\n", "ax.set_title('Farinotti 2019 thickness');" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# get the axes ready\n", "f, ax = plt.subplots(figsize=(9, 9))\n", "smap.set_cmap('viridis')\n", "smap.set_data(ds.millan_ice_thickness.where(ds.glacier_mask))\n", "smap.plot(ax=ax)\n", "smap.append_colorbar(ax=ax, label='ice thickness (m)')\n", "ax.set_title('Millan 2022 thickness');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some additional gridded attributes " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's also add some attributes that OGGM can compute for us:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Tested tasks\n", "task_list = [\n", " tasks.gridded_attributes,\n", " tasks.gridded_mb_attributes,\n", "]\n", "for task in task_list:\n", " workflow.execute_entity_task(task, gdirs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's open the gridded data file again with xarray:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "with xr.open_dataset(gdir.get_filepath('gridded_data')) as ds:\n", " ds = ds.load()\n", "# List all variables\n", "ds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The file contains several new variables with their description. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "ds.oggm_mb_above_z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot a few of them (we show how to plot them with xarray and with oggm):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "ds.slope.plot();\n", "plt.axis('equal');" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "f, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))\n", "graphics.plot_raster(gdir, var_name='aspect', cmap='twilight', ax=ax1)\n", "graphics.plot_raster(gdir, var_name='oggm_mb_above_z', ax=ax2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "