{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "tobac example: Compute bulk statistics during feature detection\n", "=== \n", "\n", "You can compute bulk statistics for features from the feature detection or the masked features from the segmentation mask. \n", "\n", "This example shows how to derive some basic statistics for precipitation features associated with isolated deep convective clouds using the same data as in [our example for precipitation tracking](https://github.com/tobac-project/tobac/blob/main/examples/Example_Precip_Tracking/Example_Precip_Tracking.ipynb). The data used in this example is downloaded from automatically as part of the notebook. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-12-18T15:02:37.722182Z", "iopub.status.busy": "2025-12-18T15:02:37.722089Z", "iopub.status.idle": "2025-12-18T15:02:38.451987Z", "shell.execute_reply": "2025-12-18T15:02:38.451721Z" } }, "outputs": [], "source": [ "# Import libraries\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "import matplotlib.pyplot as plt\n", "import datetime\n", "import shutil\n", "from six.moves import urllib\n", "from pathlib import Path\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-12-18T15:02:38.453600Z", "iopub.status.busy": "2025-12-18T15:02:38.453414Z", "iopub.status.idle": "2025-12-18T15:02:39.873069Z", "shell.execute_reply": "2025-12-18T15:02:39.872776Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "using tobac version 1.6.2\n" ] } ], "source": [ "# Import tobac itself\n", "import tobac\n", "\n", "print(\"using tobac version\", str(tobac.__version__))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-12-18T15:02:39.892268Z", "iopub.status.busy": "2025-12-18T15:02:39.892030Z", "iopub.status.idle": "2025-12-18T15:02:39.894286Z", "shell.execute_reply": "2025-12-18T15:02:39.893981Z" } }, "outputs": [], "source": [ "# Disable a few warnings:\n", "import warnings\n", "\n", "warnings.filterwarnings(\"ignore\", category=UserWarning, append=True)\n", "warnings.filterwarnings(\"ignore\", category=RuntimeWarning, append=True)\n", "warnings.filterwarnings(\"ignore\", category=FutureWarning, append=True)\n", "warnings.filterwarnings(\"ignore\", category=pd.io.pytables.PerformanceWarning)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Download example data:**\n", "\n", "Actual download has to be performed only once for all example notebooks!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-12-18T15:02:39.895598Z", "iopub.status.busy": "2025-12-18T15:02:39.895494Z", "iopub.status.idle": "2025-12-18T15:02:39.897146Z", "shell.execute_reply": "2025-12-18T15:02:39.896880Z" } }, "outputs": [], "source": [ "data_out=Path('../../../examples')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-12-18T15:02:39.898527Z", "iopub.status.busy": "2025-12-18T15:02:39.898442Z", "iopub.status.idle": "2025-12-18T15:02:39.905454Z", "shell.execute_reply": "2025-12-18T15:02:39.905236Z" } }, "outputs": [], "source": [ "# Download the data: This only has to be done once for all tobac examples and can take a while\n", "data_file = list(data_out.rglob(\"data/Example_input_Precip.nc\"))\n", "if len(data_file) == 0:\n", " file_path = \"https://zenodo.org/records/3195910/files/climate-processes/tobac_example_data-v1.0.1.zip\"\n", " # file_path='http://zenodo..'\n", " tempfile = Path(\"temp.zip\")\n", " print(\"start downloading data\")\n", " request = urllib.request.urlretrieve(file_path, tempfile)\n", " print(\"start extracting data\")\n", " shutil.unpack_archive(tempfile, data_out)\n", " tempfile.unlink()\n", " print(\"data extracted\")\n", " data_file = list(data_out.rglob(\"data/Example_input_Precip.nc\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Load Data from downloaded file:**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-12-18T15:02:39.906666Z", "iopub.status.busy": "2025-12-18T15:02:39.906581Z", "iopub.status.idle": "2025-12-18T15:02:40.404307Z", "shell.execute_reply": "2025-12-18T15:02:40.404023Z" } }, "outputs": [], "source": [ "Precip = xr.open_dataset(data_file[0])[\"surface_precipitation_average\"]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-12-18T15:02:40.405849Z", "iopub.status.busy": "2025-12-18T15:02:40.405699Z", "iopub.status.idle": "2025-12-18T15:02:40.413703Z", "shell.execute_reply": "2025-12-18T15:02:40.413465Z" } }, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray 'surface_precipitation_average' (time: 47, south_north: 198,\n",
" west_east: 198)> Size: 7MB\n",
"[1842588 values with dtype=float32]\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 376B 2013-06-19T20:05:00 ... 2013-06-1...\n",
" * south_north (south_north) int64 2kB 281 282 283 284 285 ... 475 476 477 478\n",
" * west_east (west_east) int64 2kB 281 282 283 284 285 ... 475 476 477 478\n",
" latitude (south_north, west_east) float32 157kB ...\n",
" longitude (south_north, west_east) float32 157kB ...\n",
" x (west_east) float64 2kB ...\n",
" y (south_north) float64 2kB ...\n",
" x_0 (west_east) int64 2kB ...\n",
" y_0 (south_north) int64 2kB ...\n",
"Attributes:\n",
" long_name: surface_precipitation_average\n",
" units: mm h-1| \n", " | frame | \n", "idx | \n", "hdim_1 | \n", "hdim_2 | \n", "num | \n", "threshold_value | \n", "mean_precip | \n", "total_precip | \n", "max_precip | \n", "percentiles | \n", "... | \n", "time | \n", "timestr | \n", "south_north | \n", "west_east | \n", "latitude | \n", "longitude | \n", "x | \n", "y | \n", "x_0 | \n", "y_0 | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "0 | \n", "1 | \n", "50.065727 | \n", "139.857477 | \n", "9 | \n", "1 | \n", "1.241012 | \n", "11.169106 | \n", "1.528488 | \n", "([1.4821563005447387, 1.5192213106155394],) | \n", "... | \n", "2013-06-19 20:05:00 | \n", "2013-06-19 20:05:00 | \n", "331.065727 | \n", "420.857477 | \n", "29.846362 | \n", "-94.172015 | \n", "210678.738492 | \n", "165782.863285 | \n", "420.857477 | \n", "331.065727 | \n", "
| 1 | \n", "0 | \n", "15 | \n", "120.527119 | \n", "172.500325 | \n", "4 | \n", "1 | \n", "1.25016 | \n", "5.000638 | \n", "1.267255 | \n", "([1.266197031736374, 1.267043651342392],) | \n", "... | \n", "2013-06-19 20:05:00 | \n", "2013-06-19 20:05:00 | \n", "401.527119 | \n", "453.500325 | \n", "30.166929 | \n", "-93.996892 | \n", "227000.162468 | \n", "201013.559414 | \n", "453.500325 | \n", "401.527119 | \n", "
| 2 | \n", "0 | \n", "18 | \n", "126.779273 | \n", "145.368401 | \n", "15 | \n", "1 | \n", "1.564113 | \n", "23.461691 | \n", "2.321664 | \n", "([2.268769121170044, 2.311084909439087],) | \n", "... | \n", "2013-06-19 20:05:00 | \n", "2013-06-19 20:05:00 | \n", "407.779273 | \n", "426.368401 | \n", "30.196499 | \n", "-94.139960 | \n", "213434.200454 | \n", "204139.636582 | \n", "426.368401 | \n", "407.779273 | \n", "
| 3 | \n", "0 | \n", "34 | \n", "111.611369 | \n", "155.452030 | \n", "4 | \n", "2 | \n", "2.313658 | \n", "9.25463 | \n", "2.409467 | \n", "([2.4016830801963804, 2.4079100108146667],) | \n", "... | \n", "2013-06-19 20:05:00 | \n", "2013-06-19 20:05:00 | \n", "392.611369 | \n", "436.452030 | \n", "30.126871 | \n", "-94.087317 | \n", "218476.015240 | \n", "196555.684682 | \n", "436.452030 | \n", "392.611369 | \n", "
| 4 | \n", "0 | \n", "35 | \n", "111.765231 | \n", "164.938866 | \n", "8 | \n", "2 | \n", "2.610886 | \n", "20.887089 | \n", "3.081343 | \n", "([2.995926022529602, 3.064259362220764],) | \n", "... | \n", "2013-06-19 20:05:00 | \n", "2013-06-19 20:05:00 | \n", "392.765231 | \n", "445.938866 | \n", "30.127221 | \n", "-94.037226 | \n", "223219.433218 | \n", "196632.615461 | \n", "445.938866 | \n", "392.765231 | \n", "
5 rows × 21 columns
\n", "