-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.py
executable file
·1992 lines (1691 loc) · 57.5 KB
/
utils.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
"""
Utilities used in NGA classes
"""
import os, time
import numpy as np
import matplotlib.pyplot as plt
# ===================
# General Functions
# ===================
def cpt_sqrt( a,b ):
a = np.array( a )
b = np.array( b )
return np.sqrt( a**2 + b**2 )
def RMScalc( V1,V2, Ratio=True ):
N = len(V1)
if len(V2) != N:
print 'length of array2 should be the same as array1'
raise ValueError
else:
if 0 in V2:
print 'elements in array2 should not be zeros'
raise ValueError
if Ratio:
Error = (V1-V2)/V2
else:
Error = (V1-V2)
RMS = np.sqrt( sum(Error**2) / N )
return RMS
def logline(x1,x2,y1,y2,x):
# linear interpolation
k = (y2-y1)/(x2-x1)
C = y1-k*x1
y = k*x+C
return y
def GetKey(key):
return '%.3f'%(key)
def HourMinSecToSec(BlockName=None):
hour, min, sec = time.localtime()[3:6]
sec1 = hour*60*60 + min*60 + sec
if BlockName != None:
print '%s'%BlockName
return sec1
def SecToHourMinSec(sec1,BlockName=None):
hour = sec1//3600
min = (sec1-hour*3600)//60
sec = sec1-hour*3600-min*60
if BlockName == None:
BlockName = 'the above block'
print 'Time cost of %s is %s hr %s min %s sec'%(BlockName,hour,min,sec)
return hour,min,sec
# save and load (useful metadata operations
class namespace:
"""
Namespace with object attributes initialized from a dict
Turn Dictionary keys into object attributes
d['KeyName'] -> d.KeyName
d is a dictioanry
"""
def __init__( self, d ):
self.__dict__.update(d)
# load dictionary saved in the python files
def load( f, d=None ):
"""
load variables from Python source files
Input:
f: file object (fid)
or filename with full path (string)
d: dictionary ( None default )
Output:
namespace(d) : updated dictionary
"""
if type(f) is not file:
f = open( os.path.expanduser(f) ) # get the file object
if d is None:
d = {}
exec f in d
return namespace( d )
# save dictionary into python file (used in metadata manipulation)
def save( fd, d, expand=None, keep=None, header=''):
"""
Write variables from a dict into a Python source file.
"""
if type( d ) is not dict:
d = d.__dict__
if expand is None:
expand = []
out = header
for k in sorted( d ):
if k not in expand and (keep is None or k in keep):
out += '%s = %r\n' % (k, d[k])
for k in expand:
print 'test expand'
if k in d:
if type( d[k] ) is tuple:
out += k + ' = (\n'
for item in d[k]:
out += ' %r,\n' % (item,)
out += ')\n'
elif type( d[k] ) is list:
out += k + ' = [\n'
for item in d[k]:
out += ' %r,\n' % (item,)
out += ']\n'
elif type( d[k] ) is dict:
out += k + ' = {\n'
for item in sorted( d[k] ):
out += ' %r: %r,\n' % (item, d[k][item])
out += '}\n'
else:
sys.exit( 'Cannot expand %s type %s' % ( k, type( d[k] ) ) )
if fd is not None:
if type( fd ) is not file:
fd = open( os.path.expanduser( fd ), 'w' )
fd.write( out )
return out
# this function will be used a lot (general map function as in Python)
def mapfunc(func,*args,**kwds):
"""
Modified function map tool
Account for the single argument and multiple arguments
Account for the keywords input
"""
# arguments
na = len(args) # number of arguments
args0 = {}
for ina in xrange( na ):
key1 = '%s'%ina
args0[key1] = {}
try:
tmp = len(args[ina])
if tmp == 1:
# [1,], 'a', ['AB',],{}
key2 = '%s'%(tmp-1)
args0[key1][key2] = args[ina][tmp-1]
else:
if isinstance( args[ina], str ):
# 'AB' case
key2 = '%s'%0
args0[key1][key2] = args[ina]
else:
# [1,2,...],['a','b',...],['AB','CD',...] case
for il in xrange( tmp ):
key2 = '%s'%il
args0[key1][key2] = args[ina][il]
except:
# single number as input
key2 = '%s'%0
args0[key1][key2] = args[ina]
del(args)
# keywords
keys = kwds.keys()
nk = len( keys )
if nk != 0:
# Has keywords input
kwds0 = {}
for ink,key1 in enumerate( keys ):
kwds0[key1] = {}
try:
tmp = len( kwds[key1] ) # elements each keyword has
if tmp == 1:
# [1,], 'a', ['AB',]
key2 = '%s'%(tmp-1)
kwds0[key1][key2] = kwds[key1][tmp-1]
else:
if isinstance( kwds[key1], str ):
# 'AB' case
key2 = '%s'%0
kwds0[key1][key2] = kwds[key1]
else:
# [1,2,...],['a','b',...],['AB','CD',...] case
for il in xrange( tmp ):
key2 = '%s'%il
kwds0[key1][key2] = kwds[key1][il]
except:
# single number as input
key2 = '%s'%0
kwds0[key1][key2] = kwds[key1]
del( kwds )
# get the maximum list length
nl = 0; nla = 0; nlk = 0
for ina in xrange( na ):
key1 = '%s'%ina
nla0 = len( args0[key1].keys() )
if nla0 >= nla:
nla = nla0
for ink in xrange( nk ):
key1 = keys[ink]
nlk0 = len( kwds0[key1].keys() )
if nlk0 >= nlk:
nlk = nlk0
nl = max( nlk, nla )
# check input args and kwds
for ina in xrange( na ):
key1 = '%s'%ina
nl0 = len(args0[key1].keys())
if nl0 != 1 and nl0 < nl:
print 'input argument length error!'
raise ValueError
for ink in xrange( nk ):
key1 = keys[ink]
nl0k = len(kwds0[key1].keys()) # number of elements for each arguments (for map)
if nl0k != 1 and nl0k < nl:
print 'input kwds element length error!'
raise ValueError
# map function
value = []
for il in xrange( nl ):
arg0 = []; kwd0 = {}
for ina in xrange( na ):
key1 = '%s'%ina
nl0 = len( args0[key1].keys() )
if nl0 == 1:
element = args0[key1]['0']
else:
key2 = '%s'%il
element = args0[key1][key2]
arg0.append(element)
for ink in xrange( nk ):
nlk = len(kwds0[keys[ink]]) # number of elements for each arguments (for map)
key1 = keys[ink]
if nlk == 1:
kwd0[key1] = kwds0[key1]['0']
else:
key2 = '%s'%il
kwd0[key1] = kwds0[key1][key2]
value.append( func( *arg0, **kwd0 ) )
else:
# No keywords input (use the default of the original function)
nl = 0
for ina in xrange( na ):
key1 = '%s'%ina
nl0 = len( args0[key1].keys() )
if nl0 >= nl:
nl = nl0
# check input args
for ina in xrange( na ):
key1 = '%s'%ina
nl0 = len(args0[key1].keys())
if nl0 != 1 and nl0 < nl:
print 'input argument length error!'
raise ValueError
# map function
value = []
for il in xrange( nl ):
arg0 = []; kwd0 = {}
for ina in xrange( na ):
key1 = '%s'%ina
nl0 = len( args0[key1].keys() )
if nl0 == 1:
element = args0[key1]['0']
else:
key2 = '%s'%il
element = args0[key1][key2]
arg0.append(element)
value.append( func( *arg0 ) )
return value # return is a list even taking single number for each input (attention)
# geometrical projection (general)
def projection(x,y,**kwds):
"""
Projection of lon/lat to UTM or reverse direction
input:
x,y ( lon/lat or x/y )
kwds: zone, origin, rot, inverse
output:
x,y ( x/y or lon/lat )
"""
import pyproj
zone = kwds['zone']
origin = kwds['origin']
rot = kwds['rot']
inverse = kwds['inverse']
if origin == None:
return
# geometrical origin
x0 = origin[0]; y0 = origin[1]
rot = rot*np.pi/180.
c,s = np.cos(rot),np.sin(rot)
x = np.array(x,'f')
y = np.array(y,'f')
# you can use other projections (modify here)
proj = pyproj.Proj(proj='utm',zone=zone,ellps='WGS84')
if inverse:
x0,y0 = proj(x0,y0,inverse=False)
x,y = c*x-s*y, s*x+c*y
x,y = x+x0,y+y0
x,y = proj(x,y,inverse=True)
else:
x0,y0 = proj(x0,y0,inverse=False)
x,y = proj(x,y,inverse=False)
x,y = x-x0,y-y0
x,y = x*c+y*s, -s*x+c*y
return x,y
# =========================================================================================
# NGA database related
# =========================================================================================
def RakeBin(rakes):
# rakes in degree, list
# rake: [-180,180]
# 0: strike-slip, [-180,-150], [-30,30], [150,180]
# 1: normal, [-120,-60]
# 2: reverse, [60,120]
# 3: reverse-oblique, [30,60], [120, 150]
# 4: Normal-oblique, [-150,-120], [-60, -30]
# These rules come from NGA flatfile
group = {}
groupnames = {'U':['k','Unknown'],'SS':['r','Strike-Slip'],'NM':['g','Normal'],\
'RV':['b','Reverse'],'NO':['#808080','Normal-Oblique'],'RO':['m','Reverse-Oblique']}
for ig,groupname in enumerate( groupnames.keys() ):
group[groupname] = []
for ir in xrange( len(rakes) ):
rake = rakes[ir]
if rake>180. or rake < -180. or rake == None:
group['U'].append( rake )
if -180<= rake <= -150 or -30<=rake<=30 or 150 <= rake <= 180:
group['SS'].append( rake )
if -120<=rake<=-60:
group['NM'].append( rake )
if 60 <= rake <= 120:
group['RV'].append( rake )
if 30<rake<60 or 120<rake<150:
group['RO'].append( rake )
if -150<rake<-120 or -60 < rake < -30:
group['NO'].append( rake )
return group, groupnames
def Vs30Bin(Vs30s):
# Vs30s in m/s, list
# A: => 1500
# B: [760, 1500)
# C: [360, 760)
# D: [180, 360)
# E: < 180
# This rules come from NGA flatfile
group = {}
groupnames = {'A':['k','Hard Rock'],'B':['r','Rock'],'C':['g','Dense Soil and Soft Rock'],\
'D':['b','Stiff Soil'],'E':['m','Soft Soil']}
for ikey, key in enumerate( groupnames.keys() ):
group[key] = []
for iv in xrange( len( Vs30s ) ):
Vs30 = Vs30s[iv]
if Vs30 >= 1500.:
group['A'].append( Vs30 )
if 760. <= Vs30 < 1500.:
group['B'].append( Vs30 )
if 360. <= Vs30 < 760.:
group['C'].append( Vs30 )
if 180. <= Vs30 < 360.:
group['D'].append( Vs30 )
if Vs30 < 180.:
group['E'].append( Vs30 )
return group, groupnames
# =========================================================================================
# Functions to compute exploratory variables used in GMPEs
# =========================================================================================
# ==============
# Fault type
# ==============
def rake2ftype_BA(rake):
if rake == None:
ftype = 'U'
if rake != None:
if -30. <= rake <= 30. or 150. <= rake <= 180. or -180. <= rake <=-150.:
ftype = 'SS' # strike-slip
elif 30. < rake < 150.:
ftype = 'RV' # reverse
elif -150. <= rake <= -30.:
ftype = 'NM' # normal
else:
print 'Wrong rake angle!'
raise ValueError
return ftype
def rake2ftype_CB(rake):
Frv = 0; Fnm = 0
if 30 < rake < 150:
Frv=1
if -150 < rake < -30:
Fnm=1
return Frv, Fnm
def rake2ftype_CY(rake):
Frv, Fnm = 0, 0
if 30 <= rake <= 150:
Frv = 1
elif -120 <= rake <= -60:
Fnm = 1
return Frv, Fnm
def rake2ftype_AS(rake):
Frv, Fnm = 0, 0
if 30 <= rake <= 150:
Frv = 1
elif -120 <= rake <= -60:
Fnm = 1
return Frv, Fnm
# ===================
# Fault geometry
# ===================
def calc_dip( rake ):
"""
Empirical determination of dip angle from the faulting style
Input:
rake in degree (-180<=rake<=180)
Output:
dip angle in degree
"""
if abs(rake) > 180:
print 'rake angle should be within -180 and 180'
raise ValueError
if abs(rake)<=30 or abs(rake)>=150:
dip = 90
elif -150 < rake < -30:
dip = 50
elif 30 < rake < 150:
dip = 40
return dip
def calc_Zhypo(M,rake):
"""
Compute Zhypo from empirical relations
When Ztor is unknown from input models
"""
if M < 0:
print 'Magnitude should be larger than 0'
raise ValueError
if abs(rake) > 180:
print 'rake angle should be within -180 and 180'
raise ValueError
if abs(rake) < 30 or abs(rake) > 150:
# strike-slip
Zhypo = 5.63 + 0.68 * M
else:
Zhypo = 11.24 - 0.2 * M
return Zhypo
def calc_W(M,rake):
"""
Compute fault width when not specified by input
"""
if M < 0:
print 'Magnitude should be larger than 0'
raise ValueError
# In R
if abs(rake) > 180:
print 'rake angle should be within -180 and 180'
raise ValueError
if abs(rake) < 30 or abs( rake ) > 150:
W = 10 ** (-0.76+0.27*M)
elif -150 <= rake <= -30:
W = 10 ** (-1.14+0.35*M)
elif 30 <= rake <= 150:
W = 10 ** (-1.61+0.41*M)
# In Matlab
#W = 10**(-1.01+0.32*M)
return W
def calc_Ztor(W,dip,Zhypo):
"""
Compute Ztor if not specified by input
dip should be in degree
"""
if dip <= 0 or dip > 90:
print 'dip angle should be with in (0,90]'
raise ValueError
if W <= 0:
print 'Fault width should be larger than 0'
raise ValueError
if Zhypo < 0:
print 'Zhypo should be larger than 0'
raise ValueError
Ztor = max( Zhypo-0.6*W*np.sin( dip * np.pi/ 180 ), 0 )
return Ztor
# ======================================
# Fault-Site distances (Rjb, Rx, Rrup)
# ======================================
def calc_Rx(Rjb, Ztor, W, dip, azimuth, Rrup=None):
"""
Compute distance parameter Rx from other inputs
"""
if Rjb < 0:
print 'Joyer-Boore distance Rjb should be larger than 0'
raise ValueError
if Ztor < 0:
print 'Ztor should be larger than 0'
raise ValueError
if W <= 0:
print 'Fault width should be larger than 0'
raise ValueError
if dip<=0 or dip > 90:
print 'dip angle should be (0,90]'
raise ValueError
if abs( azimuth ) > 180.0:
print 'azimuth should be width in -180.0 and 180.0'
raise ValueError
d = dip * np.pi/180 # degree to radius
a = azimuth * np.pi/180
if dip != 90:
if azimuth > 0:
if azimuth == 90:
if Rjb == 0:
if Rrup != None:
if Rrup < Ztor / np.cos(d):
Rx = np.sqrt( Rrup**2 - Ztor**2 )
else:
Rx = Rrup / np.sin( d ) - Ztor / np.tan( d )
else:
Rx = W * np.cos(d)/2.
# empirical relation (Rrup is easy to compute)
# assume that the site is located at the center of the surface projection of the rupture plane
else:
Rx = Rjb + W * np.cos(d)
else:
if Rjb*abs(np.tan(a)) <= W*np.cos(d):
Rx = Rjb * abs( np.tan(a) )
else:
Rx = Rjb * np.tan(a) * np.cos( a-np.arcsin(W*np.cos(d)*np.cos(a)/Rjb) )
else:
Rx = Rjb * np.sin(a)
else:
Rx = Rjb * np.sin(a)
return Rx
def calc_Rrup( Rx, Ztor, W, dip, azimuth, Rjb=None ):
"""
Compute the closest distance from site the the surface the fault
"""
if Ztor < 0:
print 'Ztor should be larger than 0'
raise ValueError
if W <= 0:
print 'Fault width should be larger than 0'
raise ValueError
if dip<=0 or dip > 90:
print 'dip angle should be (0,90]'
raise ValueError
if abs( azimuth ) > 180:
print 'azimuth should be width in -180 and 180'
raise ValueError
d = dip * np.pi/180 # degree to radius
a = azimuth * np.pi/180
if dip == 90 and Rjb != None:
Rrup = np.sqrt( Rjb**2 + Ztor**2 )
return Rrup
if dip != 90:
if Rx < Ztor * np.tan( d ):
Rrup1 = np.sqrt( Rx**2 + Ztor**2 )
elif Rx >= Ztor*np.tan(d) and Rx <= Ztor*np.tan(d)+W/np.cos(d):
Rrup1 = Rx*np.sin(d) + Ztor*np.cos(d)
elif Rx > Ztor*np.tan(d) + W/np.cos(d):
Rrup1 = np.sqrt( (Rx-W*np.cos(d))**2 + (Ztor+W*np.sin(d))**2 )
elif dip == 90:
Rrup1 = sqrt( Rx**2 + Ztor**2 )
if azimuth == 90 or azimuth == -90:
Ry = 0
elif azimuth == 0 or azimuth == 180 or azimuth == -180:
if Rjb == None:
print 'Rjb cannot be None in the case azimuth == 0 or azimuth == 180 or azimuth == -180'
raise ValueError
else:
Ry = Rjb
else:
Ry = abs( Rx/np.tan(a) )
Rrup = np.sqrt( Rrup1**2 + Ry**2 )
return Rrup
# One option of doing Fling and BBP distance calculation
def calc_distances(SiteGeo, Dims, Mech, ProjDict, Rrup=False, Rx=False):
"""
Compute Rjb, Rrup, Rx implicitly given fault geometry and site location (in lon/lat)
The grid generation is needed to get the explicit fault geometry for further calculation
For Fling study and broadband platform (*.src file)
"""
# UTM projection property
lon0, lat0 = ProjDict['origin'] # projection origin
ProjDict['inverse'] = False # from ll to xy
kwds = ProjDict
# sites and compute the azimuth
rlon, rlat = SiteGeo
rx, ry = projection( rlon, rlat, **kwds )
# Fault dimension and focal mechanism
Fl,dfl,Fw,dfw,ztor = Dims # fault length along strike, fault width down dip
strike, dip, rake = Mech # fault mechanism
# create fault mesh:
ProjDict['inverse'] = True # from xy to ll
kwds = ProjDict
# along strike direction: y-axis
# along dip direction: x-axis
fx = np.arange( 0, Fw+dfw, dfw ) # along dip (x direction)
fy = np.arange( 0, Fl+dfl, dfl ) - Fl/2 # along strike (y direction)
fx, fy = fx*1000, fy*1000
fxx, fyy = np.meshgrid( fx, fy )
fzz = fxx * np.sin( dip * np.pi/180. ) # in meter
sdep2d = fzz / 1000 # in km
slon2d, slat2d = projection( fxx, fyy, **kwds )
# surface projection (change in dip direction)
fxS = fx * np.cos( dip*np.pi/180. )
fxxS, fyy = np.meshgrid( fxS, fy )
slon2dS, slat2dS = projection( fxxS, fyy, **kwds )
Nlat, Nlon = slon2d.shape
Nloc = Nlat * Nlon
fxxS1d = fxxS.reshape( Nloc ) / 1000.
fxx1d = fxx.reshape( Nloc ) /1000.
fyy1d = fyy.reshape( Nloc ) /1000.
fzz1d = fzz.reshape( Nloc ) /1000.
# compute Rjb using fault and site locations and get the azimuth of all sites for later use to compute Rx
Nsta = len(rlon)
Rjb = []; Rrup = []; azimuth = []
print 'Computing Rjb, and Rrup, and azimuth...'
for ista in xrange( Nsta ):
rx0 = rx[ista] / 1000.
ry0 = ry[ista] / 1000.
# Rjb
if fxxS1d.min() <= rx0 <= fxxS1d.max() and fyy1d.min() <= ry0 <= fyy1d.max():
Rjb.append( 0 )
else:
distS = []
for iloc in xrange( Nloc ):
dx = fxxS1d[iloc] - rx0
dy = fyy1d[iloc] - ry0
distS.append( np.sqrt( dx**2 + dy**2 ) )
Rjb.append( min(distS) )
# Rrup
dist = []
for iloc in xrange( Nloc ):
dx = fxx1d[iloc] - rx0
dy = fyy1d[iloc] - ry0
dist.append( np.sqrt( dx**2 + dy**2 + fzz1d[iloc]**2 ) )
Rrup.append( min(dist) )
# compute azimuth (different from common used)
# refer to James's defination:
# The angle between positive fault strike direction and line connecting a site to the closest point on
# the surface projection of the TOP EDGE of rupture, which clockwise assumed positive !
# fy: the surface projection of the top edge of rupture
# different cases
fymin = fy.min()/1000.; fymax= fy.max()/1000.
if fymin <= ry0 <= fymax:
azimuth0 = -np.pi/2. * (rx0<0.0) + np.pi/2 * (rx0>=0.0)
if ry0 > fymax:
dx = rx0 - 0.0
dy = ry0 - fymax
if rx0 > 0.0:
azimuth0 = np.arctan( dx/dy )
elif rx0 < 0.0:
azimuth0 = -np.arctan( -dx/dy )
elif rx0 == 0.0:
azimuth0 = 0.0
if ry0 < fymin:
dx = rx0 - 0.0
dy = fymin - ry0
if rx0 > 0.0:
azimuth0 = np.pi - np.arctan( dx / dy )
elif rx0 < 0.0:
azimuth0 = np.arctan( -dx/dy ) - np.pi
elif rx0 == 0.0:
azimuth0 = np.pi
azimuth.append( azimuth0*180./np.pi )
# Compute Rx from Rjb and Rrup
Rx = mapfunc( calc_Rx, Rjb, ztor, Fw, dip, azimuth, Rrup=Rrup )
OutputDict = {}
OutputDict['Rjb'] = Rjb
OutputDict['Rx'] = Rx
OutputDict['Rrup'] = Rrup
OutputDict['azimuth'] = azimuth
return OutputDict
# =======================================================================================
# Utilities for general distance calculations (in spherical coordinates and earth-flatten)
# =======================================================================================
R = 6371. # Earth radius (km)
DegToKm = np.pi/180. * R # 1 degree to km
tol = 1e-10
def LonLatToAngleDistance( loc1, loc2, \
CalcRadius=True, \
CalcDist=True, Fast=True, \
CalcAzimuth=True, Azimuth0to2PI=False ):
"""
Convert lon/lat to radius (earth center) and/or azimuth and great circle Distances
between points on the spherical surface
Inputs:
Loc1: lon1, lat1 in degree, dep1 in km (Starting point in azimuth calculation)
Loc2: lon2, lat2 in degree, dep2 in km
Outputs:
Radius: angle between two points (central angle)
Azimuth1to2: azimuth (relative to north pole) from point 1 to point2, default: [-pi,pi]
horzDistance: distance between two points (great circle along spherical surface, km)
vertDistance: distance between two points vertically
Angles in radius
# converted from OpenSHA jave to python (credit to OpenSHA developers)
org.opensha.commons.geo.Location*
and in the website:
http://en.wikipedia.org/wiki/Haversine_formula
http://www.movable-type.co.uk/scripts/latlong.html
"""
loc1 = np.array( loc1 )
loc2 = np.array( loc2 )
lon1, lat1 = loc1[:2] * np.pi/180.
lon2, lat2 = loc2[:2] * np.pi/180.
# initialization
Radius = 0.
horzDistance = 0.
verzDistance = 0.
Azimuth1to2 = 0.
if CalcRadius:
sinDlatBy2 = np.sin( (lat2-lat1)/2.0 )
sinDlonBy2 = np.sin( (lon2-lon1)/2.0 )
c = (sinDlatBy2**2) + np.cos(lat1)*np.cos(lat2) * sinDlonBy2**2
Radius = 2.0 * np.arctan2( np.sqrt(c), np.sqrt(1-c) ) # in rad (to keep angle in between pi and -pi)
# central angle from point1 to point2
if CalcDist:
if Fast:
dlat = lat1 - lat2
dlon = (lon1 - lon2) * np.cos( (lat1+lat2)/2.0 )
horzDistance = R * np.sqrt( dlat**2 + dlon**2 )
else:
sinDlatBy2 = np.sin( (lat2-lat1)/2.0 )
sinDlonBy2 = np.sin( (lon2-lon1)/2.0 )
c = (sinDlatBy2**2) + np.cos(lat1)*np.cos(lat2) * sinDlonBy2**2
Radius = 2.0 * np.arctan2( np.sqrt(c), np.sqrt(1-c) )
horzDistance = R * Radius
dep1, dep2 = loc1[2], loc2[2]
verzDistance = dep1-dep2
if CalcAzimuth:
# calculate azimuth (p1 to p2 vector relative to p1 to north pole)
dlon = lon2-lon1
cosLat2 = np.cos(lat2)
y1 = np.sin(dlon) * cosLat2
y2 = np.cos( lat1 )*np.sin(lat2) - np.sin(lat1)*cosLat2*np.cos(dlon)
Azimuth1to2 = np.arctan2( y1, y2 )
if Azimuth0to2PI:
Azimuth1to2 = (Azimuth1to2+2*np.pi)%(2*np.pi)
return Radius, horzDistance, verzDistance, Azimuth1to2
def EndLocation(loc1, vector):
"""
Given Vector and its starting point, find the end point of the vector, where
vector has information (azimuth,horizontal distance and vertical distance)
# converted from OpenSHA jave to python (credit to OpenSHA developers)
org.opensha.commons.geo.Location*
Line extension (limited, not to infinite)
"""
loc1 = np.array( loc1 )
loc1[:2] = loc1[:2] * np.pi / 180.
lon1, lat1, dep1 = loc1
az, DH, DV = vector # az in radius
# refer to http://williams.best.vwh.net/avform.htm#LL
sinLat1 = np.sin(lat1)
cosLat1 = np.cos(lat1)
ad = DH / R
sinD = np.sin(ad)
cosD = np.cos(ad)
# compute the location information for the end point loc2
lat2 = np.arcsin( sinLat1*cosD + cosLat1*sinD*np.cos(az) )
dlon = np.arctan2(np.sin(az)*sinD*cosLat1, cosD-sinLat1*np.sin(lat2))
lon2 = lon1 + dlon
dep2 = dep1 + DV
lat2 = lat2*180./np.pi
lon2 = lon2*180./np.pi
return [lon2,lat2,dep2]
def CheckPointInPolygon(point, verts):
"""
check whether a point is inside a polygon (general coordiate and convex or non-convex)
refer to: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
inputs:
point: test point (x,y,[z]), z could be None
verts: points that define the polygon shape (the last one and the first one are not the same!)
return: True or False
"""
Ndim = len(point)
verts = np.array( verts )
dim = verts.shape[1]
if Ndim != dim:
print 'point and shape should be defined with two coordinates'
raise ValueError
# test point
testx = point[0]
testy = point[1]
vertx = verts[:,0]
verty = verts[:,1]
nvert = len(vertx)
check = False
j = nvert - 1
for i in xrange( nvert ):
c1 = verty[i]>testy
c2 = verty[j]>testy
factor = (vertx[j]-vertx[i])*(testy-verty[i])/(verty[j]-verty[i]) + vertx[i]
if c1 != c2 and testx < factor:
check = not check
j = i # edge is defined from j to i
return check
# point to 2D line
def ptToLine2D(x1,y1,x2,y2,px,py):
"""
Compute the point (px,py) to line (x1,y1)=>(x2,y2) allowing infinitely-extending of the line
Not used
"""
# get the projected point
p1 = np.array([x1,y1])
p2 = np.array([x2,y2])
p = np.array([px,py])
v = p2-p1
n = v/np.sqrt(sum(v*v)) # unit vector to show the line direction
w10 = p1-p
w10n = sum( w10*n ) * n
Ploc = w10 - w10n + p
#dist1 = np.sqrt( sum((p-Ploc)*(p-Ploc)) ) # should be the same as the below
#print dist1
# compute distance
# adjust vectors relative to point (x1,y1)
x2 -= x1*1.0
y2 -= y1*1.0
px -= x1*1.0
py -= y1*1.0
# 1. projection using dot production of adjusted vector (px,py) and (x2,y2)
dotprod = ( px * x2 + py * y2 )
projLenSq = dotprod * dotprod / (x2*x2+y2*y2) # length of the vector (x1,y1)=>projected point of (px,py) on the line
# 2. subtraction to get the closet distance (length of the vector)
lenSq = px*px + py*py - projLenSq
if lenSq < 0:
# # (px,py) is in the line specified by (x1,y1) and (x2,y2)
lenSq = 0
dist = np.sqrt( lenSq )
#return ( (-x2*py-(-px)*y2) * 1.0 /np.sqrt(x2**2+y2**2) )
return dist, Ploc
# great circle distance (point to 2D line)
def distToLine2D(loc1, loc2, loc3, Fast=False):
"""
Compute the shortest distance between a point (loc3) and a line (great circle)
that extends infinitely in both directiions. Depth is ignored.
refer to: http://williams.best.vwh.net/avform.htm#XTE
# this distance could be postive or negative
"""
loc1 = np.array( loc1 )
loc2 = np.array( loc2 )
loc3 = np.array( loc3 )
lon1, lat1 = loc1[:2] * np.pi/180.
lon2, lat2 = loc2[:2] * np.pi/180.
lon3, lat3 = loc3[:2] * np.pi/180.
if Fast:
# with earth-flatten approximation (faster)
# used in shorter distance (<=200km)
lonScale = np.cos( 0.5*lat3 + 0.25*lat1 + 0.25*lat2 ) # earth-flatten approximation factor
x2 = (lon2-lon1)*lonScale
y2 = lat2-lat1
x3 = (lon3-lon1)*lonScale
y3 = lat3 - lat1
# x1=y1 = 0
Term1 = (x2*(-y3)-(-x3)*y2)/np.sqrt(x2**2+y2**2)
# originally, Term1 =
# abs( (x3-x1)*(y2-y1) - (y3-y1)*(x2-x1) ) / np.sqrt((x2-x1)**2+(y2-y1)**2.)
# for x1=y1=0, Term1 = abs( x3*y2 - y3*x2 ) / [] = abs( -y3*x2 - (-x3)*y2 ) / []
# but here, Term1 has sign which indicates which side of point3 is located relative to the line vector (1to2)
# +: right; -:left
return Term1 * R
else:
# orignial method to compute the distance from a point to a line (spherical trigonometry)
# sin(A)/sin(a) = sin(B)/sin(b) = sin(C)/sin(c)
# A, B, and C: angle between surface circle
# a, b, and c: center angle of surface circle (great)
a13,hD,vD,az13 = LonLatToAngleDistance(loc1,loc3,CalcRadius=True,CalcDist=False,CalcAzimuth=True)
a12,hD,vD,az12 = LonLatToAngleDistance(loc1,loc2,CalcRadius=True,CalcDist=False,CalcAzimuth=True)
Daz13az12 = az13-az12
xtd = np.arcsin( np.sin(a13)*np.sin(Daz13az12) )
if abs(xtd) < tol:
return 0.0 # point3 is on the line
else:
return xtd * R # xtd could >0 or <0 to identify the location of the point3 relative to the line