Skip to content

Commit 3a75c01

Browse files
better create_particles
1 parent c3f95b8 commit 3a75c01

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
matplotlib
22
netCDF4
3-
numba
3+
numba>=0.53
44
numpy
55
opencv-python
66
pint

src/py_eddy_tracker/observations/groups.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
from numba import njit
55
from numba import types as nb_types
6-
from numpy import arange, int32, interp, median, where, zeros, meshgrid, concatenate
6+
from numpy import arange, array, int32, interp, median, where, zeros
77

8+
from ..poly import create_vertice, reduce_size, winding_number_poly
89
from .observation import EddiesObservations
9-
from ..poly import group_obs
1010

1111
logger = logging.getLogger("pet")
1212

@@ -88,6 +88,24 @@ def advect(x, y, c, t0, n_days):
8888
return t, x, y
8989

9090

91+
@njit(cache=True)
92+
def _create_meshed_particles(lons, lats, step):
93+
x_out, y_out, i_out = list(), list(), list()
94+
for i, (lon, lat) in enumerate(zip(lons, lats)):
95+
lon_min, lon_max = lon.min(), lon.max()
96+
lat_min, lat_max = lat.min(), lat.max()
97+
lon_min -= lon_min % step
98+
lon_max -= lon_max % step - step * 2
99+
lat_min -= lat_min % step
100+
lat_max -= lat_max % step - step * 2
101+
102+
for x in arange(lon_min, lon_max, step):
103+
for y in arange(lat_min, lat_max, step):
104+
if winding_number_poly(x, y, create_vertice(*reduce_size(lon, lat))):
105+
x_out.append(x), y_out.append(y), i_out.append(i)
106+
return array(x_out), array(y_out), array(i_out)
107+
108+
91109
def create_particles(eddies, step):
92110
"""create particles only inside speed contour. Avoid creating too large numpy arrays, only to me masked
93111
@@ -102,36 +120,7 @@ def create_particles(eddies, step):
102120
lon = eddies.contour_lon_s
103121
lat = eddies.contour_lat_s
104122

105-
# compute bounding boxes of each eddies
106-
lonMins = lon.min(axis=1)
107-
lonMins = lonMins - (lonMins % step)
108-
lonMaxs = lon.max(axis=1)
109-
lonMaxs = lonMaxs - (lonMaxs % step) + step * 2
110-
111-
latMins = lat.min(axis=1)
112-
latMins = latMins - (latMins % step)
113-
latMaxs = lat.max(axis=1)
114-
latMaxs = latMaxs - (latMaxs % step) + step * 2
115-
116-
lon = []
117-
lat = []
118-
# for each eddies, create mesh with particles then concatenate
119-
for lonMin, lonMax, latMin, latMax in zip(lonMins, lonMaxs, latMins, latMaxs):
120-
x0, y0 = meshgrid(arange(lonMin, lonMax, step), arange(latMin, latMax, step))
121-
122-
x0, y0 = x0.reshape(-1), y0.reshape(-1)
123-
lon.append(x0)
124-
lat.append(y0)
125-
126-
x = concatenate(lon)
127-
y = concatenate(lat)
128-
129-
_, i = group_obs(x, y, 1, 360)
130-
x, y = x[i], y[i]
131-
132-
i_start = eddies.contains(x, y, intern=True)
133-
m = i_start != -1
134-
return x[m], y[m], i_start[m]
123+
return _create_meshed_particles(lon, lat, step)
135124

136125

137126
def particle_candidate(c, eddies, step_mesh, t_start, i_target, pct, **kwargs):

0 commit comments

Comments
 (0)