Skip to content

Commit b41a05f

Browse files
committed
Add new method of extraction
1 parent d71700f commit b41a05f

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

src/py_eddy_tracker/dataset/grid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
"""
44
import logging
5-
from numpy import concatenate, int32, empty, where, array, \
5+
from numpy import concatenate, empty, where, array, \
66
sin, deg2rad, pi, ones, cos, ma, int8, histogram2d, arange, float_, \
77
linspace, errstate, int_, interp, meshgrid, nan, ceil, sinc, isnan, \
88
percentile, zeros, arctan2, arcsin, round_, nanmean, exp, mean as np_mean
@@ -1082,8 +1082,7 @@ def convolve_filter_with_dynamic_kernel(self, grid, kernel_func, lat_max=85, ext
10821082
if debug_active and len(dt) > 0:
10831083
dt_mean = np_mean(dt) * (nb_lines - i)
10841084
print('Remain ', dt_mean, 'ETA ', t0 + dt_mean, 'current kernel size :', k_shape, 'Step : %d/%d ' % (i, nb_lines), end="\r")
1085-
1086-
1085+
10871086
# Half size, k_shape must be always impair
10881087
d_lat = int((k_shape[1] - 1) / 2)
10891088
d_lon = int((k_shape[0] - 1) / 2)
@@ -1119,7 +1118,8 @@ def convolve_filter_with_dynamic_kernel(self, grid, kernel_func, lat_max=85, ext
11191118
out = ma.array(data_out, mask=data_out.mask)
11201119
else:
11211120
out = ma.array(data_out, mask=data.mask + data_out.mask)
1122-
print()
1121+
if debug_active:
1122+
print()
11231123
if out.dtype != data.dtype:
11241124
return out.astype(data.dtype)
11251125
return out

src/py_eddy_tracker/observations/tracking.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,33 @@ def extract_ids(self, tracks):
223223
mask = self.get_mask_from_id(array(tracks))
224224
return self.__extract_with_mask(mask)
225225

226+
def extract_first_obs_in_box(self, res):
227+
data = empty(self.obs.shape, dtype=[('lon', 'f4'), ('lat', 'f4'), ('track', 'i4')])
228+
data['lon'] = self.longitude - self.longitude % res
229+
data['lat'] = self.latitude - self.latitude % res
230+
data['track'] = self.obs["track"]
231+
_, indexs = unique(data, return_index=True)
232+
mask = zeros(self.obs.shape, dtype='bool')
233+
mask[indexs] = True
234+
return self.__extract_with_mask(mask)
235+
236+
def extract_in_direction(self, direction, value=0):
237+
nb_obs = self.nb_obs_by_track
238+
i_start = self.index_from_track
239+
i_stop = i_start + nb_obs - 1
240+
if direction in ('S', 'N'):
241+
d_lat = self.latitude[i_stop] - self.latitude[i_start]
242+
mask = d_lat < 0 if 'S' == direction else d_lat > 0
243+
mask &= abs(d_lat) > value
244+
else:
245+
lon_start , lon_end = self.longitude[i_start], self.longitude[i_stop]
246+
lon_end = (lon_end - (lon_start - 180)) % 360 + lon_start - 180
247+
d_lon = lon_end - lon_start
248+
mask = d_lon < 0 if 'W' == direction else d_lon > 0
249+
mask &= abs(d_lon) > value
250+
mask = mask.repeat(nb_obs)
251+
return self.__extract_with_mask(mask)
252+
226253
def extract_with_length(self, bounds):
227254
b0, b1 = bounds
228255
if b0 >= 0 and b1 >= 0:

src/py_eddy_tracker/poly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def winding_number_grid_in_poly(x_1d, y_1d, i_x0, i_x1, x_size, i_y0, xy_poly):
8181
# the winding number counter
8282
nb_x, nb_y = len(x_1d), len(y_1d)
8383
wn = empty((nb_x, nb_y), dtype=numba_types.bool_)
84-
for i in range(nb_x):
84+
for i in prange(nb_x):
8585
x_pt = x_1d[i]
8686
for j in range(nb_y):
8787
y_pt = y_1d[j]

src/scripts/EddySubSetter

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ logger = logging.getLogger("pet")
1111

1212

1313
def id_parser():
14-
parser = EddyParser('Eddy Identification')
14+
parser = EddyParser('Eddy Subsetter')
1515
parser.add_argument('filename')
1616
parser.add_argument('filename_out')
1717

@@ -30,11 +30,17 @@ def id_parser():
3030
metavar=('llcrnrlon', 'llcrnrlat', 'urcrnrlon', 'urcrnrlat'),
3131
help='Coordinates of bounding to extract'
3232
)
33+
group.add_argument('--direction', choices=['E', 'W', 'S', 'N'],
34+
help='Select only track which have an end point which go in this direction')
35+
group.add_argument('--minimal_degrees_displacement_in_direction', type=float,
36+
help='Minimal displacement in direction specified in --directio options')
37+
group.add_argument('--select_first_observation_in_box', type=float,
38+
help='Select only the first obs in each box for each tracks, value specified must be resolution')
3339
group.add_argument('--remove_var', nargs='+', type=str, help='remove all listed variable')
3440
group.add_argument('--include_var', nargs='+', type=str, help='use only listed variable, remove_var will be ignored')
3541
group.add_argument('-i', '--ids', nargs='+', type=int, help='List of tracks which will be extract')
3642

37-
group = parser.add_argument_group('Extraction options')
43+
group = parser.add_argument_group('General options')
3844
group.add_argument('--sort_time', action='store_true', help='sort all observation with time')
3945

4046
parser.add_argument('-n', '--no_raw_mode', action='store_true',
@@ -78,6 +84,18 @@ if __name__ == '__main__':
7884
remove_incomplete=args.remove_incomplete,
7985
reject_virtual=args.reject_virtual)
8086

87+
# Select only track which go in the direction specified
88+
if args.direction:
89+
if args.minimal_degrees_displacement_in_direction:
90+
dataset = dataset.extract_in_direction(
91+
args.direction,
92+
value=args.minimal_degrees_displacement_in_direction)
93+
else:
94+
dataset = dataset.extract_in_direction(args.direction)
95+
96+
if args.select_first_observation_in_box:
97+
dataset = dataset.extract_first_obs_in_box(res=args.select_first_observation_in_box)
98+
8199
if args.sort_time:
82100
logger.debug('start sorting ...')
83101
dataset.obs.sort(order=['time', 'lon', 'lat'])

0 commit comments

Comments
 (0)