{ "cells": [ { "cell_type": "markdown", "id": "cdeff258-f4af-48fb-99df-8f01e6ea1170", "metadata": {}, "source": [ "# Caching results of computations for later use\n", "\n", "These examples were provided by Florian Ziemen (ziemen@dkrz.de) for use on the Levante Supercomputer of DKRZ.\n", "Some of the ideas were contributed by Lukas Kluft and Tobi Kölling, and others.\n", "The examples are by no means meant to be perfect. They should just provide some input on how things _can_ be done.\n", "\n", "Copyright 2022 Florian Ziemen / DKRZ\n", "\n", "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n", "\n", "1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n", "\n", "2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n", "\n", "3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n", "\n", "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ] }, { "cell_type": "markdown", "id": "6a57dfdb-1a25-4fdd-8673-1183fb43a671", "metadata": {}, "source": [ "## Preparations" ] }, { "cell_type": "code", "execution_count": 1, "id": "46946fd9-a2f6-40d8-8d1e-9439e0a2e14a", "metadata": {}, "outputs": [], "source": [ "# basics\n", "import intake\n", "import numpy as np\n", "import xarray as xr\n", "import dask # memory-efficient parallel computation and delayed execution (lazy evaluation).\n", "\n", "\n", "# fewer red warnings\n", "import warnings, matplotlib\n", "\n", "warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", "warnings.filterwarnings(\"ignore\", category=matplotlib.cbook.mplDeprecation)" ] }, { "cell_type": "markdown", "id": "089e40ef-64e5-49a8-bda2-2c061ed347bf", "metadata": {}, "source": [ "### Paths for data cache\n", "\n", "We create a data cache in our /scratch/ directory. Data there will be deleted automatically after two weeks. This way, we can cache things and the cache is automatically purged after a while." ] }, { "cell_type": "code", "execution_count": 2, "id": "4ee81831-02aa-498d-a127-5d05dc9b58f8", "metadata": {}, "outputs": [], "source": [ "%run gem_helpers.ipynb # see https://easy.gems.dkrz.de/Processing/Intake/gem_helpers.html" ] }, { "cell_type": "code", "execution_count": 3, "id": "0478ce7b-4f50-4439-a9c9-a2b283281a38", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Caching results in /scratch/k/k202134/intake_demo_data\n" ] } ], "source": [ "data_cache_path = make_tempdir(\"intake_demo_data\")\n", "print(\"Caching results in\", data_cache_path)" ] }, { "cell_type": "markdown", "id": "3c8f5012-f33b-4c17-9c8c-17924083f274", "metadata": {}, "source": [ "### Helper function for caching and loading" ] }, { "cell_type": "code", "execution_count": 4, "id": "6e944c20-5f51-427b-a9b7-01bc0bfdda1d", "metadata": {}, "outputs": [], "source": [ "def cache_data(data, filename):\n", " cache_file = os.path.join(data_cache_path, filename)\n", " if not os.access(cache_file, os.R_OK):\n", " data = data.compute()\n", " data.to_netcdf(cache_file)\n", " else:\n", " if type(data) == xr.core.dataarray.DataArray:\n", " data = xr.open_dataarray(cache_file)\n", " else:\n", " data = xr.open_dataset(cache_file)\n", " return data" ] }, { "cell_type": "markdown", "id": "62e4a6d7-f869-4f43-9d39-4e5add9b2eb3", "metadata": {}, "source": [ "## The catalog containing all the data" ] }, { "cell_type": "code", "execution_count": 5, "id": "76e954d6-85e6-4fd6-98af-0a522affbfca", "metadata": {}, "outputs": [], "source": [ "catalog_file = \"/work/ka1081/Catalogs/dyamond-nextgems.json\"" ] }, { "cell_type": "code", "execution_count": 6, "id": "1a529e8e-1fc9-4041-9b19-adc52323624e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
ICON-ESM catalog with 130 dataset(s) from 88823 asset(s):
\n", " | unique | \n", "
---|---|
variable_id | \n", "546 | \n", "
project | \n", "2 | \n", "
institution_id | \n", "12 | \n", "
source_id | \n", "19 | \n", "
experiment_id | \n", "4 | \n", "
simulation_id | \n", "12 | \n", "
realm | \n", "5 | \n", "
frequency | \n", "12 | \n", "
time_reduction | \n", "4 | \n", "
grid_label | \n", "7 | \n", "
level_type | \n", "9 | \n", "
time_min | \n", "918 | \n", "
time_max | \n", "1094 | \n", "
grid_id | \n", "3 | \n", "
format | \n", "1 | \n", "
uri | \n", "88813 | \n", "
\n", " | project | \n", "source_id | \n", "experiment_id | \n", "simulation_id | \n", "
---|---|---|---|---|
0 | \n", "DYAMOND_WINTER | \n", "ARPEGE-NH-2km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
1 | \n", "DYAMOND_WINTER | \n", "GEM | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
2 | \n", "DYAMOND_WINTER | \n", "GEOS-1km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
3 | \n", "DYAMOND_WINTER | \n", "GEOS-3km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
4 | \n", "DYAMOND_WINTER | \n", "GEOS-6km | \n", "DW-CPL | \n", "r1i1p1f1 | \n", "
5 | \n", "DYAMOND_WINTER | \n", "ICON-NWP-2km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
6 | \n", "DYAMOND_WINTER | \n", "ICON-SAP-5km | \n", "DW-ATM | \n", "dpp0014 | \n", "
7 | \n", "DYAMOND_WINTER | \n", "ICON-SAP-5km | \n", "DW-CPL | \n", "dpp0029 | \n", "
8 | \n", "DYAMOND_WINTER | \n", "IFS-4km | \n", "DW-CPL | \n", "r1i1p1f1 | \n", "
9 | \n", "DYAMOND_WINTER | \n", "IFS-9km | \n", "DW-CPL | \n", "r1i1p1f1 | \n", "
10 | \n", "DYAMOND_WINTER | \n", "NICAM-3km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
11 | \n", "DYAMOND_WINTER | \n", "NICAM-3km | \n", "DW-CPL | \n", "r1i1p1f1 | \n", "
12 | \n", "DYAMOND_WINTER | \n", "SAM2-4km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
13 | \n", "DYAMOND_WINTER | \n", "SCREAM-3km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
14 | \n", "DYAMOND_WINTER | \n", "SHiELD-3km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
15 | \n", "DYAMOND_WINTER | \n", "UM-5km | \n", "DW-ATM | \n", "r1i1p1f1 | \n", "
16 | \n", "NextGEMS | \n", "ICON-ESM | \n", "Cycle2-alpha | \n", "dpp0066 | \n", "
17 | \n", "NextGEMS | \n", "ICON-ESM | \n", "Cycle2-alpha | \n", "dpp0067 | \n", "
18 | \n", "NextGEMS | \n", "ICON-SAP-5km | \n", "Cycle1 | \n", "dpp0052 | \n", "
19 | \n", "NextGEMS | \n", "ICON-SAP-5km | \n", "Cycle1 | \n", "dpp0054 | \n", "
20 | \n", "NextGEMS | \n", "ICON-SAP-5km | \n", "Cycle1 | \n", "dpp0065 | \n", "
21 | \n", "NextGEMS | \n", "IFS-FESOM2-4km | \n", "Cycle1 | \n", "hlq0 | \n", "
22 | \n", "NextGEMS | \n", "IFS-NEMO-4km | \n", "Cycle1 | \n", "hmrt | \n", "
23 | \n", "NextGEMS | \n", "IFS-NEMO-9km | \n", "Cycle1 | \n", "hmt0 | \n", "
24 | \n", "NextGEMS | \n", "IFS-NEMO-DEEPon-4km | \n", "Cycle1 | \n", "hmwz | \n", "
ICON-ESM catalog with 2 dataset(s) from 479 asset(s):
\n", " | unique | \n", "
---|---|
variable_id | \n", "37 | \n", "
project | \n", "1 | \n", "
institution_id | \n", "1 | \n", "
source_id | \n", "1 | \n", "
experiment_id | \n", "1 | \n", "
simulation_id | \n", "2 | \n", "
realm | \n", "1 | \n", "
frequency | \n", "1 | \n", "
time_reduction | \n", "1 | \n", "
grid_label | \n", "1 | \n", "
level_type | \n", "1 | \n", "
time_min | \n", "406 | \n", "
time_max | \n", "406 | \n", "
grid_id | \n", "1 | \n", "
format | \n", "1 | \n", "
uri | \n", "479 | \n", "
<xarray.DataArray 'tas' (time: 19488, height_2: 1, ncells: 20971520)>\n", "dask.array<concatenate, shape=(19488, 1, 20971520), dtype=float32, chunksize=(4, 1, 20971520), chunktype=numpy.ndarray>\n", "Coordinates:\n", " * height_2 (height_2) float64 2.0\n", " * time (time) datetime64[ns] 2020-01-20 ... 2021-02-28T23:30:00.000115200\n", " clon (ncells) float64 ...\n", " clat (ncells) float64 ...\n", "Dimensions without coordinates: ncells\n", "Attributes:\n", " standard_name: tas\n", " long_name: temperature in 2m\n", " units: K\n", " param: 0.0.0\n", " CDI_grid_type: unstructured\n", " number_of_grid_in_reference: 1