forked from dpteague/PyMASWdisp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdctypes.py
311 lines (278 loc) · 13.7 KB
/
dctypes.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
"""
Classes:
DispersionPower:
Stores processed dispersion data for a given source-offset. This class
stores the relevent processing paramters (e.g., frequency, trial values
of k or v, kres, etc.). This class contains methods to plot (1) contours
of power in various domians and/or (2) 1D "slices" of power at user-
defined frequencies.
RawDispersion:
Stores the "raw" dispersion data (i.e., frequency, velocity, and offset)
for one or more source-offsets.
This code was developed at the University of Texas at Austin.
Copyright (C) 2016 David P. Teague, Clinton M. Wood, and Brady R. Cox
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
# Import modues
import math
import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
import tkinter as tk
import shotgathers
# Width and height (in) of plots showing contours and/or "slices"
mwdth = 6
mhght = 4.25
# Class containing full dispersion processing results (including power)
# Results can be derived from FK, FDBF, slant-stack transform, or Park transform
class DispersionPower(object):
# Class attributes**********************************************************
def __init__(self, freq, peak_vals, trial_vals, val_type, kres, pnorm):
self.freq = freq # Frequencies considered in calculations
self.peak_vals = peak_vals # Wavenumber or velocity corresponding to peak power at each frequency
self.trial_vals = trial_vals # Wavenumbers or velocities considered in analysis
self.val_type = val_type # Type of values considered in analysis ("wavenumber" or "velocity")
self.kres = kres # Wavenumber above which spatial aliasing occurs
self.pnorm = pnorm # Normalized power for all frequencies and wavenumbers or velocities
# Method to plot power in various domains***********************************
def plotSpect( self, plotType="fv", plotLim=[] ):
# Plotting parameters if wavenumbers were considered in analysis
if str.lower(self.val_type)=="wavenumber":
k_vals = self.trial_vals
# Grid parameters
freq_grid, wavenum_grid = np.meshgrid( self.freq, k_vals )
vel_grid = 2*np.pi*freq_grid / wavenum_grid
wavel_grid = 2*np.pi/wavenum_grid
# Peaks
k_peak = self.peak_vals
wavel_peak = 2*np.pi / k_peak
v_peak = wavel_peak*self.freq
# Plotting parameters if velocities were considered in analysis
elif str.lower(self.val_type)=="velocity":
v_vals = self.trial_vals
# Grid parameters
freq_grid, vel_grid = np.meshgrid( self.freq, v_vals )
wavel_grid = vel_grid / freq_grid
wavenum_grid = 2*np.pi / wavel_grid
# Peaks
v_peak = self.peak_vals
k_peak = 2*np.pi*self.freq / v_peak
wavel_peak = 2*np.pi / k_peak
else:
raise ValueError("Invalid val_type. Should be \"wavenumber\" or \"velocity\".")
# Compute maximum power (for plotting purposes)
maxZ = np.nanmax( np.abs( self.pnorm ) )
# Set x- and y-axes based on plotType
# Frequency-wavenumber
if str.lower(plotType)=="fk":
xgrid = freq_grid
ygrid = wavenum_grid
xpeak = self.freq
ypeak = k_peak
if len(plotLim) == 0:
plotLim = [0, np.max(self.freq), 0, self.kres]
elif len(plotLim) != 4:
raise ValueError("plotLim should be a four element list")
xscale = "linear"
yscale = "linear"
xLabText = "Frequency (Hz)"
yLabText = "Wavenumber (rad/m)"
# Frequency-wavelength
elif str.lower(plotType)=="fw":
xgrid = freq_grid
ygrid = wavel_grid
xpeak = self.freq
ypeak = wavel_peak
if len(plotLim) == 0:
plotLim = [0, np.max(self.freq), 1, 200]
elif len(plotLim) != 4:
raise ValueError("plotLim should be a four element list")
xscale = "linear"
yscale = "log"
xLabText = "Frequency (Hz)"
yLabText = "Wavelength (m)"
# Frequency-velocity
elif str.lower(plotType)=="fv":
xgrid = freq_grid
ygrid = vel_grid
xpeak = self.freq
ypeak = v_peak
if len(plotLim) == 0:
plotLim = [0, np.max(self.freq), 0, 1000]
elif len(plotLim) != 4:
raise ValueError("plotLim should be a four element list")
xscale = "linear"
yscale = "linear"
xLabText = "Frequency (Hz)"
yLabText = "Velocity (m/s)"
# Frequency-slowness
elif str.lower(plotType)=="fp":
xgrid = freq_grid
ygrid = 1.0 / vel_grid
xpeak = self.freq
ypeak = 1.0 / v_peak
if len(plotLim) == 0:
plotLim = [0, np.max(self.freq), 1.0/1000, 1.0/100]
elif len(plotLim) != 4:
raise ValueError("plotLim should be a four element list")
xscale = "linear"
yscale = "linear"
xLabText = "Frequency (Hz)"
yLabText = "Slowness (s/m)"
# Wavelength-velocity
elif str.lower(plotType)=="wv":
xgrid = wavel_grid
ygrid = vel_grid
xpeak = wavel_peak
ypeak = v_peak
if len(plotLim) == 0:
plotLim = [1, 200, 0, 1000]
elif len(plotLim) != 4:
raise ValueError("plotLim should be a four element list")
xscale = "log"
yscale = "linear"
xLabText = "Wavelength (m)"
yLabText = "Velocity (m/s)"
# Ploting
# Set figure size equal to 2/3 screen size
# Get screen size in mm and convert to in (25.4 mm per inch)
#root = tk.Tk()
#width = root.winfo_screenmmwidth() / 25.4 * 0.66
#height = root.winfo_screenmmheight() / 25.4 * 0.66
#fig = plt.figure( figsize=(width,height) )
fig = plt.figure( figsize=(mwdth,mhght) )
ax = fig.add_axes([0.14, 0.14, 0.80, 0.80])
plt.contourf( xgrid, ygrid, np.abs(self.pnorm), np.linspace(0, maxZ, 20), cmap=plt.cm.get_cmap("jet") )
ax.plot( xpeak, ypeak, marker="o", markersize=5, markeredgecolor="w", markerfacecolor='none', linestyle="none" )
ax.axis( plotLim )
ax.set_xlabel(xLabText, fontsize=12, fontname="arial")
ax.set_ylabel(yLabText, fontsize=12, fontname="arial")
ax.set_xscale(xscale)
ax.set_yscale(yscale)
ax.set_xticklabels(ax.get_xticks(), fontsize=12, fontname="arial" )
ax.set_yticklabels(ax.get_yticks(), fontsize=12, fontname="arial" )
ax.xaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%d'))
ax.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%d'))
plt.colorbar(ticks=np.linspace(0, maxZ, 8))
# Method to plot slices in various domains**********************************
def plotSlices( self, plotType="fv", freqPlotValues=np.arange(6,22,1), xlims=[] ):
# Determine appropriate number of panels and their arrangement
n_slices = len(freqPlotValues)
xFigDim = int( math.ceil( math.sqrt(n_slices) ) )
if ( math.ceil( math.sqrt(n_slices) )*math.floor( math.sqrt(n_slices) ) < n_slices ):
yFigDim = int( math.ceil( math.sqrt(n_slices) ) )
else:
yFigDim = int( math.floor( math.sqrt(n_slices) ) )
# Create an array containing the row and column for each panel
panel = 0
panel_ids = np.zeros((n_slices,2))
for r in range(yFigDim):
for c in range(xFigDim):
if (panel+1)<=n_slices:
panel_ids[panel,0] = r+1
panel_ids[panel,1] = c+1
panel += 1
# Set figure size equal to 2/3 screen size
# Get screen size in mm and convert to in (25.4 mm per inch)
#root = tk.Tk()
#width = root.winfo_screenmmwidth() / 25.4 * 0.66
#height = root.winfo_screenmmheight() / 25.4 * 0.66
#fig = plt.figure( figsize=(width,height) )
fig = plt.figure( figsize=(mwdth,mhght) )
# Loop through freqPlotValues
for k in range(n_slices-1, -1, -1):
# Find frequency closest to freqPlotValues(k)
c_id = np.argmin( np.absolute(self.freq-freqPlotValues[k]) )
cfreq = self.freq[c_id]
# Plotting parameters
if str.lower(self.val_type)=="wavenumber":
k_vals = self.trial_vals
k_peak = self.peak_vals
v_vals = 2*np.pi*cfreq / k_vals
v_peak = 2*np.pi*cfreq / k_peak
elif str.lower(self.val_type)=="velocity":
v_vals = self.trial_vals
v_peak = self.peak_vals
k_vals = 2*np.pi*cfreq / v_vals
k_peak = 2*np.pi*cfreq / v_peak
else:
raise ValueError("Invalid value type, should be \"wavenumber\" or \"velocity\"")
# Compute maximum power
maxY = np.nanmax( np.abs(self.pnorm[:,c_id]) )
# Determine x-axis and corresponding limits based on chosen graph type
if str.lower(plotType)=="fk":
x = k_vals
xp = k_peak[c_id]
xLabText = "Wavenumber (rad/m)"
if not xlims:
xlims = (0,self.kres)
xscale = "linear"
text_xloc = 0.66*(xlims[1] - xlims[0]) + xlims[0]
elif str.lower(plotType)=="fw":
x = 2*np.pi / k_vals
xp = 2*np.pi / k_peak[c_id]
xLabText = 'Wavelength (m)'
if not xlims:
xlims = (1,200)
xscale = "log"
text_xloc = math.pow( 10, (0.66*( math.log10(xlims[1]) - math.log10(xlims[0]) ) + math.log10(xlims[0])) )
elif str.lower(plotType)=="fv":
x = v_vals
xp = v_peak[c_id]
xLabText = "Velocity (m/s)"
if not xlims:
xlims = (0,1000)
xscale = "linear"
text_xloc = 0.66*(xlims[1] - xlims[0]) + xlims[0]
elif str.lower(plotType)=="fp":
x = 1.0 / v_vals
xp = 1.0 / v_peak[c_id]
xLabText = "Slowness (s/m)"
if (k+1)==n_slices:
minX = 1.0 / np.max(v_vals)
if not xlims:
xlims = (minX, 1.0/100)
xscale = "linear"
text_xloc = 0.33*(xlims[1] - xlims[0]) + xlims[0]
else:
raise ValueError("Invalid plot type, should be \"fk\", \"fw\", \"fv\" or \"fp\"")
# Plot power at current frequency
ax = fig.add_subplot( yFigDim, xFigDim, k+1)
ax.set_xlim( xlims )
ax.set_ylim( ( 0, maxY ) )
ax.set_xscale(xscale)
ax.plot( x, np.abs( self.pnorm[:,c_id] ) )
ax.plot( xp, np.max( np.abs( self.pnorm[:,c_id] ) ), marker="*", color="r", markersize=10 )
ax.set_xticklabels(ax.get_xticks(), fontsize=9, fontname='arial' )
ax.set_yticklabels(ax.get_yticks(), fontsize=9, fontname='arial' )
ax.xaxis.set_major_formatter( mpl.ticker.FormatStrFormatter('%d') )
ax.yaxis.set_major_formatter( mpl.ticker.FormatStrFormatter('%d') )
prfreq = '%.2f' % cfreq
plt.text( text_xloc, 0.75*maxY, prfreq+" Hz", fontsize=9, fontname="arial" )
if panel_ids[k,0] == yFigDim:
ax.set_xlabel(xLabText, fontsize=9, fontname="arial")
if panel_ids[k,1] == 1:
ax.set_ylabel("Normalized Amplitude", fontsize=9, fontname="arial")
# Class containing raw dispersion processing results for one or more source offsets
class RawDispersion(object):
# Class attributes**********************************************************
def __init__(self, frequency, velocity, offset):
self.frequency = frequency # List containing frequency arrays (1 per offset)
self.velocity = velocity # List containing velocity arrays (1 per offset)
self.offset = offset # List containing offsets
# Method to remove data with excessively high/low Vs values*****************
def rmvHighVs(self, Vs_cut=3500):
for k in range(np.shape(self.frequency)[0]):
keep_id = np.logical_and( self.velocity[k] <= Vs_cut, self.velocity[k]>0 )
self.velocity[k] = self.velocity[k][keep_id]
self.frequency[k] = self.frequency[k][keep_id]