{ "cells": [ { "cell_type": "markdown", "id": "cdeff258-f4af-48fb-99df-8f01e6ea1170", "metadata": {}, "source": [ "# Using intake together with CDO\n", "\n", "These examples were provided by Florian Ziemen (ziemen@dkrz.de) for use on the Levante Supercomputer of DKRZ.\n", "The notebook is based on the icon-datashader notebook, and gives a hint on how to process data from the intake catalog with cdo.\n", "\n", "Some of the ideas were contributed by Lukas Kluft, 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 xarray as xr\n", "import dask # memory-efficient parallel computation and delayed execution (lazy evaluation).\n", "import subprocess as sp" ] }, { "cell_type": "markdown", "id": "089e40ef-64e5-49a8-bda2-2c061ed347bf", "metadata": {}, "source": [ "### Paths for storing images and a data cache" ] }, { "cell_type": "code", "execution_count": 2, "id": "0478ce7b-4f50-4439-a9c9-a2b283281a38", "metadata": {}, "outputs": [], "source": [ "%run gem_helpers.ipynb\n", "\n", "uid = getpass.getuser()\n", "image_path = make_tempdir(\"intake_demo_plots\")\n", "data_cache_path = make_tempdir(\"intake_demo_data\")" ] }, { "cell_type": "markdown", "id": "3c8f5012-f33b-4c17-9c8c-17924083f274", "metadata": {}, "source": [ "### Helper functions" ] }, { "cell_type": "code", "execution_count": 3, "id": "4ee81831-02aa-498d-a127-5d05dc9b58f8", "metadata": {}, "outputs": [], "source": [ "# this function is also part of the gem helpers\n", "\n", "\n", "def get_list_from_cat(catalog, column):\n", " \"\"\"A helper function for getting the contents of a column in an intake catalog.\n", "\n", " Call with the catalog to be inspected and the column of interest.\"\"\"\n", " return sorted(catalog.unique(column)[column][\"values\"])" ] }, { "cell_type": "markdown", "id": "62e4a6d7-f869-4f43-9d39-4e5add9b2eb3", "metadata": {}, "source": [ "## The catalog containing all the data" ] }, { "cell_type": "code", "execution_count": 4, "id": "76e954d6-85e6-4fd6-98af-0a522affbfca", "metadata": {}, "outputs": [], "source": [ "catalog_file = \"/work/ka1081/Catalogs/dyamond-nextgems.json\"" ] }, { "cell_type": "code", "execution_count": 5, "id": "1a529e8e-1fc9-4041-9b19-adc52323624e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

ICON-ESM catalog with 130 dataset(s) from 88823 asset(s):

\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
unique
variable_id546
project2
institution_id12
source_id19
experiment_id4
simulation_id12
realm5
frequency12
time_reduction4
grid_label7
level_type9
time_min918
time_max1094
grid_id3
format1
uri88813
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cat = intake.open_esm_datastore(catalog_file)\n", "cat" ] }, { "cell_type": "code", "execution_count": 6, "id": "9d3f8de7-741c-434b-b3b3-e54011ed0f3a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
experiment_idsource_idsimulation_id
0Cycle1ICON-SAP-5kmdpp0052
1Cycle1ICON-SAP-5kmdpp0054
2Cycle1ICON-SAP-5kmdpp0065
3Cycle1IFS-FESOM2-4kmhlq0
4Cycle1IFS-NEMO-4kmhmrt
5Cycle1IFS-NEMO-9kmhmt0
6Cycle1IFS-NEMO-DEEPon-4kmhmwz
7Cycle2-alphaICON-ESMdpp0066
8Cycle2-alphaICON-ESMdpp0067
9DW-ATMARPEGE-NH-2kmr1i1p1f1
10DW-ATMGEMr1i1p1f1
11DW-ATMGEOS-1kmr1i1p1f1
12DW-ATMGEOS-3kmr1i1p1f1
13DW-ATMICON-NWP-2kmr1i1p1f1
14DW-ATMICON-SAP-5kmdpp0014
15DW-ATMNICAM-3kmr1i1p1f1
16DW-ATMSAM2-4kmr1i1p1f1
17DW-ATMSCREAM-3kmr1i1p1f1
18DW-ATMSHiELD-3kmr1i1p1f1
19DW-ATMUM-5kmr1i1p1f1
20DW-CPLGEOS-6kmr1i1p1f1
21DW-CPLICON-SAP-5kmdpp0029
22DW-CPLIFS-4kmr1i1p1f1
23DW-CPLIFS-9kmr1i1p1f1
24DW-CPLNICAM-3kmr1i1p1f1
\n", "
" ], "text/plain": [ " experiment_id source_id simulation_id\n", "0 Cycle1 ICON-SAP-5km dpp0052\n", "1 Cycle1 ICON-SAP-5km dpp0054\n", "2 Cycle1 ICON-SAP-5km dpp0065\n", "3 Cycle1 IFS-FESOM2-4km hlq0\n", "4 Cycle1 IFS-NEMO-4km hmrt\n", "5 Cycle1 IFS-NEMO-9km hmt0\n", "6 Cycle1 IFS-NEMO-DEEPon-4km hmwz\n", "7 Cycle2-alpha ICON-ESM dpp0066\n", "8 Cycle2-alpha ICON-ESM dpp0067\n", "9 DW-ATM ARPEGE-NH-2km r1i1p1f1\n", "10 DW-ATM GEM r1i1p1f1\n", "11 DW-ATM GEOS-1km r1i1p1f1\n", "12 DW-ATM GEOS-3km r1i1p1f1\n", "13 DW-ATM ICON-NWP-2km r1i1p1f1\n", "14 DW-ATM ICON-SAP-5km dpp0014\n", "15 DW-ATM NICAM-3km r1i1p1f1\n", "16 DW-ATM SAM2-4km r1i1p1f1\n", "17 DW-ATM SCREAM-3km r1i1p1f1\n", "18 DW-ATM SHiELD-3km r1i1p1f1\n", "19 DW-ATM UM-5km r1i1p1f1\n", "20 DW-CPL GEOS-6km r1i1p1f1\n", "21 DW-CPL ICON-SAP-5km dpp0029\n", "22 DW-CPL IFS-4km r1i1p1f1\n", "23 DW-CPL IFS-9km r1i1p1f1\n", "24 DW-CPL NICAM-3km r1i1p1f1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a somewhat closer look using get_from_cat\n", "get_from_cat(cat, [\"experiment_id\", \"source_id\", \"simulation_id\"])\n", "# print (\"\\n\".join(get_from_cat (cat, \"variable_id\")))" ] }, { "cell_type": "markdown", "id": "e874654b-083d-4f5b-a05e-b79afdda9858", "metadata": {}, "source": [ "## Selecting 'tas' from the catalog" ] }, { "cell_type": "code", "execution_count": 7, "id": "e988f177-7e51-4727-87f1-45e7ac701c15", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

ICON-ESM catalog with 2 dataset(s) from 479 asset(s):

\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
unique
variable_id37
project1
institution_id1
source_id1
experiment_id1
simulation_id2
realm1
frequency1
time_reduction1
grid_label1
level_type1
time_min406
time_max406
grid_id1
format1
uri479
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "var = \"tas\"\n", "hits = cat.search(simulation_id=[\"dpp0066\", \"dpp0067\"], variable_id=[var])\n", "hits" ] }, { "cell_type": "markdown", "id": "2ca438c4-cd5b-4d69-bc09-eb134d4c0246", "metadata": { "tags": [] }, "source": [ "## The CDO-relevant part\n", "### Getting the file names" ] }, { "cell_type": "code", "execution_count": 8, "id": "18aa9389-9740-4d67-acbd-7817fb1d41ea", "metadata": {}, "outputs": [], "source": [ "file_cat = {}\n", "for simulation_id in (\"dpp0066\", \"dpp0067\"):\n", " file_cat[simulation_id] = get_list_from_cat(\n", " hits.search(simulation_id=simulation_id), \"uri\"\n", " )" ] }, { "cell_type": "markdown", "id": "a04da978-efed-4f1f-aa4b-c3ce96dd83d5", "metadata": { "tags": [] }, "source": [ "## Feeding things into CDO" ] }, { "cell_type": "code", "execution_count": 9, "id": "9c1c0c21-5cbc-4357-8eb6-41c27f48a7f1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['cdo', '-P', '8', '-monstd', '-select,name=tas', '[', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200120T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200121T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200122T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200123T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200124T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200125T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200126T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200127T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200128T000000Z.nc', '/work/mh0287/m300083/experiments/dpp0066/dpp0066_atm_2d_ml_20200129T000000Z.nc', ']', '/scratch/k/k202134/intake_demo_data/tas_monstd_dpp0066.nc']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "cdo(1) select: Process started\n", "cdo(1) select: Processed 10066329600 values from 370 variables over 480 timesteps.\n", "cdo monstd: Processed 10066329600 values from 1 variable over 480 timesteps [70.58s 758MB].\n", "cdo(1) select: Process started\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['cdo', '-P', '8', '-monstd', '-select,name=tas', '[', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200120T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200121T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200122T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200123T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200124T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200125T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200126T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200127T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200128T000000Z.nc', '/work/mh0287/m218027/experiments/dpp0067/dpp0067_atm_2d_ml_20200129T000000Z.nc', ']', '/scratch/k/k202134/intake_demo_data/tas_monstd_dpp0067.nc']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "cdo(1) select: Processed 40265318400 values from 370 variables over 480 timesteps.\n", "cdo monstd: Processed 40265318400 values from 1 variable over 480 timesteps [266.13s 2918MB].\n" ] } ], "source": [ "outfile_dict = {}\n", "for simulation_id, files in file_cat.items():\n", " outfile = f\"{data_cache_path}/{var}_monstd_{simulation_id}.nc\"\n", " if not os.access(outfile, os.R_OK):\n", " query = (\n", " [\n", " \"cdo\",\n", " \"-P\",\n", " \"8\",\n", " \"-monstd\",\n", " f\"-select,name={var}\",\n", " \"[\",\n", " ]\n", " + files[:10]\n", " + [\"]\", outfile]\n", " )\n", " # Note, we only use the first 10 files to save time (the [:10] in files[:10]). Remove the [:10] to compute over the whole experiment.\n", " print(query)\n", " sp.run(query)\n", " outfile_dict[simulation_id] = outfile" ] }, { "cell_type": "markdown", "id": "5676d7c3-df12-409b-8526-af980e79bf5c", "metadata": {}, "source": [ "## Loading the data into xarray" ] }, { "cell_type": "code", "execution_count": 10, "id": "1b9fa1a4-2571-4470-b16e-3a4abd42cdae", "metadata": {}, "outputs": [], "source": [ "dataset_dict = {\n", " name: xr.open_dataset(filename) for (name, filename) in outfile_dict.items()\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (based on the module python3/2022.01)", "language": "python", "name": "python3_2022_01" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.9" } }, "nbformat": 4, "nbformat_minor": 5 }