{ "cells": [ { "cell_type": "markdown", "id": "4af04318-6cd5-49f3-bd41-ca7f0a1b1fd7", "metadata": {}, "source": [ "# `SurfTrack` - Measures\n", "Compute shape, motion, temporal, and intensity characteristics of tracked\n", "surface heatwave events. This notebook continues from the SurfTrack tracking tutorial and assumes \n", "`result` (the tracked labels DataArray) and `var_notrend` (the anomaly field)\n", "are already in memory." ] }, { "cell_type": "markdown", "id": "a1728024-eaf0-4354-b61f-7b63a4f03036", "metadata": {}, "source": [ "### 1. Imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "cb679949-1db2-4871-94d7-7ed22f28dab0", "metadata": {}, "outputs": [], "source": [ "import sys\n", "import os\n", "\n", "# Go up from notebooks/ to repo root\n", "repo_root = os.path.abspath(os.path.join(os.getcwd(), '..'))\n", "sys.path.insert(0, repo_root)" ] }, { "cell_type": "code", "execution_count": 3, "id": "fde805bd-4861-49ba-a12e-2f05b1e47c13", "metadata": {}, "outputs": [], "source": [ "# Measures sub-modules\n", "from ocetrac.SurfTrack.measures.utils import (\n", " ShapeMeasures,\n", " MotionMeasures,\n", " get_duration,\n", " get_initial_detection_time,\n", " calculate_intensity_metrics,\n", " get_object_masks,\n", " run_shape_measures,\n", " run_motion_measures,\n", " run_temporal_measures,\n", " run_intensity_measures,\n", " process_objects_and_calculate_measures,\n", " lons_to_180,\n", ")\n", "from ocetrac.SurfTrack.measures.plotting import plot_displacement" ] }, { "cell_type": "markdown", "id": "c93bbde9-fbb4-4ed0-ba9d-2664bee8d5bc", "metadata": {}, "source": [ "### 2. Recap\n", "\n", "After running `SurfTracker.run()` we have:\n", "- `result` — DataArray (time, lat, lon) of integer event labels, NaN = background\n", "- `var_notrend` — DataArray (time, lat, lon) of SST anomalies (from `calculate_anomalies_trend_features`)" ] }, { "cell_type": "code", "execution_count": 44, "id": "cd41d152-ff56-4cb0-b541-5d8e83cc77e6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total tracked events: 60\n", "Event IDs: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.\n", " 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36.\n", " 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54.\n", " 55. 56. 57. 58. 59. 60.] ...\n" ] } ], "source": [ "# All unique event IDs\n", "event_ids = np.unique(result.values)\n", "event_ids = event_ids[~np.isnan(event_ids)]\n", "print(f\"Total tracked events: {len(event_ids)}\")\n", "print(f\"Event IDs: {event_ids[:]} ...\")" ] }, { "cell_type": "code", "execution_count": 26, "id": "60c3dfba-bd34-4dae-9c93-db8193cdf4ca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (time: 433, lat: 138, lon: 288)> Size: 138MB\n",
"array([[[-0.38585486, -0.3697699 , -0.35270895, ..., -0.43246681,\n",
" -0.42097259, -0.40316005],\n",
" [-0.47201802, -0.46155738, -0.45422549, ..., -0.48133999,\n",
" -0.48522698, -0.48176319],\n",
" [-0.54543594, -0.54607625, -0.53813248, ..., -0.47048807,\n",
" -0.50495293, -0.53207767],\n",
" ...,\n",
" [ 0.46768138, 0.43471435, 0.40061251, ..., 0.43944371,\n",
" 0.50179589, 0.51046424],\n",
" [ 0.6230039 , 0.55412768, 0.49083757, ..., 0.5840006 ,\n",
" 0.65754218, 0.67407608],\n",
" [ 0.79466408, 0.69131989, 0.58016453, ..., 0.78568523,\n",
" 0.84079187, 0.8529317 ]],\n",
"\n",
" [[-0.48895671, -0.46554217, -0.44811693, ..., -0.64097775,\n",
" -0.59068515, -0.53084162],\n",
" [-0.51441074, -0.47031967, -0.44859089, ..., -0.6258642 ,\n",
" -0.60710446, -0.57394917],\n",
" [-0.50192003, -0.46226531, -0.43666332, ..., -0.5055735 ,\n",
" -0.52035278, -0.53196638],\n",
"...\n",
" [ 0.63446571, 0.68550002, 0.72682059, ..., 0.41717646,\n",
" 0.4511896 , 0.55592058],\n",
" [ 0.71495333, 0.76664366, 0.81480749, ..., 0.45474103,\n",
" 0.52750041, 0.63158104],\n",
" [ 0.7618968 , 0.82198597, 0.88405636, ..., 0.50531523,\n",
" 0.59544083, 0.68877213]],\n",
"\n",
" [[ 0.12935723, 0.08974562, 0.03704887, ..., 0.20346442,\n",
" 0.18456779, 0.16207527],\n",
" [-0.00162986, -0.02147578, -0.05354738, ..., 0.03754621,\n",
" 0.02821324, 0.01754266],\n",
" [-0.15763966, -0.17312524, -0.19530826, ..., -0.12498589,\n",
" -0.13530935, -0.14498376],\n",
" ...,\n",
" [ 0.29400464, 0.2701163 , 0.27314633, ..., 0.30132432,\n",
" 0.32881736, 0.32516775],\n",
" [ 0.23614736, 0.24571236, 0.27539847, ..., 0.3047578 ,\n",
" 0.31212475, 0.26960691],\n",
" [ 0.25130636, 0.29760109, 0.36536933, ..., 0.25533547,\n",
" 0.25633358, 0.24157431]]], shape=(433, 138, 288))\n",
"Coordinates:\n",
" * time (time) object 3kB 1979-01-01 00:00:00 ... 2015-01-01 00:00:00\n",
" * lat (lat) float64 1kB -64.55 -63.61 -62.67 -61.73 ... 62.67 63.61 64.55\n",
" * lon (lon) float64 2kB 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8\n",
"Attributes:\n",
" units: K\n",
" long_name: sea surface temperature\n",
" cell_methods: time: mean