Using intake together with CDO

These examples were provided by Florian Ziemen (ziemen@dkrz.de) for use on the Levante Supercomputer of DKRZ. The notebook is based on the icon-datashader notebook, and gives a hint on how to process data from the intake catalog with cdo.

Some of the ideas were contributed by Lukas Kluft, Tobi Kölling, and others. The examples are by no means meant to be perfect. They should just provide some input on how things can be done.

Copyright 2022 Florian Ziemen / DKRZ

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  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.

  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.

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.

Preparations

[1]:
# basics
import intake
import xarray as xr
import dask  # memory-efficient parallel computation and delayed execution (lazy evaluation).
import subprocess as sp

Paths for storing images and a data cache

[2]:
%run gem_helpers.ipynb

uid = getpass.getuser()
image_path = make_tempdir("intake_demo_plots")
data_cache_path = make_tempdir("intake_demo_data")

Helper functions

[3]:
# this function is also part of the gem helpers


def get_list_from_cat(catalog, column):
    """A helper function for getting the contents of a column in an intake catalog.

    Call with the catalog to be inspected and the column of interest."""
    return sorted(catalog.unique(column)[column]["values"])

The catalog containing all the data

[4]:
catalog_file = "/work/ka1081/Catalogs/dyamond-nextgems.json"
[5]:
cat = intake.open_esm_datastore(catalog_file)
cat

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

unique
variable_id 546
project 2
institution_id 12
source_id 19
experiment_id 4
simulation_id 12
realm 5
frequency 12
time_reduction 4
grid_label 7
level_type 9
time_min 918
time_max 1094
grid_id 3
format 1
uri 88813
[6]:
# a somewhat closer look using get_from_cat
get_from_cat(cat, ["experiment_id", "source_id", "simulation_id"])
# print ("\n".join(get_from_cat (cat, "variable_id")))
[6]:
experiment_id source_id simulation_id
0 Cycle1 ICON-SAP-5km dpp0052
1 Cycle1 ICON-SAP-5km dpp0054
2 Cycle1 ICON-SAP-5km dpp0065
3 Cycle1 IFS-FESOM2-4km hlq0
4 Cycle1 IFS-NEMO-4km hmrt
5 Cycle1 IFS-NEMO-9km hmt0
6 Cycle1 IFS-NEMO-DEEPon-4km hmwz
7 Cycle2-alpha ICON-ESM dpp0066
8 Cycle2-alpha ICON-ESM dpp0067
9 DW-ATM ARPEGE-NH-2km r1i1p1f1
10 DW-ATM GEM r1i1p1f1
11 DW-ATM GEOS-1km r1i1p1f1
12 DW-ATM GEOS-3km r1i1p1f1
13 DW-ATM ICON-NWP-2km r1i1p1f1
14 DW-ATM ICON-SAP-5km dpp0014
15 DW-ATM NICAM-3km r1i1p1f1
16 DW-ATM SAM2-4km r1i1p1f1
17 DW-ATM SCREAM-3km r1i1p1f1
18 DW-ATM SHiELD-3km r1i1p1f1
19 DW-ATM UM-5km r1i1p1f1
20 DW-CPL GEOS-6km r1i1p1f1
21 DW-CPL ICON-SAP-5km dpp0029
22 DW-CPL IFS-4km r1i1p1f1
23 DW-CPL IFS-9km r1i1p1f1
24 DW-CPL NICAM-3km r1i1p1f1

Selecting ‘tas’ from the catalog

[7]:
var = "tas"
hits = cat.search(simulation_id=["dpp0066", "dpp0067"], variable_id=[var])
hits

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

unique
variable_id 37
project 1
institution_id 1
source_id 1
experiment_id 1
simulation_id 2
realm 1
frequency 1
time_reduction 1
grid_label 1
level_type 1
time_min 406
time_max 406
grid_id 1
format 1
uri 479

The CDO-relevant part

Getting the file names

[8]:
file_cat = {}
for simulation_id in ("dpp0066", "dpp0067"):
    file_cat[simulation_id] = get_list_from_cat(
        hits.search(simulation_id=simulation_id), "uri"
    )

Feeding things into CDO

[9]:
outfile_dict = {}
for simulation_id, files in file_cat.items():
    outfile = f"{data_cache_path}/{var}_monstd_{simulation_id}.nc"
    if not os.access(outfile, os.R_OK):
        query = (
            [
                "cdo",
                "-P",
                "8",
                "-monstd",
                f"-select,name={var}",
                "[",
            ]
            + files[:10]
            + ["]", outfile]
        )
        # 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.
        print(query)
        sp.run(query)
    outfile_dict[simulation_id] = outfile
['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']
cdo(1) select: Process started
cdo(1) select: Processed 10066329600 values from 370 variables over 480 timesteps.
cdo    monstd: Processed 10066329600 values from 1 variable over 480 timesteps [70.58s 758MB].
cdo(1) select: Process started
['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']
cdo(1) select: Processed 40265318400 values from 370 variables over 480 timesteps.
cdo    monstd: Processed 40265318400 values from 1 variable over 480 timesteps [266.13s 2918MB].

Loading the data into xarray

[10]:
dataset_dict = {
    name: xr.open_dataset(filename) for (name, filename) in outfile_dict.items()
}