Skip to content

Commit 0834ed2

Browse files
- changed log format, too many tabs
- adding more logs - change generator time step on GridCollection, to work it needed more files than necessary. - added coherence computation
1 parent b3f66bc commit 0834ed2

File tree

6 files changed

+213
-146
lines changed

6 files changed

+213
-146
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ Changed
1212
^^^^^^^
1313
Fixed
1414
^^^^^
15+
- GridCollection get_next_time_step & get_previous_time_step needed more files to work in the dataset list.
16+
The loop needed explicitly self.dataset[i+-1] even when i==0, therefore indice went out of range
1517
Added
1618
^^^^^
1719

20+
1821
[3.4.0] - 2021-03-29
1922
--------------------
2023
Changed

src/py_eddy_tracker/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232

3333

3434
def start_logger():
35-
FORMAT_LOG = (
36-
"%(levelname)-8s %(asctime)s %(module)s.%(funcName)s :\n\t\t\t\t\t%(message)s"
37-
)
35+
FORMAT_LOG = "%(levelname)-8s %(asctime)s %(module)s.%(funcName)s :\n\t%(message)s"
3836
logger = logging.getLogger("pet")
3937
if len(logger.handlers) == 0:
4038
# set up logging to CONSOLE

src/py_eddy_tracker/dataset/grid.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,11 +2257,13 @@ def from_netcdf_cube(cls, filename, x_name, y_name, t_name, heigth=None):
22572257
@classmethod
22582258
def from_netcdf_list(cls, filenames, t, x_name, y_name, indexs=None, heigth=None):
22592259
new = cls()
2260-
for i, t in enumerate(t):
2261-
d = RegularGridDataset(filenames[i], x_name, y_name, indexs=indexs)
2260+
for i, _t in enumerate(t):
2261+
filename = filenames[i]
2262+
logger.debug(f"load file {i:02d}/{len(t)} t={_t} : {filename}")
2263+
d = RegularGridDataset(filename, x_name, y_name, indexs=indexs)
22622264
if heigth is not None:
22632265
d.add_uv(heigth)
2264-
new.datasets.append((t, d))
2266+
new.datasets.append((_t, d))
22652267
return new
22662268

22672269
def shift_files(self, t, filename, heigth=None, **rgd_kwargs):
@@ -2273,6 +2275,7 @@ def shift_files(self, t, filename, heigth=None, **rgd_kwargs):
22732275
if heigth is not None:
22742276
d.add_uv(heigth)
22752277
self.datasets.append((t, d))
2278+
logger.debug(f"shift and adding i={len(self.datasets)} t={t} : {filename}")
22762279

22772280
def interp(self, grid_name, t, lons, lats, method="bilinear"):
22782281
"""
@@ -2433,6 +2436,7 @@ def advect(
24332436
else:
24342437
mask_particule += isnan(x) + isnan(y)
24352438
while True:
2439+
logger.debug(f"advect : t={t}")
24362440
if (backward and t <= t1) or (not backward and t >= t1):
24372441
t0, u0, v0, m0 = t1, u1, v1, m1
24382442
t1, d1 = generator.__next__()
@@ -2459,25 +2463,21 @@ def advect(
24592463
yield t, x, y
24602464

24612465
def get_next_time_step(self, t_init):
2462-
first = True
24632466
for i, (t, dataset) in enumerate(self.datasets):
24642467
if t < t_init:
24652468
continue
2466-
if first:
2467-
first = False
2468-
yield self.datasets[i - 1]
2469+
2470+
logger.debug(f"i={i}, t={t}, dataset={dataset}")
24692471
yield t, dataset
24702472

24712473
def get_previous_time_step(self, t_init):
2472-
first = True
24732474
i = len(self.datasets)
24742475
for t, dataset in reversed(self.datasets):
24752476
i -= 1
24762477
if t > t_init:
24772478
continue
2478-
if first:
2479-
first = False
2480-
yield self.datasets[i + 1]
2479+
2480+
logger.debug(f"i={i}, t={t}, dataset={dataset}")
24812481
yield t, dataset
24822482

24832483

src/py_eddy_tracker/observations/groups.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +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
6+
from numpy import arange, int32, interp, median, where, zeros, meshgrid, concatenate
77

88
from .observation import EddiesObservations
9+
from ..poly import group_obs
910

1011
logger = logging.getLogger("pet")
1112

@@ -87,7 +88,53 @@ def advect(x, y, c, t0, n_days):
8788
return t, x, y
8889

8990

90-
def particle_candidate(x, y, c, eddies, t_start, i_target, pct, **kwargs):
91+
def create_particles(eddies, step):
92+
"""create particles only inside speed contour. Avoir creating too large numpy arrays, only to me masked
93+
94+
:param eddies: network where eddies are
95+
:type eddies: network
96+
:param step: step for particles
97+
:type step: float
98+
:return: lon, lat and indices of particles in contour speed
99+
:rtype: tuple(np.array)
100+
"""
101+
102+
lon = eddies.contour_lon_s
103+
lat = eddies.contour_lat_s
104+
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]
135+
136+
137+
def particle_candidate(c, eddies, step_mesh, t_start, i_target, pct, **kwargs):
91138
"""Select particles within eddies, advect them, return target observation and associated percentages
92139
93140
:param np.array(float) x: longitude of particles
@@ -100,27 +147,28 @@ def particle_candidate(x, y, c, eddies, t_start, i_target, pct, **kwargs):
100147
:params dict kwargs: dict of params given to `advect`
101148
102149
"""
103-
104150
# Obs from initial time
105151
m_start = eddies.time == t_start
106-
107152
e = eddies.extract_with_mask(m_start)
153+
108154
# to be able to get global index
109155
translate_start = where(m_start)[0]
110-
# Identify particle in eddies (only in core)
111-
i_start = e.contains(x, y, intern=True)
112-
m = i_start != -1
113156

114-
x, y, i_start = x[m], y[m], i_start[m]
115-
# Advect
157+
x, y, i_start = create_particles(e, step_mesh)
158+
159+
# Advection
116160
t_end, x, y = advect(x, y, c, t_start, **kwargs)
161+
117162
# eddies at last date
118163
m_end = eddies.time == t_end / 86400
119164
e_end = eddies.extract_with_mask(m_end)
165+
120166
# to be able to get global index
121167
translate_end = where(m_end)[0]
168+
122169
# Id eddies for each alive particle (in core and extern)
123170
i_end = e_end.contains(x, y)
171+
124172
# compute matrix and fill target array
125173
get_matrix(i_start, i_end, translate_start, translate_end, i_target, pct)
126174

0 commit comments

Comments
 (0)