Skip to content
Snippets Groups Projects
Commit 0a24a2f1 authored by Lionel GUEZ's avatar Lionel GUEZ
Browse files

Separate the construction of `traj_vert_ind`

Separate the construction of `traj_vert_ind` and the definition of
`traj_prop`. The motivation is to be able to create the files
`*traj*.json` for only a component of the graph, so that
`plot_traj.py` can plot the trajectories in a single component, with
different colors for different trajectories. A side benefit is that
the script `trajectories.py` becomes clearer. I have measured on the
global graph for 1993-2023 that this modification does not make a
significant change on the running time of the script.
parent 4785c7aa
No related branches found
No related tags found
No related merge requests found
......@@ -28,12 +28,11 @@ import numpy as np
import util_eddies
def new_traj(ind_traj, traj_prop, n, traj_vert_ind):
def new_traj(ind_traj, traj_prop, n):
"""Assign new trajectory to vertex index n."""
ind_traj += 1
traj_prop[n] = ind_traj
traj_vert_ind.append([n])
return ind_traj
......@@ -84,16 +83,12 @@ g.vp["traj"] = traj_prop
# the index in lists traj_vert_ind, traj_segm and expanded_traj.
ind_traj = -1 # initialize index of trajectory
traj_vert_ind = []
# An element of traj_vert_ind is a trajectory, represented by a list
# of vertex indices.
in_deg_prop = g.degree_property_map("in")
out_deg_prop = g.degree_property_map("out")
my_sort = topology.topological_sort(g)
# Loop to define traj_prop:
for n in topology.topological_sort(g):
for n in my_sort:
# First, we check whether we have a phantom pattern. Phantom
# eddies may appear in altimetric data because of
# interpolation. In a phantom pattern, we have splitting
......@@ -149,16 +144,8 @@ for n in topology.topological_sort(g):
traj_prop[pred[1]],
traj_prop[pred[0]],
)
(
traj_vert_ind[traj_prop[pred[0]]][-1],
traj_vert_ind[traj_prop[pred[1]]][-1],
) = (
traj_vert_ind[traj_prop[pred[1]]][-1],
traj_vert_ind[traj_prop[pred[0]]][-1],
)
traj_prop[n] = traj_prop[n1]
traj_vert_ind[traj_prop[n1]].append(n)
if traj_prop[n] == -1:
if in_deg_prop[n] >= 1:
......@@ -173,22 +160,35 @@ for n in topology.topological_sort(g):
if closest_succ[closest_pred] == n:
# Assign to n the trajectory of closest_pred. This
# means updating traj_prop and
# traj_vert_ind. closest_pred is already in a
# trajectory, traj_prop[closest_pred]] != - 1. We
# means updating traj_prop. closest_pred is already in
# a trajectory, traj_prop[closest_pred]] != - 1. We
# extend the trajectory of closest_pred forward.
traj_prop[n] = traj_prop[closest_pred]
traj_vert_ind[traj_prop[n]].append(n)
else:
ind_traj = new_traj(ind_traj, traj_prop, n, traj_vert_ind)
ind_traj = new_traj(ind_traj, traj_prop, n)
else:
ind_traj = new_traj(ind_traj, traj_prop, n, traj_vert_ind)
ind_traj = new_traj(ind_traj, traj_prop, n)
# assert traj_prop[n] != - 1
t1 = time.perf_counter()
timings_file.write(f"traj_prop defined in {t1 - t0:.0f} s\n")
t0 = t1
# Construct traj_vert_ind from traj_prop:
traj_vert_ind = {}
for n in my_sort:
if traj_prop[n] in traj_vert_ind:
traj_vert_ind[traj_prop[n]].append(n)
else:
traj_vert_ind[traj_prop[n]] = [n]
traj_vert_ind = [traj_vert_ind[t] for t in range(len(traj_vert_ind))]
# An element of traj_vert_ind is a trajectory, represented by a list
# of vertex indices.
print("Number of trajectories: ", len(traj_vert_ind))
# We have trajectories as lists of vertex indices. Now construct
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment