3030from matplotlib .dates import julian2num , num2date
3131
3232from py_eddy_tracker .observations import EddiesObservations , VirtualEddiesObservations , TrackEddiesObservations
33- from numpy import bool_ , array , arange , ones , setdiff1d , zeros , uint16 , where , empty , isin , unique , concatenate , ma
34- from netCDF4 import Dataset
33+ from numpy import bool_ , array , arange , ones , setdiff1d , zeros , uint16 , where , empty , isin , unique , concatenate , \
34+ ma
35+ from netCDF4 import Dataset , default_fillvals
3536import logging
3637import platform
3738
@@ -55,7 +56,9 @@ def __init__(self, datasets, virtual=0, class_method=None, previous_correspondan
5556 # Correspondance dtype
5657 self .correspondance_dtype = [('in' , 'u2' ),
5758 ('out' , 'u2' ),
58- ('id' , self .ID_DTYPE )]
59+ ('id' , self .ID_DTYPE ),
60+ ('cost_value' , 'f4' )
61+ ]
5962 if class_method is None :
6063 self .class_method = EddiesObservations
6164 else :
@@ -168,7 +171,7 @@ def merge_correspondance(self, other):
168171 # We set new id available
169172 self .current_id = translate [- 1 ] + 1
170173
171- def store_correspondance (self , i_previous , i_current , nb_real_obs ):
174+ def store_correspondance (self , i_previous , i_current , nb_real_obs , association_cost ):
172175 """Storing correspondance in an array
173176 """
174177 # Create array to store correspondance data
@@ -177,6 +180,7 @@ def store_correspondance(self, i_previous, i_current, nb_real_obs):
177180 correspondance ['virtual_length' ][:] = 255
178181 # index from current_obs
179182 correspondance ['out' ] = i_current
183+ correspondance ['cost_value' ] = association_cost
180184
181185 if self .virtual :
182186 # if index in previous dataset is bigger than real obs number
@@ -314,10 +318,10 @@ def track(self):
314318 if flg_virtual :
315319 logging .debug ('%d virtual obs will be add to previous' , len (self .virtual_obs ))
316320 self .previous_obs = self .previous_obs .merge (self .virtual_obs )
317- i_previous , i_current = self .previous_obs .tracking (self .current_obs )
321+ i_previous , i_current , association_cost = self .previous_obs .tracking (self .current_obs )
318322
319323 # return true if the first time (previous2obs is none)
320- if self .store_correspondance (i_previous , i_current , nb_real_obs ):
324+ if self .store_correspondance (i_previous , i_current , nb_real_obs , association_cost ):
321325 continue
322326
323327 self .recense_dead_id_to_extend ()
@@ -376,7 +380,11 @@ def save(self, filename, dict_completion=None):
376380 for name , _ in self .correspondance_dtype :
377381 datas [name ][i , :nb_elt ] = correspondance [name ]
378382 for name , data in datas .items ():
379- h_nc .variables [name ][:] = data
383+ h_v = h_nc .variables [name ]
384+ h_v [:] = data
385+ if 'File' not in name :
386+ h_v .min = h_v [:].min ()
387+ h_v .max = h_v [:].max ()
380388
381389 h_nc .virtual_use = str (self .virtual )
382390 h_nc .virtual_max_segment = self .nb_virtual
@@ -512,6 +520,8 @@ def merge(self, until=-1):
512520 track_array_variables = self .current_obs .track_array_variables ,
513521 array_variables = self .current_obs .array_variables )
514522
523+ # All the value put at nan, necessary only for all end of track
524+ eddies ['cost_association' ][:] = default_fillvals ['f4' ]
515525 # Calculate the index in each tracks, we compute in u4 and translate
516526 # in u2 (which are limited to 65535)
517527 logging .debug ('Compute global index array (N)' )
@@ -547,6 +557,8 @@ def merge(self, until=-1):
547557 # Copy all variable
548558 for field in fields :
549559 var = field [0 ]
560+ if var == 'cost_association' :
561+ continue
550562 eddies [var ][index_final [m_first_obs ]] = self .previous_obs [var ][index_in ]
551563 # Increment
552564 self .i_current_by_tracks [i_id [m_first_obs ]] += 1
@@ -570,7 +582,10 @@ def merge(self, until=-1):
570582 # Copy all variable
571583 for field in fields :
572584 var = field [0 ]
573- eddies [var ][index_final ] = self .current_obs [var ][index_current ]
585+ if var == 'cost_association' :
586+ eddies [var ][index_final - 1 ] = self [i ]['cost_value' ]
587+ else :
588+ eddies [var ][index_final ] = self .current_obs [var ][index_current ]
574589
575590 # Add increment for each index used
576591 self .i_current_by_tracks [i_id ] += 1
0 commit comments