{ "cells": [ { "cell_type": "markdown", "id": "55f1f7bf-9589-4a43-b246-7c4c7880fa2d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Xarray engine: seasonal forecast" ] }, { "cell_type": "code", "execution_count": 1, "id": "7e0be52c-bedb-4ae7-984c-4807bf253d7f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "653a95e071ca4633aadbe42f597676a9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "seasonal_monthly.grib: 0%| | 0.00/160k [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", " \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", "
centreshortNametypeOfLevelleveldataDatedataTimestepRangedataTypenumbergridTypeforecastMonth
0lfpw2tsurface0199310010744fcmean0regular_ll1
1lfpw2tsurface0199310010744fcmean1regular_ll1
2lfpw2tsurface0199310010744fcmean2regular_ll1
3lfpw2tsurface01993100101464fcmean0regular_ll2
\n", "" ], "text/plain": [ " centre shortName typeOfLevel level dataDate dataTime stepRange dataType \\\n", "0 lfpw 2t surface 0 19931001 0 744 fcmean \n", "1 lfpw 2t surface 0 19931001 0 744 fcmean \n", "2 lfpw 2t surface 0 19931001 0 744 fcmean \n", "3 lfpw 2t surface 0 19931001 0 1464 fcmean \n", "\n", " number gridType forecastMonth \n", "0 0 regular_ll 1 \n", "1 1 regular_ll 1 \n", "2 2 regular_ll 1 \n", "3 0 regular_ll 2 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds_fl[0:4].ls(extra_keys=\"forecastMonth\")" ] }, { "cell_type": "raw", "id": "665fba14-79d5-4344-84fb-2e16da77936d", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "In order to use ``forecastMonth`` instead of ``step`` we need to use the ``dim_roles`` option in :py:meth:`~data.readers.grib.index.GribFieldList.to_xarray`." ] }, { "cell_type": "code", "execution_count": 3, "id": "c500c39a-8cdf-4e25-950e-581924879e6c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 395kB\n",
       "Dimensions:                  (number: 3, forecast_reference_time: 4, step: 6,\n",
       "                              latitude: 19, longitude: 36)\n",
       "Coordinates:\n",
       "  * number                   (number) int64 24B 0 1 2\n",
       "  * forecast_reference_time  (forecast_reference_time) datetime64[ns] 32B 199...\n",
       "  * step                     (step) int64 48B 1 2 3 4 5 6\n",
       "  * latitude                 (latitude) float64 152B 90.0 80.0 ... -80.0 -90.0\n",
       "  * longitude                (longitude) float64 288B 0.0 10.0 ... 340.0 350.0\n",
       "Data variables:\n",
       "    2t                       (number, forecast_reference_time, step, latitude, longitude) float64 394kB ...\n",
       "Attributes: (12/15)\n",
       "    param:        2t\n",
       "    paramId:      167\n",
       "    class:        c3\n",
       "    stream:       msmm\n",
       "    levtype:      sfc\n",
       "    type:         fcmean\n",
       "    ...           ...\n",
       "    fcmonth:      1\n",
       "    origin:       lfpw\n",
       "    domain:       g\n",
       "    method:       1\n",
       "    Conventions:  CF-1.8\n",
       "    institution:  ECMWF
" ], "text/plain": [ " Size: 395kB\n", "Dimensions: (number: 3, forecast_reference_time: 4, step: 6,\n", " latitude: 19, longitude: 36)\n", "Coordinates:\n", " * number (number) int64 24B 0 1 2\n", " * forecast_reference_time (forecast_reference_time) datetime64[ns] 32B 199...\n", " * step (step) int64 48B 1 2 3 4 5 6\n", " * latitude (latitude) float64 152B 90.0 80.0 ... -80.0 -90.0\n", " * longitude (longitude) float64 288B 0.0 10.0 ... 340.0 350.0\n", "Data variables:\n", " 2t (number, forecast_reference_time, step, latitude, longitude) float64 394kB ...\n", "Attributes: (12/15)\n", " param: 2t\n", " paramId: 167\n", " class: c3\n", " stream: msmm\n", " levtype: sfc\n", " type: fcmean\n", " ... ...\n", " fcmonth: 1\n", " origin: lfpw\n", " domain: g\n", " method: 1\n", " Conventions: CF-1.8\n", " institution: ECMWF" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = ds_fl.to_xarray(time_dim_mode=\"forecast\", \n", " dim_roles={\"step\": \"forecastMonth\"})\n", "ds" ] }, { "cell_type": "markdown", "id": "e917dbc1-ba05-4180-b1d8-62e04bf98d50", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "When we check the \"step\" dimension we can see its units are \"months\"." ] }, { "cell_type": "code", "execution_count": 4, "id": "850836de-db60-48ac-b42b-253ab335ceef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Size: 48B\n", "array([1, 2, 3, 4, 5, 6])\n", "Coordinates:\n", " * step (step) int64 48B 1 2 3 4 5 6\n", "Attributes:\n", " units: months\n" ] } ], "source": [ "print(ds[\"step\"])" ] }, { "cell_type": "raw", "id": "13b0fdb1-ed1f-4a0a-b77f-71115adf40ad", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "By default, the dimensions related to dimension roles are named after the roles. So, although the step dimension was generated from the \"forecastMonth\" GRIB key the dimension name is still \"step\". To override this use the ``dim_name_from_role_name=False`` option in :py:meth:`~data.readers.grib.index.GribFieldList.to_xarray`." ] }, { "cell_type": "code", "execution_count": 5, "id": "cbf6d822-546e-42ab-a8c7-ad8d7d0c61fc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 395kB\n",
       "Dimensions:                  (number: 3, forecast_reference_time: 4,\n",
       "                              forecastMonth: 6, latitude: 19, longitude: 36)\n",
       "Coordinates:\n",
       "  * number                   (number) int64 24B 0 1 2\n",
       "  * forecast_reference_time  (forecast_reference_time) datetime64[ns] 32B 199...\n",
       "  * forecastMonth            (forecastMonth) int64 48B 1 2 3 4 5 6\n",
       "  * latitude                 (latitude) float64 152B 90.0 80.0 ... -80.0 -90.0\n",
       "  * longitude                (longitude) float64 288B 0.0 10.0 ... 340.0 350.0\n",
       "Data variables:\n",
       "    2t                       (number, forecast_reference_time, forecastMonth, latitude, longitude) float64 394kB ...\n",
       "Attributes: (12/15)\n",
       "    param:        2t\n",
       "    paramId:      167\n",
       "    class:        c3\n",
       "    stream:       msmm\n",
       "    levtype:      sfc\n",
       "    type:         fcmean\n",
       "    ...           ...\n",
       "    fcmonth:      1\n",
       "    origin:       lfpw\n",
       "    domain:       g\n",
       "    method:       1\n",
       "    Conventions:  CF-1.8\n",
       "    institution:  ECMWF
" ], "text/plain": [ " Size: 395kB\n", "Dimensions: (number: 3, forecast_reference_time: 4,\n", " forecastMonth: 6, latitude: 19, longitude: 36)\n", "Coordinates:\n", " * number (number) int64 24B 0 1 2\n", " * forecast_reference_time (forecast_reference_time) datetime64[ns] 32B 199...\n", " * forecastMonth (forecastMonth) int64 48B 1 2 3 4 5 6\n", " * latitude (latitude) float64 152B 90.0 80.0 ... -80.0 -90.0\n", " * longitude (longitude) float64 288B 0.0 10.0 ... 340.0 350.0\n", "Data variables:\n", " 2t (number, forecast_reference_time, forecastMonth, latitude, longitude) float64 394kB ...\n", "Attributes: (12/15)\n", " param: 2t\n", " paramId: 167\n", " class: c3\n", " stream: msmm\n", " levtype: sfc\n", " type: fcmean\n", " ... ...\n", " fcmonth: 1\n", " origin: lfpw\n", " domain: g\n", " method: 1\n", " Conventions: CF-1.8\n", " institution: ECMWF" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = ds_fl.to_xarray(time_dim_mode=\"forecast\", \n", " dim_roles={\"step\": \"forecastMonth\"}, \n", " dim_name_from_role_name=False)\n", "ds" ] } ], "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.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }