-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy path_index_search.py
More file actions
437 lines (391 loc) · 16.2 KB
/
_index_search.py
File metadata and controls
437 lines (391 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from __future__ import annotations
from datetime import datetime
from typing import TYPE_CHECKING
import numpy as np
from parcels._typing import (
GridIndexingType,
InterpMethodOption,
)
from parcels.tools.statuscodes import (
FieldOutOfBoundError,
FieldOutOfBoundSurfaceError,
_raise_field_out_of_bound_error,
_raise_field_out_of_bound_surface_error,
_raise_field_sampling_error,
)
from .basegrid import GridType
if TYPE_CHECKING:
from parcels.xgrid import XGrid
from .field import Field
def _search_time_index(field: Field, time: datetime):
"""Find and return the index and relative coordinate in the time array associated with a given time.
Parameters
----------
field: Field
time: datetime
This is the amount of time, in seconds (time_delta), in unix epoch
Note that we normalize to either the first or the last index
if the sampled value is outside the time value range.
"""
ti = np.argmin(field._time_float <= time) - 1
tau = (time - field._time_float[ti]) / (field._time_float[ti + 1] - field._time_float[ti])
if tau < 0 or tau > 1: # TODO only for debugging; test can go?
raise ValueError(f"Time {time} is out of bounds for field time data {field.data.time.data}.")
return tau, ti
def search_indices_vertical_z(depth, gridindexingtype: GridIndexingType, z: float):
if depth[-1] > depth[0]:
if z < depth[0]:
# Since MOM5 is indexed at cell bottom, allow z at depth[0] - dz where dz = (depth[1] - depth[0])
if gridindexingtype == "mom5" and z > 2 * depth[0] - depth[1]:
return (-1, z / depth[0])
else:
_raise_field_out_of_bound_surface_error(z, None, None)
elif z > depth[-1]:
# In case of CROCO, allow particles in last (uppermost) layer using depth[-1]
if gridindexingtype in ["croco"] and z < 0:
return (-2, 1)
_raise_field_out_of_bound_error(z, None, None)
depth_indices = depth < z
if z >= depth[-1]:
zi = len(depth) - 2
else:
zi = depth_indices.argmin() - 1 if z > depth[0] else 0
else:
if z > depth[0]:
_raise_field_out_of_bound_surface_error(z, None, None)
elif z < depth[-1]:
_raise_field_out_of_bound_error(z, None, None)
depth_indices = depth > z
if z <= depth[-1]:
zi = len(depth) - 2
else:
zi = depth_indices.argmin() - 1 if z < depth[0] else 0
zeta = (z - depth[zi]) / (depth[zi + 1] - depth[zi])
while zeta > 1:
zi += 1
zeta = (z - depth[zi]) / (depth[zi + 1] - depth[zi])
while zeta < 0:
zi -= 1
zeta = (z - depth[zi]) / (depth[zi + 1] - depth[zi])
return (zi, zeta)
## TODO : Still need to implement the search_indices_vertical_s function
def search_indices_vertical_s(
field: Field,
interp_method: InterpMethodOption,
time: float,
z: float,
y: float,
x: float,
ti: int,
yi: int,
xi: int,
eta: float,
xsi: float,
):
if interp_method in ["bgrid_velocity", "bgrid_w_velocity", "bgrid_tracer"]:
xsi = 1
eta = 1
if time < field.time[ti]:
ti -= 1
if field._z4d: # type: ignore[attr-defined]
if ti == len(field.time) - 1:
depth_vector = (
(1 - xsi) * (1 - eta) * field.depth[-1, :, yi, xi]
+ xsi * (1 - eta) * field.depth[-1, :, yi, xi + 1]
+ xsi * eta * field.depth[-1, :, yi + 1, xi + 1]
+ (1 - xsi) * eta * field.depth[-1, :, yi + 1, xi]
)
else:
dv2 = (
(1 - xsi) * (1 - eta) * field.depth[ti : ti + 2, :, yi, xi]
+ xsi * (1 - eta) * field.depth[ti : ti + 2, :, yi, xi + 1]
+ xsi * eta * field.depth[ti : ti + 2, :, yi + 1, xi + 1]
+ (1 - xsi) * eta * field.depth[ti : ti + 2, :, yi + 1, xi]
)
tt = (time - field.time[ti]) / (field.time[ti + 1] - field.time[ti])
assert tt >= 0 and tt <= 1, "Vertical s grid is being wrongly interpolated in time"
depth_vector = dv2[0, :] * (1 - tt) + dv2[1, :] * tt
else:
depth_vector = (
(1 - xsi) * (1 - eta) * field.depth[:, yi, xi]
+ xsi * (1 - eta) * field.depth[:, yi, xi + 1]
+ xsi * eta * field.depth[:, yi + 1, xi + 1]
+ (1 - xsi) * eta * field.depth[:, yi + 1, xi]
)
z = np.float32(z) # type: ignore # TODO: remove type ignore once we migrate to float64
if depth_vector[-1] > depth_vector[0]:
if z < depth_vector[0]:
_raise_field_out_of_bound_error(z, None, None)
elif z > depth_vector[-1]:
_raise_field_out_of_bound_error(z, None, None)
depth_indices = depth_vector < z
if z >= depth_vector[-1]:
zi = len(depth_vector) - 2
else:
zi = depth_indices.argmin() - 1 if z > depth_vector[0] else 0
else:
if z > depth_vector[0]:
_raise_field_out_of_bound_error(z, None, None)
elif z < depth_vector[-1]:
_raise_field_out_of_bound_error(z, None, None)
depth_indices = depth_vector > z
if z <= depth_vector[-1]:
zi = len(depth_vector) - 2
else:
zi = depth_indices.argmin() - 1 if z < depth_vector[0] else 0
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
while zeta > 1:
zi += 1
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
while zeta < 0:
zi -= 1
zeta = (z - depth_vector[zi]) / (depth_vector[zi + 1] - depth_vector[zi])
return (zi, zeta)
def _search_indices_rectilinear(
field: Field, time: datetime, z: float, y: float, x: float, ti: int, ei: int | None = None, search2D=False
):
# TODO : If ei is provided, check if particle is in the same cell
if field.xdim > 1 and (not field.zonal_periodic):
if x < field.lonlat_minmax[0] or x > field.lonlat_minmax[1]:
_raise_field_out_of_bound_error(z, y, x)
if field.ydim > 1 and (y < field.lonlat_minmax[2] or y > field.lonlat_minmax[3]):
_raise_field_out_of_bound_error(z, y, x)
if field.xdim > 1:
if field._mesh_type != "spherical":
lon_index = field.lon < x
if lon_index.all():
xi = len(field.lon) - 2
else:
xi = lon_index.argmin() - 1 if lon_index.any() else 0
xsi = (x - field.lon[xi]) / (field.lon[xi + 1] - field.lon[xi])
if xsi < 0:
xi -= 1
xsi = (x - field.lon[xi]) / (field.lon[xi + 1] - field.lon[xi])
elif xsi > 1:
xi += 1
xsi = (x - field.lon[xi]) / (field.lon[xi + 1] - field.lon[xi])
else:
lon_fixed = field.lon.copy()
indices = lon_fixed >= lon_fixed[0]
if not indices.all():
lon_fixed[indices.argmin() :] += 360
if x < lon_fixed[0]:
lon_fixed -= 360
lon_index = lon_fixed < x
if lon_index.all():
xi = len(lon_fixed) - 2
else:
xi = lon_index.argmin() - 1 if lon_index.any() else 0
xsi = (x - lon_fixed[xi]) / (lon_fixed[xi + 1] - lon_fixed[xi])
if xsi < 0:
xi -= 1
xsi = (x - lon_fixed[xi]) / (lon_fixed[xi + 1] - lon_fixed[xi])
elif xsi > 1:
xi += 1
xsi = (x - lon_fixed[xi]) / (lon_fixed[xi + 1] - lon_fixed[xi])
else:
xi, xsi = -1, 0
if field.ydim > 1:
lat_index = field.lat < y
if lat_index.all():
yi = len(field.lat) - 2
else:
yi = lat_index.argmin() - 1 if lat_index.any() else 0
eta = (y - field.lat[yi]) / (field.lat[yi + 1] - field.lat[yi])
if eta < 0:
yi -= 1
eta = (y - field.lat[yi]) / (field.lat[yi + 1] - field.lat[yi])
elif eta > 1:
yi += 1
eta = (y - field.lat[yi]) / (field.lat[yi + 1] - field.lat[yi])
else:
yi, eta = -1, 0
if field.zdim > 1 and not search2D:
if field._gtype == GridType.RectilinearZGrid:
try:
(zi, zeta) = search_indices_vertical_z(field.grid, field.gridindexingtype, z)
except FieldOutOfBoundError:
_raise_field_out_of_bound_error(z, y, x)
except FieldOutOfBoundSurfaceError:
_raise_field_out_of_bound_surface_error(z, y, x)
elif field._gtype == GridType.RectilinearSGrid:
## TODO : Still need to implement the search_indices_vertical_s function
(zi, zeta) = search_indices_vertical_s(field.grid, field.interp_method, time, z, y, x, ti, yi, xi, eta, xsi)
else:
zi, zeta = -1, 0
if not ((0 <= xsi <= 1) and (0 <= eta <= 1) and (0 <= zeta <= 1)):
_raise_field_sampling_error(z, y, x)
_ei = field.ravel_index(zi, yi, xi)
return (zeta, eta, xsi, _ei)
def _search_indices_curvilinear_2d(
grid: XGrid, y: float, x: float, yi_guess: int | None = None, xi_guess: int | None = None
):
yi, xi = yi_guess, xi_guess
if yi is None:
yi = int(grid.ydim / 2) - 1
if xi is None:
xi = int(grid.xdim / 2) - 1
xsi = eta = -1.0
invA = np.array(
[
[1, 0, 0, 0],
[-1, 1, 0, 0],
[-1, 0, 0, 1],
[1, -1, 1, -1],
]
)
maxIterSearch = 1e6
it = 0
tol = 1.0e-10
# # ! Error handling for out of bounds
# TODO: Re-enable in some capacity
# if x < field.lonlat_minmax[0] or x > field.lonlat_minmax[1]:
# if grid.lon[0, 0] < grid.lon[0, -1]:
# _raise_field_out_of_bound_error(y, x)
# elif x < grid.lon[0, 0] and x > grid.lon[0, -1]: # This prevents from crashing in [160, -160]
# _raise_field_out_of_bound_error(z, y, x)
# if y < field.lonlat_minmax[2] or y > field.lonlat_minmax[3]:
# _raise_field_out_of_bound_error(z, y, x)
while xsi < -tol or xsi > 1 + tol or eta < -tol or eta > 1 + tol:
px = np.array([grid.lon[yi, xi], grid.lon[yi, xi + 1], grid.lon[yi + 1, xi + 1], grid.lon[yi + 1, xi]])
py = np.array([grid.lat[yi, xi], grid.lat[yi, xi + 1], grid.lat[yi + 1, xi + 1], grid.lat[yi + 1, xi]])
a = np.dot(invA, px)
b = np.dot(invA, py)
aa = a[3] * b[2] - a[2] * b[3]
bb = a[3] * b[0] - a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + x * b[3] - y * a[3]
cc = a[1] * b[0] - a[0] * b[1] + x * b[1] - y * a[1]
if abs(aa) < 1e-12: # Rectilinear cell, or quasi
eta = -cc / bb
else:
det2 = bb * bb - 4 * aa * cc
if det2 > 0: # so, if det is nan we keep the xsi, eta from previous iter
det = np.sqrt(det2)
eta = (-bb + det) / (2 * aa)
if abs(a[1] + a[3] * eta) < 1e-12: # this happens when recti cell rotated of 90deg
xsi = ((y - py[0]) / (py[1] - py[0]) + (y - py[3]) / (py[2] - py[3])) * 0.5
else:
xsi = (x - a[0] - a[2] * eta) / (a[1] + a[3] * eta)
if xsi < 0 and eta < 0 and xi == 0 and yi == 0:
_raise_field_out_of_bound_error(0, y, x)
if xsi > 1 and eta > 1 and xi == grid.xdim - 1 and yi == grid.ydim - 1:
_raise_field_out_of_bound_error(0, y, x)
if xsi < -tol:
xi -= 1
elif xsi > 1 + tol:
xi += 1
if eta < -tol:
yi -= 1
elif eta > 1 + tol:
yi += 1
(yi, xi) = _reconnect_bnd_indices(yi, xi, grid.ydim, grid.xdim, grid.mesh)
it += 1
if it > maxIterSearch:
print(f"Correct cell not found after {maxIterSearch} iterations")
_raise_field_out_of_bound_error(0, y, x)
xsi = max(0.0, xsi)
eta = max(0.0, eta)
xsi = min(1.0, xsi)
eta = min(1.0, eta)
if not ((0 <= xsi <= 1) and (0 <= eta <= 1)):
_raise_field_sampling_error(y, x)
return (yi, eta, xi, xsi)
## TODO : Still need to implement the search_indices_curvilinear
def _search_indices_curvilinear(field, time, z, y, x, ti, particle=None, search2D=False):
if particle:
zi, yi, xi = field.unravel_index(particle.ei)
else:
xi = int(field.xdim / 2) - 1
yi = int(field.ydim / 2) - 1
xsi = eta = -1.0
grid = field.grid
invA = np.array([[1, 0, 0, 0], [-1, 1, 0, 0], [-1, 0, 0, 1], [1, -1, 1, -1]])
maxIterSearch = 1e6
it = 0
tol = 1.0e-10
if not grid.zonal_periodic:
if x < field.lonlat_minmax[0] or x > field.lonlat_minmax[1]:
if grid.lon[0, 0] < grid.lon[0, -1]:
_raise_field_out_of_bound_error(z, y, x)
elif x < grid.lon[0, 0] and x > grid.lon[0, -1]: # This prevents from crashing in [160, -160]
_raise_field_out_of_bound_error(z, y, x)
if y < field.lonlat_minmax[2] or y > field.lonlat_minmax[3]:
_raise_field_out_of_bound_error(z, y, x)
while xsi < -tol or xsi > 1 + tol or eta < -tol or eta > 1 + tol:
px = np.array([grid.lon[yi, xi], grid.lon[yi, xi + 1], grid.lon[yi + 1, xi + 1], grid.lon[yi + 1, xi]])
if grid.mesh == "spherical":
px[0] = px[0] + 360 if px[0] < x - 225 else px[0]
px[0] = px[0] - 360 if px[0] > x + 225 else px[0]
px[1:] = np.where(px[1:] - px[0] > 180, px[1:] - 360, px[1:])
px[1:] = np.where(-px[1:] + px[0] > 180, px[1:] + 360, px[1:])
py = np.array([grid.lat[yi, xi], grid.lat[yi, xi + 1], grid.lat[yi + 1, xi + 1], grid.lat[yi + 1, xi]])
a = np.dot(invA, px)
b = np.dot(invA, py)
aa = a[3] * b[2] - a[2] * b[3]
bb = a[3] * b[0] - a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + x * b[3] - y * a[3]
cc = a[1] * b[0] - a[0] * b[1] + x * b[1] - y * a[1]
if abs(aa) < 1e-12: # Rectilinear cell, or quasi
eta = -cc / bb
else:
det2 = bb * bb - 4 * aa * cc
if det2 > 0: # so, if det is nan we keep the xsi, eta from previous iter
det = np.sqrt(det2)
eta = (-bb + det) / (2 * aa)
if abs(a[1] + a[3] * eta) < 1e-12: # this happens when recti cell rotated of 90deg
xsi = ((y - py[0]) / (py[1] - py[0]) + (y - py[3]) / (py[2] - py[3])) * 0.5
else:
xsi = (x - a[0] - a[2] * eta) / (a[1] + a[3] * eta)
if xsi < 0 and eta < 0 and xi == 0 and yi == 0:
_raise_field_out_of_bound_error(0, y, x)
if xsi > 1 and eta > 1 and xi == grid.xdim - 1 and yi == grid.ydim - 1:
_raise_field_out_of_bound_error(0, y, x)
if xsi < -tol:
xi -= 1
elif xsi > 1 + tol:
xi += 1
if eta < -tol:
yi -= 1
elif eta > 1 + tol:
yi += 1
(yi, xi) = _reconnect_bnd_indices(yi, xi, grid.ydim, grid.xdim, grid.mesh)
it += 1
if it > maxIterSearch:
print(f"Correct cell not found after {maxIterSearch} iterations")
_raise_field_out_of_bound_error(0, y, x)
xsi = max(0.0, xsi)
eta = max(0.0, eta)
xsi = min(1.0, xsi)
eta = min(1.0, eta)
if grid.zdim > 1 and not search2D:
if grid._gtype == GridType.CurvilinearZGrid:
try:
(zi, zeta) = search_indices_vertical_z(field.grid, field.gridindexingtype, z)
except FieldOutOfBoundError:
_raise_field_out_of_bound_error(z, y, x)
elif grid._gtype == GridType.CurvilinearSGrid:
(zi, zeta) = search_indices_vertical_s(field.grid, field.interp_method, time, z, y, x, ti, yi, xi, eta, xsi)
else:
zi = -1
zeta = 0
if not ((0 <= xsi <= 1) and (0 <= eta <= 1) and (0 <= zeta <= 1)):
_raise_field_sampling_error(z, y, x)
if particle:
particle.ei[field.igrid] = field.ravel_index(zi, yi, xi)
return (zeta, eta, xsi, zi, yi, xi)
def _reconnect_bnd_indices(yi: int, xi: int, ydim: int, xdim: int, sphere_mesh: bool):
if xi < 0:
if sphere_mesh:
xi = xdim - 2
else:
xi = 0
if xi > xdim - 2:
if sphere_mesh:
xi = 0
else:
xi = xdim - 2
if yi < 0:
yi = 0
if yi > ydim - 2:
yi = ydim - 2
if sphere_mesh:
xi = xdim - xi
return yi, xi