Skip to content

Commit ef22a98

Browse files
committed
update examples, and add an example of eddy animation
1 parent aff89e4 commit ef22a98

File tree

5 files changed

+181
-4
lines changed

5 files changed

+181
-4
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Track animation
3+
===============
4+
5+
Run in a terminal this script, which allow to watch eddy evolution
6+
7+
"""
8+
from matplotlib import pyplot as plt
9+
from matplotlib.collections import LineCollection
10+
import matplotlib.transforms as mt
11+
from numpy import arange
12+
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
13+
from py_eddy_tracker.poly import create_vertice
14+
import py_eddy_tracker_sample
15+
16+
# %%
17+
# Load experimental atlas, and we select one eddy
18+
a = TrackEddiesObservations.load_file(
19+
py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Anticyclonic.zarr")
20+
)
21+
eddy = a.extract_ids([9672])
22+
t0, t1 = eddy.period
23+
t = eddy.time
24+
x = eddy["contour_lon_s"]
25+
y = eddy["contour_lat_s"]
26+
27+
# %%
28+
# General value
29+
T = 25.0
30+
cmap = plt.get_cmap("viridis")
31+
COLORS = cmap(arange(T + 1) / T)
32+
33+
34+
# %%
35+
# plot
36+
fig = plt.figure(figsize=(12, 5))
37+
ax = fig.add_axes((0.05, 0.05, 0.9, 0.9))
38+
ax.set_xlim(16.5, 23)
39+
ax.set_ylim(34.5, 37)
40+
ax.set_aspect("equal")
41+
ax.grid()
42+
# init mappable
43+
txt = ax.text(16.6, 36.8, "", zorder=10)
44+
segs = list()
45+
c = LineCollection([], zorder=1)
46+
ax.add_collection(c)
47+
48+
fig.canvas.draw()
49+
plt.show(block=False)
50+
# save background for future bliting
51+
bg_cache = fig.canvas.copy_from_bbox(ax.bbox)
52+
# display contour every 2 day
53+
for t_ in range(t0, t1 + 1, 2):
54+
fig.canvas.restore_region(bg_cache)
55+
# select contour for this time step
56+
m = t == t_
57+
segs.append(create_vertice(x[m][0], y[m][0]))
58+
c.set_paths(segs)
59+
c.set_color(COLORS[-len(segs) :])
60+
txt.set_text(f"{t0} -> {t_} -> {t1}")
61+
ax.draw_artist(c)
62+
ax.draw_artist(txt)
63+
# Remove first segment to keep only T contour
64+
if len(segs) > T:
65+
segs.pop(0)
66+
# paint updated artist
67+
fig.canvas.blit(ax.bbox)
68+
69+
fig.canvas.start_event_loop(1e-10)

examples/10_tracking_diagnostics/pet_center_count.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
# %%
30-
# Plot
30+
# Plot
3131
fig = plt.figure(figsize=(12, 18.5))
3232
ax_a = fig.add_axes([0.03, 0.75, 0.90, 0.25])
3333
ax_a.set_title("Anticyclonic center frequency")

notebooks/python_module/06_grid_manipulation/pet_hide_pixel_out_eddies.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"from matplotlib import pyplot as plt\nfrom matplotlib.path import Path\nfrom numpy import ones\nfrom py_eddy_tracker.observations.observation import EddiesObservations, custom_concat\nfrom py_eddy_tracker.dataset.grid import RegularGridDataset\nfrom py_eddy_tracker import data"
29+
"from matplotlib import pyplot as plt\nfrom matplotlib.path import Path\nfrom numpy import ones\nfrom py_eddy_tracker.observations.observation import EddiesObservations\nfrom py_eddy_tracker.dataset.grid import RegularGridDataset\nfrom py_eddy_tracker.poly import create_vertice\nfrom py_eddy_tracker import data"
3030
]
3131
},
3232
{
@@ -80,7 +80,7 @@
8080
},
8181
"outputs": [],
8282
"source": [
83-
"fig = plt.figure(figsize=(12, 6))\nax = fig.add_axes((0.05, 0.05, 0.9, 0.9))\nax.set_aspect(\"equal\")\nax.set_xlim(10, 70)\nax.set_ylim(-50, -25)\n# We will used the outter contour\nx_name, y_name = a.intern(False)\nadt = g.grid(\"adt\")\nmask = ones(adt.shape, dtype='bool')\nfor eddy in a:\n i, j = Path(custom_concat(eddy[x_name], eddy[y_name])).pixels_in(g)\n mask[i, j] = False\nadt.mask[:] += ~mask\ng.display(ax, \"adt\")\na.display(ax, label=\"Anticyclonic\", color=\"g\", lw=1, extern_only=True)"
83+
"fig = plt.figure(figsize=(12, 6))\nax = fig.add_axes((0.05, 0.05, 0.9, 0.9))\nax.set_aspect(\"equal\")\nax.set_xlim(10, 70)\nax.set_ylim(-50, -25)\n# We will used the outter contour\nx_name, y_name = a.intern(False)\nadt = g.grid(\"adt\")\nmask = ones(adt.shape, dtype='bool')\nfor eddy in a:\n i, j = Path(create_vertice(eddy[x_name], eddy[y_name])).pixels_in(g)\n mask[i, j] = False\nadt.mask[:] += ~mask\ng.display(ax, \"adt\")\na.display(ax, label=\"Anticyclonic\", color=\"g\", lw=1, extern_only=True)"
8484
]
8585
},
8686
{
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\nTrack animation\n===============\n\nRun in a terminal this script, which allow to watch eddy evolution\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"from matplotlib import pyplot as plt\nfrom matplotlib.collections import LineCollection\nimport matplotlib.transforms as mt\nfrom numpy import arange\nfrom py_eddy_tracker.observations.tracking import TrackEddiesObservations\nfrom py_eddy_tracker.poly import create_vertice\nimport py_eddy_tracker_sample"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Load experimental atlas, and we select one eddy\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"a = TrackEddiesObservations.load_file(\n py_eddy_tracker_sample.get_path(\"eddies_med_adt_allsat_dt2018/Anticyclonic.zarr\")\n)\neddy = a.extract_ids([9672])\nt0, t1 = eddy.period\nt = eddy.time\nx = eddy[\"contour_lon_s\"]\ny = eddy[\"contour_lat_s\"]"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"General value\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"T = 25.0\ncmap = plt.get_cmap(\"viridis\")\nCOLORS = cmap(arange(T + 1) / T)"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"plot\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"fig = plt.figure(figsize=(12, 5))\nax = fig.add_axes((0.05, 0.05, 0.9, 0.9))\nax.set_xlim(16.5, 23)\nax.set_ylim(34.5, 37)\nax.set_aspect(\"equal\")\nax.grid()\n# init mappable\ntxt = ax.text(16.6, 36.8, \"\", zorder=10)\nsegs = list()\nc = LineCollection([], zorder=1)\nax.add_collection(c)\n\nfig.canvas.draw()\nplt.show(block=False)\n# save background for future bliting\nbg_cache = fig.canvas.copy_from_bbox(ax.bbox)\n# display contour every 2 day\nfor t_ in range(t0, t1 + 1, 2):\n fig.canvas.restore_region(bg_cache)\n # select contour for this time step\n m = t == t_\n segs.append(create_vertice(x[m][0], y[m][0]))\n c.set_paths(segs)\n c.set_color(COLORS[-len(segs) :])\n txt.set_text(f\"{t0} -> {t_} -> {t1}\")\n ax.draw_artist(c)\n ax.draw_artist(txt)\n # Remove first segment to keep only T contour\n if len(segs) > T:\n segs.pop(0)\n # paint updated artist\n fig.canvas.blit(ax.bbox)\n\n fig.canvas.start_event_loop(1e-10)"
84+
]
85+
}
86+
],
87+
"metadata": {
88+
"kernelspec": {
89+
"display_name": "Python 3",
90+
"language": "python",
91+
"name": "python3"
92+
},
93+
"language_info": {
94+
"codemirror_mode": {
95+
"name": "ipython",
96+
"version": 3
97+
},
98+
"file_extension": ".py",
99+
"mimetype": "text/x-python",
100+
"name": "python",
101+
"nbconvert_exporter": "python",
102+
"pygments_lexer": "ipython3",
103+
"version": "3.7.7"
104+
}
105+
},
106+
"nbformat": 4,
107+
"nbformat_minor": 0
108+
}

notebooks/python_module/10_tracking_diagnostics/pet_center_count.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"cell_type": "markdown",
5252
"metadata": {},
5353
"source": [
54-
"Plot \n\n"
54+
"Plot\n\n"
5555
]
5656
},
5757
{

0 commit comments

Comments
 (0)