Skip to content

Commit 606f55e

Browse files
committed
Modified pickling procedures
1 parent b282e54 commit 606f55e

File tree

3 files changed

+31
-161
lines changed

3 files changed

+31
-161
lines changed

make_eddy_track_AVISO.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
Scroll down to line ~640 to get started
3131
===============================================================================
3232
"""
33-
#from matplotlib import use as mpl_use
34-
#mpl_use('Agg')
33+
from matplotlib import use as mpl_use
34+
mpl_use('Agg')
3535
import sys
3636
import glob as glob
3737
from py_eddy_tracker_classes import plt, np, dt, Dataset, time, \
@@ -47,7 +47,7 @@
4747
from mpl_toolkits.basemap import Basemap
4848
import yaml
4949
from datetime import datetime
50-
import pickle
50+
import cPickle as pickle
5151

5252

5353
def timeit(method):
@@ -538,6 +538,21 @@ def __init__(self, AVISO_FILE, THE_DOMAIN, PRODUCT,
538538
pad2 = 2 * self.pad
539539
self.shape = (self.f().shape[0] - pad2, self.f().shape[1] - pad2)
540540

541+
542+
def __getstate__(self):
543+
"""
544+
Needed for Pickle
545+
"""
546+
print '--- removing unwanted attributes'
547+
pops = ('Mx', 'My', '_f', '_angle', '_dx', '_dy', '_gof', '_lon',
548+
'_lat', '_pm', '_pn', '_umask', '_vmask', 'eke', 'sla',
549+
'mask', 'slacopy', 'sla_coeffs', 'uspd_coeffs')
550+
result = self.__dict__.copy()
551+
for pop in pops:
552+
result.pop(pop)
553+
return result
554+
555+
541556
#@timeit
542557
def get_AVISO_data(self, AVISO_FILE):
543558
"""
@@ -638,16 +653,6 @@ def set_mask(self, sla):
638653

639654

640655

641-
def set_global_mask(self):
642-
"""
643-
"""
644-
if 'Global' in self.THE_DOMAIN:
645-
labels = ndimage.label(np.logical_not(self.sla.mask))[0]
646-
647-
else:
648-
return
649-
return
650-
651656

652657
def fillmask(self, x, mask):
653658
"""
@@ -844,7 +849,6 @@ def set_interp_coeffs(self, sla, uspd):
844849
DATE_END = config['DATE_END'] = config['DOMAIN']['DATE_END']
845850

846851
AVISO_DT14 = config['AVISO']['AVISO_DT14']
847-
AVISO_FILES = config['AVISO']['AVISO_FILES']
848852
if AVISO_DT14:
849853
PRODUCT = 'AVISO_DT14'
850854
AVISO_DT14_SUBSAMP = config['AVISO']['AVISO_DT14_SUBSAMP']
@@ -1040,7 +1044,6 @@ def set_interp_coeffs(self, sla, uspd):
10401044
A_eddy.reset_holding_variables()
10411045
C_eddy.reset_holding_variables()
10421046

1043-
#grdmask = grd.mask()[j0:j1,i0:i1]
10441047

10451048
sla = sla_grd.get_AVISO_data(AVISO_FILE)
10461049
sla_grd.set_mask(sla).uvmask()
@@ -1204,18 +1207,16 @@ def set_interp_coeffs(self, sla, uspd):
12041207
A_list_obj=None, C_list_obj=C_eddy,
12051208
sign_type=C_eddy.SIGN_TYPE, VERBOSE=C_eddy.VERBOSE)
12061209

1207-
1208-
12091210

12101211
ymd_str = ''.join((str(yr), str(mo).zfill(2), str(da).zfill(2)))
12111212

12121213

12131214
# Test pickling
12141215
with open("".join((SAVE_DIR, 'A_eddy_%s.pkl' % ymd_str)), 'wb') as save_pickle:
1215-
pickle.dump(A_eddy, save_pickle)
1216+
pickle.dump(A_eddy, save_pickle, 2)
12161217

12171218
with open("".join((SAVE_DIR, 'C_eddy_%s.pkl' % ymd_str)), 'wb') as save_pickle:
1218-
pickle.dump(C_eddy, save_pickle)
1219+
pickle.dump(C_eddy, save_pickle, 2)
12191220

12201221
## Unpickle
12211222
#with open('C_eddy.pkl', 'rb') as load_pickle:
@@ -1276,8 +1277,6 @@ def set_interp_coeffs(self, sla, uspd):
12761277

12771278
if SAVE_FIGURES: # Make figures for animations
12781279

1279-
tit = ''.join((str(yr), str(mo).zfill(2), str(da).zfill(2)))
1280-
12811280
if 'Q' in DIAGNOSTIC_TYPE:
12821281
anim_figure(A_eddy, C_eddy, Mx, My, plt.cm.RdBu_r, rtime, DIAGNOSTIC_TYPE,
12831282
SAVE_DIR, 'Q-parameter ' + ymd_str, animax, animax_cbar,

make_eddy_tracker_list_obj.py

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -903,15 +903,7 @@ def write2netcdf(self, rtime):
903903
return self
904904

905905

906-
#def get_distances(self, centlon, centlat):
907-
#"""
908-
#Return all distances between the current centroid *centlon*,
909-
#*centlat* and arrays *self.old_lon*, *self.old_lat*
910-
#"""
911-
#clon = np.full_like(self.old_lon, centlon)
912-
#clat = np.full_like(self.old_lon, centlat)
913-
#return haversine_distance_vector(clon, clat,
914-
#self.old_lon, self.old_lat)
906+
915907

916908

917909
def insert_at_index(self, xarr, ind, x):
@@ -934,103 +926,7 @@ def insert_at_index(self, xarr, ind, x):
934926
setattr(self, xarr, val)
935927

936928

937-
'''
938-
if strcompare('new_lon', xarr):
939-
try:
940-
self.new_lon[ind] = x
941-
except:
942-
self.new_lon.extend([0] *
943-
(ind - len(self.new_lon) + 1))
944-
self.new_lon[ind] = x
945-
elif strcompare('new_lat', xarr):
946-
try:
947-
self.new_lat[ind] = x
948-
except:
949-
self.new_lat.extend([0] *
950-
(ind - len(self.new_lat) + 1))
951-
self.new_lat[ind] = x
952-
elif strcompare('new_radii_s', xarr):
953-
try:
954-
self.new_radii_s[ind] = x
955-
except:
956-
self.new_radii_s.extend([0] *
957-
(ind - len(self.new_radii_s) + 1))
958-
self.new_radii_s[ind] = x
959-
elif strcompare('new_radii_e', xarr):
960-
try:
961-
self.new_radii_e[ind] = x
962-
except:
963-
self.new_radii_e.extend([0] *
964-
(ind - len(self.new_radii_e) + 1))
965-
self.new_radii_e[ind] = x
966-
elif strcompare('new_amp', xarr):
967-
try:
968-
self.new_amp[ind] = x
969-
except:
970-
self.new_amp.extend([0] *
971-
(ind - len(self.new_amp) + 1))
972-
self.new_amp[ind] = x
973-
elif strcompare('new_uavg', xarr):
974-
try:
975-
self.new_uavg[ind] = x
976-
except:
977-
self.new_uavg.extend([0] *
978-
(ind - len(self.new_uavg) + 1))
979-
self.new_uavg[ind] = x
980-
elif strcompare('new_teke', xarr):
981-
try:
982-
self.new_teke[ind] = x
983-
except:
984-
self.new_teke.extend([0] *
985-
(ind - len(self.new_teke) + 1))
986-
self.new_teke[ind] = x
987-
elif strcompare('new_temp', xarr):
988-
try:
989-
self.new_temp[ind] = x
990-
except:
991-
self.new_temp.extend([0] *
992-
(ind - len(self.new_temp) + 1))
993-
self.new_temp[ind] = x
994-
elif strcompare('new_salt', xarr):
995-
try:
996-
self.new_salt[ind] = x
997-
except:
998-
self.new_salt.extend([0] *
999-
(ind - len(self.new_salt) + 1))
1000-
self.new_salt[ind] = x
1001-
elif strcompare('new_shape_error', xarr):
1002-
try:
1003-
self.new_shape_error[ind] = x
1004-
except:
1005-
self.new_shape_error.extend([0] *
1006-
(ind - len(self.new_shape_error) + 1))
1007-
self.new_shape_error[ind] = x
1008-
1009-
elif strcompare('new_contour_e', xarr):
1010-
try:
1011-
self.new_contour_e[ind] = x
1012-
except:
1013-
self.new_contour_e.append([0] *
1014-
(ind - len(self.new_contour_e) + 1))
1015-
self.new_contour_e[ind] = x
1016-
elif strcompare('new_contour_s', xarr):
1017-
try:
1018-
self.new_contour_s[ind] = x
1019-
except:
1020-
self.new_contour_s.append([0] *
1021-
(ind - len(self.new_contour_s) + 1))
1022-
self.new_contour_s[ind] = x
1023-
elif strcompare('new_uavg_profile', xarr):
1024-
try:
1025-
self.new_uavg_profile[ind] = x
1026-
except:
1027-
self.new_uavg_profile.append([0] *
1028-
(ind - len(self.new_uavg_profile) + 1))
1029-
self.new_uavg_profile[ind] = x
1030-
else:
1031-
raise Exception
1032-
1033-
return self'''
929+
1034930

1035931

1036932

py_eddy_tracker_classes.py

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -880,30 +880,19 @@ def track_eddies(Eddy, first_record):
880880

881881
far_away = 1e9
882882

883-
# We will need these in m for ellipse method below
883+
# We will need these in M for ellipse method below
884884
old_x, old_y = Eddy.M(np.array(Eddy.old_lon), np.array(Eddy.old_lat))
885885
new_x, new_y = Eddy.M(np.array(Eddy.new_lon_tmp),
886886
np.array(Eddy.new_lat_tmp))
887887

888-
X_old = np.array([Eddy.old_lon, Eddy.old_lat]).T
889-
X_new = np.array([Eddy.new_lon_tmp, Eddy.new_lat_tmp]).T
888+
X_old = np.asfortranarray([Eddy.old_lon, Eddy.old_lat]).T
889+
X_new = np.asfortranarray([Eddy.new_lon_tmp, Eddy.new_lat_tmp]).T
890890

891891
# Use haversine distance for distance matrix between every old and new eddy
892-
#dist_mat = np.empty((X_old.shape[0], X_new.shape[0]))
893-
dist_mat = np.ones((X_old.shape[0], X_new.shape[0]), dtype=np.float64)
892+
dist_mat = np.empty((X_old.shape[0], X_new.shape[0]), order='F')
894893

895-
#print 'BEFORE dist_mat', dist_mat.min(), dist_mat.max()
896-
#print dist_mat.flags
897-
#print '--------------'
898-
dist_mat = np.asfortranarray(dist_mat)
899-
haversine.distance_matrix(np.asfortranarray(X_old),
900-
np.asfortranarray(X_new),
901-
dist_mat)
894+
haversine.distance_matrix(X_old, X_new, dist_mat)
902895
dist_mat = np.ascontiguousarray(dist_mat)
903-
#print '+++++++++++++++'
904-
#print 'AFTER dist_mat', dist_mat.min(), dist_mat.max()
905-
#print dist_mat.flags
906-
#print 'JUNK', dist_mat_junk.min(), dist_mat_junk.max()
907896

908897
dist_mat_copy = dist_mat.copy()
909898

@@ -980,28 +969,26 @@ def track_eddies(Eddy, first_record):
980969
# Loop over separation distances between old and new
981970
for new_ind, new_dist in enumerate(dist_mat[old_ind]):
982971

983-
sep_proceed = False
972+
within_range = False
984973

985974
if new_dist < far_away:
986975

987976
if 'ellipse' in Eddy.SEPARATION_METHOD:
988977
if Eddy.search_ellipse.ellipse_path.contains_point(
989978
(new_x[new_ind], new_y[new_ind])):
990-
sep_proceed = True
991-
#else:
992-
#sep_proceed = False
979+
within_range = True
993980

994981
elif 'sum_radii' in Eddy.SEPARATION_METHOD:
995982
sep_dist = Eddy.new_radii_tmp_e[new_ind]
996983
sep_dist += Eddy.old_radii_e[old_ind]
997984
sep_dist *= Eddy.sep_dist_fac
998-
sep_proceed = new_dist <= sep_dist
985+
within_range = new_dist <= sep_dist
999986

1000987
else:
1001988
Exception
1002989

1003990
# Pass only the eddies within ellipse or sep_dist
1004-
if sep_proceed:
991+
if within_range:
1005992

1006993
old_amplitude[:] = Eddy.old_amp[old_ind]
1007994
new_amplitude[:] = Eddy.new_amp_tmp[new_ind]
@@ -1102,8 +1089,6 @@ def track_eddies(Eddy, first_record):
11021089
delta_salt = np.r_[delta_salt,
11031090
np.abs(np.diff([Eddy.old_salt[old_ind], new_st[i]]))]
11041091

1105-
#Eddy.search_ellipse.view_search_ellipse(Eddy)
1106-
#print dist_arr, DIST0
11071092
# This from Penven etal (2005)
11081093
deltaX = np.sqrt((delta_area / AREA0)**2 +
11091094
(delta_amp / AMP0)**2 +
@@ -1159,18 +1144,8 @@ def track_eddies(Eddy, first_record):
11591144

11601145
# Use backup_ind to reinsert distances into dist_mat for the unused eddy/eddies
11611146
for bind in backup_ind[dx_unused]:
1162-
#print dist_mat.shape
1163-
#print dist_mat[:, bind]
1164-
#print dist_mat_copy[:, bind]
11651147
dist_mat[:, bind] = dist_mat_copy[:, bind]
1166-
#print dist_mat[:, bind]
1167-
#print '\n'
1168-
#print 'B new_eddy_inds', new_eddy_inds
11691148
new_eddy_inds[bind] = True
1170-
#print 'A new_eddy_inds', new_eddy_inds
1171-
#print '\n'
1172-
1173-
11741149

11751150
if debug_dist:
11761151
print 'backup_ind', backup_ind

0 commit comments

Comments
 (0)