-
Notifications
You must be signed in to change notification settings - Fork 42
/
tools.py
375 lines (280 loc) · 9.57 KB
/
tools.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
###############################################################################
#
# SWIT: Seismic Waveform Inversion Toolbox
#
# by Haipeng Li at USTC, [email protected]
#
# June, 2021
#
# Tools module, some of codes are from: https://github.com/rmodrak/seisflows
#
###############################################################################
import copy
import json
import os
import numpy as np
import obspy
import scipy.signal as _signal
from obspy.core import UTCDateTime
from scipy.signal import hilbert as analytic
class NumpyEncoder(json.JSONEncoder):
''' Numpy Encoder.
'''
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
class Struct:
''' Json Decoder.
'''
def __init__(self, **entries):
self.__dict__.update(entries)
def saveparjson(simu, optim):
''' Save all the simulation and optimaztion parameters as json files.
'''
path = simu.system.homepath+'parfile/'
savejson(path + 'model.json', simu.model)
savejson(path + 'source.json', simu.source)
savejson(path + 'reciver.json', simu.receiver)
savejson(path + 'system.json', simu.system)
savejson(path + 'optimize.json', optim)
def savejson(filename, obj):
''' Save object using json.
'''
with open(filename, 'w') as file:
json.dump(obj.__dict__, file, sort_keys=True, indent=4, cls=NumpyEncoder)
def loadjson(filename):
''' Load object using json.
'''
with open(filename, 'r') as file:
# parse the json file into diction and the convert into class
obj = Struct(**json.load(file))
# convert to numpy array for convience
for att in obj.__dict__:
setattr(obj, att, np.array(getattr(obj, att)))
return obj
def savebinfloat32(filename, obj):
''' Save bin file (float32)
'''
fp = open(filename, "wb")
np.asarray(obj, dtype=np.float32).tofile(fp)
fp.close()
def loadbinfloat32(filename):
''' Load bin file (float32)
'''
fp = open(filename, 'rb')
obj = np.fromfile(fp, dtype=np.float32)
fp.close()
return np.array(obj)
def loadsu(filename):
''' Reads Seismic Unix files. if nt > 32768, this function will not work.
Find solution in Seisflows/plugins/writer.py or reader.py
'''
traces = obspy.read(filename, format='SU', byteorder='<')
return traces
def savesu(filename, trace):
''' save su file, max_npts = 32767
'''
for tr in trace:
# work around obspy data type conversion
tr.data = tr.data.astype(np.float32)
max_delta = 0.065535
dummy_delta = max_delta
if trace[0].stats.delta > max_delta:
for tr in trace:
tr.stats.delta = dummy_delta
# write data to file
trace.write(filename, format='SU')
def add_su_header(trace, nt, dt, isrc, channel):
''' add su header
'''
# get parameters
irec = 0
for tr in trace:
# add headers
tr.stats.network = 'FWI'
tr.stats.station = '%d'%irec
tr.stats.location = 'Source-%d' % isrc
tr.stats.channel = channel
tr.stats.starttime = UTCDateTime("2021-01-01T00:00:00")
tr.stats.sampling_rate = 1./dt
tr.stats.distance = tr.stats.su.trace_header.group_coordinate_x - tr.stats.su.trace_header.source_coordinate_x
t0 = tr.stats.starttime
tr.trim(starttime=t0, endtime=t0 + dt*(nt-1), pad=True, nearest_sample=True, fill_value=0.)
irec += 1
return trace
def convert_wavelet_su(dt, wavelet, srcx):
''' convert wavelet to SU stream
'''
srcn = np.size(wavelet, 0)
wavelet_su = array2su(srcn, dt, wavelet)
ishot = 0
for iwvlt in wavelet_su:
iwvlt.stats.distance = srcx[ishot]
ishot+=1
return wavelet_su
def get_offset(trace):
''' get offset from trace header
'''
offset = np.zeros(len(trace))
irec = 0
for tr in trace:
offset[irec] = tr.stats.su.trace_header.group_coordinate_x - tr.stats.su.trace_header.source_coordinate_x
irec += 1
return offset
def array2su(recn, dt, traces_array):
''' convert array data to su stream
'''
from obspy.core.util import get_example_file
# get a example stream and trace
filename = get_example_file("1.su_first_trace")
stream = obspy.read(filename, format='SU', byteorder='<')
tr_example = stream[0]
traces = []
if recn > 1:
for irec in range(recn):
tr = copy.deepcopy(tr_example)
tr.data = traces_array[irec,:]
tr.stats.sampling_rate = 1./dt
tr.stats.starttime = UTCDateTime("2021-01-01T00:00:00")
# add trace
traces += [tr]
else: # signle trace
tr = copy.deepcopy(tr_example)
tr.data = traces_array
tr.stats.sampling_rate = 1./dt
tr.stats.starttime = UTCDateTime("2021-01-01T00:00:00")
# add trace
traces += [tr]
# obspy stream
return obspy.Stream(traces = traces)
def su2array(su_data):
''' extract shot gather data into array(srcn, recn, nt)
'''
recn, nt, _ = get_su_parameter(su_data)
data = np.zeros((recn, nt), dtype=np.float32)
for irec in range(recn):
data[irec, :] = su_data[irec].data[:]
return data
def get_su_parameter(trace):
''' get general parameters from hearder.
'''
recn = len(trace)
nt = len(trace[0])
dt = 1. / trace[0].stats.sampling_rate
return recn, nt, dt
def hilbert(w):
''' hilbert transformation
'''
return np.imag(analytic(w))
def array2vector(array):
''' array to vector
'''
nx, nz = array.shape[0:2]
return array.reshape(nx * nz)
def vector2array(vector, nx, nz):
''' vector to array
'''
return vector.reshape((nx, nz))
def gauss2(X, Y, mu, sigma, normalize=True):
''' Evaluates Gaussian over points of X,Y
'''
D = sigma[0, 0]*sigma[1, 1] - sigma[0, 1]*sigma[1, 0]
B = np.linalg.inv(sigma)
X = X - mu[0]
Y = Y - mu[1]
Z = B[0, 0]*X**2. + B[0, 1]*X*Y + B[1, 0]*X*Y + B[1, 1]*Y**2.
Z = np.exp(-0.5*Z)
if normalize:
Z *= (2.*np.pi*np.sqrt(D))**(-1.)
return Z
def smooth2d(Z, span = 10):
''' Smooths values on 2D rectangular grid
'''
import warnings
warnings.filterwarnings('ignore')
Z = np.copy(Z)
x = np.linspace(-2.*span, 2.*span, 2*span + 1)
y = np.linspace(-2.*span, 2.*span, 2*span + 1)
(X, Y) = np.meshgrid(x, y)
mu = np.array([0., 0.])
sigma = np.diag([span, span])**2.
F = gauss2(X, Y, mu, sigma)
F = F/np.sum(F)
W = np.ones(Z.shape)
Z = _signal.convolve2d(Z, F, 'same')
W = _signal.convolve2d(W, F, 'same')
Z = Z/W
return Z
def smooth1d(x,window_len=11,window='hanning'):
''' smooth the data using a window with requested size.
'''
if x.ndim != 1:
raise ValueError("smooth only accepts 1 dimension arrays.")
if x.size < window_len:
raise ValueError("Input vector needs to be bigger than window size.")
if window_len<3:
return x
if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
raise ValueError("Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")
s=np.r_[x[window_len-1:0:-1],x,x[-2:-window_len-1:-1]]
#print(len(s))
if window == 'flat': #moving average
w=np.ones(window_len,'d')
else:
w=eval('np.'+window+'(window_len)')
y=np.convolve(w/w.sum(),s,mode='valid')
return y
def savetxt(filename, data, datatype):
''' save txt file
'''
if os.path.exists(filename):
data = np.append(np.loadtxt(filename), data)
data = [data]
if datatype == 'int':
np.savetxt(filename, np.array(data), fmt='%d', delimiter='\n')
elif datatype == 'float':
np.savetxt(filename, np.array(data), fmt='%.4e', delimiter='\n')
else:
raise ValueError('unsupport data type')
def cleandata(datapath):
''' clean data
'''
if os.listdir(path = datapath) != []:
os.system('rm -r %s' % datapath)
os.system('mkdir %s' % datapath)
def model_misfit(true_model, inv_model):
''' Estimate the model misfit
'''
num = np.linalg.norm(true_model-inv_model, ord=2)
den = np.linalg.norm(true_model, ord=2)
return num/den
def save_inv_scheme(simu, optim, inv_scheme):
''' save current inversion results
'''
nx = simu.model.nx
nz = simu.model.nz
it = optim.iter
homepath = simu.system.homepath
outputpath = homepath + 'outputs'
savebinfloat32(outputpath+'/gradient/grad-%d.bin' % it, inv_scheme['g_now'])
savebinfloat32(outputpath+'/direction/dirc-%d.bin' % it, inv_scheme['d_now'])
savebinfloat32(outputpath+'/velocity/vp-%d.bin' % it, inv_scheme['v_now'])
if optim.iter == 1:
savetxt(outputpath+'/misfit_data.dat', inv_scheme['f_old'], 'float')
savetxt(outputpath+'/misfit_data.dat', inv_scheme['f_now'], 'float')
savetxt(outputpath+'/line_search_step.dat', inv_scheme['ls_step'], 'float')
savetxt(outputpath+'/line_search_iteration.dat', inv_scheme['ls_iter'], 'int')
if 'waveform_misfit' in inv_scheme.keys():
savetxt(outputpath+'/waveform_misfit.dat', inv_scheme['waveform_misfit'], 'float')
def loadfile_gui(filename, nx, nz):
''' load file for GUI
'''
data = np.zeros((nx, nz))
if filename.endswith('bin'):
data = loadbinfloat32(filename).reshape((nx,nz))
elif filename.endswith('dat'):
data = np.loadtxt(filename)
else:
raise ValueError('not supported model file, use: bin, txt, dat files instead')
return data