-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem2d.py
executable file
·1389 lines (1082 loc) · 41.8 KB
/
sem2d.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
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
#!/usr/bin/env python
"""
@Author :: Flomin T.
Class for manipulating SEM2DPACK output files.
see user manual for more about SEM2DPACK code.
"""
import sys
import numpy as np
import time
import matplotlib.pyplot as plt
import glob
from scipy.interpolate import griddata as gd
import scipy.signal as sp
import matplotlib.animation as anim
import multiprocessing as mp
import os
import ipdb as db
import wiggle as wig
from filters import bandpass
import pandas as pd
from stockwell import st
import warnings
from util_sys import *
import fcode as fc
from dtw import accelerated_dtw as dtw
from scipy.spatial.distance import euclidean
from obspy.signal.konnoohmachismoothing import konno_ohmachi_smoothing as konno
from mpl_toolkits.axes_grid1 import make_axes_locatable
warnings.filterwarnings("ignore",category=DeprecationWarning)
class sem2dpack(object):
"""
Class to create fortran code (SEM2DPACK) simulation objects.
It defines a set of instance attribute and instance methods for the post-processing and visualizing
each simulation outputs.
Attributes ::
-------------
-- directory : the simulation directory
-- mdict : dictionary containing spectral element grid infos
-- dt : simulation time step
-- npts : Number of points in record, npts * dt gives
the simulation time
-- nsta : number of reciever stations
-- velocity : velocity traces
-- tvec : time vector (0:dt:npts*dt)
-- fmax : maximum frequency of simulation
-- tf : transfer function in case of sedimentary basins
-- fft_freq : frequecy vector
-- rcoord : reciever stations coordinates
Instance methods (short description) ::
----------------
Static methods ::
-----------------
"""
def __init__(self,directory,freqs=[0.1,12],component='x'):
self.directory = directory
self.mdict = {}
self.dt = 0.0
self.npts = 0
self.nsta = 0
self.tvec = np.array([])
self.fmax = 0.0
self.interpolated_tf = np.array([])
self.interpolated_f = np.array([])
self.rcoord = np.array([])
self.x_interp = np.array([])
self.vs_int = np.array([])
self._component = ''
self._freqs = freqs
self._component = component
try:
self.__read_Specgrid()
self.__read_header()
except:
print(self.directory)
raise Exception('Not a sem2dpack simulation directory')
def __read_Specgrid(self):
"""
Read the properties of the spectral element grid and store them in a dictionary "self.mdict"
"""
#read mesh information
filename = self.directory + 'grid_sem2d.hdr'
g=np.genfromtxt(filename,dtype=int)
nel,npgeo,ngnod,npt,ngll = g[1,:]
#read spectral element grid coordinates
filename = self.directory + 'coord_sem2d.tab'
g=np.genfromtxt(filename,skip_header=1)
coord = g[:,1:]
#read ibool file
filename = self.directory + 'ibool_sem2d.dat'
with open(filename,'rb') as f:
ibool=np.fromfile(f,np.int32).reshape((ngll,ngll,nel),order='F')
#read gll information
filename = self.directory + 'gll_sem2d.tab'
g=np.genfromtxt(filename)
x, w, h = g[0,:], g[1,:], g[2:,:]
self.mdict ={"nel" : nel, # Number of elements in mesh
"npgeo" : npgeo, # Number of global nodes
"ngnod" : ngnod, # Polynomial order
"npt" : npt, # Number of points in spectral mesh
"ngll" : ngll, # Number of gll points
"coord" : coord, # Coordinates of all global nodes points
"ibool" : ibool, # Array for global to local mapping (node number of each element [ngll,ngll,nel])
"x" : x, # GLL coordinates on the reference element [-1,1]
"w" : w, # weights of GLL polynomials
"h" : h, # derivatives of Lagrange polynomials
}
def __read_header(self):
"""
Read seismic header file of SEM2DPACK simulation.
The method broadcasts the simulation parameters and
receiver coordinates instances.
Upon exit, the method updates the following instances:
self.dt : simulation time step
self.npts : number of points in recoord
self.nsta : number of receivers in the simulation
self.rcoord : coordinates of receivers
self.x_rcoord : if extra receivers are present (e.g receivers which store strain & strain)
"""
filename = self.directory + 'SeisHeader_sem2d.hdr'
try :
f = open(filename, 'r')
except:
msg = 'No Header file <SeisHeader_sem2d.hdr> in directory'
print(msg)
answer = input("Do you want to continue [Y/N] : ")
if answer.upper() == 'Y':
return
else:
sys.exit()
f.readline()
string = f.readline()
header_line = string.rstrip(" ").split()
self.dt = float(header_line[0])
self.npts = int(header_line[1])
self.nsta = int(header_line[2])
# Seismos
f.readline()
self.rcoord = np.zeros((self.nsta,2))
for reciever in np.arange(self.nsta):
string = f.readline()
reciever_line = string.rstrip(" ").split()
# x-coord
self.rcoord[reciever,0] = float(reciever_line[0])
# z-coord
self.rcoord[reciever,1] = float(reciever_line[1])
#extra station
try:
xsta = int(f.readline())
self.xsta = xsta
f.readline()
self.x_rcoord = np.zeros((xsta,2))
for ex_reciever in range(xsta):
xtra = f.readline()
x_reciever_line = xtra.rstrip(" ").split()
self.x_rcoord[ex_reciever,0] = float(x_reciever_line[0])
self.x_rcoord[ex_reciever,1] = float(x_reciever_line[0])
except :
print("No Extra recievers")
self.x_rcoord = None
f.close()
return self.dt, self.npts, self.nsta, self.rcoord, self.x_rcoord
@staticmethod
def readField(fname):
"""
Staticmethod which reads the snapshots binary files of a simulation.
"""
with open(fname,'rb') as f:
field = np.fromfile(f,np.float32)
return field
def read_seismo(self,filter_s=False,freqs=None,scale=None,verbose=False):
"""
Reads the seismograms or traces the simulations
Parameters ::
-------------
-- filter_s [dtype:bool] : if True seismograms are bandpassed filtered between freqs range
-- freqs [dytpe:list] : limits of frequency range for filtering.
Upon exit, the method updates the following instances:
-- self.velocity :: velocity traces
-- self.tvec :: time vector
"""
if self._component == 'x': # PSV case
filename_x = self.directory + 'Ux_sem2d.dat'
filename_z = self.directory + 'Uz_sem2d.dat'
try :
with open(filename_x, 'rb') as fid:
veloc_array_x = np.fromfile(fid,np.float32)
with open(filename_z, 'rb') as fid:
veloc_array_z = np.fromfile(fid,np.float32)
except :
raise Exception('No velocity file in {:s}'.format(self.directory))
l = len(veloc_array_x)
self.velocity = np.zeros((self.npts,self.nsta))
self.velocity_z = np.zeros((self.npts,self.nsta))
for i in range(int(l/self.nsta)):
limit1 = i*self.nsta
limit2 = (i+1)*self.nsta
self.velocity[i,:] = veloc_array_x[limit1:limit2]
self.velocity_z[i,:] = veloc_array_z[limit1:limit2]
self.tvec = np.arange(self.npts) * self.dt
if filter_s :
freqs = freqs or self._freqs
if verbose:
print('*** Filtering between {} and {} Hz'.format(freqs[0],freqs[1]))
self.velocity = self.filter_seismo(self.velocity,freqs=freqs,ftype='bandpass',dt=self.dt)
self.velocity_z = self.filter_seismo(self.velocity_z,freqs=freqs,ftype='bandpass',dt=self.dt)
if scale:
self.velocity *= scale
self.velocity_z *= scale
return self.velocity
elif self._component == 'y': # SH case
filename_y = self.directory + 'Uy_sem2d.dat'
try :
with open(filename_y, 'rb') as fid:
veloc_array = np.fromfile(fid,np.float32)
except :
raise Exception('No velocity file in {:s}'.format(self.directory))
l = len(veloc_array)
self.velocity = np.zeros((self.npts,self.nsta))
for i in range(int(l/self.nsta)):
limit1 = i*self.nsta
limit2 = (i+1)*self.nsta
self.velocity[i,:] = veloc_array[limit1:limit2]
self.tvec = np.arange(self.npts) * self.dt
if filter_s :
freqs = freqs or self._freqs
if verbose:
print('*** Filtering between {} and {} Hz'.format(freqs[0],freqs[1]))
self.velocity = self.filter_seismo(self.velocity,freqs=freqs,ftype='bandpass',dt=self.dt)
if scale:
self.velocity *= scale
return self.velocity
if scale:
self.velocity *= scale
return self.velocity
def read_stress_strain(self):
"""
Reads stress and strain information.
Defines the followinginstances:
-- self.stress
-- self.strain
"""
stress_file = self.directory + 'EXTRA_stress_sem2d.dat'
strain_file = self.directory + 'EXTRA_strain_sem2d.dat'
if os.path.isfile(stress_file) :
with open(stress_file, 'rb') as sid :
stress = np.fromfile(sid,np.float32)
with open(strain_file, 'rb') as sid :
strain = np.fromfile(sid,np.float32)
l = len(stress)
assert self.npts == (l/self.xsta)
self.stress = np.zeros( (self.npts,self.xsta) )
self.strain = np.zeros( (self.npts,self.xsta) )
for i in range(int(l/self.xsta)):
limit1 = i * self.xsta
limit2 = (i+1) * self.xsta
self.strain[i,:] = strain[limit1:limit2]
self.stress[i,:] = stress[limit1:limit2]
return self.stress, self.strain
else:
print("No stress and strain files were found")
def read_iai_param(self):
"""
Reads shear modolus, deviatoric stress, and S paramaters of the
Iai model.
Defines the following instances :
-- self.shear_mod
-- self.deviatoric_stress
-- self.s_param
"""
shear_mod_file = self.directory + 'EXTRA_current_shear_modulus_sem2d.dat'
dev_stress_file = self.directory + 'EXTRA_deviatoric_stress_sem2d.dat'
s_param_file = self.directory + 'EXTRA_S_parameter_sem2d.dat'
if os.path.isfile(shear_mod_file):
with open(shear_mod_file, 'rb') as sid:
shear_mod = np.fromfile(sid,np.float32)
with open(dev_stress_file, 'rb') as sid:
deviatoric_stress = np.fromfile(sid, np.float32)
with open(s_param_file, 'rb') as sid:
s_param = np.fromfile(sid,np.float32)
l = len(shear_mod)
assert self.npts == (l/self.xsta), 'Recording error'
self.shear_mod = np.zeros( (self.npts,self.xsta) )
self.deviatoric_stress = np.zeros( (self.npts,self.xsta) )
self.s_param = np.zeros( (self.npts,self.xsta) )
for i in range( int(l/self.xsta) ):
limit1 = i * self.xsta
limit2 = (i+1) * self.xsta
self.shear_mod[i,:] = shear_mod[limit1:limit2]
self.deviatoric_stress[i,:] = deviatoric_stress[limit1:limit2]
self.s_param[i,:] = s_param[limit1:limit2]
else :
print('No Iai model parameter files found')
def decimate_sig(self,q=4,filter_s=True):
"""
Decimate velocity traces by a factor q,
"""
self.read_seismo(filter_s=filter_s)
fe = 1/self.dt
deci_fe = fe / q
deci_dt = 1/deci_fe
self.decimated_veloc = sp.decimate(self.velocity,q,n=4,axis=0)
n = self.decimated_veloc.shape[0]
self.deci_tvec = np.arange(n) * deci_dt
return
def compute_fft(self,filt=True,freqs=[0.1,10.0],axis=0):
"""
Compute the Fourier of all the traces.
Parameters
----------
-- field ['V','D','A'] :: field on which to compute fft
Defines the following instances :
-- self.fft_sig
-- self.fft_freq
"""
if hasattr(self,'velocity') :
veloc = self.velocity
else :
if filt:
veloc = self.read_seismo(filter_s=True)
else:
veloc = self.read_seismo(filter_s=False)
detrend = np.subtract(veloc,np.mean(veloc,axis=axis)[np.newaxis,:])
s = np.abs(self.dt*np.fft.fft(detrend,axis=axis))
n = detrend.shape[0]
f = np.fft.fftfreq(n,self.dt)
if n%2:
nf = int((n+1)/2)
else:
nf = int(n/2)
self.fft_sig = s[:nf,:]
self.fft_freq = f[:nf]
return s[:nf,:],f[:nf]
@staticmethod
def interp(field,coord):
"""
Interpolates field over a meshgrid.
The meshgrid's size depends on the argument coord.
"""
xcoord = coord[:,0]
zcoord = coord[:,1]
ext = [min(xcoord), max(xcoord), min(zcoord), max(zcoord)]
x,z = np.meshgrid(np.linspace(ext[0],ext[1],1000),np.linspace(ext[2],ext[3],1000),sparse=True)
y = gd((xcoord,zcoord),field,(x,z),method='linear')
y =np.flipud(y)
return y
def animate(self,savefile=None,cmap='seismic',interval=1500,repeat_delay=1000,duration=1, background=True):
"""
Animates SEM2DPACK snapshots
"""
# Plot parameters
filename = "v"+self._component+"_*_sem2d.dat"
coord = self.mdict["coord"]
xcoord = coord[:,0]
zcoord = coord[:,1]
frames = sorted(glob.iglob(self.directory + filename))
nframe = int(len(frames)/2)
ext = [min(xcoord), max(xcoord), -1 * max(zcoord), min(zcoord)]
ims = []
field =[]
for i in range(nframe):
f = self.readField(frames[i])
field.append(f)
pool = mp.Pool(processes=os.cpu_count()) # Initializes a pool of processes
results = [pool.apply_async(self.interp,args=(x,coord)) for x in field] # run the processes
output = [p.get() for p in results] # retrieve processes information
duration = duration or (self.npts * self.dt)
fig, ax = plt.subplots(figsize=(10,4))
Writer = anim.writers['ffmpeg']
writer = Writer(fps=1,metadata=dict(artist='Flomin'))
#vmin , vmax = -5e-10, 5e-10
for i in range(nframe):
frametitle = 'Snapshot at time = {:.1f} secs'.format((i/nframe)*duration)
ttl = ax.text(0.5, 1.01, frametitle, ha='center', \
va='bottom', transform=ax.transAxes,fontsize=18)
im = ax.imshow(output[i],extent=ext,cmap=cmap,\
aspect="auto",animated=True, origin='lower')#,vmin=vmin,vmax=vmax)
#im.set_clim(vmin,vmax)
ims.append([im,ttl])
ims.append([im,])
ani = anim.ArtistAnimation(fig,ims,interval=interval,blit=False,
repeat_delay=repeat_delay)
if background:
vsfile = self.directory + 'Cs_gll_sem2d.tab'
with open(vsfile,'r') as v:
vs_int = pd.read_csv(v,sep='\s+',names=['vs','x','z'])
ax.scatter(vs_int['x'], vs_int['z'], c=vs_int['vs'], s=20, cmap='jet', zorder=0)
ax.set_xlabel('Distance along the profile [m]')
ax.set_ylabel('Depth [m]')
ax.minorticks_off()
ax.invert_yaxis()
c= plt.colorbar(im, fraction=0.1,pad=0.08,shrink=0.8)
c.set_label('particle velocity $[ms^{-1}]$')
if savefile : ani.save(savefile,writer=writer,savefig_kwargs={'pad_inches':0.01})
plt.show(block=True)
return
def plot_snapshot(self,filename,savefile=None,cmap='jet'):
"""
Plot the snapshot a particular time
"""
frame_names = "v"+self._component+"_*_sem2d.dat"
nframe = len(sorted(glob.iglob(self.directory + frame_names))) - 1
duration = self.dt * self.npts
if not isinstance(filename,str) :
raise Exception('TypeError : filename must be string ')
else :
frame_number = int(filename.split('_')[1])
filename = self.directory + filename
field = self.readField(filename)
coord = self.mdict["coord"]
xcoord = coord[:,0] ; zcoord = coord[:,1]
y = self.interp(field,coord)
vmin = np.nanmin(y)
vmax = np.nanmax(y)
a_ratio = (np.max(zcoord) - np.min(zcoord)) / (np.max(xcoord) - np.min(xcoord)) # aspect ratio
fig, ax = plt.subplots()
im = ax.imshow(y,extent=[min(xcoord)/1e3, max(xcoord)/1e3, min(zcoord), max(zcoord)],cmap=cmap,
vmin=vmin,vmax=vmax, aspect=a_ratio)
plt.tight_layout
c=plt.colorbar(im,format='%.0e', fraction=0.046, pad=0.06, shrink=0.4)
plt.ylabel('Depth [m]')
plt.xlabel('Length [m]')
c.set_clim(vmin,vmax)
c.set_label('Particle velocity $ms^{-1}}$')
plt.title('Snapshot at t = {:.3f} $sec$'.format( (frame_number/nframe)*duration) )
if savefile : plt.savefig(savefile,dpi=300)
plt.show()
def plot_wiggle(self,ssta=None,sf=None,savefile=None,stride=1,axis=None,**kwargs):
if isinstance(ssta,int):
begin = 0
end = ssta -1
elif isinstance(ssta,(list,tuple)):
begin = ssta[0]
end = ssta[1]-1
else:
begin, end = 0, -1
xx = self.rcoord[begin:end:stride,0]
if not hasattr(self,'velocity'):
print("Re-reading traces")
self.read_seismo(filter_s=True)
if sf != None:
axis = wig.wiggle(self.velocity[:,begin:end:stride],self.tvec,sf=sf,axis=axis)
else :
axis = wig.wiggle(self.velocity[:,begin:end:stride],self.tvec,axis=axis)
return axis
def plot_trace(self,trace_number=0):
if not self.velocity.size:
print("Re-reading traces")
self.read_seismo(filter_s=True)
plt.figure()
plt.plot(self.tvec,self.velocity[:,trace_number])
plt.xlabel('Time [s]')
plt.ylabel('Velocity [ms]')
plt.title('Trace number {} at x = {}'.format(trace_number,self.rcoord[trace_number,0]))
plt.show()
def compute_tf(self, nsurface, blim, smooth=True, filt=False, freqs=None,
saveBr=False, useBr=False, brockName=None,bd=40):
"""
Computes the 2D transfer function of a sedimentary basin.
-- parameters --
* nsurface (int) :: number of surface recieves
* bmin (float) :: x-coordinate of the leftwards (lower) limit between the
sedimentary basin and the bed rock
* bmax (float) :: x-coordinate of the rightwards limit between the
sedimentary basin and the bed rock
* smooth (bool) :: To apply a konno-Ohmachi smoothing to the
signal's spectra
* brockName :: Bedrock spectrum file name to load if useBr=True
* bd :: band width of Konno-Ohmachi smoothing function
-- return --
* Initializes self.raw_ssr variable which contains the transfer functions
"""
if not hasattr(self,'velocity'):
print("Reading velocity traces")
if filt:
self.read_seismo(filter_s=True)
else:
self.read_seismo(filter_s=False,freqs=freqs)
# Get rock station x-coordinates
nt, nx = self.velocity.shape
xcoord = self.rcoord[:,0]
xmin = np.where(xcoord[:nsurface]<blim[0])
xmax = np.where(xcoord[:nsurface]>blim[1])
br_sta_coord = np.append(xmin[0],xmax[0]) # bed rock station coordinates
# Compute the tranfer function on displacements
if filt :
self.compute_fft()
else:
self.compute_fft(filt=False,freqs=freqs)
if not useBr :
br_fft = self.fft_sig[:,br_sta_coord]
br_fft = br_fft.mean(axis=1)
if saveBr: np.save(brockName,br_fft)
else :
br_fft = np.load(brockName)
# Smoothing the spectrum
if smooth :
br_fft = konno(br_fft,self.fft_freq,normalize=True)
basin_fft = konno(self.fft_sig.T,self.fft_freq,normalize=True)
else:
basin_fft = self.fft_sig.T
raw_ssr = basin_fft[:nsurface,:]/br_fft[np.newaxis,:]
self.raw_ssr = raw_ssr
return self.raw_ssr
def plot_tf(self,savefile=None,cmap='jet',**kwargs):
if 'raw_ssr' in kwargs.keys():
raw_ssr = kwargs['raw_ssr'].T
xcoord = kwargs['xcoord']
fft_freq = kwargs['freq']
else:
if not hasattr(self,'raw_ssr'):
print('Must run compute_tf first !! \n Existing ...')
sys.exit()
else:
raw_ssr = self.raw_ssr.T
xcoord = self.rcoord[:,0]
fft_freq = self.fft_freq
# Interpolated array on 2d meshgrid
xmin , xmax = np.min(xcoord) , np.max(xcoord)
#set_rcParams()
#-- Plot
if 'axis' in kwargs.keys():
ax = kwargs['axis']
else:
fig, ax = plt.subplots(figsize=(8,6))
if 'clim' in kwargs:
cmin = kwargs['clim'][0]
cmax = kwargs['clim'][1]
else :
cmin = 0
cmax = 7
im = ax.imshow(raw_ssr, cmap=cmap, aspect='auto', interpolation='bilinear',vmin=cmin, vmax=cmax, \
origin='lower', extent=[xmin,xmax,0,max(fft_freq)])
#-- Set plot parameters ----
if 'xlim' in kwargs :
ax.set_xlim(kwargs['xlim'][0], kwargs['xlim'][1])
else:
ax.set_xlim(xmin,xmax)
if 'ylim' in kwargs :
ax.set_ylim(kwargs['ylim'][0], kwargs['ylim'][1])
else : ax.set_ylim(0.1,self._freqs[1])
if 'axis' in kwargs.keys():
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='3%', pad=0.2)
c = plt.colorbar(im, cax=cax, fraction=0.046, shrink=0.6)
im.set_clim(0,7)
c.set_label('Amplification')
c.ax.minorticks_off()
ax.xaxis.set_visible(False)
if 'ylabel' in kwargs :
ax.set_ylabel(kwargs['ylabel'])
else :
ax.set_ylabel('Frequency [Hz]')
if 'xlabel' in kwargs :
ax.set_xlabel(kwargs['xlabel'])
else :
ax.set_xlabel('Horizontal profile [m]')
if 'title' in kwargs : ax.set_title(kwargs['title'])
return ax
else:
if 'ylabel' in kwargs :
ax.set_ylabel(kwargs['ylabel'])
else :
ax.set_ylabel('Frequency [Hz]',fontsize=6)
if 'xlabel' in kwargs :
ax.set_xlabel(kwargs['xlabel'])
else :
ax.set_xlabel('Horizontal profile [m]',fontsize=6)
if 'title' in kwargs : ax.set_title(kwargs['title'])
cb = fig.colorbar(im, shrink=0.6, aspect=10, pad=0.02, ticks=np.linspace(cmin,cmax,cmax+1))
cb.set_label('Amplification', labelpad=15, y=0.5, rotation=90)
cb.minorticks_off()
if savefile != None:
fig.savefig(savefile,dpi=300)
plt.show()
def plot_source(self,savefile=None,source_name=None):
#if not isinstance(source_name,str):
# print('source file name must be str object')
if source_name:
source_name = source_name
else:
source_name = 'SourcesTime_sem2d.tab'
source_file = self.directory + source_name
amp = np.genfromtxt(source_file)
# plot spectra
dt = amp[1,0]-amp[0,0]
n = amp.shape[0]
if n%2:
nf = int(n+1)/2
else :
nf = int(n)/2
spec = np.fft.fft(amp[:,1])[:nf]
f = np.fft.fftfreq(n,dt)[:nf]
fig = plt.figure(figsize=(8,5))
fig.subplots_adjust(wspace=0.3)
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.plot(amp[:,0],amp[:,1])
ax2.plot(f,spec)
ax1.ticklabel_format(style='sci',scilimits=(0,0),axis='y')
ax2.ticklabel_format(style='sci',scilimits=(0,0),axis='y')
ax1.set_xlabel('time [s]',fontsize=14) ; ax1.set_ylabel('velocity [$ms^{-1}$]',fontsize=14)
ax2.set_xlabel('frequency [Hz]',fontsize=14) ; ax2.set_ylabel('amplitude',fontsize=14)
ax1.set_title('Source time function',fontsize=16)
ax2.set_title('Source spectrum',fontsize=16)
ax2.set_xlim(0,15)
ax1.set_xlim(0,2)
#plt.tight_layout
if savefile : plt.savefig(savefile)
plt.show()
@staticmethod
def plot_im(matrix,vmin,vmax,cmin,cmax,**kwargs):
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111)
im = ax.imshow(matrix,cmap='jet',aspect='auto',interpolation='bilinear', \
vmin=vmin, vmax=vmax, origin='lower', extent=extent)
if 'xlim' in kwargs : ax.set_xlim(kwargs['xlim'][0], kwargs['xlim'][1])
if 'ylim' in kwargs :
ax.set_ylim(kwargs['ylim'][0], kwargs['ylim'][1])
else : ax.set_ylim(0.1,fmax)
if 'ylabel' in kwargs : ax.set_ylabel(kwargs['ylabel'], fontsize=16)
if 'xlabel' in kwargs : ax.set_xlabel(kwargs['xlabel'], fontsize=16)
if 'title' in kwargs : ax.set_title(kwargs['title'],fontsize=18)
# colorbar
cb = fig.colorbar(im, shrink=0.5, aspect=10, pad=0.01,\
ticks=np.linspace(cmin,cmax,cmax+1), \
boundaries=np.linspace(cmin,cmax,cmax+1))
cb.set_label('Amplification', labelpad=20, y=0.5, rotation=90, fontsize=15)
plt.show()
@staticmethod
def filter_seismo(sismo,freqs=[0.1,10],ftype='bandpass',dt=0.01):
"""
filter seismograms.
Inputs:
-freqs[tuple][(0.1,10)] : corner frequencies of filter response
-ftype[str][default=bandpass] : filter type
Return:
-Updates self.velocity array.
"""
filtered_s = np.zeros(sismo.shape)
if ftype == 'bandpass':
for i in range(sismo.shape[-1]):
filtered_s[:,i] = bandpass(sismo[:,i],freqs[0],freqs[1],dt=dt,
corners=4,zerophase=True)
return filtered_s
def plot_Vs(self,vs_br=1000,cmap='jet',axis=None, clim=None, size='2%', s=2, divide=True):
"""
Makes a scatter plot of the velocities
:Params
-------
- vs_br : Bedrock velocity to be remove from the scatter plot
"""
#from scipy.spatial.distance import pdist
vsfile = self.directory + 'Cs_gll_sem2d.tab'
with open(vsfile,'r') as v:
vs_int = pd.read_csv(v,sep='\s+',names=['vs','x','z'])
tmp = vs_int.drop_duplicates()
vs_int = tmp.drop(tmp[tmp['vs']==vs_br].index)
self.gll_vs = vs_int
min_vs , max_vs = np.min(vs_int['vs']), np.max(vs_int['vs'])
if axis :
if divide:
divider = make_axes_locatable(axis)
cax = divider.append_axes('right', size=size, pad=0.2)
x = self.rcoord[:,0]
#axis.fill_between(x,np.ones(x.shape)*-34,y[7,:],facecolor='#b26400')
im = axis.scatter(vs_int['x'], vs_int['z'], c=vs_int['vs'], s=s, cmap='jet')
c = plt.colorbar(im, cax=cax, fraction=0.046, pad=0.06, shrink=0.4)
if clim: im.set_clim(*clim)
c.ax.tick_params(labelsize=10)
c.ax.minorticks_off()
c.set_label('Velocity [$ms^{-1}$]', fontsize=12, labelpad=8)
axis.tick_params(axis='both', which='major', labelsize=12)
axis.set_xlabel('Distance along the profile [m]', fontsize=14)
axis.set_ylabel('Depth [m]', fontsize=14)
return axis
else:
im = axis.scatter(vs_int['x'], vs_int['z'], c=vs_int['vs'], s=s, cmap='jet')
axis.tick_params(axis='both', which='major', labelsize=12)
if clim: im.set_clim(*clim)
return axis, im
else:
# Figure
fig , ax = plt.subplots(figsize=(12,4))
ax.scatter(vs_int['x'], vs_int['z'], c=vs_int['vs'], s=s, cmap=cmap)
plt.show(block=True)
return self.gll_vs
def compute_st(self,frmin=None,fnyq=None):
"""
Compute the stockwell transform
"""
if not self.velocity.size :
print("Reading velocity traces")
self.read_seismo(filter_s=True)
frmin = frmin or 0
fnyq = fnyq or (1./(2.*self.dt))
df = 1. / (self.npts * self.dt)
fnpt = int(fnyq/df)
stock = []
for i in range(self.nsta):
trace = self.velocity[:,i]
tmp = st.st(trace,frmin,fnpt)
stock.append(np.abs(tmp))
#stock.append(tmp)
self.stockwell = np.array(stock)
return self.stockwell, (int(frmin),int(fnyq))
def plot_stockwel(self,sta_number,frmin=0.0,fnyq=10,cmap='seismic',**kwargs):
set_rcParams()
"""
Plot the Stockwell transform of a seismogram.
:Inputs
-------
- sta_number [int] :: receiver indice to plot
- frmin [float] :: minimum frequency of stockwell transform
- fnyq [float] :: maximum frequency of stockwell transform
"""
# Check input parameters
if not isinstance(sta_number,int):
try :
sta_number = int(sta_number)
except :
msg = 'Station number must of an integer or of type convertable to an integer'
raise Exception(msg)
# Check if the compute_st method has been called
if hasattr(self,'stockwell'):
pass
else :
self.compute_st(frmin=frmin,fnyq=fnyq)
dx = self.rcoord[1,0] - self.rcoord[0,0]
if 'label' in kwargs.keys():
label = kwargs['label']
else:
label = None
fig , ax = plt.subplots(2,1,figsize=(10,7),sharex='col')
ax[0].plot(self.tvec,self.velocity[:,sta_number],'k',label=label)
ax[0].set_ylabel('Velocity [m$s^{-1}$]')
ax[0].set_title('Velocity trace at {} m'.format(str(dx * sta_number)))
ax[1].imshow(self.stockwell[sta_number,:,:],aspect='auto',extent=[0,np.max(self.tvec),frmin,fnyq],
cmap=cmap,origin='lower')
ax[1].set_title('S transform')
ax[1].set_xlabel('Time [s]')
ax[1].set_ylabel('Frequency [Hz]')
if 'key'in kwargs.keys():
fig.suptitle(kwargs['key'])
try:
ax[0].legend()
except:
pass
if 'savefile' in kwargs.keys():
fig.savefig(kwargs['savefile'])
plt.show(block=False)
def compute_pv(self,op='pgv',component=None,freqs=None,n_surf=None):
component = component or self._component
n_surf = n_surf or self.nsta
freqs = freqs or self._freqs
if not hasattr(self,'velocity'):
print("Reading velocity traces")
veloc = self.read_seismo(filter_s=True,freqs=freqs)[:,:n_surf]
else:
veloc = self.velocity[:,:n_surf]
if op == 'pgv':
maxs = np.max(np.abs(veloc),axis=0)
max_args = np.argmax(np.abs(veloc),axis=0) * self.dt
self.pgv = maxs
return self.pgv, max_args
elif op == 'pga':
# differentiate the velocities
dt = self.dt
dadx = np.gradient(veloc,dt,axis=0)
maxs = np.max(np.abs(dadx),axis=0)
self.pga = maxs
return self.pga
def compute_ai(self,atype='ABI',component=None,n_surf=None,freqs=None):
"""
atype = AI (arias intensity) or ABI (Arias based intensity)
"""
component = component or self._component
n_surf = n_surf or self.nsta
freqs = freqs or self._freqs
if not hasattr(self,'velocity'):
print("Reading velocity traces")
veloc = self.read_seismo(filter_s=True,freqs=freqs)[:,:n_surf]
else:
veloc = self.velocity[:,:n_surf]
dt = self.dt
if atype == 'AI':
dvdx = np.gradient(veloc,dt,axis=0) # acceleration
g = 9.8
a2 = dvdx**2
self.ai = (np.pi / (2*g)) * np.sum(a2,axis=0)
self.cum_ai = (np.pi / (2*g)) * np.cumsum(a2,axis=0)
elif atype == 'ABI':
self.ai = np.sum(veloc**2,axis=0)
self.cum_ai = np.cumsum(veloc**2,axis=0)