11# -*- coding: utf-8 -*-
22"""
3- ===========================================================================
43This file is part of py-eddy-tracker.
54
65 py-eddy-tracker is free software: you can redistribute it and/or modify
4645
4746@njit(cache=True)
4847def 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+ """
4954 m = ones(nb, dtype=numba_types.bool_)
5055 for i in index:
5156 m[i] = False
@@ -55,6 +60,10 @@ def reverse_index(index, nb):
5560@njit(cache=True)
5661def build_index(groups):
5762 """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)
5867 """
5968 i0, i1 = groups.min(), groups.max()
6069 amplitude = i1 - i0 + 1
@@ -75,12 +84,13 @@ def build_index(groups):
7584@njit(cache=True, fastmath=True, parallel=False)
7685def distance_grid(lon0, lat0, lon1, lat1):
7786 """
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:
8291
8392 :return: nan value for far away point, and km for other
93+ :rtype: array
8494 """
8595 nb_0 = lon0.shape[0]
8696 nb_1 = lon1.shape[0]
@@ -109,6 +119,16 @@ def distance_grid(lon0, lat0, lon1, lat1):
109119
110120@njit(cache=True, fastmath=True)
111121def 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+ """
112132 D2R = pi / 180.0
113133 sin_dlat = sin((lat1 - lat0) * 0.5 * D2R)
114134 sin_dlon = sin((lon1 - lon0) * 0.5 * D2R)
@@ -118,31 +138,21 @@ def distance(lon0, lat0, lon1, lat1):
118138 return 6370997.0 * 2 * arctan2(a_val ** 0.5, (1 - a_val) ** 0.5)
119139
120140
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-
141141@njit(cache=True, fastmath=True)
142142def 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
145143 """
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
146156 x_ref = x_g[0]
147157 y_ref = y_g[0]
148158 x_step = x_g[1] - x_ref
@@ -175,25 +185,6 @@ def interp2d_geo(x_g, y_g, z_g, m_g, x, y):
175185 return z
176186
177187
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-
197188@njit(cache=True, fastmath=True)
198189def uniform_resample(x_val, y_val, num_fac=2, fixed_size=None):
199190 """
@@ -225,8 +216,8 @@ def uniform_resample(x_val, y_val, num_fac=2, fixed_size=None):
225216def flatten_line_matrix(l_matrix):
226217 """
227218 Flat matrix and add on between each line
228- :param l_matrix: matrix of position
229219
220+ :param l_matrix: matrix of position
230221 :return: array with nan between line
231222 """
232223 nb_line, sampling = l_matrix.shape
@@ -244,6 +235,15 @@ def flatten_line_matrix(l_matrix):
244235
245236@njit(cache=True)
246237def 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+ """
247247 precision2 = precision ** 2
248248 nb = x.shape[0]
249249 x_previous, y_previous = x[0], y[0]
@@ -277,6 +277,7 @@ def simplify(x, y, precision=0.1):
277277def split_line(x, y, i):
278278 """
279279 Split x and y at each i change
280+
280281 :param x: array
281282 :param y: array
282283 :param i: array of int at each i change, we cut x, y
@@ -302,6 +303,16 @@ def split_line(x, y, i):
302303
303304@njit(cache=True)
304305def 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+ """
305316 if cut:
306317 indexs = list()
307318 nb = x.shape[0]
@@ -348,6 +359,16 @@ def wrap_longitude(x, y, ref, cut=False):
348359
349360@njit(cache=True, fastmath=True)
350361def 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+ """
351372 D2R = pi / 180.0
352373 R = 6370997
353374 dlon = (lon - lon0) * D2R
@@ -367,6 +388,16 @@ def coordinates_to_local(lon, lat, lon0, lat0):
367388
368389@njit(cache=True, fastmath=True)
369390def 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+ """
370401 D2R = pi / 180.0
371402 R = 6370997
372403 d = (x ** 2 + y ** 2) ** 0.5 / R
0 commit comments