Skip to content

Commit 2f20ca6

Browse files
create particles in class method
1 parent 3a75c01 commit 2f20ca6

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

src/py_eddy_tracker/observations/groups.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

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

8-
from ..poly import create_vertice, reduce_size, winding_number_poly
98
from .observation import EddiesObservations
109

1110
logger = logging.getLogger("pet")
@@ -88,41 +87,6 @@ def advect(x, y, c, t0, n_days):
8887
return t, x, y
8988

9089

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-
109-
def create_particles(eddies, step):
110-
"""create particles only inside speed contour. Avoid creating too large numpy arrays, only to me masked
111-
112-
:param eddies: network where eddies are
113-
:type eddies: network
114-
:param step: step for particles
115-
:type step: float
116-
:return: lon, lat and indices of particles in contour speed
117-
:rtype: tuple(np.array)
118-
"""
119-
120-
lon = eddies.contour_lon_s
121-
lat = eddies.contour_lat_s
122-
123-
return _create_meshed_particles(lon, lat, step)
124-
125-
12690
def particle_candidate(c, eddies, step_mesh, t_start, i_target, pct, **kwargs):
12791
"""Select particles within eddies, advect them, return target observation and associated percentages
12892
@@ -141,7 +105,7 @@ def particle_candidate(c, eddies, step_mesh, t_start, i_target, pct, **kwargs):
141105
# to be able to get global index
142106
translate_start = where(m_start)[0]
143107

144-
x, y, i_start = create_particles(e, step_mesh)
108+
x, y, i_start = e.create_particles(step_mesh)
145109

146110
# Advection
147111
t_end, x, y = advect(x, y, c, t_start, **kwargs)

src/py_eddy_tracker/observations/observation.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
poly_indexs,
7070
reduce_size,
7171
vertice_overlap,
72+
winding_number_poly
7273
)
7374

7475
logger = logging.getLogger("pet")
@@ -2274,6 +2275,19 @@ def nb_days(self):
22742275
"""
22752276
return self.period[1] - self.period[0] + 1
22762277

2278+
def create_particles(self, step, intern=True):
2279+
"""create particles only inside speed contour. Avoid creating too large numpy arrays, only to me masked
2280+
2281+
:param step: step for particles
2282+
:type step: float
2283+
:param bool intern: If true use speed contour instead of effective contour
2284+
:return: lon, lat and indices of particles
2285+
:rtype: tuple(np.array)
2286+
"""
2287+
2288+
xname, yname = self.intern(intern)
2289+
return _create_meshed_particles(self[xname], self[yname], step)
2290+
22772291

22782292
@njit(cache=True)
22792293
def grid_count_(grid, i, j):
@@ -2430,6 +2444,24 @@ def grid_stat(x_c, y_c, grid, x, y, result, circular=False, method="mean"):
24302444
result[elt] = v_max
24312445

24322446

2447+
@njit(cache=True)
2448+
def _create_meshed_particles(lons, lats, step):
2449+
x_out, y_out, i_out = list(), list(), list()
2450+
for i, (lon, lat) in enumerate(zip(lons, lats)):
2451+
lon_min, lon_max = lon.min(), lon.max()
2452+
lat_min, lat_max = lat.min(), lat.max()
2453+
lon_min -= lon_min % step
2454+
lon_max -= lon_max % step - step * 2
2455+
lat_min -= lat_min % step
2456+
lat_max -= lat_max % step - step * 2
2457+
2458+
for x in arange(lon_min, lon_max, step):
2459+
for y in arange(lat_min, lat_max, step):
2460+
if winding_number_poly(x, y, create_vertice(*reduce_size(lon, lat))):
2461+
x_out.append(x), y_out.append(y), i_out.append(i)
2462+
return array(x_out), array(y_out), array(i_out)
2463+
2464+
24332465
class VirtualEddiesObservations(EddiesObservations):
24342466
"""Class to work with virtual obs"""
24352467

0 commit comments

Comments
 (0)