{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GRIB specificities" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:19.525424Z", "iopub.status.busy": "2025-01-13T11:35:19.524909Z", "iopub.status.idle": "2025-01-13T11:35:20.059178Z", "shell.execute_reply": "2025-01-13T11:35:20.058709Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "# for figures in notebook\n", "\n", "# import & initialize epygram\n", "import epygram\n", "epygram.init_env()\n", "\n", "import os\n", "INPUTS_DIR = os.path.join('..', 'inputs')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:20.061410Z", "iopub.status.busy": "2025-01-13T11:35:20.061205Z", "iopub.status.idle": "2025-01-13T11:35:20.176333Z", "shell.execute_reply": "2025-01-13T11:35:20.175887Z" } }, "outputs": [], "source": [ "r = epygram.open(os.path.join(INPUTS_DIR, 'grid.arome-forecast.guyane0025+0000:00.grib'), 'r')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Several ways of iterating over all fields/messages" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:20.178422Z", "iopub.status.busy": "2025-01-13T11:35:20.178221Z", "iopub.status.idle": "2025-01-13T11:35:21.423730Z", "shell.execute_reply": "2025-01-13T11:35:21.423100Z" } }, "outputs": [], "source": [ "# Actual iterator, returns H2DField's\n", "n = 0\n", "for f in r:\n", " n += 1\n", "print(n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:21.425772Z", "iopub.status.busy": "2025-01-13T11:35:21.425570Z", "iopub.status.idle": "2025-01-13T11:35:21.428786Z", "shell.execute_reply": "2025-01-13T11:35:21.428404Z" } }, "outputs": [], "source": [ "# Manual iteration on fields\n", "n = 0\n", "while True:\n", " f = r.iter_fields()\n", " if f is None:\n", " break\n", " elif n == 0:\n", " print(type(f))\n", " n += 1\n", "print(n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:21.430590Z", "iopub.status.busy": "2025-01-13T11:35:21.430417Z", "iopub.status.idle": "2025-01-13T11:35:21.553198Z", "shell.execute_reply": "2025-01-13T11:35:21.552724Z" } }, "outputs": [], "source": [ "# Manual iterator on messages; returns epygram.formats.GRIB.GRIBmessage objects, i.e. data is not decoded\n", "n = 0\n", "while True:\n", " m = r.iter_messages()\n", " if m is None:\n", " break\n", " elif n == 0:\n", " print(type(m))\n", " n += 1\n", "print(n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:21.555053Z", "iopub.status.busy": "2025-01-13T11:35:21.554885Z", "iopub.status.idle": "2025-01-13T11:35:21.561795Z", "shell.execute_reply": "2025-01-13T11:35:21.561320Z" } }, "outputs": [], "source": [ "# Pick message at a certain index\n", "m = r.get_message_at_position(15)\n", "print(type(m))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:21.563566Z", "iopub.status.busy": "2025-01-13T11:35:21.563386Z", "iopub.status.idle": "2025-01-13T11:35:21.745687Z", "shell.execute_reply": "2025-01-13T11:35:21.745220Z" } }, "outputs": [], "source": [ "# in a GRIBmessage, GRIB key/values are read only when requested one by one,\n", "print(list(m.keys()))\n", "print('centre', m.get('centre'))\n", "print('typeOfProcessedData', m['typeOfProcessedData'])\n", "print(list(m.keys()))\n", "# or if the whole message is requested to be read\n", "m.readmessage()\n", "print(list(m.keys())[:10])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:21.747687Z", "iopub.status.busy": "2025-01-13T11:35:21.747471Z", "iopub.status.idle": "2025-01-13T11:35:21.751220Z", "shell.execute_reply": "2025-01-13T11:35:21.750798Z" } }, "outputs": [], "source": [ "# Message can then be transformed into epygram H2DField\n", "f = m.as_field()\n", "print(type(f))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:21.752912Z", "iopub.status.busy": "2025-01-13T11:35:21.752755Z", "iopub.status.idle": "2025-01-13T11:35:22.015816Z", "shell.execute_reply": "2025-01-13T11:35:22.015438Z" } }, "outputs": [], "source": [ "# And to find what are the fields present with a partial filter\n", "r.find_fields_in_resource('shortName:2t')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12" } }, "nbformat": 4, "nbformat_minor": 1 }