Skip to content

Commit 9f03135

Browse files
committed
Add feature to extract track on other criterions
1 parent fcb43db commit 9f03135

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/py_eddy_tracker/observations/tracking.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,12 @@ def get_mask_from_id(self, tracks):
177177

178178
def compute_index(self):
179179
if self.__first_index_of_track is None:
180-
s = self.tracks.max()
181-
self.__first_index_of_track = -ones(s, self.tracks.dtype)
182-
self.__obs_by_track = zeros(s, self.observation_number.dtype)
180+
s = self.tracks.max() + 1
181+
# Doesn't work => core dump with numba, maybe he wait i8 instead of u4
182+
# self.__first_index_of_track = -ones(s, self.tracks.dtype)
183+
# self.__obs_by_track = zeros(s, self.observation_number.dtype)
184+
self.__first_index_of_track = -ones(s, 'i8')
185+
self.__obs_by_track = zeros(s, 'i8')
183186
logging.debug('Start computing index ...')
184187
compute_index(self.tracks, self.__first_index_of_track, self.__obs_by_track)
185188
logging.debug('... OK')
@@ -198,6 +201,19 @@ def extract_ids(self, tracks):
198201
mask = self.get_mask_from_id(array(tracks))
199202
return self.__extract_with_mask(mask)
200203

204+
def extract_with_length(self, bounds):
205+
b0, b1 = bounds
206+
if b0 >= 0 and b1 >=0:
207+
track_mask = (self.nb_obs_by_track >= b0) * (self.nb_obs_by_track <= b1)
208+
elif b0 < 0 and b1 >= 0:
209+
track_mask = self.nb_obs_by_track <= b1
210+
elif b0 >= 0 and b1 < 0:
211+
track_mask = self.nb_obs_by_track > b0
212+
else:
213+
logging.warning('No valid value for bounds')
214+
raise Exception('One bounds must be positiv')
215+
return self.__extract_with_mask(track_mask.repeat(self.nb_obs_by_track))
216+
201217
def __extract_with_mask(self, mask, full_path=False, remove_incomplete=False, compress_id=False):
202218
"""
203219
Extract a subset of observations

src/scripts/EddySubSetter

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Subset eddy Dataset
55
"""
66
from py_eddy_tracker import EddyParser
77
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
8-
from os.path import basename, dirname
8+
import logging
99

1010

1111
def id_parser():
@@ -14,6 +14,8 @@ def id_parser():
1414
parser.add_argument('filename_out')
1515
parser.add_argument('-p', '--period', nargs=2, type=int,
1616
help='Start day and end day, if it s negative value we will add to day min and add to day max, if 0 it s not use')
17+
parser.add_argument('-l', '--length', nargs=2, type=int,
18+
help='Minimal and maximal quantity of observation for one track, ones bounds could be negative, it will be not use')
1719
parser.add_argument('-f', '--full_path', action='store_true',
1820
help='Extract path, if one obs or more are selected')
1921
parser.add_argument('-d', '--remove_incomplete', action='store_true',
@@ -34,12 +36,20 @@ if __name__ == '__main__':
3436
# Original dataset
3537
dataset = TrackEddiesObservations.load_from_netcdf(args.filename, raw_data=False if args.no_raw_mode else True)
3638

39+
# Select with id
3740
if args.ids is not None:
3841
dataset = dataset.extract_ids(args.ids)
42+
43+
# Select with length
44+
if args.length is not None:
45+
dataset = dataset.extract_with_length(args.length)
46+
47+
# Select with a start date and end date
3948
if args.period is not None:
4049
dataset = dataset.extract_with_period(args.period, full_path=args.full_path,
4150
remove_incomplete=args.remove_incomplete)
4251

52+
# Select track which go through an area
4353
if args.area is not None:
4454
area = dict(llcrnrlon=args.area[0],
4555
llcrnrlat=args.area[1],
@@ -49,5 +59,8 @@ if __name__ == '__main__':
4959
dataset = dataset.extract_with_area(area, full_path=args.full_path,
5060
remove_incomplete=args.remove_incomplete)
5161

52-
if len(dataset) != 0:
62+
# if no data, no output will be written
63+
if len(dataset) == 0:
64+
logging.warning("No data are selected, out file couldn't be create")
65+
else:
5366
dataset.write_netcdf(filename=args.filename_out)

0 commit comments

Comments
 (0)