Skip to content

Commit 73d324b

Browse files
author
adelepoulle
committed
use of slots to try to manage leaks
1 parent 9e60118 commit 73d324b

File tree

6 files changed

+188
-87
lines changed

6 files changed

+188
-87
lines changed

src/py_eddy_tracker/grid/__init__.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ..tracking_objects import nearest
1111

1212

13-
class PyEddyTracker(object):
13+
class BaseData(object):
1414
"""
1515
Base object
1616
@@ -29,7 +29,42 @@ class PyEddyTracker(object):
2929
ROMS: get_ROMS_f_pm_pn
3030
3131
"""
32-
32+
33+
__slots__ = (
34+
'zero_crossing',
35+
'slice_i',
36+
'slice_j',
37+
'slice_i_pad',
38+
'slice_j_pad',
39+
'slice_i_unpad',
40+
'slice_j_unpad',
41+
'eke',
42+
'u_val',
43+
'upad',
44+
'v_val',
45+
'vpad',
46+
'_uspd',
47+
'm_val',
48+
'm_x',
49+
'm_y',
50+
'_lon',
51+
'_lat',
52+
'_f_val',
53+
'_gof',
54+
'_pm',
55+
'_pn',
56+
'_dx',
57+
'_dy',
58+
'_umask',
59+
'_vmask',
60+
'grid_filename',
61+
'grid_date',
62+
'domain',
63+
'pad',
64+
'shape',
65+
'mask',
66+
)
67+
3368
GRAVITY = 9.81
3469
earth_radius = 6371315.0
3570

@@ -73,7 +108,7 @@ def read_nc(self, varname, indices=slice(None)):
73108
indices : slice
74109
"""
75110
with Dataset(self.grid_filename) as h_nc:
76-
return h_nc.variables[varname][:][indices]
111+
return h_nc.variables[varname][indices]
77112

78113
@property
79114
def nc_variables(self):
@@ -120,7 +155,7 @@ def set_initial_indices(self):
120155
lonmin, lonmax = self.lonmin, self.lonmax
121156
latmin, latmax = self.latmin, self.latmax
122157

123-
logging.info('Setting initial indices to *%s* domain', self.the_domain)
158+
logging.info('Setting initial indices to *%s* domain', self.domain)
124159
logging.info('lonmin = %s, lonmax = %s, latmin = %s, latmax = %s',
125160
lonmin, lonmax, latmin, latmax)
126161
latmin_offset = latmin + (0.5 * (latmax - latmin))
@@ -287,7 +322,6 @@ def get_aviso_f_pm_pn(self):
287322
p_n[-1] = p_n[-2]
288323
self._dy = p_n
289324
self._pn = reciprocal(p_n)
290-
return self
291325

292326
def u2rho_2d(self, uu_in):
293327
"""
@@ -343,27 +377,19 @@ def set_basemap(self, with_pad=True):
343377
logging.info('Computing Basemap')
344378
# Create Basemap instance for Mercator projection.
345379
self.m_val = Proj(
346-
'+proj=merc '
347-
'+llcrnrlon=%(llcrnrlon)f '
348-
'+llcrnrlat=%(llcrnrlat)f '
349-
'+urcrnrlon=%(urcrnrlon)f '
350-
'+urcrnrlat=%(urcrnrlat)f '
351-
'+lat_ts=%(lat_ts)f' %
352-
dict(
353-
llcrnrlon=self.lonmin - 1,
354-
urcrnrlon=self.lonmax + 1,
355-
llcrnrlat=self.latmin - 1,
356-
urcrnrlat=self.latmax + 1,
357-
lat_ts=0.5 * (self.latmin + self.latmax)
358-
)
380+
proj='merc',
381+
llcrnrlon=self.lonmin - 1,
382+
urcrnrlon=self.lonmax + 1,
383+
llcrnrlat=self.latmin - 1,
384+
urcrnrlat=self.latmax + 1,
385+
lat_ts=0.5 * (self.latmin + self.latmax)
359386
)
360387

361388
if with_pad:
362389
x_val, y_val = self.m_val(self.lonpad, self.latpad)
363390
else:
364391
x_val, y_val = self.m_val(self.lon, self.lat)
365392
self.m_x, self.m_y = x_val, y_val
366-
return self
367393

368394
def set_geostrophic_velocity(self, zeta):
369395
"""
@@ -382,7 +408,6 @@ def set_geostrophic_velocity(self, zeta):
382408
self.vpad[:] = self.u2rho_2d(
383409
ma.array((zeta1 - zeta2) *0.5 * (pm1 + pm2), mask=self.umask))
384410
self.vpad *= self.gof
385-
return self
386411

387412
def set_u_v_eke(self, pad=2):
388413
"""
@@ -425,7 +450,6 @@ def set_interp_coeffs(self, sla, uspd):
425450
self.lat[:, 0], self.lon[0], sla, kx=1, ky=1)
426451
self.uspd_coeffs = interpolate.RectBivariateSpline(
427452
self.lat[:, 0], self.lon[0], uspd, kx=1, ky=1)
428-
return self
429453

430454
@staticmethod
431455
def create_index_inverse(slice_to_inverse, size):

src/py_eddy_tracker/grid/aviso.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import logging
99
from netCDF4 import Dataset
1010

11-
from . import PyEddyTracker
11+
from . import BaseData
1212

1313

14-
class AvisoGrid(PyEddyTracker):
14+
class AvisoGrid(BaseData):
1515
"""
1616
Class to satisfy the need of the eddy tracker
1717
to have a grid class
@@ -20,6 +20,26 @@ class AvisoGrid(PyEddyTracker):
2020
m=100.,
2121
cm=1.,
2222
)
23+
__slots__ = (
24+
'lonmin',
25+
'lonmax',
26+
'latmin',
27+
'latmax',
28+
'lon_name',
29+
'lat_name',
30+
'grid_name',
31+
'_lon',
32+
'_lat',
33+
'fillval',
34+
'_angle',
35+
'sla_coeffs',
36+
'uspd_coeffs',
37+
'__lon',
38+
'__lat',
39+
'__lonpad',
40+
'__latpad',
41+
'labels',
42+
)
2343

2444
def __init__(self, aviso_file, the_domain,
2545
lonmin, lonmax, latmin, latmax, grid_name, lon_name,
@@ -30,7 +50,7 @@ def __init__(self, aviso_file, the_domain,
3050
super(AvisoGrid, self).__init__()
3151
logging.info('Initialising the *AVISO_grid*')
3252
self.grid_filename = aviso_file
33-
self.the_domain = the_domain
53+
self.domain = the_domain
3454
self.lonmin = float(lonmin)
3555
self.lonmax = float(lonmax)
3656
self.latmin = float(latmin)
@@ -50,12 +70,12 @@ def __init__(self, aviso_file, the_domain,
5070
self._lon, self._lat = meshgrid(self._lon, self._lat)
5171
self._angle = zeros(self._lon.shape)
5272

53-
if 'MedSea' in self.the_domain:
73+
if 'MedSea' in self.domain:
5474
self._lon -= 360.
5575

5676
# zero_crossing, used for handling a longitude range that
5777
# crosses zero degree meridian
58-
if lonmin < 0 and lonmax >= 0 and 'MedSea' not in self.the_domain:
78+
if lonmin < 0 and lonmax >= 0 and 'MedSea' not in self.domain:
5979
self.zero_crossing = True
6080

6181
self.sla_coeffs = None
@@ -73,6 +93,9 @@ def __init__(self, aviso_file, the_domain,
7393
# self.shape = (self.f_coriolis.shape[0] - pad2,
7494
# self.f_coriolis.shape[1] - pad2)
7595

96+
def set_filename(self, file_name):
97+
self.grid_filename = file_name
98+
7699
def get_aviso_data(self, aviso_file):
77100
"""
78101
Read nc data from AVISO file
@@ -104,10 +127,9 @@ def get_aviso_data(self, aviso_file):
104127
zeta = zeta.T
105128

106129
zeta *= self.KNOWN_UNITS[units] # units to cm
107-
if hasattr(zeta, 'mask'):
108-
return zeta
109-
else:
110-
return ma.array(zeta)
130+
if not hasattr(zeta, 'mask'):
131+
zeta = ma.array(zeta)
132+
return zeta
111133

112134
def set_mask(self, sla):
113135
"""
@@ -116,7 +138,7 @@ def set_mask(self, sla):
116138
self.mask = None
117139
else:
118140
self.mask = sla.mask.copy()
119-
if 'Global' in self.the_domain:
141+
if 'Global' in self.domain:
120142

121143
# Close Drake Passage
122144
minus70 = argmin(abs(self.lonpad[0] + 70))
@@ -156,14 +178,14 @@ def set_mask(self, sla):
156178
plus9 = argmin(abs(self.latpad[:, 0] - 9))
157179
sea_label = self.labels[plus9, plus200]
158180
self.mask += self.labels != sea_label
159-
return self
160181

161182
def fillmask(self, data, mask):
162183
"""
163184
Fill missing values in an array with an average of nearest
164185
neighbours
165186
From http://permalink.gmane.org/gmane.comp.python.scientific.user/19610
166187
"""
188+
raise Exception('Use convolution to fill data')
167189
assert data.ndim == 2, 'data must be a 2D array.'
168190
fill_value = 9999.99
169191
data[mask == 0] = fill_value

src/py_eddy_tracker/property_functions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ def collection_loop(contours, grd, eddy, x_i=None, c_s_xi=None):
394394
# Loop over individual c_s contours (i.e., every eddy in field)
395395
for cont in contour_paths:
396396
# Filter for closed contours
397+
398+
# I don't understand the cost of this addition
399+
#~ eddy.swirl.is_valid(eddy.swirl.level_index[corrected_coll_index] + i_cont)
397400
if not cont.isvalid:
398401
continue
399402
centlon_e, centlat_e, eddy_radius_e, aerr = cont.fit_circle()

0 commit comments

Comments
 (0)