{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Reading data from objects" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import numpy as np\n", "\n", "import earthkit.data as ekd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "demo_ndarray = np.random.rand(10,10)*10.\n", "demo_lats = np.arange(0,91,10)\n", "demo_lons = np.arange(0,91,10)\n", "# demo_ndarray" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 800B\n",
       "Dimensions:      (latitude: 10, longitude: 10)\n",
       "Dimensions without coordinates: latitude, longitude\n",
       "Data variables:\n",
       "    temperature  (latitude, longitude) float64 800B 9.924 6.907 ... 9.595 7.987\n",
       "Attributes:\n",
       "    purpose:  Demonstration\n",
       "    source:   EarthkitExamples
" ], "text/plain": [ " Size: 800B\n", "Dimensions: (latitude: 10, longitude: 10)\n", "Dimensions without coordinates: latitude, longitude\n", "Data variables:\n", " temperature (latitude, longitude) float64 800B 9.924 6.907 ... 9.595 7.987\n", "Attributes:\n", " purpose: Demonstration\n", " source: EarthkitExamples" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demo_da = xr.DataArray(\n", " demo_ndarray, name=\"temperature\", dims={'latitude':demo_lats, 'longitude': demo_lons},\n", " attrs={'units': 'Celsius', \"standard_name\": \"air_temperature\"}\n", ")\n", "demo_ds = demo_da.to_dataset().assign_attrs({'purpose': 'Demonstration', 'source': 'EarthkitExamples'})\n", "\n", "demo_ds" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9.92431552, 6.90655349, 5.7868388 , 8.41121321, 1.69326597,\n", " 5.8638489 , 7.88491778, 5.32100601, 2.51267228, 6.40874136],\n", " [7.20922708, 4.84160044, 3.19112609, 9.86412211, 1.93044519,\n", " 3.21458366, 6.8855625 , 0.89282633, 9.18236597, 9.39104825],\n", " [8.3800421 , 5.35997137, 9.3995762 , 6.78596141, 0.27519014,\n", " 4.44691476, 4.17433485, 3.90901429, 0.45603054, 8.67484758],\n", " [0.61742956, 8.75248349, 7.88558635, 9.58852544, 7.3215772 ,\n", " 5.61184126, 9.82122202, 7.91967289, 0.49805722, 2.7364674 ],\n", " [2.55413561, 6.54633036, 6.05313804, 0.15144518, 1.93220753,\n", " 2.3002996 , 1.44260029, 7.37107842, 7.46102275, 9.88265362],\n", " [7.97129689, 6.79109449, 5.18966605, 3.93028723, 9.46987938,\n", " 1.29953927, 4.96990227, 0.84910365, 7.41595432, 7.16546269],\n", " [0.9570322 , 5.11237267, 3.14709369, 9.48180342, 8.72560246,\n", " 3.79490185, 8.82257466, 0.68210994, 0.65729065, 1.13449939],\n", " [0.81227942, 6.27177992, 9.11285075, 8.06514698, 1.7646168 ,\n", " 4.39226812, 7.39714433, 5.35081803, 9.11160899, 1.12502894],\n", " [7.62276549, 2.6358015 , 9.85262721, 9.52385975, 5.46631338,\n", " 3.68646726, 8.33371783, 7.36604175, 1.53462783, 0.54594352],\n", " [3.89025704, 7.51555425, 0.46221449, 3.89854723, 2.44635243,\n", " 0.08044784, 3.79812982, 6.92937516, 9.59464767, 7.98681252]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ek_nd = ekd.from_object(demo_ndarray)\n", "ek_nd.to_numpy()" ] }, { "cell_type": "code", "execution_count": 5, "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", " \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", "
temperature
latitudelongitude
009.924316
16.906553
25.786839
38.411213
41.693266
.........
950.080448
63.798130
76.929375
89.594648
97.986813
\n", "

100 rows × 1 columns

\n", "
" ], "text/plain": [ " temperature\n", "latitude longitude \n", "0 0 9.924316\n", " 1 6.906553\n", " 2 5.786839\n", " 3 8.411213\n", " 4 1.693266\n", "... ...\n", "9 5 0.080448\n", " 6 3.798130\n", " 7 6.929375\n", " 8 9.594648\n", " 9 7.986813\n", "\n", "[100 rows x 1 columns]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ek_da = ekd.from_object(demo_da)\n", "ek_da.to_pandas()" ] }, { "cell_type": "code", "execution_count": 6, "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", " \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", "
temperature
latitudelongitude
009.924316
16.906553
25.786839
38.411213
41.693266
.........
950.080448
63.798130
76.929375
89.594648
97.986813
\n", "

100 rows × 1 columns

\n", "
" ], "text/plain": [ " temperature\n", "latitude longitude \n", "0 0 9.924316\n", " 1 6.906553\n", " 2 5.786839\n", " 3 8.411213\n", " 4 1.693266\n", "... ...\n", "9 5 0.080448\n", " 6 3.798130\n", " 7 6.929375\n", " 8 9.594648\n", " 9 7.986813\n", "\n", "[100 rows x 1 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ek_ds = ekd.from_object(demo_ds)\n", "ek_ds.to_pandas()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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": 4 }