{ "cells": [ { "cell_type": "markdown", "id": "b74f6a93-148e-4668-a401-6104f54838e1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Xarray engine: hypercube with holes" ] }, { "cell_type": "markdown", "id": "8b3010be-7895-4fd2-8ad6-a57db7845396", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This notebook demonstrates how to work with input data not forming a full hypercube. " ] }, { "cell_type": "markdown", "id": "190cab07-5f50-410c-9c56-604a95a64ef0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "First, we get pressure level GRIB data with t, u and v variables, where u only contains the subset of the levels in t and v. This data forms a hypercube with holes." ] }, { "cell_type": "code", "execution_count": 1, "id": "f27152d7-42b2-4f65-a2a1-eda3d309b604", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d191b679f67b440aabe786cdbcb919c8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "tuv_pl_holes.grib: 0%| | 0.00/2.05k [00:00, ?B/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import earthkit.data as ekd\n", "ds_fl = ekd.from_source(\"sample\", \"tuv_pl_holes.grib\")" ] }, { "cell_type": "raw", "id": "699d1157-88bf-4ace-9fa2-4d2987489303", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Converting the input to Xarray with :py:meth:`~data.readers.grib.index.GribFieldList.to_xarray` fails because holes are not allowed by default." ] }, { "cell_type": "code", "execution_count": 2, "id": "c27352d5-7fa3-4bdc-92d7-06042e253eea", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variable \"u\" has inconsistent dimension \"levelist\" compared to other variables. Expected values: (6)\n", "[300, 400, 500, 700, 850, 1000], got: (2) [500, 700]. Length mismatch: 6 != 2\n" ] } ], "source": [ "try:\n", " ds_fl.to_xarray()\n", "except ValueError as e:\n", " import textwrap\n", " print(textwrap.fill(str(e), 100))" ] }, { "cell_type": "markdown", "id": "d185e460-6ed9-46e4-8977-c4b831a9251e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We need to use the ``allow_holes=True`` option to make the conversion work." ] }, { "cell_type": "code", "execution_count": 3, "id": "6d2cd04d-6fa1-4ea5-a9a7-ccf72ebc55b0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset> Size: 12kB\n",
"Dimensions: (level: 6, latitude: 7, longitude: 12)\n",
"Coordinates:\n",
" * level (level) int64 48B 300 400 500 700 850 1000\n",
" * latitude (latitude) float64 56B 90.0 60.0 30.0 0.0 -30.0 -60.0 -90.0\n",
" * longitude (longitude) float64 96B 0.0 30.0 60.0 90.0 ... 270.0 300.0 330.0\n",
"Data variables:\n",
" t (level, latitude, longitude) float64 4kB ...\n",
" u (level, latitude, longitude) float64 4kB ...\n",
" v (level, latitude, longitude) float64 4kB ...\n",
"Attributes:\n",
" class: od\n",
" stream: oper\n",
" levtype: pl\n",
" type: an\n",
" expver: 0001\n",
" date: 20180801\n",
" time: 1200\n",
" domain: g\n",
" number: 0\n",
" Conventions: CF-1.8\n",
" institution: ECMWF