1
1
# -*- coding: utf-8 -*-
2
2
"""
3
- ===========================================================================
4
3
This file is part of py-eddy-tracker.
5
4
6
5
py-eddy-tracker is free software: you can redistribute it and/or modify
46
45
47
46
@njit (cache = True )
48
47
def reverse_index (index , nb ):
48
+ """
49
+ :param array index: index of group which will be set to False
50
+ :param array nb: Count for each group
51
+ :return: mask of value selected
52
+ :rtype: array
53
+ """
49
54
m = ones (nb , dtype = numba_types .bool_ )
50
55
for i in index :
51
56
m [i ] = False
@@ -55,6 +60,10 @@ def reverse_index(index, nb):
55
60
@njit (cache = True )
56
61
def build_index (groups ):
57
62
"""We expected that variable is monotonous, and return index for each step change
63
+
64
+ :param array groups: array which contain group to be separated
65
+ :return: (first_index of each group, last_index of each group, value to shift group)
66
+ :rtype: (array, array, int)
58
67
"""
59
68
i0 , i1 = groups .min (), groups .max ()
60
69
amplitude = i1 - i0 + 1
@@ -75,12 +84,13 @@ def build_index(groups):
75
84
@njit (cache = True , fastmath = True , parallel = False )
76
85
def distance_grid (lon0 , lat0 , lon1 , lat1 ):
77
86
"""
78
- :param lon0:
79
- :param lat0:
80
- :param lon1:
81
- :param lat1:
87
+ :param array lon0:
88
+ :param array lat0:
89
+ :param array lon1:
90
+ :param array lat1:
82
91
83
92
:return: nan value for far away point, and km for other
93
+ :rtype: array
84
94
"""
85
95
nb_0 = lon0 .shape [0 ]
86
96
nb_1 = lon1 .shape [0 ]
@@ -109,6 +119,16 @@ def distance_grid(lon0, lat0, lon1, lat1):
109
119
110
120
@njit (cache = True , fastmath = True )
111
121
def distance (lon0 , lat0 , lon1 , lat1 ):
122
+ """
123
+ Compute distance between points from each line
124
+
125
+ :param float lon0:
126
+ :param float lat0:
127
+ :param float lon1:
128
+ :param float lat1:
129
+ :return: distance (in m)
130
+ :rtype: array
131
+ """
112
132
D2R = pi / 180.0
113
133
sin_dlat = sin ((lat1 - lat0 ) * 0.5 * D2R )
114
134
sin_dlon = sin ((lon1 - lon0 ) * 0.5 * D2R )
@@ -118,31 +138,21 @@ def distance(lon0, lat0, lon1, lat1):
118
138
return 6370997.0 * 2 * arctan2 (a_val ** 0.5 , (1 - a_val ) ** 0.5 )
119
139
120
140
121
- @njit (cache = True )
122
- def distance_vincenty (lon0 , lat0 , lon1 , lat1 ):
123
- """ better than haversine but buggy ??"""
124
- D2R = pi / 180.0
125
- dlon = (lon1 - lon0 ) * D2R
126
- cos_dlon = cos (dlon )
127
- cos_lat1 = cos (lat0 * D2R )
128
- cos_lat2 = cos (lat1 * D2R )
129
- sin_lat1 = sin (lat0 * D2R )
130
- sin_lat2 = sin (lat1 * D2R )
131
- return 6370997.0 * arctan2 (
132
- (
133
- (cos_lat2 * sin (dlon ) ** 2 )
134
- + (cos_lat1 * sin_lat2 - sin_lat1 * cos_lat2 * cos_dlon ) ** 2
135
- )
136
- ** 0.5 ,
137
- sin_lat1 * sin_lat2 + cos_lat1 * cos_lat2 * cos_dlon ,
138
- )
139
-
140
-
141
141
@njit (cache = True , fastmath = True )
142
142
def interp2d_geo (x_g , y_g , z_g , m_g , x , y ):
143
- """For geographic grid, test of cicularity
144
- Maybe test if we are out of bounds
145
143
"""
144
+ For geographic grid, test of cicularity
145
+
146
+ :param array x_g: coordinates of grid
147
+ :param array y_g: coordinates of grid
148
+ :param array z_g: Grid value
149
+ :param array m_g: Boolean grid, True if value is masked
150
+ :param array x: coordinate where interpolate z
151
+ :param array y: coordinate where interpolate z
152
+ :return: z interpolated
153
+ :rtype: array
154
+ """
155
+ # TODO : Maybe test if we are out of bounds
146
156
x_ref = x_g [0 ]
147
157
y_ref = y_g [0 ]
148
158
x_step = x_g [1 ] - x_ref
@@ -175,25 +185,6 @@ def interp2d_geo(x_g, y_g, z_g, m_g, x, y):
175
185
return z
176
186
177
187
178
- @njit (cache = True , fastmath = True , parallel = False )
179
- def custom_convolution (data , mask , kernel ):
180
- """do sortin at high lattitude big part of value are masked"""
181
- nb_x = kernel .shape [0 ]
182
- demi_x = int ((nb_x - 1 ) / 2 )
183
- demi_y = int ((kernel .shape [1 ] - 1 ) / 2 )
184
- out = empty (data .shape [0 ] - nb_x + 1 )
185
- for i in prange (out .shape [0 ]):
186
- if mask [i + demi_x , demi_y ] == 1 :
187
- w = (mask [i : i + nb_x ] * kernel ).sum ()
188
- if w != 0 :
189
- out [i ] = (data [i : i + nb_x ] * kernel ).sum () / w
190
- else :
191
- out [i ] = nan
192
- else :
193
- out [i ] = nan
194
- return out
195
-
196
-
197
188
@njit (cache = True , fastmath = True )
198
189
def uniform_resample (x_val , y_val , num_fac = 2 , fixed_size = None ):
199
190
"""
@@ -225,8 +216,8 @@ def uniform_resample(x_val, y_val, num_fac=2, fixed_size=None):
225
216
def flatten_line_matrix (l_matrix ):
226
217
"""
227
218
Flat matrix and add on between each line
228
- :param l_matrix: matrix of position
229
219
220
+ :param l_matrix: matrix of position
230
221
:return: array with nan between line
231
222
"""
232
223
nb_line , sampling = l_matrix .shape
@@ -244,6 +235,15 @@ def flatten_line_matrix(l_matrix):
244
235
245
236
@njit (cache = True )
246
237
def simplify (x , y , precision = 0.1 ):
238
+ """
239
+ Will remove all middle point which are closer than precision
240
+
241
+ :param array x:
242
+ :param array y:
243
+ :param float precision: if two points have distance inferior to precision with remove next point
244
+ :return: (x,y)
245
+ :rtype: (array,array)
246
+ """
247
247
precision2 = precision ** 2
248
248
nb = x .shape [0 ]
249
249
x_previous , y_previous = x [0 ], y [0 ]
@@ -277,6 +277,7 @@ def simplify(x, y, precision=0.1):
277
277
def split_line (x , y , i ):
278
278
"""
279
279
Split x and y at each i change
280
+
280
281
:param x: array
281
282
:param y: array
282
283
:param i: array of int at each i change, we cut x, y
@@ -302,6 +303,16 @@ def split_line(x, y, i):
302
303
303
304
@njit (cache = True )
304
305
def wrap_longitude (x , y , ref , cut = False ):
306
+ """
307
+ Will wrap contiguous longitude with reference like west bound
308
+
309
+ :param array x:
310
+ :param array y:
311
+ :param float ref: longitude of reference, all the new value will be between ref and ref + 360
312
+ :param bool cut: if True line will be cut at the bounds
313
+ :return: lon,lat
314
+ :rtype: (array,array)
315
+ """
305
316
if cut :
306
317
indexs = list ()
307
318
nb = x .shape [0 ]
@@ -348,6 +359,16 @@ def wrap_longitude(x, y, ref, cut=False):
348
359
349
360
@njit (cache = True , fastmath = True )
350
361
def coordinates_to_local (lon , lat , lon0 , lat0 ):
362
+ """
363
+ Take latlong coordinates to transform in local coordinates (in m)
364
+
365
+ :param array x: coordinates to transform
366
+ :param array y: coordinates to transform
367
+ :param float lon0: longitude of local reference
368
+ :param float lat0: latitude of local reference
369
+ :return: x,y
370
+ :retype: (array, array)
371
+ """
351
372
D2R = pi / 180.0
352
373
R = 6370997
353
374
dlon = (lon - lon0 ) * D2R
@@ -367,6 +388,16 @@ def coordinates_to_local(lon, lat, lon0, lat0):
367
388
368
389
@njit (cache = True , fastmath = True )
369
390
def local_to_coordinates (x , y , lon0 , lat0 ):
391
+ """
392
+ Take local coordinates (in m) to transform to latlong
393
+
394
+ :param array x: coordinates to transform
395
+ :param array y: coordinates to transform
396
+ :param float lon0: longitude of local reference
397
+ :param float lat0: latitude of local reference
398
+ :return: lon,lat
399
+ :retype: (array, array)
400
+ """
370
401
D2R = pi / 180.0
371
402
R = 6370997
372
403
d = (x ** 2 + y ** 2 ) ** 0.5 / R
0 commit comments