1717from py_eddy_tracker .data import get_demo_path
1818from py_eddy_tracker .dataset .grid import GridCollection
1919from py_eddy_tracker .observations .network import NetworkObservations
20+ from py_eddy_tracker .observations .groups import particle_candidate
2021from py_eddy_tracker .poly import group_obs
2122
2223start_logger ().setLevel ("ERROR" )
@@ -124,81 +125,6 @@ def update(frame):
124125ani = VideoAnimation (a .fig , update , frames = arange (20200 , 20269 , step ), interval = 200 )
125126
126127
127- # %%
128- # In which observations are the particle
129- # --------------------------------------
130- def advect (x , y , c , t0 , delta_t ):
131- """
132- Advect particle from t0 to t0 + delta_t, with data cube.
133- """
134- kw = dict (nb_step = 6 , time_step = 86400 / 6 )
135- if delta_t < 0 :
136- kw ["backward" ] = True
137- delta_t = - delta_t
138- p = c .advect (x , y , "u" , "v" , t_init = t0 , ** kw )
139- for _ in range (delta_t ):
140- t , x , y = p .__next__ ()
141- return t , x , y
142-
143-
144- def particle_candidate (x , y , c , eddies , t_start , i_target , pct , ** kwargs ):
145- # Obs from initial time
146- m_start = eddies .time == t_start
147- e = eddies .extract_with_mask (m_start )
148- # to be able to get global index
149- translate_start = where (m_start )[0 ]
150- # Identify particle in eddies (only in core)
151- i_start = e .contains (x , y , intern = True )
152- m = i_start != - 1
153- x , y , i_start = x [m ], y [m ], i_start [m ]
154- # Advect
155- t_end , x , y = advect (x , y , c , t_start , ** kwargs )
156- # eddies at last date
157- m_end = eddies .time == t_end / 86400
158- e_end = eddies .extract_with_mask (m_end )
159- # to be able to get global index
160- translate_end = where (m_end )[0 ]
161- # Id eddies for each alive particle (in core and extern)
162- i_end = e_end .contains (x , y )
163- # compute matrix and fill target array
164- get_matrix (i_start , i_end , translate_start , translate_end , i_target , pct )
165-
166-
167- @njit (cache = True )
168- def get_matrix (i_start , i_end , translate_start , translate_end , i_target , pct ):
169- nb_start , nb_end = translate_start .size , translate_end .size
170- # Matrix which will store count for every couple
171- count = zeros ((nb_start , nb_end ), dtype = nb_types .int32 )
172- # Number of particles in each origin observation
173- ref = zeros (nb_start , dtype = nb_types .int32 )
174- # For each particle
175- for i in range (i_start .size ):
176- i_end_ = i_end [i ]
177- i_start_ = i_start [i ]
178- if i_end_ != - 1 :
179- count [i_start_ , i_end_ ] += 1
180- ref [i_start_ ] += 1
181- for i in range (nb_start ):
182- for j in range (nb_end ):
183- pct_ = count [i , j ]
184- # If there are particles from i to j
185- if pct_ != 0 :
186- # Get percent
187- pct_ = pct_ / ref [i ] * 100.0
188- # Get indices in full dataset
189- i_ , j_ = translate_start [i ], translate_end [j ]
190- pct_0 = pct [i_ , 0 ]
191- if pct_ > pct_0 :
192- pct [i_ , 1 ] = pct_0
193- pct [i_ , 0 ] = pct_
194- i_target [i_ , 1 ] = i_target [i_ , 0 ]
195- i_target [i_ , 0 ] = j_
196- elif pct_ > pct [i_ , 1 ]:
197- pct [i_ , 1 ] = pct_
198- i_target [i_ , 1 ] = j_
199- return i_target , pct
200-
201-
202128# %%
203129# Particle advection
204130# ^^^^^^^^^^^^^^^^^^
@@ -217,12 +143,12 @@ def get_matrix(i_start, i_end, translate_start, translate_end, i_target, pct):
217143# Forward run
218144i_target_f , pct_target_f = - ones (shape , dtype = "i4" ), zeros (shape , dtype = "i1" )
219145for t in range (t_start , t_end - dt ):
220- particle_candidate (x0 , y0 , c , n , t , i_target_f , pct_target_f , delta_t = dt )
146+ particle_candidate (x0 , y0 , c , n , t , i_target_f , pct_target_f , n_days = dt )
221147
222148# Backward run
223149i_target_b , pct_target_b = - ones (shape , dtype = "i4" ), zeros (shape , dtype = "i1" )
224150for t in range (t_start + dt , t_end ):
225- particle_candidate (x0 , y0 , c , n , t , i_target_b , pct_target_b , delta_t = - dt )
151+ particle_candidate (x0 , y0 , c , n , t , i_target_b , pct_target_b , n_days = - dt )
226152
227153# %%
228154fig = plt .figure (figsize = (10 , 10 ))
0 commit comments