{ "cells": [ { "cell_type": "markdown", "id": "415a2c2a-8d00-48fc-9a02-6fc79aac663f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Xarray engine: step range" ] }, { "cell_type": "markdown", "id": "b8e40382-ef81-46d7-8c94-2a01bd3a5214", "metadata": {}, "source": [ "Get input GRIB2 data containing precipitation forecast for step ranges." ] }, { "cell_type": "code", "execution_count": 1, "id": "85d04283-3488-477f-90dd-ee27f0a91935", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "936ac87a643d414781370a22f10a9904", "version_major": 2, "version_minor": 0 }, "text/plain": [ "lsp_step_range.grib2: 0%| | 0.00/1.17k [00:00\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", "
paramstepstepRangestartStependStep
0lsp71-7271-727172
1lsp72-7372-737273
\n", "" ], "text/plain": [ " param step stepRange startStep endStep\n", "0 lsp 71-72 71-72 71 72\n", "1 lsp 72-73 72-73 72 73" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import earthkit.data as ekd\n", "ds_fl = ekd.from_source(\"sample\", \"lsp_step_range.grib2\")\n", "ds_fl.ls(keys=[\"param\", \"step\", \"stepRange\", \"startStep\", \"endStep\"])" ] }, { "cell_type": "raw", "id": "b2fab96a-8435-4ed3-b43d-e7f5dcc27141", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "When we convert GRIB data to Xarray with :py:meth:`~data.readers.grib.index.GribFieldList.to_xarray` the step dimension is defined by the \"step\" :ref:`dimension role `. By default, this role is using the \"step_timedelta\" generated metadata key that is the timedelta representation of the \"endStep\" GRIB key." ] }, { "cell_type": "code", "execution_count": 2, "id": "5b6872c9-97b6-4336-89ba-4e6491605f90", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 2kB\n",
       "Dimensions:    (step: 2, latitude: 7, longitude: 12)\n",
       "Coordinates:\n",
       "  * step       (step) timedelta64[ns] 16B 3 days 3 days 01:00:00\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",
       "    lsp        (step, latitude, longitude) float64 1kB ...\n",
       "Attributes:\n",
       "    param:        lsp\n",
       "    paramId:      142\n",
       "    class:        d1\n",
       "    stream:       oper\n",
       "    levtype:      sfc\n",
       "    type:         fc\n",
       "    expver:       0001\n",
       "    date:         20250527\n",
       "    time:         0\n",
       "    domain:       g\n",
       "    Conventions:  CF-1.8\n",
       "    institution:  ECMWF
" ], "text/plain": [ " Size: 2kB\n", "Dimensions: (step: 2, latitude: 7, longitude: 12)\n", "Coordinates:\n", " * step (step) timedelta64[ns] 16B 3 days 3 days 01:00:00\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", " lsp (step, latitude, longitude) float64 1kB ...\n", "Attributes:\n", " param: lsp\n", " paramId: 142\n", " class: d1\n", " stream: oper\n", " levtype: sfc\n", " type: fc\n", " expver: 0001\n", " date: 20250527\n", " time: 0\n", " domain: g\n", " Conventions: CF-1.8\n", " institution: ECMWF" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = ds_fl.to_xarray()\n", "ds" ] }, { "cell_type": "markdown", "id": "5e0f85a6-30bd-4dfe-ae98-b9c13304a465", "metadata": {}, "source": [ "We can check the \"step\" coordinate in the dataset to see that it matches the \"endStep\" values." ] }, { "cell_type": "code", "execution_count": 3, "id": "f9a4b868-29dd-4bb1-bbaa-18caa68f405e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[72, 73]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# convert to hours from ns\n", "[int(x* 1E-9/(3600)) for x in ds[\"step\"].values]" ] }, { "cell_type": "markdown", "id": "542a047c-39d8-4ec1-9194-bb362e9de4f7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This default behaviour can be overridden by specifying custom ``dim_roles``. E.g. to get the step from the \"startStep\" key we can use:" ] }, { "cell_type": "code", "execution_count": 4, "id": "5ea06ff7-70c6-4967-80ce-7b7b6fa12fa5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[71, 72]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = ds_fl.to_xarray(dim_roles={\"step\": \"startStep\"})\n", "[int(x* 1E-9/(3600)) for x in ds[\"step\"].values]" ] }, { "cell_type": "code", "execution_count": null, "id": "495387d6-331c-4dc5-90fa-e05a6da9b998", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "dev", "language": "python", "name": "dev" }, "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.11.12" } }, "nbformat": 4, "nbformat_minor": 5 }