{ "cells": [ { "cell_type": "markdown", "id": "102b1cf9-33a5-4310-af4d-bcd17d598d29", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## BUFR: using TEMP data" ] }, { "cell_type": "markdown", "id": "e11bb141-5f5d-42c2-9083-3cccbf9b7799", "metadata": {}, "source": [ "We load a BUFR file with radiosonde observations. First we ensure the example file is available." ] }, { "cell_type": "code", "execution_count": 1, "id": "41f29800-f75d-4adf-a7b9-7fb0381dadd5", "metadata": {}, "outputs": [], "source": [ "import earthkit.data as ekd\n", "ekd.download_example_file(\"temp_10.bufr\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "f0430464-fd40-4964-b64d-4629ddd98909", "metadata": {}, "outputs": [], "source": [ "ds = ekd.from_source(\"file\", \"temp_10.bufr\")" ] }, { "cell_type": "markdown", "id": "b07a6358-ad5a-4bd4-a4a3-c24c635cea39", "metadata": {}, "source": [ "#### Message list" ] }, { "cell_type": "code", "execution_count": 3, "id": "07571864-48af-451b-be4a-6a42436d193c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(ds)" ] }, { "cell_type": "code", "execution_count": 4, "id": "f360de9c-4584-4c78-a948-eaab7e45932d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BUFRMessage(type=2,subType=101,subsets=1,20081208,120000)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[1]" ] }, { "cell_type": "raw", "id": "a05776c0-b71d-42eb-93d9-443b83342b93", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can call :py:meth:`~data.readers.bufr.bufr.BUFRList.head`, :py:meth:`~data.readers.bufr.bufr.BUFRList.tail` and :py:meth:`~data.readers.bufr.bufr.BUFRList.ls` to see metadata from the header section of the BUFR messages: " ] }, { "cell_type": "code", "execution_count": 5, "id": "30e7749a-ea8b-43d2-90d2-31907f228513", "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", " \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", "
editiontypesubtypecmvlvsubsetscomprtypicalDatetypicalTimeidentlatlon
0321019813110200812081200000283667.3726.63
1321019813110200812081200000140056.903.35
2321019813110200812081200000141558.875.67
3321019813110200812081200000100170.93-8.67
4321019813110200812081200000115267.2814.45
\n", "
" ], "text/plain": [ " edition type subtype c mv lv subsets compr typicalDate typicalTime \\\n", "0 3 2 101 98 13 1 1 0 20081208 120000 \n", "1 3 2 101 98 13 1 1 0 20081208 120000 \n", "2 3 2 101 98 13 1 1 0 20081208 120000 \n", "3 3 2 101 98 13 1 1 0 20081208 120000 \n", "4 3 2 101 98 13 1 1 0 20081208 120000 \n", "\n", " ident lat lon \n", "0 02836 67.37 26.63 \n", "1 01400 56.90 3.35 \n", "2 01415 58.87 5.67 \n", "3 01001 70.93 -8.67 \n", "4 01152 67.28 14.45 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.head()" ] }, { "cell_type": "markdown", "id": "c9f29286-cf1c-4e3f-ba70-a715fcd81530", "metadata": {}, "source": [ "#### Inspecting message contents" ] }, { "cell_type": "raw", "id": "2edde77d-4e8e-4fb4-8569-700392310fa7", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can :py:meth:`~data.readers.bufr.bufr.BUFRMessage.dump` the contents of each message into a tree view. The dump below tells us that the message represents a given station and contains vertical profiles on a set of pressure levels (see the **data section**)." ] }, { "cell_type": "code", "execution_count": 6, "id": "613f01a9-8d0b-4dba-b74d-643aa40083ad", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
BUFRMessage(type=2,subType=101,subsets=1,20081208,120000)
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[1].dump()" ] }, { "cell_type": "markdown", "id": "c0f22bfb-9e5f-4a69-ad87-b4d26fd7fb27", "metadata": {}, "source": [ "#### Converting to Pandas" ] }, { "cell_type": "raw", "id": "324fa8d6-4a80-4a3c-98eb-7185c66734e4", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "BUFR data can be extracted into a Pandas dataframe using :py:meth:`~data.readers.bufr.bufr.BUFRList.to_pandas`, which passes all the arguments to the `read_bufr() `_ method from :xref:`pdbufr`.\n", "\n", "The following example shows how the get the temperature profile for a given station. Please note that \"WMO_station_id\" and \"data_datetime\" are generated keys and not present in the BUFR messages themselves." ] }, { "cell_type": "code", "execution_count": 7, "id": "53ac87d6-09b9-46c3-bc54-07fd2a175b05", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pressureairTemperaturedata_datetimeWMO_station_id
0100300.0279.82008-12-08 12:00:001415
1100000.0280.02008-12-08 12:00:001415
298900.0NaN2008-12-08 12:00:001415
397800.0NaN2008-12-08 12:00:001415
492500.0275.22008-12-08 12:00:001415
...............
581840.0NaN2008-12-08 12:00:001415
591790.0197.52008-12-08 12:00:001415
601500.0193.12008-12-08 12:00:001415
611380.0NaN2008-12-08 12:00:001415
621370.0193.12008-12-08 12:00:001415
\n", "

63 rows × 4 columns

\n", "
" ], "text/plain": [ " pressure airTemperature data_datetime WMO_station_id\n", "0 100300.0 279.8 2008-12-08 12:00:00 1415\n", "1 100000.0 280.0 2008-12-08 12:00:00 1415\n", "2 98900.0 NaN 2008-12-08 12:00:00 1415\n", "3 97800.0 NaN 2008-12-08 12:00:00 1415\n", "4 92500.0 275.2 2008-12-08 12:00:00 1415\n", ".. ... ... ... ...\n", "58 1840.0 NaN 2008-12-08 12:00:00 1415\n", "59 1790.0 197.5 2008-12-08 12:00:00 1415\n", "60 1500.0 193.1 2008-12-08 12:00:00 1415\n", "61 1380.0 NaN 2008-12-08 12:00:00 1415\n", "62 1370.0 193.1 2008-12-08 12:00:00 1415\n", "\n", "[63 rows x 4 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = ds.to_pandas(columns=(\"WMO_station_id\", \"data_datetime\", \"pressure\", \"airTemperature\"),\n", " filters={\"WMO_station_id\": 1415})\n", "df" ] }, { "cell_type": "code", "execution_count": null, "id": "06ca403f-0998-473c-a733-f834f4b61fdf", "metadata": {}, "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 }