Skip to content

Commit 36f7b6c

Browse files
author
adelepoulle
committed
move action in class method
1 parent 7974898 commit 36f7b6c

File tree

3 files changed

+76
-45
lines changed

3 files changed

+76
-45
lines changed

share/tracking.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
DIAGNOSTIC_TYPE: 'SLA'
44

55
PATHS:
6+
# Files produces with EddyIdentification
67
FILES_PATTERN: /homelocal/adelepoulle/20151110_eddy_track/Anticyclonic_2014*.nc
7-
#~ FILES_PATTERN: /homelocal/adelepoulle/20151110_eddy_track/Cyclonic_2014*.nc
88
# Path and filename of Chelton et al (1998) Rossby radius data
99
# Obtain file from:
1010
# http://www-po.coas.oregonstate.edu/research/po/research/rossby_radius/
1111
RW_PATH: 'rossrad.dat'
1212
# Path for saving of outputs
1313
SAVE_DIR: 'test/'
1414

15-
TRACK_DURATION_MIN: 15 # Number of observations
15+
# Minimum number of observations to store eddy
16+
TRACK_DURATION_MIN: 15
1617
VIRTUAL_LEGNTH_MAX: 2
1718

1819
SEPARATION_METHOD: 'ellipse'

src/py_eddy_tracker/tracking.py

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def __init__(self, datasets, virtual=0):
6060
self.datasets = datasets
6161
self.previous2_obs = None
6262
self.previous_obs = None
63-
self.current_obs = EddiesObservations.load_from_netcdf(
64-
self.datasets[0])
63+
self.current_obs = None
6564

6665
# To use virtual obs
6766
# Number of obs which can prolongate real observations
@@ -82,21 +81,48 @@ def __init__(self, datasets, virtual=0):
8281
self.nb_obs = 0
8382
self.eddies = None
8483

84+
def reset_dataset_cache(self):
85+
self.previous2_obs = None
86+
self.previous_obs = None
87+
self.current_obs = None
88+
8589
def swap_dataset(self, dataset):
8690
"""
8791
"""
8892
self.previous2_obs = self.previous_obs
8993
self.previous_obs = self.current_obs
9094
self.current_obs = EddiesObservations.load_from_netcdf(dataset)
9195

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
96102
correspondance['out'] = i_current
97103

98104
if self.virtual:
105+
# if index in previous dataset is bigger than real obs number
106+
# it's a virtual data
99107
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+
100126

101127
def id_generator(self, nb_id):
102128
"""Generation id and incrementation
@@ -153,6 +179,8 @@ def track(self):
153179
"""
154180
START = True
155181
FLG_VIRTUAL = False
182+
self.reset_dataset_cache()
183+
self.swap_dataset(self.datasets[0])
156184
# We begin with second file, first one is in previous
157185
for file_name in self.datasets[1:]:
158186
self.swap_dataset(file_name)
@@ -169,7 +197,7 @@ def track(self):
169197
i_previous, i_current = self.previous_obs.tracking(self.current_obs)
170198
nb_match = i_previous.shape[0]
171199

172-
#~ self.store_correspondance(i_previous, i_current)
200+
#~ self.store_correspondance(i_previous, i_current, nb_real_obs)
173201
correspondance = array(i_previous, dtype=self.correspondance_dtype)
174202
correspondance['out'] = i_current
175203

@@ -204,7 +232,7 @@ def track(self):
204232

205233
# new_id is equal to UINT32_MAX we must add a new ones
206234
# we count the number of new
207-
mask_new_id = correspondance['id'] == UINT32_MAX
235+
mask_new_id = correspondance['id'] == self.UINT32_MAX
208236
nb_new_tracks = mask_new_id.sum()
209237
logging.debug('%d birth in this step', nb_new_tracks)
210238
# Set new id
@@ -247,77 +275,80 @@ def prepare_merging(self):
247275
# Compute index of each tracks
248276
self.i_current_by_tracks = self.nb_obs_by_tracks.cumsum() - self.nb_obs_by_tracks
249277
# Number of global obs
250-
self.nb_obs = nb_obs_by_tracks.sum()
278+
self.nb_obs = self.nb_obs_by_tracks.sum()
251279
logging.info('%d tracks identified', self.current_id)
252280
logging.info('%d observations will be join', self.nb_obs)
253281

254282
def merge(self):
283+
"""Merge all the correspondance in one array with all fields
284+
"""
255285
# Start create netcdf to agglomerate all eddy
256-
self.eddies = TrackEddiesObservations(size=self.nb_obs)
286+
eddies = TrackEddiesObservations(size=self.nb_obs)
257287

258288
# Calculate the index in each tracks, we compute in u4 and translate
259289
# in u2 (which are limited to 65535)
260290
logging.debug('Compute global index array (N)')
261-
n = arange(nb_obs,
291+
n = arange(self.nb_obs,
262292
dtype='u4') - self.i_current_by_tracks.repeat(self.nb_obs_by_tracks)
263-
self.eddies['n'][:] = uint16(n)
293+
eddies['n'][:] = uint16(n)
264294
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)
266296

267297
# Start loading identification again to save in the finals tracks
268298
# Load first file
269-
eddies_previous = EddiesObservations.load_from_netcdf(self.datasets[0])
299+
self.swap_dataset(self.datasets[0])
270300
# 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
272304

273305
# 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,
275307
dtype=bool_)
276308

277-
for i, file_name in enumerate(FILENAMES[1:]):
309+
for i, file_name in enumerate(self.datasets[1:]):
278310
# Load current file (we begin with second one)
279-
self.current_obs = EddiesObservations.load_from_netcdf(file_name)
311+
self.swap_dataset(file_name)
280312
# We select the list of id which are involve in the correspondance
281313
i_id = self[i]['id']
282314
# 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]
284316

285317
# First obs of eddies
286318
m_first_obs = -first_obs_save_in_tracks[i_id]
287319
if m_first_obs.any():
288-
# Index in the current file
320+
# Index in the previous file
289321
index_in = self[i]['in'][m_first_obs]
290322
# 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]
294326
# Increment
295-
i_current_by_tracks[i_id[m_first_obs]] += 1
327+
self.i_current_by_tracks[i_id[m_first_obs]] += 1
296328
# Active this flag, we have only one first by tracks
297329
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]
302331

303332
if self.virtual:
304333
# If the flag virtual in correspondance is active,
305334
# the previous is virtual
306335
m_virtual = self[i]['virtual']
307336
if m_virtual.any():
308-
index_virtual = index_final[m_virtual]
309337
# Incrementing index
310-
i_current_by_tracks[i_id[m_virtual]
338+
self.i_current_by_tracks[i_id[m_virtual]
311339
] += self[i]['virtual_length'][m_virtual]
312340
# Get new index
313-
index_final = i_current_by_tracks[i_id]
341+
index_final = self.i_current_by_tracks[i_id]
314342

343+
# Index in the current file
344+
index_current = self[i]['out']
345+
315346
# 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]
319350

320351
# 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

src/scripts/EddyTracking

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
"""
4+
Track eddy with Identification file produce with EddyIdentification
45
"""
5-
66
from py_eddy_tracker import EddyParser
77
from glob import glob
88
from yaml import load as yaml_load
99
from py_eddy_tracker.tracking import Correspondances
1010
from py_eddy_tracker.observations import \
1111
EddiesObservations, TrackEddiesObservations
1212
import logging
13-
from numpy import array, arange, bool_, uint16, unique, setdiff1d, \
14-
ones, zeros
13+
from numpy import unique
1514
import datetime as dt
1615

1716

@@ -55,7 +54,7 @@ if __name__ == '__main__':
5554

5655
CORRESPONDANCES.prepare_merging()
5756

58-
CORRESPONDANCES.merge()
57+
FINAL_EDDIES = CORRESPONDANCES.merge()
5958

6059
# We flag obs
6160
if CORRESPONDANCES.virtual:
@@ -70,10 +69,10 @@ if __name__ == '__main__':
7069
logging.info('Duration : %s', FULL_TIME)
7170

7271
logging.info('The longest tracks have %d observations',
73-
nb_obs_by_tracks.max())
72+
CORRESPONDANCES.nb_obs_by_tracks.max())
7473

7574
SUBSET_EDDIES = FINAL_EDDIES.extract_longer_eddies(
76-
NB_OBS_MIN, nb_obs_by_tracks.repeat(nb_obs_by_tracks))
75+
NB_OBS_MIN, CORRESPONDANCES.nb_obs_by_tracks.repeat(CORRESPONDANCES.nb_obs_by_tracks))
7776

7877
logging.info('%d tracks will be saved',
7978
len(unique(SUBSET_EDDIES['track'])))

0 commit comments

Comments
 (0)