Skip to content

Commit 6bead13

Browse files
committed
Move function
1 parent eb9e3b3 commit 6bead13

File tree

3 files changed

+41
-53
lines changed

3 files changed

+41
-53
lines changed

src/py_eddy_tracker/dataset/grid.py

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
isnan,
2929
percentile,
3030
zeros,
31-
arctan2,
32-
arcsin,
3331
round_,
3432
nanmean,
3533
exp,
@@ -50,8 +48,8 @@
5048
from ..observations.observation import EddiesObservations
5149
from ..eddy_feature import Amplitude, Contours
5250
from .. import VAR_DESCR
53-
from ..generic import distance, interp2d_geo, fit_circle, uniform_resample
54-
from ..poly import poly_contain_poly, winding_number_grid_in_poly, winding_number_poly
51+
from ..generic import distance, interp2d_geo, fit_circle, uniform_resample, coordinates_to_local, local_to_coordinates
52+
from ..poly import poly_contain_poly, winding_number_grid_in_poly, winding_number_poly, create_vertice
5553

5654
logger = logging.getLogger("pet")
5755

@@ -86,14 +84,6 @@ def lat(self):
8684
BasePath.lat = lat
8785

8886

89-
@njit(cache=True)
90-
def prepare_for_kdtree(x_val, y_val):
91-
data = empty((x_val.shape[0], 2))
92-
data[:, 0] = x_val
93-
data[:, 1] = y_val
94-
return data
95-
96-
9787
@njit(cache=True)
9888
def uniform_resample_stack(vertices, num_fac=2, fixed_size=None):
9989
x_val, y_val = vertices[:, 0], vertices[:, 1]
@@ -195,45 +185,6 @@ def _get_pixel_in_unregular(vertices, x_c, y_c, x_start, x_stop, y_start, y_stop
195185
return i_x, i_y
196186

197187

198-
@njit(cache=True, fastmath=True)
199-
def coordinates_to_local(lon, lat, lon0, lat0):
200-
D2R = pi / 180.0
201-
R = 6370997
202-
dlon = (lon - lon0) * D2R
203-
sin_dlat = sin((lat - lat0) * 0.5 * D2R)
204-
sin_dlon = sin(dlon * 0.5)
205-
cos_lat0 = cos(lat0 * D2R)
206-
cos_lat = cos(lat * D2R)
207-
a_val = sin_dlon ** 2 * cos_lat0 * cos_lat + sin_dlat ** 2
208-
module = R * 2 * arctan2(a_val ** 0.5, (1 - a_val) ** 0.5)
209-
210-
azimuth = pi / 2 - arctan2(
211-
cos_lat * sin(dlon),
212-
cos_lat0 * sin(lat * D2R) - sin(lat0 * D2R) * cos_lat * cos(dlon),
213-
)
214-
return module * cos(azimuth), module * sin(azimuth)
215-
216-
217-
@njit(cache=True, fastmath=True)
218-
def local_to_coordinates(x, y, lon0, lat0):
219-
D2R = pi / 180.0
220-
R = 6370997
221-
d = (x ** 2 + y ** 2) ** 0.5 / R
222-
a = -(arctan2(y, x) - pi / 2)
223-
lat = arcsin(sin(lat0 * D2R) * cos(d) + cos(lat0 * D2R) * sin(d) * cos(a))
224-
lon = (
225-
lon0
226-
+ arctan2(
227-
sin(a) * sin(d) * cos(lat0 * D2R), cos(d) - sin(lat0 * D2R) * sin(lat)
228-
)
229-
/ D2R
230-
)
231-
return lon, lat / D2R
232-
233-
234-
BasePath.fit_circle = fit_circle_path
235-
236-
237188
def pixels_in(self, grid):
238189
if not hasattr(self, "_slice"):
239190
self._slice = grid.bbox_indice(self.vertices)
@@ -1081,7 +1032,7 @@ def compute_pixel_path(self, x0, y0, x1, y1):
10811032
def init_pos_interpolator(self):
10821033
logger.debug("Create a KdTree could be long ...")
10831034
self.index_interp = cKDTree(
1084-
prepare_for_kdtree(self.x_c.reshape(-1), self.y_c.reshape(-1))
1035+
create_vertice(self.x_c.reshape(-1), self.y_c.reshape(-1))
10851036
)
10861037

10871038
logger.debug("... OK")

src/py_eddy_tracker/generic.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
pi,
2727
cos,
2828
arctan2,
29+
arcsin,
2930
empty,
3031
nan,
3132
absolute,
@@ -354,3 +355,39 @@ def wrap_longitude(x, y, ref, cut=False):
354355
for i in range(nb):
355356
out[i] = (x[i] - ref) % 360 + ref
356357
return out, y
358+
359+
360+
@njit(cache=True, fastmath=True)
361+
def coordinates_to_local(lon, lat, lon0, lat0):
362+
D2R = pi / 180.0
363+
R = 6370997
364+
dlon = (lon - lon0) * D2R
365+
sin_dlat = sin((lat - lat0) * 0.5 * D2R)
366+
sin_dlon = sin(dlon * 0.5)
367+
cos_lat0 = cos(lat0 * D2R)
368+
cos_lat = cos(lat * D2R)
369+
a_val = sin_dlon ** 2 * cos_lat0 * cos_lat + sin_dlat ** 2
370+
module = R * 2 * arctan2(a_val ** 0.5, (1 - a_val) ** 0.5)
371+
372+
azimuth = pi / 2 - arctan2(
373+
cos_lat * sin(dlon),
374+
cos_lat0 * sin(lat * D2R) - sin(lat0 * D2R) * cos_lat * cos(dlon),
375+
)
376+
return module * cos(azimuth), module * sin(azimuth)
377+
378+
379+
@njit(cache=True, fastmath=True)
380+
def local_to_coordinates(x, y, lon0, lat0):
381+
D2R = pi / 180.0
382+
R = 6370997
383+
d = (x ** 2 + y ** 2) ** 0.5 / R
384+
a = -(arctan2(y, x) - pi / 2)
385+
lat = arcsin(sin(lat0 * D2R) * cos(d) + cos(lat0 * D2R) * sin(d) * cos(a))
386+
lon = (
387+
lon0
388+
+ arctan2(
389+
sin(a) * sin(d) * cos(lat0 * D2R), cos(d) - sin(lat0 * D2R) * sin(lat)
390+
)
391+
/ D2R
392+
)
393+
return lon, lat / D2R

src/py_eddy_tracker/poly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def bbox_intersection(x0, y0, x1, y1):
144144
@njit(cache=True)
145145
def create_vertice(x, y):
146146
nb = x.shape[0]
147-
v = empty((nb, 2))
147+
v = empty((nb, 2), dtype=x.dtype)
148148
for i in range(nb):
149149
v[i, 0] = x[i]
150150
v[i, 1] = y[i]

0 commit comments

Comments
 (0)