Skip to content

Commit 932eeaa

Browse files
committed
Add a fonction to try to fit circle with equal area
1 parent 1fbe622 commit 932eeaa

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/py_eddy_tracker/dataset/grid.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
winding_number_grid_in_poly,
6262
winding_number_poly,
6363
create_vertice,
64+
poly_area,
6465
)
6566

6667
logger = logging.getLogger("pet")
@@ -126,10 +127,32 @@ def mean_on_regular_contour(
126127
return values.mean()
127128

128129

129-
def fit_circle_path(self):
130+
def fit_circle_path(self, method="fit"):
130131
if not hasattr(self, "_circle_params"):
131-
self._circle_params = _fit_circle_path(self.vertices)
132-
return self._circle_params
132+
self._circle_params = dict()
133+
if method not in self._circle_params.keys():
134+
if method == "fit":
135+
self._circle_params["fit"] = _fit_circle_path(self.vertices)
136+
if method == "equal_area":
137+
self._circle_params["equal_area"] = _circle_from_equal_area(self.vertices)
138+
return self._circle_params[method]
139+
140+
141+
@njit(cache=True, fastmath=True)
142+
def _circle_from_equal_area(vertice):
143+
lons, lats = vertice[:, 0], vertice[:, 1]
144+
# last coordinates == first
145+
lon0, lat0 = lons[1:].mean(), lats[1:].mean()
146+
c_x, c_y = coordinates_to_local(lons, lats, lon0, lat0)
147+
# Some time, edge is only a dot of few coordinates
148+
d_lon = lons.max() - lons.min()
149+
d_lat = lats.max() - lats.min()
150+
if d_lon < 1e-7 and d_lat < 1e-7:
151+
# logger.warning('An edge is only define in one position')
152+
# logger.debug('%d coordinates %s,%s', len(lons),lons,
153+
# lats)
154+
return 0, -90, nan, nan
155+
return lon0, lat0, (poly_area(create_vertice(c_x, c_y)) / pi) ** 0.5, nan
133156

134157

135158
@njit(cache=True, fastmath=True)
@@ -146,10 +169,10 @@ def _fit_circle_path(vertice):
146169
# logger.debug('%d coordinates %s,%s', len(lons),lons,
147170
# lats)
148171
return 0, -90, nan, nan
149-
centlon_e, centlat_e, eddy_radius_e, aerr = fit_circle(c_x, c_y)
150-
centlon_e, centlat_e = local_to_coordinates(centlon_e, centlat_e, lon0, lat0)
151-
centlon_e = (centlon_e - lon0 + 180) % 360 + lon0 - 180
152-
return centlon_e, centlat_e, eddy_radius_e, aerr
172+
centlon, centlat, eddy_radius, err = fit_circle(c_x, c_y)
173+
centlon, centlat = local_to_coordinates(centlon, centlat, lon0, lat0)
174+
centlon = (centlon - lon0 + 180) % 360 + lon0 - 180
175+
return centlon, centlat, eddy_radius, err
153176

154177

155178
@njit(cache=True, fastmath=True)

src/py_eddy_tracker/poly.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ def poly_contain_poly(xy_poly_out, xy_poly_in):
5959
return True
6060

6161

62+
@njit(cache=True)
63+
def poly_area(vertice):
64+
p_area = 0
65+
nb_elt = vertice.shape[0]
66+
for i_elt in range(1, nb_elt - 1):
67+
p_area += vertice[i_elt, 0] * (vertice[1 + i_elt, 1] - vertice[i_elt - 1, 1])
68+
return abs(p_area) * 0.5
69+
70+
6271
@njit(cache=True)
6372
def winding_number_poly(x, y, xy_poly):
6473
nb_elt = xy_poly.shape[0]

0 commit comments

Comments
 (0)