Skip to content

Commit 2ecca83

Browse files
committed
remove repeated point in contour to speed up computation
1 parent 19b2b4f commit 2ecca83

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/py_eddy_tracker/observations/observation.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,26 @@ def grid_count_pixel_in(
21942194
grid_count_(grid, i, j)
21952195

21962196

2197+
@njit(cache=True)
2198+
def reduce_size(x, y):
2199+
"""
2200+
Reduce array size if last position is repeated, in order to save compute time
2201+
2202+
:param array x: longitude
2203+
:param array y: latitude
2204+
2205+
:return: reduce arrays x,y
2206+
:rtype: ndarray,ndarray
2207+
"""
2208+
i = x.shape[0]
2209+
x0, y0 = x[0], y[0]
2210+
while True:
2211+
i -= 1
2212+
if x[i] != x0 or y[i] != y0:
2213+
i += 1
2214+
return x[:i], y[:i]
2215+
2216+
21972217
@njit(cache=True)
21982218
def poly_indexs(x_p, y_p, x_c, y_c):
21992219
"""
@@ -2208,9 +2228,10 @@ def poly_indexs(x_p, y_p, x_c, y_c):
22082228
nb_c = x_c.shape[0]
22092229
indexs = -ones(nb_p, dtype=numba_types.int32)
22102230
for i in range(nb_c):
2211-
x_c_min, y_c_min = x_c[i].min(), y_c[i].min()
2212-
x_c_max, y_c_max = x_c[i].max(), y_c[i].max()
2213-
v = create_vertice(x_c[i], y_c[i])
2231+
x_, y_ = reduce_size(x_c[i], y_c[i])
2232+
x_c_min, y_c_min = x_.min(), y_.min()
2233+
x_c_max, y_c_max = x_.max(), y_.max()
2234+
v = create_vertice(x_, y_)
22142235
for j in range(nb_p):
22152236
if indexs[j] != -1:
22162237
continue
@@ -2235,9 +2256,10 @@ def insidepoly(x_p, y_p, x_c, y_c):
22352256
nb_c = x_c.shape[0]
22362257
flag = zeros(nb_p, dtype=numba_types.bool_)
22372258
for i in range(nb_c):
2238-
x_c_min, y_c_min = x_c[i].min(), y_c[i].min()
2239-
x_c_max, y_c_max = x_c[i].max(), y_c[i].max()
2240-
v = create_vertice(x_c[i], y_c[i])
2259+
x_, y_ = reduce_size(x_c[i], y_c[i])
2260+
x_c_min, y_c_min = x_.min(), y_.min()
2261+
x_c_max, y_c_max = x_.max(), y_.max()
2262+
v = create_vertice(x_, y_)
22412263
for j in range(nb_p):
22422264
if flag[j]:
22432265
continue

0 commit comments

Comments
 (0)