{ "cells": [ { "cell_type": "markdown", "id": "27b6be13-d0a5-41e1-9479-f1028b01610f", "metadata": {}, "source": [ "## Interpolating HEALPix GRIB fields" ] }, { "cell_type": "raw", "id": "4cf645d5-c3bb-46a3-afd9-60446adf40ba", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This example shows how to interpolate GRIB data defined on a HEALPix nested grid. We will also see how to inspect and plot the resulting data and how to convert it to xarray.\n", "\n", "To make this notebook work :xref:`earthkit-data` (version >= 0.6.0) and :xref:`earthkit-plots` have to be installed. The data will be represented as an `earthkit-data GRIB FieldList `_." ] }, { "cell_type": "markdown", "id": "68d978ff-0c55-47d5-983b-829467675af0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Interpolation" ] }, { "cell_type": "code", "execution_count": 1, "id": "8c1cff92-971e-4494-b847-b2df7e7ee53c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a4416f71d13e4123b90e2109fd2fd4e7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "H8_nested_multi.grib2: 0%| | 0.00/4.92k [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import earthkit.plots\n", "\n", "figure = earthkit.plots.Figure(size=(9, 6), rows=1, columns=2)\n", "figure.add_map().plot(r[0])\n", "figure.add_map().plot(r[1])\n", "figure.coastlines()\n", "figure.subplot_titles()\n", "figure.legend()\n", "figure.gridlines(sharey=True)\n", "figure.show()" ] }, { "cell_type": "markdown", "id": "4eee7b1e-fbe3-43d9-b185-867c7d8946aa", "metadata": {}, "source": [ "#### Converting the results to xarray" ] }, { "cell_type": "code", "execution_count": 3, "id": "8fd12a93-6cf9-4d6e-83e2-0efd9e545a48", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 44kB\n",
       "Dimensions:    (step: 2, latitude: 37, longitude: 72)\n",
       "Coordinates:\n",
       "  * step       (step) timedelta64[ns] 16B 00:00:00 12:00:00\n",
       "  * latitude   (latitude) float64 296B 90.0 85.0 80.0 75.0 ... -80.0 -85.0 -90.0\n",
       "  * longitude  (longitude) float64 576B 0.0 5.0 10.0 15.0 ... 345.0 350.0 355.0\n",
       "Data variables:\n",
       "    2t         (step, latitude, longitude) float64 43kB ...\n",
       "Attributes:\n",
       "    param:        2t\n",
       "    paramId:      167\n",
       "    class:        od\n",
       "    stream:       oper\n",
       "    levtype:      sfc\n",
       "    type:         fc\n",
       "    expver:       0001\n",
       "    date:         20240323\n",
       "    time:         1200\n",
       "    domain:       g\n",
       "    Conventions:  CF-1.8\n",
       "    institution:  ECMWF
" ], "text/plain": [ " Size: 44kB\n", "Dimensions: (step: 2, latitude: 37, longitude: 72)\n", "Coordinates:\n", " * step (step) timedelta64[ns] 16B 00:00:00 12:00:00\n", " * latitude (latitude) float64 296B 90.0 85.0 80.0 75.0 ... -80.0 -85.0 -90.0\n", " * longitude (longitude) float64 576B 0.0 5.0 10.0 15.0 ... 345.0 350.0 355.0\n", "Data variables:\n", " 2t (step, latitude, longitude) float64 43kB ...\n", "Attributes:\n", " param: 2t\n", " paramId: 167\n", " class: od\n", " stream: oper\n", " levtype: sfc\n", " type: fc\n", " expver: 0001\n", " date: 20240323\n", " time: 1200\n", " domain: g\n", " Conventions: CF-1.8\n", " institution: ECMWF" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.to_xarray()" ] }, { "cell_type": "markdown", "id": "5a6380b4-03b6-497f-b1d2-e36860830830", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Writing the results to disk" ] }, { "cell_type": "markdown", "id": "3b206f6d-7a17-4128-818d-f12262d8f5ed", "metadata": {}, "source": [ "Write the resulting fieldlist to disk:" ] }, { "cell_type": "code", "execution_count": 4, "id": "9c6b8bb6-a734-4930-b322-7c362c915a51", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/93/w0p869rx17q98wxk83gn9ys40000gn/T/ipykernel_42625/2197547179.py:2: DeprecatedWarning: save is deprecated as of 0.13.0. Use to_target() instead\n", " r.save(out_file)\n" ] } ], "source": [ "out_file = \"_res_H8_nested_to_5x5.grib\"\n", "r.save(out_file)" ] } ], "metadata": { "kernelspec": { "display_name": "mir", "language": "python", "name": "mir" }, "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.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }