-
Notifications
You must be signed in to change notification settings - Fork 0
/
mega_volume_2017.py
364 lines (288 loc) · 16.7 KB
/
mega_volume_2017.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# common conversion point stacking
root='/raid3/sdat2/Parra/'
# import modules
import sys
#sys.path.append('/raid2/sc845/Python/lib/python/')
#sys.path.append('/raid2/sc845/Tomographic_models/EU60/')
#sys.path.append('/raid2/sc845/Python/geographiclib-1.34/')
from geographiclib.geodesic import Geodesic as geo
import numpy as np
#from matplotlib.mlab import griddata
import scipy
from scipy import interpolate
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
import os
import os.path
import math
import msgpack
import msgpack_numpy as m
m.patch()
import shutil
from matplotlib.colors import LogNorm
import matplotlib.cm as cm
from obspy import read
from scipy import stats
import mpl_toolkits
# definition of the half width of the fresnel zone
knotspacing = lambda r, vs: 1./2.*np.sqrt(((10./3.*vs)+r)**2.-r**2.) # in m for a 10s wave
def haversine(lat1, long1, lats2, longs2):
"""
Calculate the distance between two points on earth in m
"""
d=[]
for i in range(len(lats2)):
lat2=lats2[i]
long2=longs2[i]
earth_radius = 6371.e3 # m
dLat = math.radians(lat2 - lat1)
dLong = math.radians(long2 - long1)
a = (math.sin(dLat / 2) ** 2 +
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dLong / 2) ** 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d.append(earth_radius * c)
return float(d[0])
def weight(distance,depth,vs,factor):
'''
Calculates the weight based on the distance and the fresnel zone width for a given depth
'''
#print knotspacing(depth*1.e3,vs)
delta=distance/(factor*knotspacing(depth*1.e3,vs)) # distance in m ~~~~~~ fresnel zone times factor~~~
#print delta
if delta>2:
weight=0
elif delta>1:
weight=.25*(2.-delta)**3.
else:
weight=.75*delta**3.-1.5*delta**2.+1.
return weight
class VOL(dict):
'''
Initiates a dictionary for the CCP volume
'''
def __init__(self,*arg,**kw):
super(VOL,self).__init__(*arg,**kw)
self.__dict__=self
def __getattr__(self,name):
return self[name]
class ccp_volume(object):
"""
Handling large stacked volumes
"""
def __init__(self,*arg,**kw):
self.VOL=VOL(*arg,**kw)
#############################################################################
# Start a new volume dictionary
#############################################################################
def start_empty_volume(self,name='trialarea_1',filter='jgf1',conversion='prem', factor=2., lonmin=None, lonmax=None, lonrez=None, latmin=None,latmax=None,latrez=None, depmin=None,depmax=None,deprez=None):
'''
Start the empty CCP volume
Parameters
---------
name String that defines the name of the directory where the volumes is stored
filter Sring that refers to the name of the receiver function to be used (If you add a new one, this needs to be implemented in the cod)
conversion String for the conversion model to be used (default = prem)
Receiver functions needs to contain dictionary with this conversion
factor This sets the amount of smoothing beyond the Fresnel Zone. Recommended to start with 2 (and maybe try 1 later)
lonmin, lonmax, lonrez Longitude bounds and number of steps to set up the grid
latmin, latmax, latrez Latitude bounds and number of steps to set up the grid
depmin, depmax, deprez Depths bounds and number of steps to set up the grid
'''
#Make directory for the volumes
dirout='/raid3/sdat2/Parra/CCP_Stack/Volumes/'+name+'_'+filter+'_'+conversion+'_'+str(factor)
if not os.path.exists(dirout):
os.makedirs(dirout)
if not os.path.exists(dirout+'/RF_lists/'):
os.makedirs(dirout+'/RF_lists/')
# Store initial empty PICKLE
outfilename='/raid3/sdat2/Parra/CCP_Stack/Volumes/'+name+'_'+filter+'_'+conversion+'_'+str(factor)+'/Stack_0.PICKLE'
donefilename='/raid3/sdat2/Parra/CCP_Stack/Volumes/'+name+'_'+filter+'_'+conversion+'_'+str(factor)+'/list_of_stacks.txt'
#setup volume grid
grid_depth=np.linspace(depmin,depmax,num=deprez)
grid_lon=np.linspace(lonmin,lonmax,num=lonrez)
grid_lat=np.linspace(latmin,latmax,num=latrez)
# Read in STW105 for the velocities used in the fresnel zone width
# (I forget why I use this model instead of prem. Not that it matters much...it's only used for the weighting factors)
###Replace with PREM?
table=[]
for line in open('/raid3/sdat2/Parra/CCP_Stack/CCP_Scripts/STW105.txt').readlines()[3:]:
if line[0]!='#':
numbers= list(map(float,line.split()))
table.append(numbers)
table=np.array(table)
refdepths=(6371.e3-table[:,0])
refVp=(table[:,6])
refVs=(table[:,7])
####### Define volumes
volume=np.zeros([len(grid_lon),len(grid_lat),len(grid_depth)])
volumeweight=np.zeros([len(grid_lon),len(grid_lat),len(grid_depth)]) #tracks summed weight
volumesigma=np.zeros([len(grid_lon),len(grid_lat),len(grid_depth)]) # tracks weighted difference
volumesign=np.zeros([len(grid_lon),len(grid_lat),len(grid_depth)]) #tracks stack of sign
num=np.zeros([len(grid_lon),len(grid_lat),len(grid_depth)]) # tracks number of RFs involved
grid_vs= np.interp(grid_depth,refdepths[::-1]/1.e3,refVs[::-1])
grid_vp= np.interp(grid_depth,refdepths[::-1]/1.e3,refVp[::-1])
# Store values and write out volume ##could replace with 3 lines
self.VOL['lonmin']=lonmin
self.VOL['lonmax']=lonmax
self.VOL['latmin']=latmin
self.VOL['latmax']=latmax
self.VOL['depmin']=depmin
self.VOL['depmax']=depmax
self.VOL['grid_depth']=grid_depth
self.VOL['grid_lon']=grid_lon
self.VOL['grid_lat']=grid_lat
self.VOL['volume']=volume
self.VOL['volumeweight']=volumeweight
self.VOL['volumesign']=volumesign
self.VOL['volumesigma']=volumesigma
self.VOL['num']=num
self.VOL['count']=0
self.VOL['grid_vs']=grid_vs
self.VOL['grid_vp']=grid_vp
with open(outfilename,'wb') as handle: #fails here - unable to serialise array
msgpack.pack(self.VOL,handle)
handle.close()
with open(root + 'CCP_Stack/Volumes/'+name+'_'+filter+'_'+conversion+'_'+str(factor)+'/filenames.dat','a') as handle:
handle.write('%d %s \n'% (0, outfilename))
handle.close()
print('DONE CREATING EMPTY VOLUME')
return self
#############################################################################
# Load latest volume to dictionary
#############################################################################
def load_latest(self,name='trialarea_1',filter='jgf1',conversion='prem',factor=2.):
'''
Loads latest volume
'''
print(name)
line=open(root + 'CCP_Stack/Volumes/'+name+'_'+filter+'_'+conversion+'_'+str(factor)+'/filenames.dat','r').readlines()[-1]
runnum=int(float(line.split()[0]))
volumefile=line.split()[1]
print(runnum, volumefile)
# get last stackfile name
####### Read in volumes
self.VOL.update(msgpack.unpack(open(volumefile,'rb'), use_list=False,object_hook=m.decode))
#del self.VOL['trackRFs']
return self
#############################################################################
# Add list of receiver functions to Volume
#############################################################################
def addlist(self,rflist,name='trialarea_1',filter='jgf1',conversion='prem',factor=2.):
'''
Adds list of PICKLE files to volume
The code has no trouble adding the same PICKLE file twice, which means those are not double weighted in the stack. Might need a fix...
'''
## set volume lats and lons
rffilter=filter
line=open('/raid3/sdat2/Parra/CCP_Stack/Volumes/'+name+'_'+rffilter+'_'+conversion+'_'+str(factor)+'/filenames.dat','r').readlines()[-1]
runnum=int(float(line.split()[0]))
volumefile=line.split()[1]
print(runnum, volumefile)
rffile=open('/raid3/sdat2/Parra/CCP_Stack/Volumes/'+name+'_'+rffilter+'_'+conversion+'_'+str(factor)+'/RF_lists/rflist'+(str(runnum+1))+'.dat','a')
for i in range(len(rflist)): #range(cat.count()): # loop through all receiver functions for a given station
print(i, 'out of', len(rflist))
if os.path.isfile(rflist[i]):
#try:
# Read and retrieve receiver function
seis=read(rflist[i],format='PICKLE')
self.VOL.count=self.VOL.count+1
rffile.write("%d %s \n" % (int(self.VOL.count),rflist[i]))
RF = np.real(getattr(seis[0],filter)['iterativedeconvolution'])
indm=np.argmax(np.abs(RF))
if np.mean(RF[indm-10:indm+10])<0.: # flip receiver function if needed
RF=RF*-1.
RF=RF/np.max(np.abs(RF)) # Normalize RF
for d in range(len(self.VOL.grid_depth)): # loop through all depths
# find (lat,lon) of the ray path at this given depth 3D
x = np.argmin(np.abs((seis[0].conversions[conversion]['depths']-self.VOL.grid_depth[d])))
#find (lat,lon) of the ray path at this given depth 3D
latx = seis[0].conversions[conversion]['latitudes'][x]
lonx = seis[0].conversions[conversion]['longitudes'][x]
x_RF = np.argmin(np.abs((seis[0].conversions[conversion]['depthsfortime']-self.VOL.grid_depth[d])))
# loop through all lats and lons for the given depth
# put some limits on grid to use
lonind = np.argmin(np.abs(self.VOL.grid_lon-lonx))
inds=int(round(d/30.)+3.) # widen as going deeper
lonlim = np.arange(lonind-inds,lonind+inds)
latind = np.argmin(np.abs(self.VOL.grid_lat-latx))
latlim = np.arange(latind-inds,latind+inds)
for k in lonlim:
for j in latlim:
if k>-1 and j>-1 and k < len(self.VOL.grid_lon) and j < len(self.VOL.grid_lat):
# Stack into 1D stacks
dist = haversine(latx,lonx,[self.VOL.grid_lat[j]],[self.VOL.grid_lon[k]]) # calculate distance
w = weight(dist,self.VOL.grid_depth[d],self.VOL.grid_vs[d],factor) # calculate weight using S wave fresnel zone
if w>0:
#self.VOL.trackRFs[k][j][d].append(self.VOL.count)
self.VOL.num[k,j,d] = self.VOL.num[k,j,d]+1. # count number of receiver functions
self.VOL.volume[k,j,d] = self.VOL.volume[k,j,d]+w*RF[x_RF] # stack receiver function into volume
self.VOL.volumeweight[k,j,d] = self.VOL.volumeweight[k,j,d]+w # stack weights
self.VOL.volumesigma[k,j,d] = self.VOL.volumesigma[k,j,d]+w*(RF[x_RF]-(self.VOL.volume[k,j,d]/self.VOL.volumeweight[k,j,d]))**2. # stack sign
self.VOL.volumesign[k,j,d] = self.VOL.volumesign[k,j,d]+w*np.sign(RF[x_RF]) # stack sign of receiver function
print('RFs used', self.VOL.count)
# Write out at the end of list
outfilename=root+'CCP_Stack/Volumes/'+name+'_'+rffilter+'_'+conversion+'_'+str(factor)+'/Stack_'+str(int(runnum+1))+'.PICKLE'
with open(outfilename,'wb') as handle:
msgpack.pack(self.VOL,handle)
handle.close()
with open(root+'CCP_Stack/Volumes/'+name+'_'+filter+'_'+conversion+'_'+str(factor)+'/filenames.dat','a') as handle:
handle.write('%d %s \n'% (runnum+1, outfilename))
handle.close()
rffile.close()
print('DONE')
return self
#############################################################################
# Add list of receiver functions to Volume
#############################################################################
def removelist(self,rflist,name='trialarea_1',filter='jgf1',conversion='prem',factor=2.):
## UNTESTED
rffilter=filter
line=open('/raid3/sdat2/parra/CCP_Stack/Volumes/'+name+'_'+rffilter+'_'+conversion+'_'+str(factor)+'/filenames.dat','r').readlines()[-1]
runnum=int(float(line.split()[0]))
volumefile=line.split()[1]
print(runnum, volumefile)
rffile=open('/raid3/sdat2/parra/CCP_Stack/Volumes/'+name+'_'+rffilter+'_'+conversion+'_'+str(factor)+'/RF_lists/rflistrm'+(str(runnum+1))+'.dat','a')
for i in range(len(rflist)): #range(cat.count()): # loop through all receiver functions for a given station
print(i, 'out of', len(rflist))
if rflist[i][:3]=='Dat':
rflist[i]='../'+rflist[i]
if os.path.isfile(rflist[i]):
seis=read(rflist[i],format='PICKLE')
self.VOL.count=self.VOL.count-1
rffile.write("%d %s \n" % (int(self.VOL.count),rflist[i]))
RF = np.real(getattr(seis[0],filter)['iterativedeconvolution'])
indm=np.argmax(np.abs(RF))
if np.mean(RF[indm-10:indm+10])<0.: # flip receiver function if needed
RF=RF*-1.
RF=RF/np.max(np.abs(RF))
for d in range(len(self.VOL.grid_depth)): # loop through all depths
# find (lat,lon) of the ray path at this given depth 3D
x = np.argmin(np.abs((seis[0].conversions[conversion]['depths']-self.VOL.grid_depth[d])))
latx = seis[0].conversions[conversion]['latitudes'][x]
lonx = seis[0].conversions[conversion]['longitudes'][x]
x_RF = np.argmin(np.abs((seis[0].conversions[conversion]['depthsfortime']-self.VOL.grid_depth[d])))
for k in lonlim:
for j in latlim:
if k>-1 and j>-1 and k < len(self.VOL.grid_lon) and j < len(self.VOL.grid_lat):
# Stack into 1D stacks
dist = haversine(latx,lonx,[self.VOL.grid_lat[j]],[self.VOL.grid_lon[k]]) # calculate distance
w = weight(dist,self.VOL.grid_depth[d],self.VOL.grid_vs[d],factor) # calculate weight using S wave fresnel zone
if w>0:
#self.VOL.trackRFs[k][j][d].append(self.VOL.count)
self.VOL.num[k,j,d] = self.VOL.num[k,j,d]-1. # count number of receiver functions
self.VOL.volume[k,j,d] = self.VOL.volume[k,j,d]-w*RF[x_RF] # stack receiver function into volume
self.VOL.volumeweight[k,j,d] = self.VOL.volumeweight[k,j,d]-w # stack weights
self.VOL.volumesigma[k,j,d] = self.VOL.volumesigma[k,j,d]-w*(RF[x_RF]-(self.VOL.volume[k,j,d]/self.VOL.volumeweight[k,j,d]))**2. # stack sign
self.VOL.volumesign[k,j,d] = self.VOL.volumesign[k,j,d]-w*np.sign(RF[x_RF]) # stack sign of receiver function
print('RFs used', self.VOL.count)
outfilename='/raid3/sdat2/Parra/CCP_Stack/Volumes/'+name+'_'+rffilter+'_'+conversion+'_'+str(factor)+'/Stack_'+str(int(runnum+1))+'.PICKLE'
with open(outfilename,'wb') as handle:
msgpack.pack(self.VOL,handle)
handle.close()
with open('/raid3/sdat2/Parra/CCP_Stack/Volumes/'+name+'_'+filter+'_'+conversion+'_'+str(factor)+'/filenames.dat','a') as handle:
handle.write('%d %s \n'% (runnum+1, outfilename))
handle.close()
rffile.close()
print('DONE WITH SUBSTRACTING DATA')
return self