Skip to content

Commit c40e965

Browse files
committed
Add examples
1 parent a44471c commit c40e965

File tree

7 files changed

+167
-5
lines changed

7 files changed

+167
-5
lines changed

examples/pet_center_count.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
g.vars['count'] = g.vars['count'] / (step ** 2 * (t1 - t0))
2525
m = g.display(ax, name='count', vmin=0, vmax=2)
2626
ax.grid()
27-
cb = plt.colorbar(m)
27+
cb = plt.colorbar(m, cax=fig.add_axes([0.95, 0.05, 0.01, 0.9]))
2828
cb.set_label('Eddies by 1°^2 by day')
2929

examples/pet_display_track.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Display Tracks
3+
======================
4+
5+
"""
6+
7+
from matplotlib import pyplot as plt
8+
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
9+
import py_eddy_tracker_sample
10+
11+
a = TrackEddiesObservations.load_file(
12+
py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Anticyclonic.zarr")
13+
)
14+
c = TrackEddiesObservations.load_file(
15+
py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Cyclonic.zarr")
16+
)
17+
18+
a = a.extract_with_length((7 * 20, -1))
19+
c = c.extract_with_length((7 * 20, -1))
20+
21+
# Plot
22+
fig = plt.figure(figsize=(15, 8))
23+
ax = fig.add_subplot(111)
24+
ax.set_aspect("equal")
25+
ax.set_xlim(-5, 37)
26+
ax.set_ylim(30, 46)
27+
a.plot(ax, ref=-10, label="Anticyclonic", color="b", lw=0.1)
28+
c.plot(ax, ref=-10, label="Cyclonic", color="r", lw=0.1)
29+
ax.legend()

examples/pet_histo_amplitude.py renamed to examples/pet_histo.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Amplitude Histogram
2+
Parameter Histogram
33
===================
44
55
"""
@@ -22,10 +22,21 @@
2222

2323
fig = plt.figure()
2424
ax = fig.add_subplot(111)
25-
bins = arange(0,150,1)
25+
bins = arange(0,200,1)
2626
ax.hist(a['radius_s'] / 1000., histtype='step', bins=bins, label='Anticyclonic')
2727
ax.hist(c['radius_s'] / 1000., histtype='step', bins=bins, label='Cyclonic')
2828
ax.set_xlabel('Speed_radius (km)')
2929
ax.set_xlim(0,150)
3030
ax.legend()
31+
ax.grid()
32+
33+
fig = plt.figure()
34+
ax = fig.add_subplot(111)
35+
bins = arange(0,100,1)
36+
ax.hist(a['speed_average'] * 100., histtype='step', bins=bins, label='Anticyclonic')
37+
ax.hist(c['speed_average'] * 100., histtype='step', bins=bins, label='Cyclonic')
38+
ax.set_xlabel('speed_average (cm/s)')
39+
40+
ax.set_xlim(0,50)
41+
ax.legend()
3142
ax.grid()

examples/pet_lifetime.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Lifetime Histogram
3+
===================
4+
5+
"""
6+
from matplotlib import pyplot as plt
7+
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
8+
import py_eddy_tracker_sample
9+
from numpy import arange
10+
11+
a = TrackEddiesObservations.load_file(py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Anticyclonic.zarr"))
12+
c = TrackEddiesObservations.load_file(py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Cyclonic.zarr"))
13+
14+
# Plot
15+
fig = plt.figure()
16+
ax_lifetime = fig.add_axes([0.05, 0.55, .4, .4])
17+
ax_cum_lifetime = fig.add_axes([0.55, 0.55, .4, .4])
18+
ax_ratio_lifetime = fig.add_axes([0.05, 0.05, .4, .4])
19+
ax_ratio_cum_lifetime = fig.add_axes([0.55, 0.05, .4, .4])
20+
21+
22+
cum_a, bins, _ = ax_cum_lifetime.hist(a['n'], histtype='step', bins=arange(0,800,1), label='Anticyclonic', color='b')
23+
cum_c, bins, _ = ax_cum_lifetime.hist(c['n'], histtype='step', bins=arange(0,800,1), label='Cyclonic', color='r')
24+
25+
x = (bins[1:] + bins[:-1]) / 2.
26+
ax_ratio_cum_lifetime.plot(x, cum_c/cum_a)
27+
28+
nb_a, nb_c = cum_a[:-1] - cum_a[1:], cum_c[:-1] - cum_c[1:]
29+
ax_lifetime.plot(x[1:], nb_a, label='Anticyclonic', color='b')
30+
ax_lifetime.plot(x[1:], nb_c, label='Cyclonic', color='r')
31+
32+
ax_ratio_lifetime.plot(x[1:], nb_c / nb_a)
33+
34+
35+
for ax in (ax_lifetime, ax_cum_lifetime, ax_ratio_cum_lifetime, ax_ratio_lifetime):
36+
ax.set_xlim(0,365)
37+
if ax in (ax_lifetime, ax_cum_lifetime):
38+
ax.set_ylim(1,None)
39+
ax.set_yscale('log')
40+
ax.legend()
41+
else:
42+
ax.set_ylim(0,2)
43+
ax.set_ylabel('Ratio Cyclonic/Anticyclonic')
44+
ax.set_xlabel('Lifetime (days)')
45+
ax.grid()

examples/pet_propagation.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Propagation Histogram
3+
===================
4+
5+
"""
6+
from matplotlib import pyplot as plt
7+
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
8+
from py_eddy_tracker.generic import distance
9+
import py_eddy_tracker_sample
10+
from numpy import arange, empty
11+
from numba import njit
12+
13+
14+
@njit(cache=True)
15+
def cum_distance_by_track(distance, track):
16+
tr_previous = 0
17+
d_cum = 0
18+
new_distance = empty(track.shape, dtype=distance.dtype)
19+
for i in range(distance.shape[0]):
20+
tr = track[i]
21+
if i != 0 and tr != tr_previous:
22+
d_cum = 0
23+
new_distance[i] = d_cum
24+
d_cum += distance[i]
25+
tr_previous = tr
26+
new_distance[i + 1] = d_cum
27+
return new_distance
28+
29+
a = TrackEddiesObservations.load_file(
30+
py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Anticyclonic.zarr")
31+
)
32+
c = TrackEddiesObservations.load_file(
33+
py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Cyclonic.zarr")
34+
)
35+
d_a = distance(a.longitude[:-1], a.latitude[:-1], a.longitude[1:], a.latitude[1:])
36+
d_c = distance(c.longitude[:-1], c.latitude[:-1], c.longitude[1:], c.latitude[1:])
37+
d_a = cum_distance_by_track(d_a, a["track"]) / 1000.
38+
d_c = cum_distance_by_track(d_c, c["track"]) / 1000.
39+
40+
# Plot
41+
fig = plt.figure()
42+
ax_propagation = fig.add_axes([0.05, 0.55, 0.4, 0.4])
43+
ax_cum_propagation = fig.add_axes([0.55, 0.55, 0.4, 0.4])
44+
ax_ratio_propagation = fig.add_axes([0.05, 0.05, 0.4, 0.4])
45+
ax_ratio_cum_propagation = fig.add_axes([0.55, 0.05, 0.4, 0.4])
46+
47+
48+
bins = arange(0, 1500, 25)
49+
cum_a, bins, _ = ax_cum_propagation.hist(
50+
d_a, histtype="step", bins=bins, label="Anticyclonic", color="b"
51+
)
52+
cum_c, bins, _ = ax_cum_propagation.hist(
53+
d_c, histtype="step", bins=bins, label="Cyclonic", color="r"
54+
)
55+
56+
x = (bins[1:] + bins[:-1]) / 2.0
57+
ax_ratio_cum_propagation.plot(x, cum_c / cum_a)
58+
59+
nb_a, nb_c = cum_a[:-1] - cum_a[1:], cum_c[:-1] - cum_c[1:]
60+
ax_propagation.plot(x[1:], nb_a, label="Anticyclonic", color="b")
61+
ax_propagation.plot(x[1:], nb_c, label="Cyclonic", color="r")
62+
63+
ax_ratio_propagation.plot(x[1:], nb_c / nb_a)
64+
65+
66+
for ax in (ax_propagation, ax_cum_propagation, ax_ratio_cum_propagation, ax_ratio_propagation):
67+
ax.set_xlim(0, 1000)
68+
if ax in (ax_propagation, ax_cum_propagation):
69+
ax.set_ylim(1, None)
70+
ax.set_yscale("log")
71+
ax.legend()
72+
else:
73+
ax.set_ylim(0, 2)
74+
ax.set_ylabel("Ratio Cyclonic/Anticyclonic")
75+
ax.set_xlabel("Propagation (km)")
76+
ax.grid()

src/py_eddy_tracker/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def split_line(x, y, i):
272272
273273
Returns: x and y separate by nan at each i jump
274274
"""
275-
nb_jump = len(where(i[1:] - i[0] != 0)[0])
275+
nb_jump = len(where(i[1:] - i[:-1] != 0)[0])
276276
nb_value = x.shape[0]
277277
final_size = (nb_jump - 1) + nb_value
278278
new_x = empty(final_size, dtype=x.dtype)

src/py_eddy_tracker/observations/tracking.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,10 @@ def __extract_with_mask(
355355
def plot(self, ax, ref=None, ** kwargs):
356356
if "label" in kwargs:
357357
kwargs["label"] += " (%s eddies)" % (self.nb_obs_by_track != 0).sum()
358-
x, y = split_line(self.longitude, self.latitude, self.tracks)
358+
x = self.longitude
359359
if ref is not None:
360360
x = (x - ref) % 360 + ref
361+
x, y = split_line(x, self.latitude, self.tracks)
361362
return ax.plot(x, y, **kwargs)
362363

363364

0 commit comments

Comments
 (0)