Skip to content

Commit 3df15b9

Browse files
committed
Add method to list merging/spliting event from network
1 parent 16691a2 commit 3df15b9

File tree

2 files changed

+113
-12
lines changed

2 files changed

+113
-12
lines changed

examples/16_network/pet_relative.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
# ---------
1616
# Load data where observations are put in same network but no segmentation
1717
e = TrackEddiesObservations.load_file(data.get_path("c568803.nc"))
18+
# FIXME : Must be rewrote
19+
e.lon[:] = (e.lon + 180) % 360 - 180
20+
e.contour_lon_e[:] = ((e.contour_lon_e.T - e.lon + 180) % 360 - 180 + e.lon).T
21+
e.contour_lon_s[:] = ((e.contour_lon_s.T - e.lon + 180) % 360 - 180 + e.lon).T
22+
##############
1823
n = NetworkObservations.from_split_network(e, e.split_network(intern=False, window=5))
1924

2025
# %%
@@ -113,18 +118,46 @@
113118
# %%
114119
fig = plt.figure(figsize=(15, 5))
115120
ax = fig.add_axes([0.04, 0.06, 0.90, 0.88])
116-
close_to_i = n.relative(i, order=1)
117-
ax.set_title(f"Close segments ({close_to_i.infos()})")
118-
_ = close_to_i.display_timeline(ax)
121+
close_to_i1 = n.relative(i, order=1)
122+
ax.set_title(f"Close segments ({close_to_i1.infos()})")
123+
_ = close_to_i1.display_timeline(ax)
119124
# %%
120125
fig = plt.figure(figsize=(15, 5))
121126
ax = fig.add_axes([0.04, 0.06, 0.90, 0.88])
122-
close_to_i = n.relative(i, order=2)
123-
ax.set_title(f"Close segments ({close_to_i.infos()})")
124-
_ = close_to_i.display_timeline(ax)
127+
close_to_i2 = n.relative(i, order=2)
128+
ax.set_title(f"Close segments ({close_to_i2.infos()})")
129+
_ = close_to_i2.display_timeline(ax)
125130
# %%
126131
fig = plt.figure(figsize=(15, 5))
127132
ax = fig.add_axes([0.04, 0.06, 0.90, 0.88])
128-
close_to_i = n.relative(i, order=3)
129-
ax.set_title(f"Close segments ({close_to_i.infos()})")
130-
_ = close_to_i.display_timeline(ax)
133+
close_to_i3 = n.relative(i, order=3)
134+
ax.set_title(f"Close segments ({close_to_i3.infos()})")
135+
_ = close_to_i3.display_timeline(ax)
136+
137+
# %%
138+
# Display track on map
139+
# --------------------
140+
fig = plt.figure(figsize=(15, 8))
141+
ax = fig.add_axes([0.04, 0.06, 0.90, 0.88], projection="full_axes")
142+
close_to_i2.plot(ax)
143+
_ = ax.set_xlim(-10, 20), ax.set_ylim(-37, -21), ax.grid()
144+
145+
# %%
146+
# Get merging event
147+
# -----------------
148+
fig = plt.figure(figsize=(15, 8))
149+
ax = fig.add_axes([0.04, 0.06, 0.90, 0.88], projection="full_axes")
150+
merging = close_to_i2.merging_event()
151+
merging.display(ax)
152+
ax.set_xlim(-10, 20), ax.set_ylim(-37, -21), ax.grid()
153+
merging
154+
155+
# %%
156+
# Get spliting event
157+
# ------------------
158+
fig = plt.figure(figsize=(15, 8))
159+
ax = fig.add_axes([0.04, 0.06, 0.90, 0.88], projection="full_axes")
160+
spliting = close_to_i2.spliting_event()
161+
spliting.display(ax)
162+
ax.set_xlim(-10, 20), ax.set_ylim(-37, -21), ax.grid()
163+
spliting

src/py_eddy_tracker/observations/network.py

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from numba import njit
99
from numpy import arange, array, bincount, empty, ones, uint32, unique
1010

11-
from ..generic import build_index
11+
from ..generic import build_index, wrap_longitude
1212
from ..poly import bbox_intersection, vertice_overlap
1313
from .observation import EddiesObservations
1414
from .tracking import TrackEddiesObservations
@@ -232,16 +232,84 @@ def scatter_timeline(self, ax, name, factor=1, event=True, **kwargs):
232232
return mappables
233233

234234
def insert_virtual(self):
235+
# TODO
235236
pass
236237

237238
def merging_event(self):
238-
pass
239+
indices = list()
240+
for i, b0, b1 in self.iter_on("segment"):
241+
nb = i.stop - i.start
242+
if nb == 0:
243+
continue
244+
i_n = self.next_obs[i.stop - 1]
245+
if i_n != -1:
246+
indices.append(i.stop - 1)
247+
indices = list(set(indices))
248+
nb = len(indices)
249+
new = EddiesObservations(
250+
nb,
251+
track_extra_variables=self.track_extra_variables,
252+
track_array_variables=self.track_array_variables,
253+
array_variables=self.array_variables,
254+
only_variables=self.only_variables,
255+
raw_data=self.raw_data,
256+
)
257+
258+
for k in new.obs.dtype.names:
259+
new[k][:] = self[k][indices]
260+
new.sign_type = self.sign_type
261+
return new
239262

240263
def spliting_event(self):
241-
pass
264+
indices = list()
265+
for i, b0, b1 in self.iter_on("segment"):
266+
nb = i.stop - i.start
267+
if nb == 0:
268+
continue
269+
i_p = self.previous_obs[i.start]
270+
if i_p != -1:
271+
indices.append(i.start)
272+
indices = list(set(indices))
273+
nb = len(indices)
274+
new = EddiesObservations(
275+
nb,
276+
track_extra_variables=self.track_extra_variables,
277+
track_array_variables=self.track_array_variables,
278+
array_variables=self.array_variables,
279+
only_variables=self.only_variables,
280+
raw_data=self.raw_data,
281+
)
282+
283+
for k in new.obs.dtype.names:
284+
new[k][:] = self[k][indices]
285+
new.sign_type = self.sign_type
286+
return new
242287

243288
def fully_connected(self):
244289
self.only_one_network()
290+
# TODO
291+
292+
def plot(self, ax, ref=None, **kwargs):
293+
"""
294+
This function will draw path of each trajectory
295+
296+
:param matplotlib.axes.Axes ax: ax to draw
297+
:param float,int ref: if defined, all coordinates will be wrapped with ref like west boundary
298+
:param dict kwargs: keyword arguments for Axes.plot
299+
:return: a list of matplotlib mappables
300+
"""
301+
mappables = list()
302+
if "label" in kwargs:
303+
kwargs["label"] = self.format_label(kwargs["label"])
304+
for i, b0, b1 in self.iter_on("segment"):
305+
nb = i.stop - i.start
306+
if nb == 0:
307+
continue
308+
x, y = self.lon[i], self.lat[i]
309+
if ref is not None:
310+
x, y = wrap_longitude(x, y, ref, cut=True)
311+
mappables.append(ax.plot(x, y, **kwargs)[0])
312+
return mappables
245313

246314
def remove_dead_branch(self, nobs=3):
247315
""""""

0 commit comments

Comments
 (0)