{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Gaussian Kernel Density Estimation (method 01)\n", "\n", "One of the methods used to calculate the density of particles is a 2-dimensional horizontal Gaussian Kernel Density Estimation (GKDE). This is a non-parametric (applicable to a non-Gaussian distribution) estimate of the probability density function of a field. \n", "\n", "In this example below, the GKDE is applied to a snapshot of particle positions, on all particles in the whole domain.\n", "\n", "We use the python SciPy stats gaussian\\_kde algorithm with a default kernel size (bandwidth), which is calculated following Scott's Rule [Scott, 1992]. This bandwidth selector calculates the optimal bandwidth value that avoids both over-smoothing and under-smoothing.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Author(s): Laura Gomez Navarro\n", "* Created on: 11/11/22\n", "* Last updated on:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wed Jun 28 16:04:39 2023\n" ] } ], "source": [ "import time\n", "\n", "print(time.ctime(time.time()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 0. Imports and package versions" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from glob import glob\n", "import xarray as xr\n", "import numpy as np\n", "from scipy.stats import gaussian_kde\n", "\n", "import matplotlib.pyplot as plt\n", "import cartopy\n", "import cartopy.crs as ccrs\n", "from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER\n", "import matplotlib.ticker as mticker\n", "import inspect\n", "import sys" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true, "tags": [] }, "outputs": [ { "data": { "application/json": { "Software versions": [ { "module": "Python", "version": "3.8.5 64bit [Clang 10.0.0 ]" }, { "module": "IPython", "version": "7.20.0" }, { "module": "OS", "version": "macOS 10.16 x86_64 i386 64bit" }, { "module": "numpy", "version": "1.19.2" }, { "module": "xarray", "version": "0.16.2" }, { "module": "scipy", "version": "1.6.0" } ] }, "text/html": [ "
| Software | Version |
|---|---|
| Python | 3.8.5 64bit [Clang 10.0.0 ] |
| IPython | 7.20.0 |
| OS | macOS 10.16 x86_64 i386 64bit |
| numpy | 1.19.2 |
| xarray | 0.16.2 |
| scipy | 1.6.0 |
| Wed Jun 28 16:04:46 2023 CEST | |
<xarray.Dataset>\n",
"Dimensions: (obs: 121, traj: 144)\n",
"Dimensions without coordinates: obs, traj\n",
"Data variables:\n",
" trajectory (traj, obs) float64 0.0 0.0 0.0 0.0 ... 143.0 143.0 143.0 143.0\n",
" time (traj, obs) datetime64[ns] 2021-01-01T00:30:00 ... 2021-01-31...\n",
" lat (traj, obs) float32 -39.17 -39.02 -38.9 ... -39.19 -39.21 -39.25\n",
" lon (traj, obs) float32 22.5 22.55 22.61 22.66 ... 17.16 17.18 17.17\n",
" z (traj, obs) float32 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0\n",
" U (traj, obs) float32 nan 2.598e-06 ... 1.845e-07 -7.776e-07\n",
" V (traj, obs) float32 nan 5.796e-06 ... -1.211e-06 -2.669e-06\n",
"Attributes:\n",
" feature_type: trajectory\n",
" Conventions: CF-1.6/CF-1.7\n",
" ncei_template_version: NCEI_NetCDF_Trajectory_Template_v2.0\n",
" parcels_version: 2.3.1.dev20+g92f2fb90\n",
" parcels_mesh: sphericalarray([[ 0., 0., 0., ..., 0., 0., 0.],\n",
" [ 1., 1., 1., ..., 1., 1., 1.],\n",
" [ 2., 2., 2., ..., 2., 2., 2.],\n",
" ...,\n",
" [141., 141., 141., ..., 141., 141., 141.],\n",
" [142., 142., 142., ..., 142., 142., 142.],\n",
" [143., 143., 143., ..., 143., 143., 143.]])array([['2021-01-01T00:30:00.000000000', '2021-01-01T06:30:00.000000000',\n",
" '2021-01-01T12:30:00.000000000', ..., '2021-01-30T12:30:00.000000000',\n",
" '2021-01-30T18:30:00.000000000', '2021-01-31T00:30:00.000000000'],\n",
" ['2021-01-01T00:30:00.000000000', '2021-01-01T06:30:00.000000000',\n",
" '2021-01-01T12:30:00.000000000', ..., '2021-01-30T12:30:00.000000000',\n",
" '2021-01-30T18:30:00.000000000', '2021-01-31T00:30:00.000000000'],\n",
" ['2021-01-01T00:30:00.000000000', '2021-01-01T06:30:00.000000000',\n",
" '2021-01-01T12:30:00.000000000', ..., '2021-01-30T12:30:00.000000000',\n",
" '2021-01-30T18:30:00.000000000', '2021-01-31T00:30:00.000000000'],\n",
" ...,\n",
" ['2021-01-01T00:30:00.000000000', '2021-01-01T06:30:00.000000000',\n",
" '2021-01-01T12:30:00.000000000', ..., '2021-01-30T12:30:00.000000000',\n",
" '2021-01-30T18:30:00.000000000', '2021-01-31T00:30:00.000000000'],\n",
" ['2021-01-01T00:30:00.000000000', '2021-01-01T06:30:00.000000000',\n",
" '2021-01-01T12:30:00.000000000', ..., '2021-01-30T12:30:00.000000000',\n",
" '2021-01-30T18:30:00.000000000', '2021-01-31T00:30:00.000000000'],\n",
" ['2021-01-01T00:30:00.000000000', '2021-01-01T06:30:00.000000000',\n",
" '2021-01-01T12:30:00.000000000', ..., '2021-01-30T12:30:00.000000000',\n",
" '2021-01-30T18:30:00.000000000', '2021-01-31T00:30:00.000000000']],\n",
" dtype='datetime64[ns]')array([[-39.166668, -39.023876, -38.904987, ..., -37.456646, -37.43389 ,\n",
" -37.488907],\n",
" [-39.166668, -39.055485, -39.003204, ..., -38.98379 , -38.98158 ,\n",
" -38.997955],\n",
" [-39.166668, -39.209393, -39.376793, ..., -40.00204 , -40.010117,\n",
" -40.02809 ],\n",
" ...,\n",
" [-34.583332, -34.773434, -35.00076 , ..., -34.65815 , -34.588226,\n",
" -34.52226 ],\n",
" [-34.583332, -34.66703 , -34.7913 , ..., -39.15673 , -39.334305,\n",
" -39.513103],\n",
" [-34.583332, -34.587185, -34.60994 , ..., -39.193733, -39.211082,\n",
" -39.25398 ]], dtype=float32)array([[22.5 , 22.547514, 22.607336, ..., 26.906063, 26.797806, 26.698011],\n",
" [22.916666, 23.089357, 23.296543, ..., 27.901531, 28.012968, 28.164171],\n",
" [23.333334, 23.61503 , 23.908384, ..., 27.667067, 27.645962, 27.643606],\n",
" ...,\n",
" [26.25 , 25.866007, 25.449963, ..., 17.211655, 17.284988, 17.336267],\n",
" [26.666666, 26.344868, 25.925909, ..., 13.803558, 13.742613, 13.677705],\n",
" [27.083334, 26.926388, 26.71817 , ..., 17.15514 , 17.17541 , 17.166965]],\n",
" dtype=float32)array([[0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" ...,\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)array([[ nan, 2.597646e-06, 2.767847e-06, ..., -3.317469e-06,\n",
" -5.994304e-06, -2.201192e-06],\n",
" [ nan, 8.876400e-06, 1.002607e-05, ..., 5.008096e-06,\n",
" 5.821934e-06, 7.830603e-06],\n",
" [ nan, 1.393304e-05, 1.235174e-05, ..., -8.949883e-07,\n",
" -6.879567e-07, 1.717140e-07],\n",
" ...,\n",
" [ nan, -1.905466e-05, -1.861986e-05, ..., 3.830198e-06,\n",
" 2.962539e-06, 1.925747e-06],\n",
" [ nan, -1.713974e-05, -2.094000e-05, ..., -2.493254e-06,\n",
" -3.214069e-06, -2.799593e-06],\n",
" [ nan, -8.068844e-06, -1.109233e-05, ..., 1.658384e-06,\n",
" 1.844593e-07, -7.776144e-07]], dtype=float32)array([[ nan, 5.795511e-06, 5.306074e-06, ..., 1.302789e-06,\n",
" -1.119043e-07, -4.836137e-06],\n",
" [ nan, 3.527368e-06, 1.323293e-06, ..., 9.175749e-07,\n",
" -6.233992e-07, -4.962131e-07],\n",
" [ nan, -4.892519e-06, -1.079000e-05, ..., 1.360431e-07,\n",
" -8.286600e-07, -6.589117e-07],\n",
" ...,\n",
" [ nan, -8.928485e-06, -1.232287e-05, ..., 3.249278e-06,\n",
" 3.223366e-06, 2.770371e-06],\n",
" [ nan, -4.034645e-06, -8.001747e-06, ..., -7.918349e-06,\n",
" -8.593543e-06, -7.880011e-06],\n",
" [ nan, 5.917896e-08, -2.591159e-06, ..., -6.002495e-07,\n",
" -1.211249e-06, -2.668733e-06]], dtype=float32)