{ "cells": [ { "cell_type": "markdown", "id": "69237222-3be8-4f6b-985c-0a8181730dfb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## list-of-dicts: overview" ] }, { "cell_type": "raw", "id": "1e5a6456-1177-49c9-a878-3de2780e7118", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The :ref:`data-sources-lod` source reads data from a list of dictionaries. Each dictionary represents a single field and the result is a FieldList consisting of ArrayField fields. " ] }, { "cell_type": "markdown", "id": "fdc25718-33bc-41fd-8b7b-29794e470e24", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "First, we create the input data using generic metadata keys. The latitudes and longitudes specified as disctinct values, the 2D field geometry will be automatically built from them." ] }, { "cell_type": "code", "execution_count": 1, "id": "235c8a8e-0ced-4f94-aee1-fcfab98f2300", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "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", " {\"param\": \"d\", \"level\": 850, **prototype},\n", " {\"param\": \"d\", \"level\": 600, **prototype},\n", " ]" ] }, { "cell_type": "raw", "id": "fe39a320-88c5-4c7e-96e2-cdde455be402", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Next, load the data into fieldlist as a :ref:`data-sources-lod` source using :ref:`from_source() `." ] }, { "cell_type": "code", "execution_count": 2, "id": "936a92f9-3fcc-4abf-aae0-c056646f6def", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "ds = ekd.from_source(\"list-of-dicts\", d)" ] }, { "cell_type": "code", "execution_count": 3, "id": "6cbfe88e-553f-43fc-a946-8e7be60d8b87", "metadata": { "editable": true, "scrolled": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\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", "
paramlevelbase_datetimevalid_datetimestepnumber
0t500None2018-08-01T09:00:00+00:00NoneNone
1t850None2018-08-01T09:00:00+00:00NoneNone
2u500None2018-08-01T09:00:00+00:00NoneNone
3u850None2018-08-01T09:00:00+00:00NoneNone
4d850None2018-08-01T09:00:00+00:00NoneNone
5d600None2018-08-01T09:00:00+00:00NoneNone
\n", "
" ], "text/plain": [ " param level base_datetime valid_datetime step number\n", "0 t 500 None 2018-08-01T09:00:00+00:00 None None\n", "1 t 850 None 2018-08-01T09:00:00+00:00 None None\n", "2 u 500 None 2018-08-01T09:00:00+00:00 None None\n", "3 u 850 None 2018-08-01T09:00:00+00:00 None None\n", "4 d 850 None 2018-08-01T09:00:00+00:00 None None\n", "5 d 600 None 2018-08-01T09:00:00+00:00 None None" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.ls()" ] }, { "cell_type": "code", "execution_count": 4, "id": "374f2be3-7eb4-48f6-909c-f1b6620ea020", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'lat': array([[ 10., 10.],\n", " [ 0., 0.],\n", " [-10., -10.]]),\n", " 'lon': array([[20., 40.],\n", " [20., 40.],\n", " [20., 40.]])}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].to_latlon()" ] }, { "cell_type": "code", "execution_count": 5, "id": "bc82d798-55a5-4fd6-b692-e59c4b5df942", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(array([ 10., 10., 0., 0., -10., -10.]),\n", " array([20., 40., 20., 40., 20., 40.]))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].grid_points()" ] }, { "cell_type": "code", "execution_count": 6, "id": "2c6d57a0-5a10-43d6-9b69-529ac4606d05", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 2],\n", " [3, 4],\n", " [5, 6]],\n", "\n", " [[1, 2],\n", " [3, 4],\n", " [5, 6]],\n", "\n", " [[1, 2],\n", " [3, 4],\n", " [5, 6]],\n", "\n", " [[1, 2],\n", " [3, 4],\n", " [5, 6]],\n", "\n", " [[1, 2],\n", " [3, 4],\n", " [5, 6]],\n", "\n", " [[1, 2],\n", " [3, 4],\n", " [5, 6]]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.to_numpy()" ] }, { "cell_type": "code", "execution_count": 7, "id": "11ce5424-0e3b-40f5-9184-73c8f058cb18", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[500, 850, 500, 850, 850, 600]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.metadata(\"level\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "bbe7dd94-c445-4e9b-aafc-06ee3ef55139", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'base_time': None,\n", " 'valid_time': datetime.datetime(2018, 8, 1, 9, 0, tzinfo=tzutc())}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].metadata().datetime()" ] }, { "cell_type": "code", "execution_count": 9, "id": "11f1015b-7dfd-4574-9990-61b528b5eb2b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2018, 8, 1, 9, 0, tzinfo=tzutc())" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].metadata().valid_datetime()" ] }, { "cell_type": "code", "execution_count": 10, "id": "d8942c7c-ed21-4b75-a5bb-d084207b2692", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\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", "
paramlevelbase_datetimevalid_datetimestepnumber
0t500None2018-08-01T09:00:00+00:00NoneNone
1t850None2018-08-01T09:00:00+00:00NoneNone
\n", "
" ], "text/plain": [ " param level base_datetime valid_datetime step number\n", "0 t 500 None 2018-08-01T09:00:00+00:00 None None\n", "1 t 850 None 2018-08-01T09:00:00+00:00 None None" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.sel(param=\"t\").ls()" ] }, { "cell_type": "code", "execution_count": 11, "id": "0d3ab9fa-24c1-46d3-900c-4e162b65f1f8", "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", " \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", "
paramlevelbase_datetimevalid_datetimestepnumber
0t500None2018-08-01T09:00:00+00:00NoneNone
1u500None2018-08-01T09:00:00+00:00NoneNone
2d600None2018-08-01T09:00:00+00:00NoneNone
3t850None2018-08-01T09:00:00+00:00NoneNone
4u850None2018-08-01T09:00:00+00:00NoneNone
5d850None2018-08-01T09:00:00+00:00NoneNone
\n", "
" ], "text/plain": [ " param level base_datetime valid_datetime step number\n", "0 t 500 None 2018-08-01T09:00:00+00:00 None None\n", "1 u 500 None 2018-08-01T09:00:00+00:00 None None\n", "2 d 600 None 2018-08-01T09:00:00+00:00 None None\n", "3 t 850 None 2018-08-01T09:00:00+00:00 None None\n", "4 u 850 None 2018-08-01T09:00:00+00:00 None None\n", "5 d 850 None 2018-08-01T09:00:00+00:00 None None" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.order_by(\"level\").ls()" ] } ], "metadata": { "kernelspec": { "display_name": "dev_ecc", "language": "python", "name": "dev_ecc" }, "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }