{ "cells": [ { "cell_type": "markdown", "id": "af0d7600-d3fb-4b3d-98b2-630f613c4f9e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## list-of-dicts: defining geography" ] }, { "cell_type": "raw", "id": "150bca58-93b3-4ae9-b7fe-b5350cbc117b", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This example demonstrates the various ways to define the values and the geography for :ref:`data-sources-lod` fieldlists. " ] }, { "cell_type": "raw", "id": "756ea884-74b9-4a46-a7f0-3a306648700b", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "First, we create a helper method to generate the input data." ] }, { "cell_type": "code", "execution_count": 1, "id": "49a5d9d0-2b27-4662-941f-2e7fc89b1059", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import earthkit.data as ekd\n", "\n", "def build_lod(data):\n", " prototype = {\n", " **data,\n", " \"valid_datetime\": \"2018-08-01T09:00:00Z\",\n", " \"param\": \"t\", \n", " }\n", " \n", " lod = [\n", " {\"level\": 500, **prototype},\n", " {\"level\": 850, **prototype},\n", " ]\n", " return lod" ] }, { "cell_type": "markdown", "id": "5e0e6d2a-77f2-4829-b03f-809b5073771f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Disctinct latitudes-longitudes" ] }, { "cell_type": "markdown", "id": "df0ee6f1-5681-401d-a00e-3f67b15aa064", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "For structured grids, the latitudes and longitudes can be specified as distinct values, the 2D field structure will be automatically built from them." ] }, { "cell_type": "code", "execution_count": 2, "id": "fdde9002-e8fe-47d1-9ff6-5fb9928c9328", "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", "
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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"latitudes\": np.array([10.0, 0.0, -10.0]),\n", " \"longitudes\": np.array([20, 40.0]),\n", " \"values\": np.array([1, 2, 3, 4, 5, 6])}\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", build_lod(data))\n", "ds.ls()" ] }, { "cell_type": "code", "execution_count": 3, "id": "d745b047-ea0a-4acd-b4e2-d9a9d406fcc4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "((3, 2),\n", " {'lat': array([[ 10., 10.],\n", " [ 0., 0.],\n", " [-10., -10.]]),\n", " 'lon': array([[20., 40.],\n", " [20., 40.],\n", " [20., 40.]])},\n", " array([[1, 2],\n", " [3, 4],\n", " [5, 6]]))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[0].shape, ds[0].to_latlon(), ds[0].to_numpy()" ] }, { "cell_type": "markdown", "id": "b4ea54ff-4164-4621-8c3a-9780c3ae95dc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can define the same data by using the actual shape for the \"values\"." ] }, { "cell_type": "code", "execution_count": 4, "id": "89640c9d-3e6b-402d-8192-d243f23d6ad6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "((3, 2),\n", " {'lat': array([[ 10., 10.],\n", " [ 0., 0.],\n", " [-10., -10.]]),\n", " 'lon': array([[20., 40.],\n", " [20., 40.],\n", " [20., 40.]])},\n", " array([[1, 2],\n", " [3, 4],\n", " [5, 6]]))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"latitudes\": np.array([10.0, 0.0, -10.0]),\n", " \"longitudes\": np.array([20, 40.0]),\n", " \"values\": np.array([[1, 2], [3, 4], [5, 6]])}\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", build_lod(data))\n", "ds[0].shape, ds[0].to_latlon(), ds[0].to_numpy()" ] }, { "cell_type": "markdown", "id": "0b4b846d-4e9d-4467-9d6a-6245e3b75655", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Alternatively, the \"disctinctLatitudes\" and \"distinctLongitudes\" keys can be used. The result will be the same." ] }, { "cell_type": "code", "execution_count": 5, "id": "b575c6cc-46ef-4f80-a2ae-8321640f7d5f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "((3, 2),\n", " {'lat': array([[ 10., 10.],\n", " [ 0., 0.],\n", " [-10., -10.]]),\n", " 'lon': array([[20., 40.],\n", " [20., 40.],\n", " [20., 40.]])},\n", " array([[1, 2],\n", " [3, 4],\n", " [5, 6]]))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"distinctLatitudes\": np.array([10.0, 0.0, -10.0]),\n", " \"distinctLongitudes\": np.array([20, 40.0]),\n", " \"values\": np.array([1, 2, 3, 4, 5, 6])}\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", build_lod(data))\n", "ds[0].shape, ds[0].to_latlon(), ds[0].to_numpy()" ] }, { "cell_type": "markdown", "id": "993d527b-e4bd-41a4-864d-d19ff19f382d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Full latitudes and longitudes" ] }, { "cell_type": "markdown", "id": "4240441e-acb7-4a7d-aca6-b5d2a5a0ba43", "metadata": { "editable": true, "raw_mimetype": "", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The other supported case is when the \"latitudes\" and \"longitudes\" contain all the points." ] }, { "cell_type": "markdown", "id": "d2c5dbae-93b2-4551-8476-50ec286c27a2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This example is the same as the distinct case above but the \"latitudes\" and \"longitudes\" now contain all the grid points." ] }, { "cell_type": "code", "execution_count": 6, "id": "a3cd11de-3468-4130-a135-2f800d370441", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "((6,),\n", " {'lat': array([ 10., 10., 0., 0., -10., -10.]),\n", " 'lon': array([20., 40., 20., 40., 20., 40.])},\n", " array([1, 2, 3, 4, 5, 6]))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"latitudes\": np.array([10., 10., 0., 0., -10.0, -10.0]),\n", " \"longitudes\": np.array([20, 40.0, 20., 40., 20., 40.]),\n", " \"values\": np.array([1, 2, 3, 4, 5, 6])}\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", build_lod(data))\n", "ds[0].shape, ds[0].to_latlon(), ds[0].to_numpy()" ] }, { "cell_type": "markdown", "id": "01321a54-5642-45d9-900b-18d6383208d0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can introduce the 2D structure by using a proper shape either in the \"latitudes\"/\"longitudes\" or the \"values\". The field's shape will be the one with the higher dimensionality." ] }, { "cell_type": "code", "execution_count": 7, "id": "fe935f37-745c-4d88-924c-383b5ba510c6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "((3, 2),\n", " {'lat': array([[ 10., 10.],\n", " [ 0., 0.],\n", " [-10., -10.]]),\n", " 'lon': array([[20., 40.],\n", " [20., 40.],\n", " [20., 40.]])},\n", " array([[1, 2],\n", " [3, 4],\n", " [5, 6]]))" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"latitudes\": np.array([[10., 10.], [0., 0.], [-10.0, -10.0]]),\n", " \"longitudes\": np.array([[20.0, 40.], [20., 40.], [20., 40.]]),\n", " \"values\": np.array([1, 2, 3, 4, 5, 6])}\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", build_lod(data))\n", "ds[0].shape, ds[0].to_latlon(), ds[0].to_numpy()" ] }, { "cell_type": "markdown", "id": "b14dba07-316b-4610-8d14-68badef494d3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "In this mode we are free to use arbitrary \"latitudes\" and \"longitudes\" without having any structure." ] }, { "cell_type": "code", "execution_count": 8, "id": "875234f9-43fa-4bb5-bc00-cca1c11fa0f9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "((5,),\n", " {'lat': array([10., 10., 6., 6., 6.]),\n", " 'lon': array([ 20., 40., -40., 0., 40.])},\n", " array([1, 2, 3, 4, 5]))" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\"latitudes\": np.array([10., 10., 6., 6., 6.]),\n", " \"longitudes\": np.array([20, 40.0, -40., 0., 40.]),\n", " \"values\": np.array([1, 2, 3, 4, 5])}\n", "\n", "ds = ekd.from_source(\"list-of-dicts\", build_lod(data))\n", "ds[0].shape, ds[0].to_latlon(), ds[0].to_numpy()" ] }, { "cell_type": "code", "execution_count": null, "id": "cd15dae5-b01b-4c33-8d34-d214fb787f67", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] } ], "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 }