{ "cells": [ { "cell_type": "markdown", "id": "ee0f0104-8077-45f1-9746-58f29b64db92", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## list-of-dict: converting to Xarray" ] }, { "cell_type": "raw", "id": "6cadbfbf-c7af-4927-8927-c320d9160c4f", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This example demonstrates how :ref:`data-sources-lod` fieldlists can be converted into Xarray." ] }, { "cell_type": "markdown", "id": "2e087423-8c96-49b4-984c-f15472fa8381", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Data containing geography" ] }, { "cell_type": "code", "execution_count": 1, "id": "1e5ebf7a-2fc6-453a-9e14-6b04b5135810", "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: 248B\n",
                            "Dimensions:    (levelist: 2, latitude: 3, longitude: 2)\n",
                            "Coordinates:\n",
                            "  * levelist   (levelist) int64 16B 500 850\n",
                            "  * latitude   (latitude) float64 24B 10.0 0.0 -10.0\n",
                            "  * longitude  (longitude) float64 16B 20.0 40.0\n",
                            "Data variables:\n",
                            "    t          (levelist, latitude, longitude) float64 96B ...\n",
                            "    u          (levelist, latitude, longitude) float64 96B ...\n",
                            "Attributes:\n",
                            "    Conventions:  CF-1.8\n",
                            "    institution:  ECMWF
" ], "text/plain": [ " Size: 248B\n", "Dimensions: (levelist: 2, latitude: 3, longitude: 2)\n", "Coordinates:\n", " * levelist (levelist) int64 16B 500 850\n", " * latitude (latitude) float64 24B 10.0 0.0 -10.0\n", " * longitude (longitude) float64 16B 20.0 40.0\n", "Data variables:\n", " t (levelist, latitude, longitude) float64 96B ...\n", " u (levelist, latitude, longitude) float64 96B ...\n", "Attributes:\n", " Conventions: CF-1.8\n", " institution: ECMWF" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import earthkit.data as ekd\n", "\n", "prototype = {\n", " \"latitudes\": [10.0, 0.0, -10.0],\n", " \"longitudes\": [20, 40.0],\n", " \"values\": [1, 2, 3, 4, 5, 6],\n", " \"valid_datetime\": \"2018-08-01T09:00:00Z\",\n", " }\n", "\n", "d = [\n", " {\"param\": \"t\", \"level\": 500, **prototype},\n", " {\"param\": \"t\", \"level\": 850, **prototype},\n", " {\"param\": \"u\", \"level\": 500, **prototype},\n", " {\"param\": \"u\", \"level\": 850, **prototype},\n", " ]\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", d)\n", "ds.to_xarray()" ] }, { "cell_type": "markdown", "id": "94b46ec8-614b-480a-8ffe-0b1dd4e344bb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Data without geography" ] }, { "cell_type": "code", "execution_count": 2, "id": "7ea3d8bf-a432-4aef-94d9-5ac0c6b19503", "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: 208B\n",
                            "Dimensions:   (levelist: 2, values: 6)\n",
                            "Coordinates:\n",
                            "  * levelist  (levelist) int64 16B 500 850\n",
                            "Dimensions without coordinates: values\n",
                            "Data variables:\n",
                            "    t         (levelist, values) float64 96B ...\n",
                            "    u         (levelist, values) float64 96B ...\n",
                            "Attributes:\n",
                            "    Conventions:  CF-1.8\n",
                            "    institution:  ECMWF
" ], "text/plain": [ " Size: 208B\n", "Dimensions: (levelist: 2, values: 6)\n", "Coordinates:\n", " * levelist (levelist) int64 16B 500 850\n", "Dimensions without coordinates: values\n", "Data variables:\n", " t (levelist, values) float64 96B ...\n", " u (levelist, values) float64 96B ...\n", "Attributes:\n", " Conventions: CF-1.8\n", " institution: ECMWF" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prototype = {\n", " \"values\": [1, 2, 3, 4, 5, 6],\n", " \"valid_datetime\": \"2018-08-01T09:00:00Z\",\n", " }\n", "\n", "d = [\n", " {\"param\": \"t\", \"level\": 500, **prototype},\n", " {\"param\": \"t\", \"level\": 850, **prototype},\n", " {\"param\": \"u\", \"level\": 500, **prototype},\n", " {\"param\": \"u\", \"level\": 850, **prototype},\n", " ]\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", d)\n", "ds.to_xarray()" ] }, { "cell_type": "code", "execution_count": null, "id": "c6463409-7686-4d90-8cab-00a04b7119bb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "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 }