{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# ⌚ Herbie Latest\n", "\n", "A little helper to get the most recent model data. (Not super efficient, but it works).\n", "\n", "> ## WARNING: This is deprecated in favor of `HerbieLatest`\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from herbie import Herbie, Herbie_latest, FastHerbie\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Could not find 1/6 GRIB files.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.04 s, sys: 274 ms, total: 1.31 s\n", "Wall time: 5.62 s\n" ] }, { "data": { "text/plain": [ "\u001b[48;2;255;255;255m\u001b[38;2;136;33;27m▌\u001b[0m\u001b[38;2;12;53;118m\u001b[48;2;240;234;210m▌\u001b[38;2;0;0;0m\u001b[1mHerbie\u001b[0m HRRR model \u001b[3msfc\u001b[0m product initialized \u001b[38;2;41;130;13m2024-Jan-19 04:00 UTC\u001b[92m F00\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3msource=aws\u001b[0m" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "H = Herbie_latest()\n", "H" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[48;2;255;255;255m\u001b[38;2;136;33;27m▌\u001b[0m\u001b[38;2;12;53;118m\u001b[48;2;240;234;210m▌\u001b[38;2;0;0;0m\u001b[1mHerbie\u001b[0m GFS model \u001b[3mpgrb2.0p25\u001b[0m product initialized \u001b[38;2;41;130;13m2022-Dec-10 18:00 UTC\u001b[92m F06\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3msource=aws\u001b[0m" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = Herbie_latest(model=\"gfs\", n=6, freq=\"6H\", fxx=[6])\n", "H" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Latest with a loop\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from herbie import Herbie\n", "import pandas as pd\n", "\n", "# Create a list of dates to try\n", "dates = pd.date_range(\n", " pd.Timestamp.utcnow().floor(\"1h\").tz_localize(None), periods=6, freq=\"-1H\"\n", ")\n", "\n", "# Find first existing Herbie object\n", "for i in dates:\n", " H = Herbie(i, model=\"hrrr\", priority=[\"aws\", \"nomads\"])\n", " if H.grib:\n", " break" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DatetimeIndex(['2024-01-19 05:00:00', '2024-01-19 04:00:00',\n", " '2024-01-19 03:00:00', '2024-01-19 02:00:00',\n", " '2024-01-19 01:00:00', '2024-01-19 00:00:00'],\n", " dtype='datetime64[ns]', freq='-1H')\n" ] } ], "source": [ "print(dates)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2024-01-19 05:00:00', '2024-01-19 04:00:00',\n", " '2024-01-19 03:00:00', '2024-01-19 02:00:00',\n", " '2024-01-19 01:00:00', '2024-01-19 00:00:00'],\n", " dtype='datetime64[ns]', freq='-1H')" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "# Create a list of dates to try\n", "dates = pd.date_range(\n", " pd.Timestamp.utcnow().floor(\"1h\").tz_localize(None), periods=6, freq=\"-1H\"\n", ")\n", "dates" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "💔 Did not find ┊ model=hrrr ┊ \u001b[3mproduct=sfc\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 05:00 UTC\u001b[92m F00\u001b[0m\n", "✅ Found ┊ model=hrrr ┊ \u001b[3mproduct=sfc\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 04:00 UTC\u001b[92m F00\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mGRIB2 @ aws\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mIDX @ aws\u001b[0m\n", "CPU times: user 159 ms, sys: 24 ms, total: 182 ms\n", "Wall time: 2.09 s\n" ] } ], "source": [ "%%time\n", "\n", "for i in dates:\n", " H = Herbie(i, model=\"hrrr\", priority=[\"aws\", \"nomads\"])\n", " if H.grib:\n", " break" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[48;2;255;255;255m\u001b[38;2;136;33;27m▌\u001b[0m\u001b[38;2;12;53;118m\u001b[48;2;240;234;210m▌\u001b[38;2;0;0;0m\u001b[1mHerbie\u001b[0m HRRR model \u001b[3msfc\u001b[0m product initialized \u001b[38;2;41;130;13m2024-Jan-19 04:00 UTC\u001b[92m F00\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3msource=aws\u001b[0m" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Most recent GFS (loop)\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DatetimeIndex(['2024-01-19 00:00:00', '2024-01-18 18:00:00',\n", " '2024-01-18 12:00:00', '2024-01-18 06:00:00'],\n", " dtype='datetime64[ns]', freq='-6H')\n", "✅ Found ┊ model=gfs ┊ \u001b[3mproduct=pgrb2.0p25\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 00:00 UTC\u001b[92m F00\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mGRIB2 @ aws\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mIDX @ aws\u001b[0m\n", "CPU times: user 97 ms, sys: 12.3 ms, total: 109 ms\n", "Wall time: 848 ms\n" ] } ], "source": [ "%%time\n", "\n", "from herbie import Herbie\n", "import pandas as pd\n", "\n", "# Create a list of dates to try. GFS runs every 6 hours\n", "dates = pd.date_range(\n", " pd.Timestamp.utcnow().floor(\"6h\").tz_localize(None), periods=4, freq=\"-6H\"\n", ")\n", "\n", "print(dates)\n", "\n", "# Find first existing Herbie object\n", "for i in dates:\n", " H = Herbie(i, model=\"gfs\", priority=[\"aws\", \"nomads\"])\n", " if H.grib:\n", " break" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DatetimeIndex(['2024-01-19 05:00:00', '2024-01-19 04:00:00',\n", " '2024-01-19 03:00:00', '2024-01-19 02:00:00',\n", " '2024-01-19 01:00:00', '2024-01-19 00:00:00',\n", " '2024-01-18 23:00:00', '2024-01-18 22:00:00',\n", " '2024-01-18 21:00:00', '2024-01-18 20:00:00',\n", " '2024-01-18 19:00:00', '2024-01-18 18:00:00'],\n", " dtype='datetime64[ns]', freq='-1H')\n", "💔 Did not find ┊ model=gfs ┊ \u001b[3mproduct=pgrb2.0p25\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 05:00 UTC\u001b[92m F00\u001b[0m\n", "💔 Did not find ┊ model=gfs ┊ \u001b[3mproduct=pgrb2.0p25\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 04:00 UTC\u001b[92m F00\u001b[0m\n", "💔 Did not find ┊ model=gfs ┊ \u001b[3mproduct=pgrb2.0p25\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 03:00 UTC\u001b[92m F00\u001b[0m\n", "💔 Did not find ┊ model=gfs ┊ \u001b[3mproduct=pgrb2.0p25\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 02:00 UTC\u001b[92m F00\u001b[0m\n", "💔 Did not find ┊ model=gfs ┊ \u001b[3mproduct=pgrb2.0p25\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 01:00 UTC\u001b[92m F00\u001b[0m\n", "✅ Found ┊ model=gfs ┊ \u001b[3mproduct=pgrb2.0p25\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 00:00 UTC\u001b[92m F00\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mGRIB2 @ aws\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mIDX @ aws\u001b[0m\n", "CPU times: user 638 ms, sys: 54.9 ms, total: 693 ms\n", "Wall time: 7.6 s\n" ] } ], "source": [ "%%time\n", "\n", "from herbie import Herbie\n", "import pandas as pd\n", "\n", "# Create a list of dates to try. GFS runs every 6 hours\n", "dates = pd.date_range(\n", " pd.Timestamp.utcnow().floor(\"1h\").tz_localize(None), periods=12, freq=\"-1H\"\n", ")\n", "\n", "print(dates)\n", "\n", "# Find first existing Herbie object\n", "for i in dates:\n", " H = Herbie(i, model=\"gfs\", priority=[\"aws\", \"nomads\"])\n", " if H.grib:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Wait for specific time\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "import time\n", "\n", "time.sleep(5)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✅ Found ┊ model=hrrr ┊ \u001b[3mproduct=sfc\u001b[0m ┊ \u001b[38;2;41;130;13m2024-Jan-19 05:00 UTC\u001b[92m F00\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mGRIB2 @ aws\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mIDX @ aws\u001b[0m\n" ] } ], "source": [ "import time\n", "\n", "now = pd.Timestamp.utcnow().floor(\"1h\").tz_localize(None)\n", "attempts = 0\n", "H = Herbie(now, model=\"hrrr\", priority=[\"aws\", \"nomads\"])\n", "while H.grib is None:\n", " # Try to find file again\n", " H = Herbie(now, model=\"hrrr\", priority=[\"aws\", \"nomads\"])\n", "\n", " # Wait 5 seconds\n", " time.sleep(5)\n", " attempts += 1\n", " print(f\"{attempts=}\")" ] } ], "metadata": { "kernelspec": { "display_name": "herbie-dev", "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.12.2" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "ac511961bc6976a3d289cde8fc247e6b0e9298cf513cb762aa0a7df646d192ed" } } }, "nbformat": 4, "nbformat_minor": 2 }