@@ -60,8 +60,7 @@ def __init__(self, datasets, virtual=0):
60
60
self .datasets = datasets
61
61
self .previous2_obs = None
62
62
self .previous_obs = None
63
- self .current_obs = EddiesObservations .load_from_netcdf (
64
- self .datasets [0 ])
63
+ self .current_obs = None
65
64
66
65
# To use virtual obs
67
66
# Number of obs which can prolongate real observations
@@ -82,21 +81,48 @@ def __init__(self, datasets, virtual=0):
82
81
self .nb_obs = 0
83
82
self .eddies = None
84
83
84
+ def reset_dataset_cache (self ):
85
+ self .previous2_obs = None
86
+ self .previous_obs = None
87
+ self .current_obs = None
88
+
85
89
def swap_dataset (self , dataset ):
86
90
"""
87
91
"""
88
92
self .previous2_obs = self .previous_obs
89
93
self .previous_obs = self .current_obs
90
94
self .current_obs = EddiesObservations .load_from_netcdf (dataset )
91
95
92
- def store_correspondance (self , i_previous , i_current ):
93
- correspondance = array (
94
- i_previous ,
95
- dtype = self .correspondance_dtype )
96
+ def store_correspondance (self , i_previous , i_current , nb_real_obs ):
97
+ """Storing correspondance in an array
98
+ """
99
+ # Create array to store correspondance data
100
+ correspondance = array (i_previous , dtype = self .correspondance_dtype )
101
+ # index from current_obs
96
102
correspondance ['out' ] = i_current
97
103
98
104
if self .virtual :
105
+ # if index in previous dataset is bigger than real obs number
106
+ # it's a virtual data
99
107
correspondance ['virtual' ] = i_previous >= nb_real_obs
108
+
109
+ if self .previous2_obs is None :
110
+ # First time we set ID (Program starting)
111
+ nb_match = i_previous .shape [0 ]
112
+ # Set an id for each match
113
+ correspondance ['id' ] = self .id_generator (nb_match )
114
+ self .append (correspondance )
115
+ return
116
+
117
+ # We set all id to UINT32_MAX
118
+ id_previous = ones (len (self .previous_obs ),
119
+ dtype = self .ID_DTYPE ) * self .UINT32_MAX
120
+ # We get old id for previously eddies tracked
121
+ id_previous [self [- 1 ]['out' ]] = self [- 1 ]['id' ]
122
+ # We store ID in correspondance if the ID is UINT32_MAX, we never
123
+ # track it before
124
+ correspondance ['id' ] = id_previous [correspondance ['in' ]]
125
+
100
126
101
127
def id_generator (self , nb_id ):
102
128
"""Generation id and incrementation
@@ -153,6 +179,8 @@ def track(self):
153
179
"""
154
180
START = True
155
181
FLG_VIRTUAL = False
182
+ self .reset_dataset_cache ()
183
+ self .swap_dataset (self .datasets [0 ])
156
184
# We begin with second file, first one is in previous
157
185
for file_name in self .datasets [1 :]:
158
186
self .swap_dataset (file_name )
@@ -169,7 +197,7 @@ def track(self):
169
197
i_previous , i_current = self .previous_obs .tracking (self .current_obs )
170
198
nb_match = i_previous .shape [0 ]
171
199
172
- #~ self.store_correspondance(i_previous, i_current)
200
+ #~ self.store_correspondance(i_previous, i_current, nb_real_obs )
173
201
correspondance = array (i_previous , dtype = self .correspondance_dtype )
174
202
correspondance ['out' ] = i_current
175
203
@@ -204,7 +232,7 @@ def track(self):
204
232
205
233
# new_id is equal to UINT32_MAX we must add a new ones
206
234
# we count the number of new
207
- mask_new_id = correspondance ['id' ] == UINT32_MAX
235
+ mask_new_id = correspondance ['id' ] == self . UINT32_MAX
208
236
nb_new_tracks = mask_new_id .sum ()
209
237
logging .debug ('%d birth in this step' , nb_new_tracks )
210
238
# Set new id
@@ -247,77 +275,80 @@ def prepare_merging(self):
247
275
# Compute index of each tracks
248
276
self .i_current_by_tracks = self .nb_obs_by_tracks .cumsum () - self .nb_obs_by_tracks
249
277
# Number of global obs
250
- self .nb_obs = nb_obs_by_tracks .sum ()
278
+ self .nb_obs = self . nb_obs_by_tracks .sum ()
251
279
logging .info ('%d tracks identified' , self .current_id )
252
280
logging .info ('%d observations will be join' , self .nb_obs )
253
281
254
282
def merge (self ):
283
+ """Merge all the correspondance in one array with all fields
284
+ """
255
285
# Start create netcdf to agglomerate all eddy
256
- self . eddies = TrackEddiesObservations (size = self .nb_obs )
286
+ eddies = TrackEddiesObservations (size = self .nb_obs )
257
287
258
288
# Calculate the index in each tracks, we compute in u4 and translate
259
289
# in u2 (which are limited to 65535)
260
290
logging .debug ('Compute global index array (N)' )
261
- n = arange (nb_obs ,
291
+ n = arange (self . nb_obs ,
262
292
dtype = 'u4' ) - self .i_current_by_tracks .repeat (self .nb_obs_by_tracks )
263
- self . eddies ['n' ][:] = uint16 (n )
293
+ eddies ['n' ][:] = uint16 (n )
264
294
logging .debug ('Compute global track array' )
265
- self . eddies ['track' ][:] = arange (self .current_id ).repeat (self .nb_obs_by_tracks )
295
+ eddies ['track' ][:] = arange (self .current_id ).repeat (self .nb_obs_by_tracks )
266
296
267
297
# Start loading identification again to save in the finals tracks
268
298
# Load first file
269
- eddies_previous = EddiesObservations . load_from_netcdf (self .datasets [0 ])
299
+ self . swap_dataset (self .datasets [0 ])
270
300
# Set type of eddy with first file
271
- self .eddies .sign_type = eddies_previous .sign_type
301
+ eddies .sign_type = self .current_obs .sign_type
302
+ # Fields to copy
303
+ fields = self .current_obs .obs .dtype .descr
272
304
273
305
# To know if the track start
274
- first_obs_save_in_tracks = zeros (i_current_by_tracks .shape ,
306
+ first_obs_save_in_tracks = zeros (self . i_current_by_tracks .shape ,
275
307
dtype = bool_ )
276
308
277
- for i , file_name in enumerate (FILENAMES [1 :]):
309
+ for i , file_name in enumerate (self . datasets [1 :]):
278
310
# Load current file (we begin with second one)
279
- self .current_obs = EddiesObservations . load_from_netcdf (file_name )
311
+ self .swap_dataset (file_name )
280
312
# We select the list of id which are involve in the correspondance
281
313
i_id = self [i ]['id' ]
282
314
# Index where we will write in the final object
283
- index_final = i_current_by_tracks [i_id ]
315
+ index_final = self . i_current_by_tracks [i_id ]
284
316
285
317
# First obs of eddies
286
318
m_first_obs = - first_obs_save_in_tracks [i_id ]
287
319
if m_first_obs .any ():
288
- # Index in the current file
320
+ # Index in the previous file
289
321
index_in = self [i ]['in' ][m_first_obs ]
290
322
# Copy all variable
291
- for var , _ in eddies_current . obs . dtype . descr :
292
- self . eddies [var ][index_final [m_first_obs ]
293
- ] = eddies_previous [var ][index_in ]
323
+ for var , _ in fields :
324
+ eddies [var ][index_final [m_first_obs ]
325
+ ] = self . previous_obs [var ][index_in ]
294
326
# Increment
295
- i_current_by_tracks [i_id [m_first_obs ]] += 1
327
+ self . i_current_by_tracks [i_id [m_first_obs ]] += 1
296
328
# Active this flag, we have only one first by tracks
297
329
first_obs_save_in_tracks [i_id ] = True
298
- index_final = i_current_by_tracks [i_id ]
299
-
300
- # Index in the current file
301
- index_current = self [i ]['out' ]
330
+ index_final = self .i_current_by_tracks [i_id ]
302
331
303
332
if self .virtual :
304
333
# If the flag virtual in correspondance is active,
305
334
# the previous is virtual
306
335
m_virtual = self [i ]['virtual' ]
307
336
if m_virtual .any ():
308
- index_virtual = index_final [m_virtual ]
309
337
# Incrementing index
310
- i_current_by_tracks [i_id [m_virtual ]
338
+ self . i_current_by_tracks [i_id [m_virtual ]
311
339
] += self [i ]['virtual_length' ][m_virtual ]
312
340
# Get new index
313
- index_final = i_current_by_tracks [i_id ]
341
+ index_final = self . i_current_by_tracks [i_id ]
314
342
343
+ # Index in the current file
344
+ index_current = self [i ]['out' ]
345
+
315
346
# Copy all variable
316
- for var , _ in eddies_current . obs . dtype . descr :
317
- self . eddies [var ][index_final
318
- ] = eddies_current [var ][index_current ]
347
+ for var , _ in fields :
348
+ eddies [var ][index_final
349
+ ] = self . current_obs [var ][index_current ]
319
350
320
351
# Add increment for each index used
321
- i_current_by_tracks [i_id ] += 1
322
- eddies_previous = eddies_current
323
-
352
+ self . i_current_by_tracks [i_id ] += 1
353
+ self . previous_obs = self . current_obs
354
+ return eddies
0 commit comments