Skip to content

Commit 0ad32d2

Browse files
authored
Merge pull request AntSimi#128 from AntSimi/particles_drift
Particles drift
2 parents b9a801e + 574a5e4 commit 0ad32d2

File tree

4 files changed

+194
-4
lines changed

4 files changed

+194
-4
lines changed
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
Time grid computation
22
=====================
3-
4-
.. warning::
5-
6-
Time grid is under development, API could move quickly!
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Build path of particle drifting
3+
===============================
4+
5+
"""
6+
7+
from matplotlib import pyplot as plt
8+
from numpy import arange, meshgrid
9+
10+
from py_eddy_tracker import start_logger
11+
from py_eddy_tracker.data import get_demo_path
12+
from py_eddy_tracker.dataset.grid import GridCollection
13+
14+
start_logger().setLevel("ERROR")
15+
16+
# %%
17+
# Load data cube
18+
c = GridCollection.from_netcdf_cube(
19+
get_demo_path("dt_med_allsat_phy_l4_2005T2.nc"),
20+
"longitude",
21+
"latitude",
22+
"time",
23+
heigth="adt",
24+
)
25+
26+
# %%
27+
# Advection properties
28+
nb_days, step_by_day = 10, 6
29+
nb_time = step_by_day * nb_days
30+
kw_p = dict(nb_step=1, time_step=86400 / step_by_day)
31+
t0 = 20210
32+
33+
# %%
34+
# Get paths
35+
x0, y0 = meshgrid(arange(32, 35, 0.5), arange(32.5, 34.5, 0.5))
36+
x0, y0 = x0.reshape(-1), y0.reshape(-1)
37+
t, x, y = c.path(x0, y0, "u", "v", t_init=t0, **kw_p, nb_time=nb_time)
38+
39+
# %%
40+
# Plot paths
41+
ax = plt.figure(figsize=(9, 6)).add_subplot(111, aspect="equal")
42+
ax.plot(x0, y0, "k.", ms=20)
43+
ax.plot(x, y, lw=3)
44+
ax.set_title("10 days particle paths")
45+
ax.set_xlim(31, 35), ax.set_ylim(32, 34.5)
46+
ax.grid()
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
"\n# Build path of particle drifting\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 numpy import arange, meshgrid\n\nfrom py_eddy_tracker import start_logger\nfrom py_eddy_tracker.data import get_demo_path\nfrom py_eddy_tracker.dataset.grid import GridCollection\n\nstart_logger().setLevel(\"ERROR\")"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Load data cube\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"c = GridCollection.from_netcdf_cube(\n get_demo_path(\"dt_med_allsat_phy_l4_2005T2.nc\"),\n \"longitude\",\n \"latitude\",\n \"time\",\n heigth=\"adt\",\n)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"Advection properties\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"nb_days, step_by_day = 10, 6\nnb_time = step_by_day * nb_days\nkw_p = dict(nb_step=1, time_step=86400 / step_by_day)\nt0 = 20210"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"Get paths\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"x0, y0 = meshgrid(arange(32, 35, 0.5), arange(32.5, 34.5, 0.5))\nx0, y0 = x0.reshape(-1), y0.reshape(-1)\nt, x, y = c.path(x0, y0, \"u\", \"v\", t_init=t0, **kw_p, nb_time=nb_time)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Plot paths\n\n"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [],
100+
"source": [
101+
"ax = plt.figure(figsize=(9, 6)).add_subplot(111, aspect=\"equal\")\nax.plot(x0, y0, \"k.\", ms=20)\nax.plot(x, y, lw=3)\nax.set_title(\"10 days particle paths\")\nax.set_xlim(31, 35), ax.set_ylim(32, 34.5)\nax.grid()"
102+
]
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "Python 3",
108+
"language": "python",
109+
"name": "python3"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 3
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython3",
121+
"version": "3.7.7"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 0
126+
}

src/py_eddy_tracker/dataset/grid.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,6 +2517,28 @@ def get_previous_time_step(self, t_init):
25172517
logger.debug(f"i={i}, t={t}, dataset={dataset}")
25182518
yield t, dataset
25192519

2520+
def path(self, x0, y0, *args, nb_time=2, **kwargs):
2521+
"""
2522+
At each call it will update position in place with u & v field
2523+
2524+
:param array x0: Longitude of obs to move
2525+
:param array y0: Latitude of obs to move
2526+
:param int nb_time: Number of iteration for particle
2527+
:param dict kwargs: look at :py:meth:`GridCollection.advect`
2528+
2529+
:return: t,x,y
2530+
2531+
.. minigallery:: py_eddy_tracker.GridCollection.path
2532+
"""
2533+
particles = self.advect(x0.copy(), y0.copy(), *args, **kwargs)
2534+
t = empty(nb_time + 1, dtype="f8")
2535+
x = empty((nb_time + 1, x0.size), dtype=x0.dtype)
2536+
y = empty(x.shape, dtype=y0.dtype)
2537+
t[0], x[0], y[0] = kwargs.get("t_init"), x0, y0
2538+
for i in range(nb_time):
2539+
t[i + 1], x[i + 1], y[i + 1] = particles.__next__()
2540+
return t, x, y
2541+
25202542

25212543
@njit(cache=True)
25222544
def advect_t(x_g, y_g, u_g0, v_g0, m_g0, u_g1, v_g1, m_g1, x, y, m, weigths, half_w=0):

0 commit comments

Comments
 (0)