Skip to content

Commit 8ad4b97

Browse files
committed
Small optimisation to 'nearest' function
1 parent 3d9d530 commit 8ad4b97

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

make_eddy_track_AVISO.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ def set_index_padding(self, pad=2):
161161
'''
162162
print '--- Setting padding indices with pad=%s' %pad
163163

164+
self.pad = pad
165+
164166
def get_str(thestr, pad):
165167
'''
166168
Get start indices for pad
@@ -238,7 +240,7 @@ def nearest_point(self, lon, lat):
238240
'''
239241
Get indices to a point (lon, lat) in the grid
240242
'''
241-
i, j = eddy_tracker.nearest(lon, lat, self._lon, self._lat)
243+
i, j = eddy_tracker.nearest(lon, lat, self._lon, self._lat, self._lon.shape)
242244
return i, j
243245

244246

@@ -505,6 +507,8 @@ def __init__(self, AVISO_file, lonmin, lonmax, latmin, latmax, with_pad=True, us
505507
self.make_gridmask(with_pad, use_maskoceans).uvmask()
506508
self.get_AVISO_f_pm_pn()
507509
self.set_u_v_eke()
510+
pad2 = 2 * self.pad
511+
self.shape = (self.f().shape[0] - pad2, self.f().shape[1] - pad2)
508512

509513

510514
def get_AVISO_data(self, AVISO_file):
@@ -807,12 +811,12 @@ def pcol_2dxy(self, x, y):
807811
#savedir = '/marula/emason/aviso_eddy_tracking/pablo_exp/'
808812
#savedir = '/marula/emason/aviso_eddy_tracking/new_AVISO_test/'
809813
#savedir = '/marula/emason/aviso_eddy_tracking/new_AVISO_SUBSAMP-3days/'
810-
#savedir = '/marula/emason/aviso_eddy_tracking/junk/'
814+
savedir = '/marula/emason/aviso_eddy_tracking/junk/'
811815
#savedir = '/marula/emason/aviso_eddy_tracking/new_AVISO_test/BlackSea/'
812816
#savedir = '/marula/emason/aviso_eddy_tracking/Corridor_V3_Dec2014/'
813817
#savedir = '/marula/emason/aviso_eddy_tracking/CLS_test_1_acd66ba338a9/'
814818
#savedir = '/marula/emason/aviso_eddy_tracking/CLS_test_2/'
815-
savedir = '/marula/emason/aviso_eddy_tracking/CLS_test_3/'
819+
#savedir = '/marula/emason/aviso_eddy_tracking/CLS_test_3/'
816820
#savedir = '/path/to/save/your/outputs/'
817821

818822

@@ -909,10 +913,10 @@ def pcol_2dxy(self, x, y):
909913
#latmin = -47.
910914
#latmax = -24.
911915

912-
#lonmin = -38. # small test
913-
#lonmax = -25.
914-
#latmin = 30.
915-
#latmax = 38.
916+
lonmin = -38. # small test
917+
lonmax = -25.
918+
latmin = 30.
919+
latmax = 38.
916920

917921

918922
elif the_domain in 'MedSea':

make_eddy_tracker_list_obj.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def haversine_distance_vector(lon1, lat1, lon2, lat2):
4747
lat1 = np.asfortranarray(lat1.copy())
4848
lon2 = np.asfortranarray(lon2.copy())
4949
lat2 = np.asfortranarray(lat2.copy())
50-
dist = np.asfortranarray(np.empty_like(lon1))
50+
dist = np.asfortranarray(np.empty(lon1.shape))
5151
hav.haversine_distvec(lon1, lat1, lon2, lat2, dist)
5252
return dist
5353

@@ -57,28 +57,41 @@ def newPosition(lonin, latin, angle, distance):
5757
Given the inputs (base lon, base lat, angle, distance) return
5858
the lon, lat of the new position...
5959
'''
60-
lonin = np.asfortranarray(np.copy(lonin))
61-
latin = np.asfortranarray(np.copy(latin))
62-
angle = np.asfortranarray(np.copy(angle))
63-
distance = np.asfortranarray(np.copy(distance))
64-
lon = np.asfortranarray(np.empty_like(lonin))
65-
lat = np.asfortranarray(np.empty_like(lonin))
66-
hav.waypoint_vec(lonin, latin, angle, distance, lon, lat)
67-
return lon, lat
60+
lon = np.asfortranarray(lonin.copy())
61+
lat = np.asfortranarray(latin.copy())
62+
angle = np.asfortranarray(angle.copy())
63+
distance = np.asfortranarray(distance.copy())
64+
lonout = np.asfortranarray(np.empty(lonin.shape))
65+
latout = np.asfortranarray(np.empty(lonin.shape))
66+
hav.waypoint_vec(lon, lat, angle, distance, lonout, latout)
67+
return lon[0], lat[0]
6868

6969

70-
def nearest(lon_pt, lat_pt, lon2d, lat2d):
70+
#def nearest(lon_pt, lat_pt, lon2d, lat2d):
71+
#"""
72+
#Return the nearest i, j point to a given lon, lat point
73+
#in a lat/lon grid
74+
#"""
75+
#lon2d, lat2d = lon2d.copy(), lat2d.copy()
76+
#lon2d -= lon_pt
77+
#lat2d -= lat_pt
78+
#d = np.hypot(lon2d, lat2d)
79+
#j, i = np.unravel_index(d.argmin(), d.shape)
80+
#return i, j
81+
82+
83+
def nearest(lon_pt, lat_pt, lon2d, lat2d, theshape):
7184
"""
7285
Return the nearest i, j point to a given lon, lat point
7386
in a lat/lon grid
7487
"""
75-
lon2d, lat2d = lon2d.copy(), lat2d.copy()
76-
lon2d -= lon_pt
77-
lat2d -= lat_pt
78-
d = np.hypot(lon2d, lat2d)
79-
j, i = np.unravel_index(d.argmin(), d.shape)
88+
#print type(lon_pt), lon_pt
89+
lon_pt += -lon2d
90+
lat_pt += -lat2d
91+
d = np.sqrt(lon_pt**2 + lat_pt**2)
92+
j, i = np.unravel_index(d.argmin(), theshape)
8093
return i, j
81-
94+
8295

8396
def uniform_resample(x, y, num_fac=2, kind='linear'):
8497
'''
@@ -1111,17 +1124,18 @@ def get_angle(deg, ang):
11111124
return deg - np.rad2deg(ang)
11121125

11131126
grdangle = grd.angle()[j,i]
1114-
1127+
#print type(centlon), type(centlat)
11151128
a_lon, a_lat = newPosition(centlon, centlat, get_angle(0, grdangle), radius)
11161129
b_lon, b_lat = newPosition(centlon, centlat, get_angle(90, grdangle), radius)
11171130
c_lon, c_lat = newPosition(centlon, centlat, get_angle(180, grdangle), radius)
11181131
d_lon, d_lat = newPosition(centlon, centlat, get_angle(270, grdangle), radius)
11191132

11201133
# Get i,j of bounding box around eddy
1121-
a_i, a_j = nearest(a_lon, a_lat, grd.lon(), grd.lat())
1122-
b_i, b_j = nearest(b_lon, b_lat, grd.lon(), grd.lat())
1123-
c_i, c_j = nearest(c_lon, c_lat, grd.lon(), grd.lat())
1124-
d_i, d_j = nearest(d_lon, d_lat, grd.lon(), grd.lat())
1134+
#print grd.lon().shape, grd.lat().shape, grd.shape
1135+
a_i, a_j = nearest(a_lon, a_lat, grd.lon(), grd.lat(), grd.shape)
1136+
b_i, b_j = nearest(b_lon, b_lat, grd.lon(), grd.lat(), grd.shape)
1137+
c_i, c_j = nearest(c_lon, c_lat, grd.lon(), grd.lat(), grd.shape)
1138+
d_i, d_j = nearest(d_lon, d_lat, grd.lon(), grd.lat(), grd.shape)
11251139

11261140
self.imin = np.maximum(np.min([a_i, b_i, c_i, d_i]) - 5, 0) # Must not go
11271141
self.jmin = np.maximum(np.min([a_j, b_j, c_j, d_j]) - 5, 0) # below zero

py_eddy_tracker_classes.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,16 @@ def collection_loop(CS, grd, rtime, A_list_obj, C_list_obj,
702702
cx, cy = Eddy.M(contlon_e, contlat_e)
703703
centlon_e, centlat_e, eddy_radius_e, aerr = fit_circle(cx, cy)
704704
#print centlon_e, centlat_e
705+
705706
aerr = np.atleast_1d(aerr)
706707
# Filter for shape: >35% (>55%) is not an eddy for Q (SLA)
707708
if np.logical_and(aerr >= 0., aerr <= Eddy.shape_err[collind]):
708709

709-
# Get centroid in lon lat
710+
# Get centroid in lon lat
711+
#print type(centlon_e), type(centlat_e), 1111
710712
centlon_e, centlat_e = Eddy.M.projtran(centlon_e, centlat_e, inverse=True)
713+
# For some reason centlat_e is transformed to 'float'...
714+
centlon_e, centlat_e = np.float64(centlon_e), np.float64(centlat_e)
711715

712716
# Get eddy_radius_e (NOTE: if Q, we overwrite eddy_radius_e defined ~8 lines above)
713717
if 'Q' in Eddy.diag_type:
@@ -729,10 +733,15 @@ def collection_loop(CS, grd, rtime, A_list_obj, C_list_obj,
729733

730734
if proceed0:
731735
# Get indices of centroid
736+
737+
#print type(centlon_e), type(centlat_e)
732738
centi, centj = eddy_tracker.nearest(centlon_e, centlat_e,
733739
grd.lon(),
734-
grd.lat())
740+
grd.lat(), grd.shape)
735741
# If condition not True this eddy has already been detected
742+
743+
#print type(centlon_e), type(centlat_e)
744+
736745
if 'Q' in Eddy.diag_type:
737746

738747
if xi[centj, centi] != Eddy.fillval:
@@ -983,7 +992,7 @@ def collection_loop(CS, grd, rtime, A_list_obj, C_list_obj,
983992

984993
# Get indices of speed-based centroid
985994
centi, centj = eddy_tracker.nearest(centlon_s, centlat_s,
986-
grd.lon(), grd.lat())
995+
grd.lon(), grd.lat(), grd.shape)
987996

988997
# Set the bounds
989998
#bounds = np.atleast_2d(np.int16([imin, imax, jmin, jmax]))

0 commit comments

Comments
 (0)