forked from GuilhermeZakarewicz/DiffraPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffrapy.py
More file actions
1581 lines (1295 loc) · 52.1 KB
/
Diffrapy.py
File metadata and controls
1581 lines (1295 loc) · 52.1 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
import numpy as np
import time
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from scipy.sparse import csr_matrix
from scipy.sparse import lil_matrix
from tqdm import tqdm
from scipy.fft import rfft, rfftfreq, irfft
import cmath
def ricker(nps,fr,dt):
"""
Generate a Ricker wavelet signal (Mexican Hat).
Parameters:
-----------
nps : int
The number of samples in the output signal.
fr : float
The central frequency of the wavelet in Hz.
dt : float
The time step of the output signal in seconds.
Returns:
--------
numpy.ndarray
The Ricker wavelet signal as a 1-D numpy array.
"""
npt = nps * dt
t = np.arange(-npt/2,npt/2,dt)
rick1=(1-t *t * fr**2 *np.pi**2 ) * np.exp(- t**2 * np.pi**2 * fr**2 )
rick=rick1[int(np.round(nps/2))-(int(np.round(1/fr/dt)))+1:nps]
l = len(rick)
if l<nps:
rick2=np.append(rick,np.zeros([1,nps-1]))
l=nps
rick=rick2
return np.array(rick)
def sub2ind(array_shape, rows, cols):
"""
Convert subscripts to linear indices.
Parameters:
-----------
array_shape : tuple of int
The shape of the array to which the indices correspond.
rows : array-like
The row indices.
cols : array-like
The column indices.
Returns:
--------
array-like
The linear indices corresponding to the input subscripts.
Notes:
------
- The input arrays `rows` and `cols` must have the same shape.
- The function assumes 0-based indexing.
- The function uses broadcasting to compute the linear indices.
Examples:
---------
>>> sub2ind((3, 4), [0, 1, 2], [0, 1, 2])
array([0, 5, 10])
>>> sub2ind((3, 4), [2, 1], [3, 2])
array([11, 6])
"""
return cols*array_shape[0] + rows
def buildL2(L,Z,X,ind,z0,x0,z1,x1):
"""
Calculates the lengths of the line segments defined by the endpoints
(z0, x0) and (z1, x1), and updates the values in the specified indices
of the 2D array L.
Given a 2D array `L` representing a grid of line segment lengths,
and indices `Z` and `X` indicating the size of the grid,
this function updates the `L` array with the length of a line segment
defined by two points, specified by `z0`, `x0`, `z1`, and `x1`.
Parameters:
-----------
L : numpy.ndarray of shape (Z*X, Z*X)
2D array containing the pairwise distances between all points in the grid.
Z : int
Number of rows in the grid.
X : int
Number of columns in the grid.
ind : int
Index in the flattened 2D array L where the distance values should be updated.
z0, x0 : float
Coordinates of the starting point of the line segment.
z1, x1 : float
Coordinates of the ending point of the line segment.
Returns:
--------
L : numpy.ndarray of shape (Z*X, Z*X)
The input array L with the updated distance values.
"""
[pz,px,j]=lineseg2(z0,x0,z1,x1)
for i in range(0,j-1):
l = np.linalg.norm([pz[i+1]-pz[i],px[i+1]-px[i]])
#a = np.floor((pz[i+1]+pz[i])/2)-1
#teste***
a = int(np.floor((pz[i+1]+pz[i])/2)-1)
if a == Z:
a = Z-1
elif a==-1:
a = 0
#b = np.floor((px[i+1]+px[i])/2)-1
#teste***
b = int(np.floor((px[i+1]+px[i])/2)-1)
if b == X:
b = X-1
elif b == -1:
b = 0
L[ind,sub2ind([Z,X],a,b)]=l
return L
def subs2(sZ,sX):
"""
Construct a sparse matrix D of size (2sZ-1)*(2sX-1) x (2sZ-2)*(2sX-2),
where each row of D corresponds to a horizontal or vertical line
segment in a grid of size sZ x sX, and each column of D corresponds
to the length of the line segment.
Parameters:
-----------
sZ : int
The number of rows in the grid.
sX : int
The number of columns in the grid.
Returns:
--------
scipy.sparse.lil_matrix: A sparse matrix of size
(2sZ-1)*(2sX-1) x (2sZ-2)*(2sX-2),
where each row corresponds to a horizontal or vertical
line segment in the grid, and each column corresponds to
the length of the line segment.
"""
z = 2*sZ-1
x = 2*sX-1
z1 = z-1
x1 = x-1
#dA = csr_matrix(np.zeros([z*x,z1*x1]))
dA = lil_matrix((z*x,z1*x1))
for j in range(0,z):
for i in range(0,x):
dA = buildL2(dA,z1,x1,sub2ind([z,x],j,i),sZ,sX,j,i)
return dA
def lineseg2(z0,x0,z1,x1):
"""
Compute a line segment between two points in 2D space using Bresenham's algorithm.
Parameters:
-----------
z0 : float
The z-coordinate of the starting point.
x0 : float
The x-coordinate of the starting point.
z1 : float
The z-coordinate of the end point.
x1 : float
The x-coordinate of the end point.
Returns:
--------
[pz,px,j] : list
A list containing three items:
- `pz`: A sorted array of z-coordinates along the line segment.
- `px`: A sorted array of x-coordinates along the line segment.
- `j`: The number of coordinates in the line segment.
Notes:
------
- This function uses Bresenham's algorithm to compute a line segment between two points in 2D space.
- The line segment is returned as two arrays, `pz` and `px`, representing the z- and x-coordinates along the segment, respectively.
- The length of the line segment is returned as `j`.
- The function assumes that the input coordinates are in increasing order (i.e., z0 <= z1 and x0 <= x1).
Examples:
---------
>>> lineseg2(0, 0, 3, 3)
[[0., 1., 1., 2., 2., 3., 3., 4.],[0., 1., 1., 2., 2., 3., 3., 4.]),8]
"""
z1=z1+1
x1=x1+1
dz = (z1-z0)
dx = (x1-x0)
sgnz = np.sign(dz)
sgnx = np.sign(dx)
pz=[]
px=[]
pz.append(z0)
px.append(x0)
j = 2
if sgnz!=0:
zrange = np.arange(z0+sgnz,z1,sgnz)
for z in zrange:
pz.append(z)
px.append(x0 + (z-z0)*dx/dz)
j = j+1
if sgnx!=0:
xrange = np.arange(x0+sgnx,x1,sgnx)
for x in xrange:
px.append(x)
pz.append(z0+(x-x0)*dz/dx)
j = j+1
pz.append(z1)
px.append(x1)
px = np.sort(px)
pz = np.sort(pz)
if sgnx==-sgnz:
px=np.flip(px)
return [pz,px,j]
def Mray(SW,SP,DX):
"""
Compute the travel time of a seismic ray with respect to a source position.
Given a slowness model `SW`, a starting point `SP` and a step size `DX`,
this function traces a seismic ray from `SP` through `SW` and computes
the travel time of the ray at each point. It is based on the shortest path method.
The output is a table `Ttable` of travel times that correspond to the traced
ray path.
Parameters:
-----------
SW : numpy.ndarray
2D array of slowness values representing the slowness model (1/velocity model).
SP : tuple or list
Tuple or list of two integers representing the starting point of the ray [0,src].
DX : float
Step size for tracing the ray through the slowness model (model's discretization).
Grid spacing along the x-axis.
Returns:
--------
Ttable : numpy.ndarray
2D array of travel times corresponding to the traced ray path through the
slowness model, taking into account the travel time of the ray with respect
to the surface normal at each point. It has the same dimensions [ntr,nz] of
the SW grid.
Notes:
------
The algorithm works by iteratively propagating the ray through the slowness model
in small steps of size `DX` and computing the travel time of the ray at each point
(grid node). All the visited nodes record the travel time value. Among all the time
values, the function decide which is the minimum. Then, it uses the selected node as
a secondary source (following the Huygen's principle) and repeat the process until all
interested nodes are visited.
References:
-----------
[1] Moser, T.J. (1991). Shortest path calculation of seismic rays: Geophysics,
56, 59–67.
[2] Shearer, P.M. (2009). Introduction to Seismology, Second Edition. Cambridge
University Press.
"""
[Z,X]=SW.shape
ddef = 10000
delt = np.max(SW.flatten())
sZ = 7; sX = 7;
dA = subs2(sZ,sX)
ZZ = Z+2*sZ-1
XX = X+2*sX-1
T = np.ones([ZZ,XX])*ddef
mark = np.ones([ZZ,XX])*ddef
Z2 = Z + 2*sZ - 2
X2 = X+2*sX - 2
S = np.ones([Z2,X2])
Z1 = np.arange(sZ-1,Z+sZ)
X1 = np.arange(sX-1,X+sX)
mark[np.ix_(Z1.flatten(),X1.flatten())] = 0
Z2 = np.arange(sZ-1,Z+sZ-1)
X2 = np.arange(sX-1,X+sX-1)
S[np.ix_(Z2.flatten(),X2.flatten())] = SW
S[np.ix_([Z+sZ-1],X2.flatten())] = 2*S[np.ix_([Z+sZ-2],X2.flatten())] - S[np.ix_([Z+sZ-3],X2.flatten())]
S[np.ix_(Z2.flatten(),[X+sX-1])] = 2*S[np.ix_(Z2.flatten(),[X+sX-2])] - S[np.ix_(Z2.flatten(),[X+sX-3])]
S[np.ix_([Z+sZ-1],[X+sX-1])] = 2*S[np.ix_([Z+sZ-2],[X+sX-2])] - S[np.ix_([Z+sZ-3],[X+sX-3])]
dz = -sZ+1
dx = -sX+1
SP = np.array(SP)
z = SP[0]
x = SP[1]
z = z+sZ-1
x = x+sX-1
T[z,x] = 0
mark[z,x] = ddef
a = 2*sZ-1
b = 2*sX-1
aa = np.arange(-sZ+1,sZ)
bb = np.arange(-sX+1,sX)
aas = np.arange(-sZ+1,sZ-1)
bs = np.arange(-sX+1,sX-1)
AS = S[np.ix_((aas+z).flatten(),(bs+x).flatten())]
aaa = aa + z
bbb = bb + x
TT = T[np.ix_(aaa.flatten(),bbb.flatten())]
K = dA*AS.flatten()+T[z,x]
KK=np.reshape(K,[13,13])
BB=np.minimum(KK,TT)
T[np.ix_(aaa.flatten(),bbb.flatten())] = np.minimum(np.reshape(dA*AS.flatten('F')+T[z,x],[a,b]),TT)
maxt = np.max(np.max(T[z-1:z+1,x-1:x+1]))
while 1:
H = np.argwhere(T + mark <= maxt + delt)
hz = H[:,0]
hx = H[:,1]
hsz = len(hz)
for ii in range(0,hsz):
z = hz[ii]
x = hx[ii]
maxt = np.max([maxt,T[z,x]])
mark[z,x] = ddef
AS = S[np.ix_((aas + z).flatten(), (bs + x).flatten())]
aaa = aa + z
bbb = bb + x
TT = T[np.ix_(aaa.flatten(),bbb.flatten())]
T[np.ix_(aaa.flatten(),bbb.flatten())] = np.minimum(np.reshape(dA*AS.flatten('F')+T[z,x],[a,b]),TT)
if mark[np.ix_(Z2.flatten(),X2.flatten())].all():
break
Ttable = T[np.ix_(Z2.flatten(),X2.flatten())]*DX
return Ttable
def raymodel3(SW,dx,nx,filename):
"""
Computes travel times of seismic waves through a 2D subsurface model
using the MRay function for ray tracing. Performs seismic ray tracing
by calling the Mray function to compute travel times for each source-
receiver pair and then saves the resulting travel time data to a binary
file.
Parameters:
-----------
SW : numpy.ndarray
A 2D array representing the subsurface model.
dx : float
Grid spacing along both x and y axes.
nx : int
Number of grid points along the x axis.
filename : str
Name of the file to save the computed travel times.
Returns:
--------
traveltimesrc : list
A list containing the travel times of all the seismic waves
from the source point to all the grid points in the model.
Each element in the list is a 2D array of shape [nx,nz],
representing the travel times from a single source point.
It is saved in the binary file `filename`.
"""
DX = dx
traveltimesrc=[]
sx=np.arange(0,nx)*DX
for ixsrc in tqdm(range(0,nx)):
SP = [0,ixsrc]
Ttable = Mray(SW,SP,DX)
traveltimesrc.append(Ttable[:,:])
with open(filename, 'wb') as f:
np.save(f, traveltimesrc)
return traveltimesrc
def kirchhoffModeling(nsx,ngx,dsx,nx,nt,dt,TTh,R,W,filename):
"""
Computes synthetic seismic common-shot gathers by Kirchhoff modeling.
The method consists of computing the travel times of seismic waves from
a source point to all the grid points in the model using a ray tracing
algorithm, and then calculating the seismic amplitudes at each receiver
location by summing the contributions of all the rays that reach that point.
The amplitudes of the seismic waves are calculated using the Kirchhoff
integral, which is an integral equation that relates the incident wave
field to the scattered wave field at a given receiver location.
Parameters:
-----------
nsx : int
Number of sources (shots).
ngx : int
Number of receivers (traces).
dsx : int
Step size for sources along the x-axis.
nx : int
Number of grid points along the x-axis.
nt : int
Number of time samples.
dt : float
Time step.
TTh : numpy.ndarray
A 3D array of shape [nsx,nz,nx] containing the travel times of
all the seismic waves from all the source points to all the grid points
in the model. It is calculated before by the `raymodel3` function.
R : numpy.ndarray
A 2D array of shape [nz,nx] representing the reflectivity coefficients
of the subsurface model.
W : numpy.ndarray
A 1D array of length nt representing the temporal source waveform.
It is a Ricker wavelet calculated by the `ricker` function.
filename : str
Base name of the files to save the computed common-shot gathers. One
file will be saved for each source point, with a suffix indicating
the source index.
Returns:
--------
files : list of numpy.ndarray
A list of length nsx containing the common-shot gathers computed for
each source point. Each element in the list is a 2D array of shape
[nt-2,ngx], representing the seismic trace data for a given source
(sx).
"""
nsx=nx
ngx=nx
#Loop over shots
for isx in tqdm(range(0,nsx,dsx)):
D = np.zeros([nt,ngx])
TSX = (TTh[isx,:,:]/dt+1).astype(int) # Traveltime (indexes)
# Loop Over Traces
for gx in range(0,ngx):
TXG=(TTh[gx,:,:]/dt+1).astype(int) # Traveltime in heterogeneous medium (indexes)
#Loop over time sample in a trace
for t in range(0,nt):
M=W[t-(TSX+TXG)+nt+1]*R
#D[t,gx]=np.sum(M.flatten('F'));
D[t,gx]=np.sum(M.flatten())
gather1=np.diff(D[:,:],n=2,axis=0)
file = str(filename)+"_{}".format(isx)
with open(file, "wb") as f:
np.save(f, gather1)
files = []
for i in range(0,nsx,dsx):
file = str(filename)+"_{}".format(isx)
with open(file, 'rb') as file:
(gather) = np.load(file)
files.append(gather)
return files
def slant(p,tau,data1,dx,dz,z_ini,x_ini):
"""
Computes the slant stack value for a given tau-p.
Parameters:
-----------
p : float
Slope of the line (angular coefficient).
tau : float
Linear coefficient of the line.
data1 : numpy.ndarray
A 2D array of shape [nz,nx] representing the reflectivity model
of the subsurface.
dx : float
Sampling interval in the x direction.
dz : float
Sampling interval in the z direction.
z_ini : int
Index of the first sample of the gather in the z direction.
x_ini : int
Index of the first sample of the gather in the x direction.
Returns:
--------
s : float
Coherence value (semblance).
x_grid : numpy.ndarray
Indices of the samples in the x direction.
z_grid : numpy.ndarray
Indices of the samples in the z direction.
Notes:
------
- This function calculates the coherence of a line parameterized by (tau,p)
in relation with the reflectivity model data1.
- It is possible to use (x_grid,y_grid) to plot the defined line over the
model and visually check the funcionality of the function.
References:
-----------
[1] Neidell, N. S. (1997). Perceptions in seismic imaging Part 2: Reflective and
diffractive contributions to seismic imaging. The Leading Edge 16:8, 1121-1123
"""
[nz1,ntr1]=data1.shape
dataC = data1.copy()
x = np.arange(x_ini*dx,(x_ini+ntr1)*dx,dx)
z = tau + p*x
z_grid = np.int64(np.round(z/dz)-z_ini)
x_grid = np.int64((np.round(x/dx))-x_ini)
s_n = 0
s_d = 0
plot = "no" #Only change for testing and debugging
for k in range(ntr1):
if (z_grid[k]<nz1) & (z_grid[k]>=0):
s_n += dataC[z_grid[k],x_grid[k]]
s_d += dataC[z_grid[k],x_grid[k]]**2
if plot == "yes":
dataCC=data1.copy()
dataCC[z_grid[k],x_grid[k]] = .5
plt.imshow(dataC, extent=[0, ntr1*dx,nz1*dt, 0])
plt.imshow(dataCC, extent=[0, ntr1*dx,nz1*dt, 0])
plt.axis('auto')
plt.plot(x,z,'r',label="t (x) = tau + p*x \nt (x) = %s + %s*x" % (tau,p))
plt.legend()
plt.colorbar()
plt.show()
if s_d == 0:
s=1e-16 #avoid nan's
else:
s = s_n**2/ntr1/s_d
return s, x_grid, z_grid
def slant_local(data1,x_ini,z_ini,dx,dz,x0,z0,p):
"""
Calculates the slant stack measure at a local location specified by the coordinates x0 and z0
(image point), for a given slope value p.
Parameters:
-----------
data1 : numpy.ndarray of shape (nz1, ntr1)
A 2D array of shape [nz,nx] representing the reflectivity model
of the subsurface.
x_ini : float
The x coordinate of the first trace in the seismic data.
z_ini : float
The z coordinate of the first sample in the seismic data.
dx : float
The x sampling interval in the seismic data.
dz : float
The z sampling interval in the seismic data.
x0 : float
The x coordinate of the central point of the slant stack line.
z0 : float
The z coordinate of the central point of the slant stack line.
p : float
The slowness value used to calculate the slant stack measure.
Returns:
--------
s : float
Coherence value (semblance).
x_grid : numpy.ndarray of shape (ntr1,)
Indices of the samples in the x direction.
z_grid : numpy.ndarray of shape (ntr1,)
Indices of the samples in the z direction.
Notes:
------
- This function calculates the coherence of a line of slope defined by `p`
in relation with the reflectivity model data1 for a given image point (x0,z0).
- It differs from the `slant` function only in the way each one defines the line.
In this case, it is not necessary to input the intercept time, only the image point
(x0,z0) and the slope (p).
- It is possible to use (x_grid,y_grid) to plot the defined line over the
model and visually check the funcionality of the function.
References:
-----------
[1] Neidell, N. S. (1997). Perceptions in seismic imaging Part 2: Reflective and
diffractive contributions to seismic imaging. The Leading Edge 16:8, 1121-1123
"""
#[nz1,ntr1]=data1.shape
#dataC = data1.copy()
#x = np.arange(x_ini,(x_ini+ntr1),1)
#z = p*(x-x0) + z0
#x_grid = np.int64((np.round(x))-x_ini)
#z_grid = np.int64(np.round(z)-z_ini)
[nz1,ntr1]=data1.shape
dataC = data1.copy()
x = np.arange(x_ini*dx,(x_ini+ntr1)*dx,dx) #now considering the dx,dz information
z = p*(x-(x0*dx))+ z0*dz
z_grid = np.int64(np.round(z/dz)-z_ini)
x_grid = np.int64((np.round(x/dx))-x_ini)
s_n = 0
s_d = 0
plot = "no" #WARNING! Change only for debugging!
if plot=="yes":
plt.imshow(dataC, aspect="auto")
print(f"shape da matriz = {data1.shape}")
print(f"ponto imagem = [{z0},{x0}]")
print(f"p = {p}")
print(f"x_grid = {x_grid}")
print(f"z_grid = {z_grid}")
for k in range(ntr1):
if (x_grid[k]<ntr1) and (z_grid[k]>=0 and z_grid[k]<nz1):
s_n += dataC[z_grid[k],x_grid[k]]
s_d += dataC[z_grid[k],x_grid[k]]**2
if plot=="yes":
plt.plot(x_grid[k],z_grid[k],"r.")
if s_d == 0:
s=1e-16 #avoid nan's
else:
s = s_n**2/ntr1/s_d
if plot=="yes":
plt.show()
print(f"s = {s}")
return s, x_grid, z_grid
def slant_local_p(data1,x_ini,z_ini,dx,dz,x0,z0,pmin,pmax,dp):
"""
Calculates the maximum coherence value and its corresponding slope for a given
data and a range of slopes. Uses the `slant_local` function to calculate the
coherence values over a range of slopes.
Parameters:
-----------
data1 : numpy.ndarray of shape (nz1, ntr1)
A 2D array of shape [nz,nx] representing the reflectivity model
of the subsurface.
x_ini : float
The x coordinate of the first trace in the seismic data.
z_ini : float
The z coordinate of the first sample in the seismic data.
dx : float
The x sampling interval in the seismic data.
dz : float
The z sampling interval in the seismic data.
x0 : float
The x coordinate of the central point of the slant stack line.
z0 : float
The z coordinate of the central point of the slant stack line.
pmin, pmax : float
Minimum and maximum values of the slopes to consider.
dp : float
Step size for the slope range.
Returns:
--------
Smax : float
Maximum coherence value obtained for the given slope range.
pmax : float
Slope corresponding to the maximum slant stack value.
"""
ps = np.arange(pmin,pmax,dp)
Smax = 0
pmax = 0
for ip in range(len(ps)):
S,x,z = slant_local(data1,x_ini,z_ini,dx,dz,x0,z0,ps[ip])
if S>Smax:
Smax = S
pmax = ps[ip]
return Smax,pmax
def local_windowS(data1,xwin,zwin,x_ini,z_ini,dx,dz,x0,z0,pmin,pmax,dp):
"""
Computes the slant stack maximum value and corresponding p value for
a local window centered at (x_ini, z_ini).
The window is defined as data1[z_ini:(z_ini+zwin),x_ini:(x_ini+xwin)].
Auxiliary function used by `local_window`.
Parameters:
-----------
data1 : numpy.ndarray of shape (nz1, ntr1)
A 2D array of shape [nz,nx] representing the reflectivity model
of the subsurface.
xwin : int
The width of the window in number of samples along the x-axis.
zwin : int
The height of the window in number of samples along the z-axis.
x_ini : float
The x coordinate of the first trace in the seismic data.
z_ini : float
The z coordinate of the first sample in the seismic data.
dx : float
The x sampling interval in the seismic data.
dz : float
The z sampling interval in the seismic data.
x0 : float
The x coordinate of the central point of the slant stack line.
z0 : float
The z coordinate of the central point of the slant stack line.
pmin, pmax : float
Minimum and maximum values of the slopes to consider.
dp : float
Step size for the slope range.
Returns:
--------
s : float
The maximum coherence value.
pm : float
Slope corresponding to the maximum slant stack value.
"""
W=data1[z_ini:(z_ini+zwin),x_ini:(x_ini+xwin)]
s, pm = slant_local_p(W,x_ini,z_ini,dx,dz,x0,z0,pmin,pmax,dp)
return s, pm
def local_window(data1,xwin,zwin,x_ini,z_ini,dx,dz,pmin,pmax,dp):
"""
Compute local slant stack for a given model.
Parameters:
-----------
data1 : numpy.ndarray of shape (nz1, ntr1)
A 2D array of shape [nz,nx] representing the reflectivity model
of the subsurface.
xwin : int
The width of the window in number of samples along the x-axis.
zwin : int
The height of the window in number of samples along the z-axis.
x_ini : float
The x coordinate of the first trace in the seismic data.
z_ini : float
The z coordinate of the first sample in the seismic data.
dx : float
The x sampling interval in the seismic data.
dz : float
The z sampling interval in the seismic data.
x0 : float
The x coordinate of the central point of the slant stack line.
z0 : float
The z coordinate of the central point of the slant stack line.
pmin, pmax : float
Minimum and maximum values of the slopes to consider.
dp : float
Step size for the slope range.
Returns:
--------
p_max : ndarray of shape (ntr1, nz1); matrix
Maximum slope values computed from local windowed slant stacks.
s_max : ndarray of shape (ntr1, nz1); matrix
Maximum correlation coefficients computed from local windowed slant stacks.
Notes:
------
- The `pmax` matrix is used for the calculation of the horizontal and vertical components
of reflector normal, necessary for the anti-stationary phase filter w(s,x,r) construction.
"""
p = np.arange(pmin,pmax,dp)
[ntr1,nz1] = data1.T.shape
p_max = np.zeros(data1.shape)
s_max = np.zeros(data1.shape)
for i in tqdm(range(0,nz1)): #eixo t
for j in range(0,ntr1): #eixo x
#Caso A
if (i-np.int64(zwin/2))<=0:
z_ini=0 #t_ini em grid
zwinA=np.int64(zwin/2)+i
#Caso A1
if (j-np.int64(xwin/2))<=0:
#print("1")
x_ini=0
xwinA=np.int64(xwin/2)+j
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
#Caso A2
elif (j-np.int64(xwin/2))>0 and (j+np.int64(xwin/2))<ntr1:
#print("2")
x_ini=j-np.int64(xwin/2)
xwinA=xwin
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
# Caso A3
elif (j+np.int64(xwin/2))>=ntr1:
#print("3")
x_ini=j-np.int64(xwin/2)
xwinA= xwin - (j + np.int64(xwin/2) - ntr1)
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
#Caso B
elif (i+np.int64(zwin/2))>=nz1:
z_ini=(i-np.int64(zwin/2))
zwinA=zwin - (i + np.int64(zwin/2) - nz1)
tau = np.arange(z_ini*dz, (z_ini+zwinA)*dz,dz)
#Caso B1
if (j-np.int64(xwin/2))<=0:
#print("1")
x_ini=0
xwinA=np.int64(xwin/2)+j
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
#Caso B2
elif (j-np.int64(xwin/2))>0 and (j+np.int64(xwin/2))<ntr1:
#print("2")
x_ini=j-np.int64(xwin/2)
xwinA= xwin
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
# Caso B3
elif (j+np.int64(xwin/2))>=ntr1:
#print("3")
x_ini=j-np.int64(xwin/2)
xwinA= xwin - (j + np.int64(xwin/2) - ntr1)
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
# Caso C
elif (i-np.int64(zwin/2))>0 and (i+np.int64(zwin/2))<nz1:
z_ini=(i-np.int64(zwin/2))
zwinA = zwin
tau = np.arange(z_ini*dz, (z_ini+zwinA)*dz,dz)
#Caso C1
if (j-np.int64(xwin/2))<=0:
#print("1")
x_ini=0
xwinA=np.int64(xwin/2)+j
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
#Caso C2
elif (j-np.int64(xwin/2))>0 and (j+np.int64(xwin/2))<ntr1:
#print("2")
x_ini=j-np.int64(xwin/2)
xwinA= xwin
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
# Caso C3
elif (j+np.int64(xwin/2))>=ntr1:
#print("3")
x_ini=j-np.int64(xwin/2)
xwinA= xwin - (j + np.int64(xwin/2) - ntr1)
smaxS,pmaxS = local_windowS(data1,xwinA,zwinA,x_ini,z_ini,dx,dz,j,i,pmin,pmax,dp)
p_max[i,j]=pmaxS
s_max[i,j]=smaxS
return p_max,s_max
def taper(ntr,nz,app,isx,igx):
"""
Applies a Hanning taper to the seismic data to reduce edge effects.
This function computes a taper window for a seismic trace. The taper
window is used to gradually reduce the amplitude of the trace towards
its beginning and end, in order to avoid edge effects when processing
the data.
The function uses the np.hanning() function to compute a Hanning window
of length 2*app. The window is centered around the midpoint of the
source and receiver traces (cmp), and is then applied to the appropriate
section of the ar array based on the position of the midpoint and the
length of the trace.
Parameters:
-----------
ntr : int
Number of traces in the seismic data.
nz : int
Number of grid points in the z-axis.
app : int
Number of samples to apply the taper at each edge of the trace.
isx : int
Index of the current source in the survey.
igx : int
Index of the current trace in the seismic data.
Returns:
--------
ar : numpy.ndarray
A 2D array of shape [nz,ntr] representing the tapered seismic data.
"""
ar = np.zeros([nz,ntr])
cmp = int((isx+igx)/2)
window = np.hanning(2*app)
if (cmp-app)<0:
if (cmp+app)>ntr:
ntr_2 = int(ntr/2)
lw_2 = int(len(window)/2)
ar[:,:] = window[(lw_2 - ntr_2):(lw_2 + ntr_2)]
else:
ar[:,0:(cmp+app)] = window[abs(cmp-app):]
elif (cmp+app)>ntr:
ar[:,(abs(cmp-app)):] = window[0:(ntr - abs(cmp-app))]
else:
ar[:,(cmp-app):(cmp+app)] = window
return ar
def peso(TTh,dt,X,Y,igx,isx):
"""
Computes the Kirchhoff weighting function w(s,r,x) for a given source-
receiver pair in a 2D subsurface model, also called imaging condition
or anti-stationary phase filter.
Parameters:
-----------
TTh : numpy.ndarray
A 3D array of shape [nsx,nz,nx] containing the travel times of
all the seismic waves from all the source points to all the grid points