{ "cells": [ { "cell_type": "markdown", "id": "570a34d8-7958-4de7-b4b1-76d369ce4146", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## GRIB: generating time series" ] }, { "cell_type": "raw", "id": "5eb4fabf-b21e-4e10-b572-8095daaacd70", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We will read temperature data from a GRIB file containing forecast steps. First, we ensure the example file is available, then read it with :ref:`from_source() `." ] }, { "cell_type": "code", "execution_count": 1, "id": "87e4f240-b145-4c4d-9906-38db9a7b3a99", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import earthkit.data as ekd\n", "from earthkit.geo import nearest_point_haversine" ] }, { "cell_type": "code", "execution_count": 2, "id": "8ff42074-f9af-4f1b-a7c3-3d19ae54cf43", "metadata": {}, "outputs": [], "source": [ "ekd.download_example_file(\"time_series.grib\")\n", "ds = ekd.from_source(\"file\", \"time_series.grib\").sel(param=\"t\")" ] }, { "cell_type": "markdown", "id": "e35f59b4-586c-42b5-a24f-5a8c50db98aa", "metadata": {}, "source": [ "Our data contains 4 steps." ] }, { "cell_type": "code", "execution_count": 3, "id": "a635a172-ca2c-446d-b61e-7851821747f9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 3, 6, 9]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.metadata(\"step\")" ] }, { "cell_type": "markdown", "id": "24fd9b22-becd-404d-a240-0bde0d952d16", "metadata": {}, "source": [ "We define a **reference point** and get the index of the nearest gridpoint. We utilise the fact that all the fields have the same grid, so we need not do it field by field. " ] }, { "cell_type": "code", "execution_count": 4, "id": "2578089a-c895-44a0-be5d-e6f5d6f30998", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([12])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "latlon = ds.to_latlon()\n", "lat = latlon[\"lat\"]\n", "lon = latlon[\"lon\"]\n", "\n", "p_ref = (51.45, -0.97)\n", "idx, dist = nearest_point_haversine(p_ref, (lat, lon))\n", "idx" ] }, { "cell_type": "markdown", "id": "a5f1d0e8-41da-4a8a-b3a6-ea8e0c50b420", "metadata": {}, "source": [ "With the resulting index we can get the values at the nearest gridpoint." ] }, { "cell_type": "code", "execution_count": 5, "id": "e8b8cc37-efab-4870-bc54-20ebb6c8faa7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[280.44058228],\n", " [280.31297302],\n", " [280.2789917 ],\n", " [280.08499146]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = ds.values[:,idx]\n", "v" ] }, { "cell_type": "markdown", "id": "68f4738a-48f7-488d-8534-631c6422ae27", "metadata": {}, "source": [ "We extract the datetime for each step." ] }, { "cell_type": "code", "execution_count": 6, "id": "4809ecd5-837e-4d36-b411-cced61c306f6", "metadata": {}, "outputs": [], "source": [ "t = ds.metadata(\"valid_datetime\")" ] }, { "cell_type": "markdown", "id": "571d3091-d881-4060-b7f7-89da3be302a8", "metadata": {}, "source": [ "With this we can now print out the time series." ] }, { "cell_type": "code", "execution_count": 7, "id": "eb68fa7b-dc43-4b71-a843-8454ced5b4cb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2020-12-21T12:00:00 [280.44058228]\n", "2020-12-21T15:00:00 [280.31297302]\n", "2020-12-21T18:00:00 [280.2789917]\n", "2020-12-21T21:00:00 [280.08499146]\n" ] } ], "source": [ "for v1, v2 in zip(t, v):\n", " print(v1, v2)" ] }, { "cell_type": "markdown", "id": "bb92e03b-3ab7-4470-a423-ad3fca9262cb", "metadata": {}, "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 }