-
Notifications
You must be signed in to change notification settings - Fork 42
/
plot.py
312 lines (257 loc) · 9.96 KB
/
plot.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
###############################################################################
#
# SWIT: Seismic Waveform Inversion Toolbox
#
# by Haipeng Li at USTC, [email protected]
#
# June, 2021
#
# Plot module
#
###############################################################################
import time
import matplotlib
matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import numpy as np
import matplotlib.pyplot as plt
from multiprocessing import Pool
from tools import loadsu, add_su_header, convert_wavelet_su, savebinfloat32
### Plot acquisition geometry
def plot_geometry(simu):
''' Plot source and receiver acquisition geometry
'''
srcn = simu.source.n
srcxz = simu.source.xz
recxz = simu.receiver.xz
figpath = simu.system.homepath + 'figures/'
# Acquisition geometry
fig = plt.figure()
ax = fig.add_subplot(111)
for isrc in range(srcn):
if np.mod(isrc,1) == 0:
plt.scatter(recxz[isrc,:,0], 0 * recxz[isrc,:, 1] + 1 * isrc + 1, c = 'green', marker='o', s = 2)
plt.scatter(srcxz[isrc, 0], 0 * srcxz[isrc, 1] + 1 * isrc + 1, c = 'red', marker='^', s = 6)
ax.set_ylim(0, srcn+1)
plt.xlabel('Distance-x (m)', fontsize=12)
plt.ylabel('Shot #', fontsize=12)
plt.title('Acquisition Geometry, %d sources\n' % (srcn), fontsize=14)
plt.savefig(figpath + 'Acquisition-Geometry.png', dpi=300)
plt.close()
### Plot source time function (STF).
def plot_stf(simu, isrc=1, stf_type='obs', t_end=1.0):
''' Plot source time function in the time and Frequency domanin
'''
if isrc < 1 or isrc > simu.source.n:
raise ValueError('isrc exceeds source range: 1~%d.\n'%simu.source.n)
else:
ISRC = isrc
isrc = isrc - 1
t_end = np.min((t_end, simu.model.dt*(simu.model.nt-1)))
# set data for plot
stf_time = simu.source.wavelet[isrc, :]
stf_spectrum = np.abs(np.fft.fft(stf_time))
freqs = np.fft.fftfreq(len(stf_time), simu.model.dt)
idx = np.argsort(freqs)
idx = idx[int(len(idx) / 2):]
nt = np.array(t_end/simu.model.dt)
nt = nt.astype(int)
# t0, t1 in ms; relative amps
WAVELET_EXTENT = (0, t_end, -1.2, 1.2)
SPECTRUM_EXTENT = (0, 50, 0, 1.2) # f0, f1, p0, p1
# Figure
fig = plt.figure()
# subplot1: time domain
ax1 = fig.add_subplot(2, 1, 1)
ax1.xaxis.set_label_text('Time (s)', fontsize=12)
ax1.yaxis.set_label_text('Normalized Amplitude', fontsize=12)
ax1.set_title('Source wavelet - source %d - %s' % (ISRC, stf_type), fontsize=16)
ax1.axis(WAVELET_EXTENT)
ax1.plot(simu.model.t[0:nt+1], stf_time[0:nt+1] / abs(stf_time[0:nt+1]).max(), 'g-')
# subplot2: frequency domain
ax2 = fig.add_subplot(2, 1, 2)
ax2.xaxis.set_label_text('Frequency (Hz)', fontsize=12)
ax2.yaxis.set_label_text('Amplitude', fontsize=12)
ax2.axis(SPECTRUM_EXTENT)
ax2.fill(freqs[idx], stf_spectrum[idx] / abs(stf_spectrum).max(), 'c')
ax2.plot(freqs[idx], stf_spectrum[idx] / abs(stf_spectrum).max(), 'b')
fig.tight_layout()
plt.savefig(simu.system.homepath + 'figures/STF-%s-src%d.png' % (stf_type, ISRC), dpi=300)
plt.close()
### Plot model (2D): velocity, gradient
def plot_model2D(simu, data, vmin, vmax, filename, colormap = 'jet'):
''' Plot model material (2D), e.g. vp, vs, and rho.
'''
xx = simu.model.xx
zz = simu.model.zz
figpath = simu.system.homepath + 'figures/model/'
figaspect = simu.system.figaspect
## Figure: model_2D.
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111)
if colormap in ['my_seismic_cmap']:
plotopts = {
'vmin': vmin,
'vmax': vmax,
'cmap': my_seismic_cmap(), # my colormap
'extent': (xx[0], xx[-1], zz[-1], 0)
}
elif colormap in ['seismic']:
plotopts = {
'vmin': vmin,
'vmax': vmax,
'cmap': plt.cm.bwr, # my colormap
'extent': (xx[0], xx[-1], zz[-1], 0)
}
else:
plotopts = {
'vmin': vmin,
'vmax': vmax,
'cmap': colormap, # my colormap
'extent': (xx[0], xx[-1], zz[-1], 0)
}
im = ax.imshow(data, **plotopts)
fig.colorbar(im, shrink=0.5, extend='both')
ax.xaxis.set_label_text('Distance (m)')
ax.yaxis.set_label_text('Depth (m)')
ax.set_title(filename)
ax.set_aspect(figaspect)
plt.savefig(figpath + filename + '.png', dpi=300)
plt.close()
### Plot trace (waveform)
def plot_trace(simu, filename, simu_type='syn', suffix='', src_space=5, trace_space=5, scale=1.0, color='r', plot_dx=2000):
''' Plot trace for SU stream data.
'''
# get prepared
nt = simu.model.nt
dt = simu.model.dt
srcn = simu.source.n
srcn = simu.source.n
nproc = simu.system.mpiproc
homepath = simu.system.homepath
figpath = homepath + 'figures/waveform/'
src = list(range(0, srcn))
if src_space > 1:
src = src[0:-1:src_space]
# submit plot
pool = Pool(nproc)
for isrc in range(srcn):
datapath = homepath + 'data/' + simu_type + '/src%d_sg%s.su'%(isrc+1, suffix)
figname = figpath + 'shot%03d-' % isrc + filename + '.png'
pool.apply_async(plot_trace_serial, args=(datapath, figname, trace_space, scale, color,
plot_dx,nt, dt, isrc, 'pressure', ))
time.sleep(0.001)
pool.close()
pool.join()
### Plot trace (waveform), serial version
def plot_trace_serial(datapath, figname, trace_space, scale, color, plot_dx, nt, dt, isrc, comp):
''' Plot trace for SU stream data, serial version.
'''
trace = loadsu(datapath)
trace = add_su_header(trace, nt, dt, isrc, comp)
offset_min = trace[0].stats.distance
offset_max = trace[-1].stats.distance
fig = plt.figure(figsize=(10, 6))
trace[0:-1:trace_space].plot(
# recordstart = t1, recordlength = t2,
type='section', scale=scale, time_down=True,
offset_min=offset_min-200, offset_max=offset_max+200, plot_dx=plot_dx,
fillcolors=(color, ''), morm_method='trace', method='full',
linewidth=0.25, grid_width=0.5, alpha=1.0, dpi=200, fig=fig)
plt.savefig(figname, dpi=200)
plt.close()
### Plot wavelet
def plot_wavelet(simu, wavelet, filename, scale=1.0, color='r', plot_dx=1000, t_end = 1.0):
''' plot wavelet
'''
dt = simu.model.dt
nt = simu.model.nt
srcx = simu.source.xz[:,0]
if t_end > dt * (nt-1):
t_end = dt * (nt-1)
# convert wavelet to the SU format
wavelet = convert_wavelet_su(dt, wavelet, srcx)
figpath = simu.system.homepath + 'figures/'
fig = plt.figure(figsize=(10, 6))
wavelet.plot(type='section', scale=scale, time_down=True, plot_dx=plot_dx,
recordstart = 0., recordlength = t_end,
fillcolors=(color, ''), morm_method='trace', method='full',
linewidth=0.25, grid_width=0.5, alpha=1.0, dpi=200, fig=fig)
plt.savefig(figpath + filename + '.png', dpi=300)
plt.close()
### Plot misfit
def plot_misfit(simu, misfit, mistype):
''' plot misfit
'''
figpath = simu.system.homepath + 'figures/'
fig = plt.figure(figsize=(10, 6))
plt.plot(misfit, marker='o')
plt.xlabel('iteration', fontsize=14)
plt.ylabel('misfit %s' % mistype, fontsize=14)
plt.savefig(figpath + 'misfit_%s.png' % mistype, dpi=300)
plt.close()
### Plot result after one iteration
def plot_inv_scheme(simu, optim, inv_scheme):
''' plot mnewspaper outputs
'''
nx = simu.model.nx
nz = simu.model.nz
it = optim.iter
vpmin = optim.vpmin
vpmax = optim.vpmax
vp = simu.model.vp
grad = inv_scheme['g_now']
dire = inv_scheme['d_now']
grad_caxis = np.max(abs(grad)) * 0.9
dirc_caxis = np.max(abs(dire)) * 0.9
plot_model2D(simu, vp.T, vpmin, vpmax, 'vp-%03d' % it)
plot_model2D(simu, grad.reshape(nx, nz).T, -grad_caxis, grad_caxis, 'grad-%03d' % it, colormap = 'seismic')
plot_model2D(simu, dire.reshape(nx, nz).T, -dirc_caxis, dirc_caxis, 'dire-%03d' % it, colormap = 'seismic')
if optim.iter == 1 :
plot_trace(simu, 'syn-proc-initial', simu_type = 'syn', suffix='_proc', src_space=1, trace_space=5, scale=0.8, color='r')
elif optim.iter == optim.maxiter:
data_misfit = np.loadtxt('./outputs/misfit_data.dat')
data_misfit = data_misfit / data_misfit[0]
plot_misfit(simu, data_misfit, 'data')
plot_trace(simu, 'syn-proc-final', simu_type = 'syn', suffix='_proc', src_space=1, trace_space=5, scale=0.8, color='b')
else:
pass
### Plot result for RTM
def plot_rtm(simu, optim, inv_scheme):
''' plot mnewspaper outputs
'''
nx = simu.model.nx
nz = simu.model.nz
grad = inv_scheme['g_now']
if optim.marine_or_land.lower() in ['marine', 'offshore']:
grad_caxis = np.max(abs(grad)) * 0.1
else:
grad_caxis = np.max(abs(grad)) * 0.4
plot_model2D(simu, grad.reshape(nx, nz).T, -grad_caxis, grad_caxis, 'RTM-image', colormap = 'gray')
# save RTM image
savebinfloat32(simu.system.homepath + 'outputs/gradient/RTM-image.bin', inv_scheme['g_now'])
def my_seismic_cmap():
''' my seismic cmap
'''
cdict = {'red': ((0.0, 0.0, 0.0),
(0.1, 0.5, 0.5),
(0.2, 0.0, 0.0),
(0.4, 0.2, 0.2),
(0.6, 0.0, 0.0),
(0.8, 1.0, 1.0),
(1.0, 1.0, 1.0)),
'green':((0.0, 0.0, 0.0),
(0.1, 0.0, 0.0),
(0.2, 0.0, 0.0),
(0.4, 1.0, 1.0),
(0.6, 1.0, 1.0),
(0.8, 1.0, 1.0),
(1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 0.0),
(0.1, 0.5, 0.5),
(0.2, 1.0, 1.0),
(0.4, 1.0, 1.0),
(0.6, 0.0, 0.0),
(0.8, 0.0, 0.0),
(1.0, 0.0, 0.0))}
my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)
return my_cmap