Skip to content

Commit 0bbc74f

Browse files
committed
indice correction over bounds
1 parent 484387f commit 0bbc74f

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

src/py_eddy_tracker/dataset/grid.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from numpy import concatenate, int32, empty, maximum, where, array, \
66
sin, deg2rad, pi, ones, cos, ma, int8, histogram2d, arange, float_, \
77
linspace, errstate, int_, column_stack, interp, meshgrid, nan, ceil, sinc, float64, isnan, \
8-
floor, percentile
8+
floor, percentile, zeros
99
from datetime import datetime
1010
from scipy.special import j1
1111
from netCDF4 import Dataset
@@ -363,6 +363,8 @@ def grid(self, varname):
363363
if i_x > i_y:
364364
self.variables_description[varname]['infos']['transpose'] = True
365365
self.vars[varname] = self.vars[varname].T
366+
if not hasattr(self.vars[varname], 'mask'):
367+
self.vars[varname] = ma.array(self.vars[varname], mask=zeros(self.vars[varname].shape, dtype='bool'))
366368
return self.vars[varname]
367369

368370
def high_filter(self, grid_name, x_cut, y_cut):
@@ -402,7 +404,7 @@ def eddy_identification(self, grid_height, uname, vname, date, step=0.005, shape
402404
d_z = z_max -z_min
403405
data_tmp = data[~data.mask]
404406
epsilon = 0.001 # in %
405-
z_min_p, z_max_p = percentile(data_tmp, epsilon), percentile(data_tmp,100-epsilon)
407+
z_min_p, z_max_p = percentile(data_tmp, epsilon), percentile(data_tmp, 100 - epsilon)
406408
d_zp = z_max_p - z_min_p
407409
if d_z / d_zp > 2:
408410
logging.warning('Maybe some extrema are present zmin %f (m) and zmax %f (m) will be replace by %f and %f',

src/py_eddy_tracker/eddy_feature.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ def __init__(self, contour, contour_height, data, interval):
5959
self.contour = contour
6060
# Link on original grid (local view) or copy if it's on bound
6161
slice_x, slice_y = contour.bbox_slice
62-
if slice_x.start > slice_x.stop:
62+
on_bounds = slice_x.start > slice_x.stop
63+
if on_bounds:
6364
self.grid_extract = ma.concatenate((data[slice_x.start:, slice_y], data[:slice_x.stop, slice_y]))
6465
else:
6566
self.grid_extract = data[slice_x, slice_y]
6667
# => maybe replace pixel out of contour by nan?
6768
self.mask = zeros(self.grid_extract.shape, dtype='bool')
68-
self.mask[contour.pixels_index[0] - slice_x.start, contour.pixels_index[1] - slice_y.start] = True
69+
if on_bounds:
70+
i_x = (contour.pixels_index[0] - slice_x.start) % data.shape[0]
71+
else:
72+
i_x = contour.pixels_index[0] - slice_x.start
73+
self.mask[i_x, contour.pixels_index[1] - slice_y.start] = True
6974

7075
# Only pixel in contour
7176
self.sla = data[contour.pixels_index]
@@ -77,19 +82,9 @@ def __init__(self, contour, contour_height, data, interval):
7782
def within_amplitude_limits(self):
7883
"""Need update
7984
"""
80-
return True
85+
return (self.interval * 2) <= self.amplitude
8186
return self.eddy.ampmin <= self.amplitude <= self.eddy.ampmax
8287

83-
def _set_cyc_amplitude(self):
84-
"""Get amplitude for cyclone
85-
"""
86-
self.amplitude = self.h_0 - self.sla.min()
87-
88-
def _set_acyc_amplitude(self):
89-
"""Get amplitude for anticyclone
90-
"""
91-
self.amplitude = self.sla.max() - self.h_0
92-
9388
def all_pixels_below_h0(self, level):
9489
"""
9590
Check CSS11 criterion 1: The SSH values of all of the pixels
@@ -101,17 +96,23 @@ def all_pixels_below_h0(self, level):
10196
else:
10297
# All local extrema index on th box
10398
lmi_i, lmi_j = self._set_local_extrema(1)
99+
nb = len(lmi_i)
104100
slice_x, slice_y = self.contour.bbox_slice
105-
if len(lmi_i) == 1:
106-
i, j = lmi_i[0] + slice_x.start, lmi_j[0] + slice_y.start
101+
if nb == 0:
102+
logging.warning('No extrema found in contour in level %f', level)
103+
return False
104+
elif nb == 1:
105+
i, j = lmi_i[0], lmi_j[0]
107106
else:
108107
# Verify if several extrema are seriously below contour
109108
nb_real_extrema = ((level - self.grid_extract[lmi_i, lmi_j]) >= 2 * self.interval).sum()
110109
if nb_real_extrema > self.mle:
111110
return False
112111
index = self.grid_extract[lmi_i, lmi_j].argmin()
113-
i, j = lmi_i[index] + slice_x.start, lmi_j[index] + slice_y.start
114-
self._set_cyc_amplitude()
112+
i, j = lmi_i[index], lmi_j[index]
113+
self.amplitude = abs(self.grid_extract[i, j] - self.h_0)
114+
i += slice_x.start
115+
j += slice_y.start
115116
return i, j
116117

117118
def all_pixels_above_h0(self, level):
@@ -121,23 +122,27 @@ def all_pixels_above_h0(self, level):
121122
"""
122123
# In some case pixel value must be very near of contour bounds
123124
if ((self.sla - self.h_0) < - self.EPSILON).any() or (hasattr(self.sla, 'mask') and self.sla.mask.any()):
124-
# i.e.,with self.amplitude == 0
125125
return False
126126
else:
127-
128127
# All local extrema index on th box
129128
lmi_i, lmi_j = self._set_local_extrema(-1)
129+
nb = len(lmi_i)
130130
slice_x, slice_y = self.contour.bbox_slice
131-
if len(lmi_i) == 1:
132-
i, j = lmi_i[0] + slice_x.start, lmi_j[0] + slice_y.start
131+
if nb == 0:
132+
logging.warning('No extrema found in contour in level %f', level)
133+
return False
134+
elif nb == 1:
135+
i, j = lmi_i[0], lmi_j[0]
133136
else:
134137
# Verify if several extrema are seriously above contour
135138
nb_real_extrema = ((self.grid_extract[lmi_i, lmi_j] - level) >= 2 * self.interval).sum()
136139
if nb_real_extrema > self.mle:
137140
return False
138141
index = self.grid_extract[lmi_i, lmi_j].argmax()
139-
i, j = lmi_i[index] + slice_x.start, lmi_j[index] + slice_y.start
140-
self._set_cyc_amplitude()
142+
i, j = lmi_i[index], lmi_j[index]
143+
self.amplitude = abs(self.grid_extract[i, j] - self.h_0)
144+
i += slice_x.start
145+
j += slice_y.start
141146
return i, j
142147

143148
def _set_local_extrema(self, sign):
@@ -149,7 +154,7 @@ def _set_local_extrema(self, sign):
149154
# Only index in contour
150155
m = self.mask[i_x, i_y]
151156
i_x, i_y = i_x[m], i_y[m]
152-
# Verify if some extramum is contigus
157+
# Verify if some extremum is contigus
153158
nb_extrema = len(i_x)
154159
if nb_extrema > 1:
155160
# Group
@@ -316,7 +321,7 @@ def __init__(self, x, y, z, levels, bbox_surface_min_degree, wrap_x=False, keep_
316321
logging.debug('Y shape : %s', y.shape)
317322
logging.debug('Z shape : %s', z.shape)
318323
logging.info('Start computing iso lines with %d levels from %f to %f ...', len(levels), levels[0], levels[-1])
319-
self.contours = ax.contour(x, y, z.T, levels, cmap='Spectral_r')
324+
self.contours = ax.contour(x, y, z.T, levels, cmap='rainbow')
320325
if wrap_x:
321326
self.find_wrapcut_path_and_join(x[0], x[-1])
322327
logging.info('Finish computing iso lines')

0 commit comments

Comments
 (0)