-
Notifications
You must be signed in to change notification settings - Fork 21
/
PyVelest.py
3138 lines (2217 loc) · 120 KB
/
PyVelest.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
#___________________IMPORT FROM NUMPY, SCIPY
from numpy import loadtxt, diff, ones, random, array, hstack, mean, pi, where, median, degrees, ones_like
from numpy import arange, sqrt, linalg, arccos, arctan2, rad2deg, zeros_like, abs, linspace, savez, load
from scipy import zeros, shape, concatenate
#___________________IMPORT EXTRA LIBRARIES
##from osgeo import gdal
import tarfile
import operator
import sys
import glob
from os import system, remove, path, getcwd, chdir, mkdir, makedirs, sep, getcwd
from datetime import datetime as dt
sys.path.append(path.join('tools','utility'))
from PyNordicRW import Read_Nordic
from PyNordic2CNV import CNV2Nordic, ReadCNV
from collections import OrderedDict
from shutil import copy, rmtree, move
from LatLon import lat_lon as ll
#___________________IMPORT FROM MATPLOTLIB
import mpl_toolkits
mpl_toolkits.__path__.append('/usr/lib/python2.7/dist-packages/mpl_toolkits/')
from initial_mpl import init_plotting_isi
from matplotlib.colors import LightSource
from matplotlib.cbook import get_sample_data
from matplotlib.colors import LinearSegmentedColormap
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Ellipse
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib import ticker, gridspec
import matplotlib.pyplot as plt
#___________________ some usseful functions
def k2d(kilometer, radius=6371):
return kilometer / (2.0 * radius * pi / 360.0)
def d2k(degrees, radius=6371):
return degrees * (2.0 * radius * pi / 360.0)
def reject_outliers(data, m = 2.):
d = abs(data - median(data))
mdev = median(d)
s = d/mdev if mdev else 0.
inx = where(s<m)
good_data = data[inx]
good_data_ind = where(s<m)
inx = where(s>m)
bad_data = data[inx]
bad_data_ind = where(s>m)
return good_data,good_data_ind[0],bad_data,bad_data_ind[0]
#___________________ Main class
class main():
"""
Description:
A class for running velest in automatic mode (synthetic
model generation, control file prepration, analysing and
plot results).
"""
def __init__(self):
self.project_nm = raw_input("\n+++ Enter Project Name: <Please use the name of velocity model you're using>\n\n")
self.input_dic = self.read_par()
#*-*-*-*-*- Cheeking if topography data exists
#
# Extract tar.gz file may take times
def extract_topo(self):
if self.topo_flag == 'True':
here = getcwd()
chdir(path.join('gdb','db','srtm'))
if not path.exists('IRN.tif'):
print "+++ It seems, You're running 'PyVelest' for the first time on your system!"
print "+++ You'll never see this message next time."
print "\n--> Extracting topography data , Please wait ...\n"
tar = tarfile.open('IRN.tif.tar.gz', "r:gz")
tar.extractall()
tar.close()
chdir(here)
#*-*-*-*-*- Remove old files and do empy all folders
#
# Remove files from velout.
# Remove tmp folder if exist.
def clean(self):
if path.exists(path.join('tmp',self.project_nm)):
rmtree(path.join('tmp',self.project_nm))
if path.exists(path.join('velout',self.project_nm)):
rmtree(path.join('velout',self.project_nm))
def write_cmn(self, input_dic, output='velest.cmn', project_nm='test'):
output = open(output,'w')
now = dt.now()
header1 = "CONTROL-FILE FOR PROGRAM V E L E S T (%s)"%(now.strftime('%Y-%m-%d'))
header1 = header1.center(70,'*')
header2 = project_nm
# write Headers
output.write(header1+'\n')
output.write(header2+'\n')
# write the Main lines
d = input_dic
for _ in d:
if d[_] == "None":
d[_] = ' '
l1 = '*** olat olon icoordsystem zshift itrial ztrial ised'
l2 = '*** neqs nshot rotate'
l3 = '*** isingle iresolcalc'
l4 = '*** dmax itopo zmin veladj zadj lowveloclay'
l5 = '*** nsp swtfac vpvs nmod'
l6 = '*** othet xythet zthet vthet stathet'
l7 = '*** nsinv nshcor nshfix iuseelev iusestacorr'
l8 = '*** iturbo icnvout istaout ismpout'
l9 = '*** irayout idrvout ialeout idspout irflout irfrout iresout'
l10 = '*** delmin ittmax invertratio'
l11 = '*** Modelfile'
l12 = '*** Stationfile'
l13 = '*** Seismofile'
l14 = '*** File with region names'
l15 = '*** File with region coordinates'
l16 = '*** File #1 with topo data'
l17 = '*** File #2 with topo data'
l18 = '*** File with Earthquake data'
l19 = '*** File with Shot data'
l20 = '*** Main print output file'
l21 = '*** File with single event locations'
l22 = '*** File with final hypocenters in *.cnv format'
l23 = '*** File with new station corrections'
l24 = '*** File with summary cards (e.g. for plotting)'
l25 = '*** File with raypoints'
l26 = '*** File with derivatives'
l27 = '*** File with ALEs'
l28 = '*** File with Dirichlet spreads'
l29 = '*** File with reflection points'
l30 = '*** File with refraction points'
l31 = '*** File with residuals'
l32 = '******* END OF THE CONTROL-FILE FOR PROGRAM V E L E S T *******'
for l in [l1,l2,l3,l4,l5,l6,l7,l8,l9,l10]:
output.write(l+'\n')
l = l.split()
l = l[1:]
l = " ".join([d[v] for v in l ])
output.write(l+'\n')
output.write(l11+'\n')
output.write(path.join('velinp',self.velmod_name)+'\n')
output.write(l12+'\n')
output.write(d['Stationfile']+'\n')
output.write(l13+'\n')
output.write(d['Seismofile']+'\n')
output.write(l14+'\n')
output.write(d['RegionNamesfile']+'\n')
output.write(l15+'\n')
output.write(d['Region_coorfile']+'\n')
output.write(l16+'\n')
output.write(d['Topo_data_1file']+'\n')
output.write(l17+'\n')
output.write(d['Topo_data_2file']+'\n')
output.write(l18+'\n')
output.write(d['EQ_datafile']+'\n')
output.write(l19+'\n')
output.write(d['Shot_datafile']+'\n')
output.write(l20+'\n')
output.write(d['Main_printoutfile']+'\n')
output.write(l21+'\n')
output.write(d['Single_ev_locfile']+'\n')
output.write(l22+'\n')
output.write(d['Final_hypfile']+'\n')
output.write(l23+'\n')
output.write(d['Station_corrfile']+'\n')
output.write(l24+'\n')
output.write(d['Summary_cardsfile']+'\n')
output.write(l25+'\n')
output.write(d['Raypointsfile']+'\n')
output.write(l26+'\n')
output.write(d['Derivativesfile']+'\n')
output.write(l27+'\n')
output.write(d['ALEsfile']+'\n')
output.write(l28+'\n')
output.write(d['Dirichlet_sprfile']+'\n')
output.write(l29+'\n')
output.write(d['Reflection_pntfile']+'\n')
output.write(l30+'\n')
output.write(d['Refraction_pntfile']+'\n')
output.write(l31+'\n')
output.write(d['Residualsfile']+'\n')
output.write(l32+'\n')
output.close()
return project_nm
#*-*-*-*-*- read VELEST input parameters into a dictionary
#
# Given the parameter file [./par/par.dat], read all VELEST
# required parameters.
def read_par(self, par_file=path.join('par','par.dat')):
print "\n+++ Read VELEST input parameters ..."
self.input_par = loadtxt(par_file,comments='#',dtype=str,delimiter=':')
self.input_dic = {}
for i in self.input_par:
self.input_dic[i[0].strip()] = i[1].strip()
# Read topo.dat
parfile = loadtxt(path.join('par','topo.dat'),skiprows=3,comments='#',dtype=str,delimiter='=')
par_dic = {}
for _ in parfile:
par_dic[_[0].strip()] = _[1].strip()
self.lon_min, self.lon_max = float(par_dic['LON_MIN']), float(par_dic['LON_MAX'])
self.lat_min, self.lat_max = float(par_dic['LAT_MIN']), float(par_dic['LAT_MAX'])
self.dep_min, self.dep_max = float(par_dic['DEP_MIN']), float(par_dic['DEP_MAX'])
self.mag_min, self.mag_max = float(par_dic['MAG_MIN']), float(par_dic['MAG_MAX'])
self.mag_flag = par_dic['MAG_FLG']
self.max_her = float(par_dic['MAX_HER'])
self.bin_her = float(par_dic['BIN_HER'])
self.max_der = float(par_dic['MAX_DER'])
self.bin_der = float(par_dic['BIN_DER'])
self.max_rms = float(par_dic['MAX_RMS'])
self.bin_rms = float(par_dic['BIN_RMS'])
self.max_tdf = float(par_dic['TIMEDIF'])
self.max_ldf = float(par_dic['LOCDIF'])
self.gmtcpt = par_dic['GMT_CPT']
self.pltcpt = eval('plt.cm.'+par_dic['PLT_CPT'])
self.cptflag = par_dic['GMTPLTF']
self.srtm_tif = par_dic['SRTMTIF']
self.res_x = par_dic['RES_P_X']
self.res_y = par_dic['RES_P_Y']
self.topo_flag = par_dic['TOPOFLG']
self.nstd = float(par_dic['NSTD'])
self.sc_len = int(float(par_dic['SC_LEN']))
self.bat_dep = par_dic['BAT_DEP']
self.plt_flt = par_dic['PLT_FLT']
self.plt_stn = par_dic['PLT_STN']
self.ot_adj = float(par_dic['OT_ADJ'])
self.hr_adj = float(par_dic['HR_ADJ'])
self.dp_adj = float(par_dic['DP_ADJ'])
# Read velocity models in velinp directory and let user to choose wich one must be used
inp_syntvel = path.join('par','syntvel.dat')
inp = loadtxt(inp_syntvel,dtype=str,comments='#',delimiter=':')
self.velmod_name = inp[16][1].strip()
return self.input_dic
#*-*-*-*-*- write new VELEST parameter to velest.cmn file
#
# Write VELEST parameters into VELEST's cnv
# file [./velinp/velest.cmn]
def write_par(self):
self.project_nm = self.write_cmn(input_dic=self.input_dic, output='velest.cmn',project_nm=self.project_nm)
print "+++ Write VELEST control file ..."
#*-*-*-*-*- read velocity model and station files
#
# Read VELEST velocity file [./velinp/model.mod] and
# station file [./velinp/station.sta] and parse it into
# a dictionary.
def read_VelSta(self,inp_vel=path.join('velinp','model.mod'), inp_sta=path.join('velinp','station.sta')):
inp_vel=path.join('velinp',self.velmod_name)
sta_dic = {}
with open(inp_sta) as f:
for l in f:
if '(a4,f7.4,a1,1x,f8.4,a1,1x,i4,1x,i1,1x,i3,1x,f5.2,2x,f5.2)' not in l and l.strip():
nm = l[0:4]
lat = float(l[4:11])
lon = float(l[13:21])
elv = float(l[22:27])
rfn = int(l[30:33].strip())
sta_dic[nm] = {'lat':lat, 'lon':lon, 'elv':elv, 'rfn':rfn}
with open(inp_vel) as f:
vel_dic = {}
p_vel,p_dep,p_vdamp = [], [], []
s_vel,s_dep,s_vdamp = [], [], []
start_p_flag = False
start_s_flag = False
for l in f:
if "P-VELOCITY MODEL" in l:
start_p_flag = True
if "S-VELOCITY MODEL" in l:
start_s_flag = True
start_p_flag = False
if start_p_flag and len(l.split()) >= 3:
p_vel.append(float(l.split()[0]))
p_dep.append(float(l.split()[1]))
p_vdamp.append(float(l.split()[2]))
vel_dic['P'] = {'p_vel':p_vel, 'p_dep':p_dep, 'p_dmp':p_vdamp}
if start_s_flag and len(l.split()) >= 3:
s_vel.append(float(l.split()[0]))
s_dep.append(float(l.split()[1]))
s_vdamp.append(float(l.split()[2]))
vel_dic['S'] = {'s_vel':s_vel, 's_dep':s_dep, 's_dmp':s_vdamp}
return vel_dic,sta_dic
#*-*-*-*-*- Make synthetic velocity models
#
# Read parameter file's ./par/syntvel.dat and make
# synthetic velocity models
def mk_syn_model(self,inp_syntvel=path.join('par','syntvel.dat')):
print "+++ Make synthetic velocity models ..."
inp = loadtxt(inp_syntvel,dtype=str,comments='#',delimiter=':')
vel_dic,_ = self.read_VelSta(inp_vel=path.join('velinp',self.velmod_name),inp_sta=self.input_dic['Stationfile'])
pvel = vel_dic['P']['p_vel']
pdep = vel_dic['P']['p_dep']
pdmp = vel_dic['P']['p_dmp']
try:
sdmp = vel_dic['S']['s_dmp']
except KeyError:
sdmp = ones(len(pdmp))
self.vel_mode = inp[0][1].strip()
self.dep_mode = inp[1][1].strip()
self.vel_min = float(inp[2][1])
self.vel_max = float(inp[3][1])
self.dep_min = float(inp[4][1])
self.dep_max = float(inp[5][1])
self.vel_dev = float(inp[6][1])
self.dep_dev = float(inp[7][1])
self.nmod = int(inp[8][1].strip())
self.vpvs = float(inp[9][1])
self.ors = float(inp[10][1])
self.grd = int(inp[11][1].strip())
self.bad_evnt_herr = int(inp[12][1].strip())
self.bad_evnt_zerr = int(inp[13][1].strip())
self.bad_evnt_rms = float(inp[14][1].strip())
self.res_max_rw = float(inp[15][1].strip())
self.velmod_name = inp[16][1].strip()
if not path.exists(path.join('tmp',self.project_nm,'synthvel')):
makedirs(path.join('tmp',self.project_nm,'synthvel'))
else:
rmtree(path.join('tmp',self.project_nm,'synthvel'))
makedirs(path.join('tmp',self.project_nm,'synthvel'))
output = path.join('tmp',self.project_nm,'synthvel')
orig_velmod = array([vel_dic['P']['p_vel'],vel_dic['P']['p_dep'],vel_dic['P']['p_dmp']]).T
# generate random model with no gradient
if self.grd == 0:
if self.vel_mode == "u":
vel = array([random.uniform(i-self.vel_dev*(1.0/j),i+self.vel_dev*(1.0/j),self.nmod) for i,j in zip(pvel,pdmp)]).T
elif self.vel_mode == "n":
vel = array([random.normal(i,self.vel_dev*(1.0/j),self.nmod) for i,j in zip(pvel,pdmp)]).T
if self.dep_mode == "u":
dep = array([random.uniform(i-self.dep_dev*(1.0/j),i+self.dep_dev*(1.0/j),self.nmod) for i,j in zip(pdep,pdmp)]).T
elif self.dep_mode == "n":
dep = array([random.normal(i,self.dep_dev*(1.0/j),self.nmod) for i,j in zip(pdep,pdmp)]).T
# generate random model with positive gradient
elif self.grd == 1:
vel = []
dep = []
if self.vel_mode == "u":
for _ in range(self.nmod):
vel.append(sorted(random.uniform(i-self.vel_dev*(1.0/j),i+self.vel_dev*(1.0/j)) for i,j in zip(orig_velmod[:,0],pdmp)))
elif self.vel_mode == "n":
for _ in range(self.nmod):
vel.append(sorted(random.normal(i,self.vel_dev*(1.0/j)) for i,j in zip(orig_velmod[:,0],pdmp)))
if self.dep_mode == "u":
for _ in range(self.nmod):
dep.append(sorted(random.uniform(i-self.dep_dev*(1.0/j),i+self.dep_dev*(1.0/j)) for i,j in zip(orig_velmod[:,1],pdmp)))
elif self.dep_mode == "n":
for _ in range(self.nmod):
dep.append(sorted(random.normal(i,self.dep_dev*(1.0/j)) for i,j in zip(orig_velmod[:,1],pdmp)))
vel = array(vel)
dep = array(dep)
dep_org = [i[1] for i in orig_velmod]
vel_org = [i[0] for i in orig_velmod]
dep_org[0] = self.dep_min
for d in range(len(dep)):
dep[d][0] = self.dep_min
nl = len(dep[0]) # number of layers for each random model
# Write to a new model file
for i in range(self.nmod):
fid = file(path.join(output,"model_"+str(i+1)+".mod"),'w')
fid.write('%s%d%s\n'%("Model: model_",i+1,".mod"))
fid.write('%s%d %s\n'%(" ",len(dep[0])," vel,depth,vdamp,phase(f5.2,5x,f7.2,2x,f7.3,3x,a1)"))
for j in range(len(dep[0])):
if j==0:
fid.write('%5.2f %11.2f %8.3f %26s\n'%(vel[i][j],dep[i][j],pdmp[j],"P-VELOCITY MODEL"))
else:
fid.write('%5.2f %11.2f %8.3f\n'%(vel[i][j],dep[i][j],1.0))
fid.write('%s%s\n'%(" ",len(dep[0])))
for j in range(len(dep[0])):
if j==0:
fid.write('%5.2f %11.2f %8.3f %26s\n'%(vel[i][j]/self.vpvs,dep[i][j],pdmp[j],"S-VELOCITY MODEL"))
else:
fid.write('%5.2f %11.2f %8.3f\n'%(vel[i][j]/self.vpvs,dep[i][j],1.0))
fid.close()
# Plot synthetic models
init_plotting_isi(9,9)
plt.rcParams['axes.labelsize'] = 8
ax = plt.subplot(111)
[i.set_linewidth(0.6) for i in ax.spines.itervalues()]
for i in range(self.nmod):
y = [-1*d for d in dep[i]]
yy = [-1*d for d in dep_org]
x = list(hstack([[m,n] for m,n in zip(vel[i],vel[i])]))
xx = list(hstack([[m,n] for m,n in zip(vel_org,vel_org)]))
y = list(hstack([[m,n] for m,n in zip(y,y)]))
yy = list(hstack([[m,n] for m,n in zip(yy,yy)]))
y.pop(0)
yy.pop(0)
y.append(-1*self.dep_max)
yy.append(-1*self.dep_max)
if i == 0:
ax.plot(x, y, 'r-', linewidth=1.0, label='Random')
else:
ax.plot(x, y, 'r-', linewidth=1.0)
ax.set_ylim(ymin=-self.dep_max,ymax=-self.dep_min)
ax.set_title("Synthetic Models")
ax.set_xlabel("Velocity (km/s)")
ax.set_ylabel("Depth (km)")
velmod_org = [xx,yy]
ax.plot(xx, yy, 'k-', linewidth=1.0,label='Reference')
ax.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
ax.set_xlim(self.vel_min, self.vel_max)
ax.locator_params(axis='x',nbins=7)
ax.locator_params(axis='y',nbins=7)
plt.legend(loc=1, fontsize=6)
ax_ = inset_axes(ax, width="30%", height="40%", loc=3)
[i.set_linewidth(0.6) for i in ax_.spines.itervalues()]
ax_.tick_params(labelsize=6)
binwidth = 2.0
data = self.orig_dep
bins = arange(min(data), max(data) + binwidth, binwidth)
ax_.hist(self.orig_dep, bins=bins, orientation="horizontal", rwidth=0.8, color='grey', edgecolor='r')
ax_.locator_params(axis='x',nbins=5)
ax_.locator_params(axis='y',nbins=5)
ax_.set_xlabel('Number of EQs (#)', fontsize=6)
ax_.xaxis.set_label_position("top")
ax_.xaxis.tick_top()
ax_.set_ylabel('Depth (km)', fontsize=6)
ax_.yaxis.set_label_position("right")
ax_.yaxis.tick_right()
ax_.set_ylim(0,self.dep_max)
ax_.set_ylim(ax_.get_ylim()[::-1])
ax_.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
plt.tight_layout()
plt.savefig(path.join('figs',self.project_nm,"synt_models_"+self.project_nm+'.tiff'))
plt.close()
#*-*-*-*-*- Run VELEST program
#
# Run VELEST for each model
#
def run_velest(self):
print "+++ Run VELEST ..."
if not path.exists(path.join('velout',self.project_nm)):
makedirs(path.join('velout',self.project_nm))
if path.exists(path.join('tmp',self.project_nm,'synthvel')):
self.synthvel_files = glob.glob(path.join('tmp',self.project_nm,'synthvel','*.mod'))
self.synthvel_files = sorted(self.synthvel_files, key=lambda x: int(x.split(sep)[-1].split('_')[1].split('.')[0]))
print ' --- %d synthetic model(s) found ...'%(len(self.synthvel_files))
else:
self.synthvel_files = []
self.nmod = 0
if self.synthvel_files:
copy('velest.cmn',path.join('tmp',self.project_nm))
for m,c in zip(self.synthvel_files,range(len(self.synthvel_files))):
tmp_cmn = open(path.join('tmp',self.project_nm,'tmp.cmn'),'w')
with open(path.join('tmp',self.project_nm,'velest.cmn')) as f:
for line in f:
if path.join('velinp',self.velmod_name) in line:
tmp_cmn.write(line.replace(path.join('velinp',self.velmod_name), m))
elif path.join('velout','velest.out') in line:
tmp_cmn.write(line.replace(path.join('velout','velest.out'),
path.join('velout',self.project_nm,'velest'+str(c+1)+'.out')))
elif path.join('velout','final_loc.cnv') in line:
tmp_cmn.write(line.replace(path.join('velout','final_loc.cnv'),
path.join('velout',self.project_nm,'final_loc'+str(c+1)+'.cnv')))
elif path.join('velout','stations_corr.sta') in line:
tmp_cmn.write(line.replace(path.join('velout','stations_corr.sta'),
path.join('velout',self.project_nm,'stations_corr'+str(c+1)+'.sta')))
else:
tmp_cmn.write(line)
tmp_cmn.close()
move(path.join('tmp',self.project_nm,'tmp.cmn'),'velest.cmn')
print ' --- Run for synthetic model:',c+1
system('velest > /dev/null')
remove(path.join('tmp',self.project_nm,'velest.cmn'))
for _ in [path.join('velout','velest.out'),
path.join('velout','final_loc.cnv'),
path.join('velout','stations_corr.sta')]:
if path.exists(_):
remove(_)
else:
print ' --- No synthetic model was found ...'
print ' --- Run for initial velocity model ...'
system('velest > /dev/null')
#*-*-*-*-*- Analyse VELEST results
#
# Analyse VELEST results and plot
# them.
def analyse_velest_res(self):
print "+++ Analyse VELEST outputs ..."
inp_syntvel = path.join('par','syntvel.dat')
inp = loadtxt(inp_syntvel,dtype=str,comments='#',delimiter=':')
self.vel_mode = inp[0][1].strip()
self.dep_mode = inp[1][1].strip()
self.vel_min = float(inp[2][1])
self.vel_max = float(inp[3][1])
self.dep_min = float(inp[4][1])
self.dep_max = float(inp[5][1])
self.vel_dev = float(inp[6][1])
self.dep_dev = float(inp[7][1])
self.nmod = int(inp[8][1].strip())
self.vpvs = float(inp[9][1])
self.nit = int(self.input_dic['ittmax'])
# if multiple synthetic velocity model has been defined
if self.synthvel_files:
self.rms_res = [[] for sm in self.synthvel_files]
self.avg_abs_adj = [[] for sm in self.synthvel_files]
self.vel_adj = [[] for sm in self.synthvel_files]
self.svel = {'P':[[] for sm in self.synthvel_files],
'S':[[] for sm in self.synthvel_files]}
self.sdep = {'P':[[] for sm in self.synthvel_files],
'S':[[] for sm in self.synthvel_files]}
self.stacor = {'P':[[] for sm in self.synthvel_files],
'S':[[] for sm in self.synthvel_files]}
s_vel, s_dep = [], [] # if nsp = 1, just one model will be produced
# read each VELEST output to extract inversion results
for sm in range(self.nmod):
with open(path.join('velout',self.project_nm,'velest%d.out'%(sm+1))) as f:
self.p_vel_adj_flag = False
self.s_vel_adj_flag = False
vel_flag = False
iter_counter = 0
for l in f:
if 'DATVAR=' in l and iter_counter <= self.nit:
self.rms_res[sm].append(float(l[68:].strip()))
iter_counter+=1
if 'A V E R A G E of ABSOLUTE ADJUSTMENTS' in l and iter_counter-1 <= self.nit:
self.avg_abs_adj[sm].append([float(i) for i in l[42:].strip().split()])
if 'Velocity model 1' in l and iter_counter-1 <= self.nit:
self.p_vel_adj_flag = True
tmp_vel = []
p_vel, p_dep = [], []
if 'Velocity model 2' in l and iter_counter-1 <= self.nit:
self.s_vel_adj_flag = True
s_vel, s_dep = [], []
if self.p_vel_adj_flag and 'Velocity model 1' not in l and len(l.split())==3:
tmp_vel.append((float(l.split()[2]),float(l.split()[1])))
p_vel.append(float(l.split()[0]))
p_dep.append(float(l.split()[2]))
if self.s_vel_adj_flag and 'Velocity model 2' not in l and len(l.split())==3:
s_vel.append(float(l.split()[0]))
s_dep.append(float(l.split()[2]))
if not l.strip() and self.p_vel_adj_flag:
self.p_vel_adj_flag = False
self.vel_adj[sm].append(tmp_vel)
if not l.strip() and self.s_vel_adj_flag:
self.s_vel_adj_flag = False
self.svel['P'][sm].append(p_vel)
self.sdep['P'][sm].append(p_dep)
self.svel['S'][sm].append(s_vel)
self.sdep['S'][sm].append(s_dep)
self.p_outlier_models = []
self.s_outlier_models = []
self.p_good_models = []
self.s_good_models = []
vp = array([_[0] for _ in self.svel['P']])
vs = array([_[0] for _ in self.svel['S']])
for _ in range(len(vp[0])):
good_model, good_model_ind, bad_model, bad_model_ind = reject_outliers(vp[:,_],m=self.ors)
self.p_outlier_models.extend(list(bad_model_ind))
for _ in range(len(vs[0])):
good_model, good_model_ind, bad_model, bad_model_ind = reject_outliers(vs[:,_],m=self.ors)
self.s_outlier_models.extend(list(bad_model_ind))
self.p_outlier_models = list(set(self.p_outlier_models))
self.p_good_models = list(set(range(self.nmod)) - set(self.p_outlier_models))
self.svel_p = [i[0] for i in self.svel['P']]
self.sdep_p = [i[0] for i in self.sdep['P']]
self.nomc = len(self.p_good_models)
self.s_outlier_models = list(set(self.s_outlier_models))
self.s_good_models = list(set(range(self.nmod)) - set(self.s_outlier_models))
# plot results, Time/Location/Velocity adjusment
init_plotting_isi(18,10)
plt.rcParams['axes.labelsize'] = 7
output = path.join('figs','invres_'+self.project_nm)
iter_color = ['k','r','g','b','c','y','m','purple','pink','brown'] # iteration color, maximum 10
ax1 = plt.subplot(2,3,1)
[i.set_linewidth(0.6) for i in ax1.spines.itervalues()]
self.minmod = {}
for nm in self.p_good_models:
x = range(1,1+self.nit)
ax1.plot(x, [self.avg_abs_adj[nm][_][0] for _ in range(len(self.avg_abs_adj[nm]))], marker='o', ms=3, lw=.75,color='grey')
self.minmod[nm] = mean([self.avg_abs_adj[nm][_][0] for _ in range(len(self.avg_abs_adj[nm]))])
self.ind = sorted(self.minmod.items(), key=operator.itemgetter(1))[0][0]
self.minmod = [self.avg_abs_adj[self.ind][i][0] for i in range(len(self.avg_abs_adj[self.ind]))]
ax1.plot(x, self.minmod,marker='o',color='red', ms=3, lw=.75,label='Minimum model,%d'%(self.ind))
ax1.set_xlabel('Iteration (#)')
ax1.set_ylabel('Origin Time Adjustment (s)')
ax1.set_ylim(0.0,self.ot_adj)
ax1.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
ax1.locator_params(axis='x',nbins=5)
ax1.locator_params(axis='y',nbins=5)
ax1.legend(loc=1, fontsize=5)
ax1.set_xticks(x)
ax1.set_xticklabels(x)
ax2 = plt.subplot(2,3,2)
[i.set_linewidth(0.6) for i in ax2.spines.itervalues()]
self.minmod = {}
for nm in self.p_good_models:
x = range(1,1+self.nit)
ax2.plot(x, [self.avg_abs_adj[nm][_][1] for _ in range(len(self.avg_abs_adj[nm]))], marker='o', ms=3, lw=.75, color='grey')
self.minmod[nm]=mean([self.avg_abs_adj[nm][_][1] for _ in range(len(self.avg_abs_adj[nm]))])
self.ind = sorted(self.minmod.items(), key=operator.itemgetter(1))[0][0]
self.minmod = [self.avg_abs_adj[self.ind][i][1] for i in range(len(self.avg_abs_adj[self.ind]))]
ax2.plot(x, self.minmod,marker='o', ms=3, lw=.75, color='red',label='Minimum model,%d'%(self.ind))
ax2.set_xlabel('Iteration (#)')
ax2.set_ylabel('Longitude Adjustment (km)')
ax2.set_ylim(0.0,self.hr_adj)
ax2.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
ax2.locator_params(axis='x',nbins=5)
ax2.locator_params(axis='y',nbins=5)
ax2.legend(loc=1, fontsize=5)
ax2.set_xticks(x)
ax2.set_xticklabels(x)
ax3 = plt.subplot(2,3,3)
[i.set_linewidth(0.6) for i in ax3.spines.itervalues()]
self.minmod = {}
for nm in self.p_good_models:
x = range(1,1+self.nit)
ax3.plot(x, [self.avg_abs_adj[nm][_][2] for _ in range(len(self.avg_abs_adj[nm]))], marker='o', ms=3, lw=.75, color='grey')
self.minmod[nm]=mean([self.avg_abs_adj[nm][_][2] for _ in range(len(self.avg_abs_adj[nm]))])
self.ind = sorted(self.minmod.items(), key=operator.itemgetter(1))[0][0]
self.minmod = [self.avg_abs_adj[self.ind][i][2] for i in range(len(self.avg_abs_adj[self.ind]))]
ax3.plot(x, self.minmod,marker='o', ms=3, lw=.75, color='red',label='Minimum model,%d'%(self.ind))
ax3.set_xlabel('Iteration (#)')
ax3.set_ylabel('Latitude Adjustment (km)')
ax3.set_ylim(0.0,self.hr_adj)
ax3.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
ax3.locator_params(axis='x',nbins=5)
ax3.locator_params(axis='y',nbins=5)
ax3.legend(loc=1, fontsize=5)
ax3.set_xticks(x)
ax3.set_xticklabels(x)
ax4 = plt.subplot(2,3,4)
[i.set_linewidth(0.6) for i in ax4.spines.itervalues()]
self.minmod = {}
for nm in self.p_good_models:
x = range(1,1+self.nit)
ax4.plot(x, [self.avg_abs_adj[nm][_][3] for _ in range(len(self.avg_abs_adj[nm]))], marker='o', ms=3, lw=.75, color='grey')
self.minmod[nm]=mean([self.avg_abs_adj[nm][_][3] for _ in range(len(self.avg_abs_adj[nm]))])
self.ind = sorted(self.minmod.items(), key=operator.itemgetter(1))[0][0]
self.minmod = [self.avg_abs_adj[self.ind][i][3] for i in range(len(self.avg_abs_adj[self.ind]))]
ax4.plot(x, self.minmod,marker='o', ms=3, lw=.75, color='red',label='Minimum model,%d'%(self.ind))
ax4.set_xlabel('Iteration (#)')
ax4.set_ylabel('Depth Adjustment (km)')
ax4.set_ylim(0.0,self.dp_adj)
ax4.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
ax4.locator_params(axis='x',nbins=5)
ax4.locator_params(axis='y',nbins=5)
ax4.legend(loc=1, fontsize=5)
ax4.set_xticks(x)
ax4.set_xticklabels(x)
ax5 = plt.subplot(2,3,5)
[i.set_linewidth(0.6) for i in ax5.spines.itervalues()]
self.minmod = {}
self.test = {}
for nm in self.p_good_models:
x = range(0,1+self.nit)
ax5.plot(x, self.rms_res[nm],marker='o', ms=3, lw=.75, color='grey')
self.minmod[nm]=mean(self.rms_res[nm])
self.ind = sorted(self.minmod.items(), key=operator.itemgetter(1))[0][0]
self.minmod = self.rms_res[self.ind]
ax5.plot(x, self.minmod,marker='o', ms=3, lw=.75, color='red',label='Minimum model,%d'%(self.ind))
ax5.set_xlabel('Iteration (#)')
ax5.set_ylabel('RMS-Residual (s)')
ax5.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
ax5.locator_params(axis='x',nbins=5)
ax5.locator_params(axis='y',nbins=5)
ax5.legend(loc=1, fontsize=5)
ax5.set_xticks(x)
ax5.set_xticklabels(x)
ax6 = plt.subplot(2,3,6)
[i.set_linewidth(0.6) for i in ax6.spines.itervalues()]
x,y, ymax = [],[],[]
for it,c,s in zip(self.vel_adj[self.ind],range(len(iter_color)),linspace(3,12,self.nit)[::-1]):
color = iter_color[c]
for p in it:
x.append(p[0])
y.append(p[1])
ymax.append(max([abs(_) for _ in y]))
it = array(it)
ax6.plot([it[:,1],zeros_like(it[:,1])],[it[:,0],it[:,0]],
markevery=it[:,0].size,lw=s,c=color,label='Iter: '+str(c+1))
ax6.set_xlabel('Velocity Adjusment (km/s)')
ax6.set_ylabel('Depth (km)')
ax6.set_xlim(-max(ymax),max(ymax))
ax6.locator_params(axis='x',nbins=5)
ax6.locator_params(axis='y',nbins=5)
ax6.set_ylim(ax6.get_ylim()[::-1])
ax6.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(zip(labels, handles))
lgnd=plt.legend(by_label.values(), by_label.keys(),fontsize=5)
for legobj in lgnd.legendHandles:
legobj.set_linewidth(3.0)
ax6.axvline(lw=1,color='k',linestyle='--')
plt.tight_layout()
plt.savefig(path.join('figs',self.project_nm,'velest_analytics_'+self.project_nm+'.tiff'))
plt.close()
# plot results, final velocity, station correction beside final location using minimum model
cnv_dic = ReadCNV(inpcnv=path.join('velout',self.project_nm,'final_loc%d.cnv'%(self.ind+1)))
vel_dic,sta_dic = self.read_VelSta(inp_vel=path.join('velinp',self.velmod_name),inp_sta=self.input_dic['Stationfile'])
print "+++ Plotting data statistics ..."
ovel = [] # origin
odep = [] # origin
stnm = sta_dic.keys()
stlat = [sta_dic[i]['lat'] for i in sta_dic] # station latitude
stlon = [sta_dic[i]['lon'] for i in sta_dic] # station longitude
stref = [sta_dic[i]['rfn'] for i in sta_dic] # station reference
evlat = [cnv_dic[i]['lat'] for i in cnv_dic] # event latitude
evlon = [cnv_dic[i]['lon'] for i in cnv_dic] # event longitude
evdep = [cnv_dic[i]['dep'] for i in cnv_dic] # event depth
evgap = [cnv_dic[i]['gap'] for i in cnv_dic] # event gap
evrms = [cnv_dic[i]['rms'] for i in cnv_dic] # event rms
evarr = [cnv_dic[i]['arr'] for i in cnv_dic] # event arrival time
nump = [cnv_dic[i]['arr']['wt'].count(0)+
cnv_dic[i]['arr']['wt'].count(1)+
cnv_dic[i]['arr']['wt'].count(2)+
cnv_dic[i]['arr']['wt'].count(3) for i in cnv_dic] # event arrival time number
refid = stref.index(max(stref))
for _,__ in zip(range(len(vel_dic['P']['p_vel'])),range(len(vel_dic['P']['p_dep']))):
ovel.append(vel_dic['P']['p_vel'][_])
ovel.append(vel_dic['P']['p_vel'][__])
odep.append(vel_dic['P']['p_dep'][__])
odep.append(vel_dic['P']['p_dep'][__])
odep.pop(0)
odep.append(self.dep_max)
tmpvel_p = [[] for nm in range(self.nmod)]
tmpdep_p = [[] for nm in range(self.nmod)]
for nm in range(self.nmod):
for _,__ in zip(self.svel_p[nm],self.sdep_p[nm]):
tmpvel_p[nm].append(_)
tmpvel_p[nm].append(_)
tmpdep_p[nm].append(__)
tmpdep_p[nm].append(__)
tmpdep_p[nm].pop(0)
tmpdep_p[nm].append(self.dep_max)
self.svel_p[nm] = tmpvel_p[nm]
self.sdep_p[nm] = tmpdep_p[nm]
# plot-No 1, velocity model
gs = gridspec.GridSpec(4, 4)
fig = plt.figure()