{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Decumulate rain fields\n", "\n", "and write it back to GRIB, ensuring proper validity" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:33:57.057763Z", "iopub.status.busy": "2025-01-13T11:33:57.057271Z", "iopub.status.idle": "2025-01-13T11:33:57.598080Z", "shell.execute_reply": "2025-01-13T11:33:57.597590Z" } }, "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')\n", "OUTPUTS_DIR = os.path.join('..', 'outputs')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:33:57.600240Z", "iopub.status.busy": "2025-01-13T11:33:57.600027Z", "iopub.status.idle": "2025-01-13T11:33:57.926233Z", "shell.execute_reply": "2025-01-13T11:33:57.925865Z" } }, "outputs": [], "source": [ "paths = [os.path.join(INPUTS_DIR, 'grid.arome-forecast.guyane0025+00{:02}:00.grib'.format(i))\n", " for i in (0, 12, 24)]\n", "resources = [epygram.open(p, 'r') for p in paths]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:33:57.928295Z", "iopub.status.busy": "2025-01-13T11:33:57.928116Z", "iopub.status.idle": "2025-01-13T11:33:58.256439Z", "shell.execute_reply": "2025-01-13T11:33:58.255842Z" } }, "outputs": [], "source": [ "rain = [r.readfield({'discipline':0, 'parameterCategory':1, 'parameterNumber':65})\n", " for r in resources[1:]] # start from 1: term 0, no rain" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:33:58.258613Z", "iopub.status.busy": "2025-01-13T11:33:58.258461Z", "iopub.status.idle": "2025-01-13T11:33:58.262645Z", "shell.execute_reply": "2025-01-13T11:33:58.262183Z" } }, "outputs": [], "source": [ "# rain is accumulated from beginning of forecast\n", "print([r.mean() for r in rain])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:33:58.264347Z", "iopub.status.busy": "2025-01-13T11:33:58.264196Z", "iopub.status.idle": "2025-01-13T11:33:58.267030Z", "shell.execute_reply": "2025-01-13T11:33:58.266657Z" } }, "outputs": [], "source": [ "for i in range(len(rain)):\n", " if i == 0: continue # term 1, no decumulation to do\n", " rain[i].operation('-', rain[i-1])\n", " rain[i].validity[0].set(cumulativeduration=(rain[i].validity[0].get()-rain[i-1].validity[0].get()))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:33:58.268852Z", "iopub.status.busy": "2025-01-13T11:33:58.268568Z", "iopub.status.idle": "2025-01-13T11:33:58.271830Z", "shell.execute_reply": "2025-01-13T11:33:58.271450Z" } }, "outputs": [], "source": [ "print([r.mean() for r in rain])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:33:58.273374Z", "iopub.status.busy": "2025-01-13T11:33:58.273184Z", "iopub.status.idle": "2025-01-13T11:33:58.334959Z", "shell.execute_reply": "2025-01-13T11:33:58.334458Z" } }, "outputs": [], "source": [ "rain_series = epygram.open(os.path.join(OUTPUTS_DIR,'rain_series.grib'), 'w', fmt='GRIB')\n", "for r in rain:\n", " print(r.validity[0])\n", " rain_series.writefield(r)\n", "rain_series.close()" ] }, { "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 }