forked from jolivetr/csi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRectangularPatches.py
More file actions
4032 lines (3304 loc) · 143 KB
/
RectangularPatches.py
File metadata and controls
4032 lines (3304 loc) · 143 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
'''
A parent class that deals with rectangular patches fault
Started by R. Jolivet, November 2013
Main contributors:
R. Jolivet, Ecole normale supérieure, France
Z. Duputel, Univ. de Strasbourg, France,
B. Riel, CalTech, USA
F. Ortega-Culaciati, Univ. de Santiago, Chile
'''
# Externals
import numpy as np
import pyproj as pp
import matplotlib.pyplot as plt
import matplotlib.path as path
import scipy.interpolate as sciint
from scipy.linalg import block_diag
from copy import deepcopy
import copy
import sys
import os
# Personals
from .Fault import Fault
from .stressfield import stressfield
from . import okadafull
from .geodeticplot import geodeticplot as geoplot
from .gps import gps as gpsclass
from .csiutils import colocated
def getDist(ij):
f,i,lim = ij
p1 = f.patch[i]
dist = []
for j in range(i):
p2 = f.patch[j]
dist.append(f.distancePatchToPatch(p1, p2, distance='center', lim=lim))
return i,dist
def getDistHV(ij):
f,i,lim = ij
p1 = f.patch[i]
c1 = self.getcenter(p1)
dist = []
for j in range(i):
p2 = f.patch[j]
c2 = self.getcenter(p2)
dist.append( (np.sqrt((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2), np.sqrt((c1[2]-c2[2])**2)) )
return i,dist
class RectangularPatches(Fault):
'''
Classes implementing a fault made of rectangular patches. Inherits from Fault
Args:
* name : Name of the fault.
Kwargs:
* utmzone : UTM zone (optional, default=None)
* lon0 : Longitude of the center of the UTM zone
* lat0 : Latitude of the center of the UTM zone
* ellps : ellipsoid (optional, default='WGS84')
* verbose : Speak to me (default=True)
'''
# ----------------------------------------------------------------------
# Initialize class
def __init__(self, name, utmzone=None, ellps='WGS84', lon0=None, lat0=None, verbose=True):
# Base class init
super(RectangularPatches,self).__init__(name,
utmzone=utmzone,
ellps=ellps,
lon0=lon0,
lat0=lat0,
verbose=verbose)
# Specify the type of patch
self.patchType = 'rectangle'
# Allocate depth and number of patches
self.numz = None # Number of patches along dip
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Set depth caracteristics
def setdepth(self, nump=None, top=None, width=None):
'''
Set depth patch attributes
Args:
* nump : Number of fault patches at depth.
* top : Depth of the top row
* width : Width of the patches
'''
# If there is patches
if self.patch is not None:
depth = [[p[2] for p in patch] for patch in self.patch]
depth = np.unique(np.array(depth).flatten())
self.z_patches = depth.tolist()
self.top = np.min(depth)
self.depth = np.max(depth)
# Set depth
if top is not None:
self.top = top
if nump is not None:
self.numz = nump
if width is not None:
self.width = width
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Extrapolate surface trace of the fault
def extrapolate(self, length_added=50, tol=2., fracstep=5., extrap='ud'):
'''
Extrapolates the surface trace. This is usefull when building deep patches
for interseismic loading.
Args:
* length_added : Length to add when extrapolating.
* tol : Tolerance to find the good length.
* fracstep : control each jump size.
* extrap : combination of 'u' (extrapolate from the beginning) and 'd' (extrapolate from the end). Default is 'ud'
'''
# print
print ("Extrapolating the fault for {} km".format(length_added))
# Check if the fault has been interpolated before
if self.xi is None:
print ("Run the discretize() routine first")
return
# Build the interpolation routine
import scipy.interpolate as scint
fi = scint.interp1d(self.xi, self.yi)
# Build the extrapolation routine
fx = self.extrap1d(fi)
# make lists
self.xi = self.xi.tolist()
self.yi = self.yi.tolist()
if 'd' in extrap:
# First guess for first point
xt = self.xi[0] - length_added/2.
yt = fx(xt)
d = np.sqrt( (xt-self.xi[0])**2 + (yt-self.yi[0])**2)
# Loop to find the best distance
while np.abs(d-length_added)>tol:
# move up or down
if (d-length_added)>0:
xt = xt + d/fracstep
else:
xt = xt - d/fracstep
# Get the corresponding yt
yt = fx(xt)
# New distance
d = np.sqrt( (xt-self.xi[0])**2 + (yt-self.yi[0])**2)
# prepend the thing
self.xi.reverse()
self.xi.append(xt)
self.xi.reverse()
self.yi.reverse()
self.yi.append(yt)
self.yi.reverse()
if 'u' in extrap:
# First guess for the last point
xt = self.xi[-1] + length_added/2.
yt = fx(xt)
d = np.sqrt( (xt-self.xi[-1])**2 + (yt-self.yi[-1])**2)
# Loop to find the best distance
while np.abs(d-length_added)>tol:
# move up or down
if (d-length_added)<0:
xt = xt + d/fracstep
else:
xt = xt - d/fracstep
# Get the corresponding yt
yt = fx(xt)
# New distance
d = np.sqrt( (xt-self.xi[-1])**2 + (yt-self.yi[-1])**2)
# Append the result
self.xi.append(xt)
self.yi.append(yt)
# Make them array again
self.xi = np.array(self.xi)
self.yi = np.array(self.yi)
# Build the corresponding lon lat arrays
self.loni, self.lati = self.xy2ll(self.xi, self.yi)
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Get fault strike for each patch
def getStrikes(self):
'''
Returns an array of strike angle for each patch (radians).
Returns:
* strike : Array of angles in radians
'''
# all done in one line
return np.array([self.getpatchgeometry(p)[5] for p in self.patch])
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Get patch dip angles
def getDips(self):
'''
Returns an array of dip angles for each patch (radians)
Returns:
* dip : Array of angles in radians
'''
# all done in one line
return np.array([self.getpatchgeometry(p)[6] for p in self.patch])
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Split patches
def splitPatchesHoriz(self, nPatches, equiv=False, indices=None, keepSlip=False):
'''
Splits all the patches in nPatches Horizontally. Directly modifies the
patch attribute. Default behavior re-intializes slip to zero.
If keepSlip, then slip values are mapped directly.
Args:
* nPatches : Number of new patches per patch (can be a list of the size of indices or an integer).
Kwargs:
* equiv : Do it on the equivalentPatches (default False)
* indices : Specify which patches to split (list of int)
* keepSlip : Map current slip values to new patches
'''
# Check style
if type(nPatches) is int:
nPatches = [nPatches]*len(indices)
if type(indices) is not list and indices is not None:
indices = list(indices)
# Check which patches we want to split
if indices is None:
patches2split = self.patch
indices = range(len(self.patch))
else:
patches2split = [self.patch[i] for i in indices]
# Create a list of new patches
newPatches = []
newPatchesLL = []
# keepSlip?
if keepSlip: newSlip = []
# Iterate over the patches
for ipatch,npatch,patch in zip(indices, nPatches, patches2split):
# Get the 4 corners
c1, c2, c3, c4 = patch
if type(c1) is not list:
c1 = c1.tolist()
c2 = c2.tolist()
c3 = c3.tolist()
c4 = c4.tolist()
# Compute the new lengths
xlength = (c2[0] - c1[0])/float(npatch)
ylength = (c2[1] - c1[1])/float(npatch)
# Iterate
for i in range(npatch):
# Corners
x1 = c1[0] + i*xlength
y1 = c1[1] + i*ylength
z1 = c1[2]
lon1, lat1 = self.xy2ll(x1, y1)
x2 = c1[0] + (i+1)*xlength
y2 = c1[1] + (i+1)*ylength
z2 = c1[2]
lon2, lat2 = self.xy2ll(x2, y2)
x3 = c4[0] + (i+1)*xlength
y3 = c4[1] + (i+1)*ylength
z3 = c3[2]
lon3, lat3 = self.xy2ll(x3, y3)
x4 = c4[0] + i*xlength
y4 = c4[1] + i*ylength
z4 = c3[2]
lon4, lat4 = self.xy2ll(x4, y4)
# Patch
patch = np.array( [ [x1, y1, z1],
[x2, y2, z2],
[x3, y3, z3],
[x4, y4, z4] ])
# Patch ll
patchll = np.array( [ [lon1, lat1, z1],
[lon2, lat2, z2],
[lon3, lat3, z3],
[lon4, lat4, z4] ])
# Store
newPatches.append(patch)
newPatchesLL.append(patchll)
if keepSlip: newSlip.append(self.slip[ipatch,:])
# Delete the patches we just split
self.deletepatches(indices)
# Add the patches
self.patch = self.patch+newPatches
self.patch2ll()
# Compute the equivalent patches
if equiv:
self.computeEquivRectangle()
# Initialize slip
if keepSlip:
self.slip = np.vstack((self.slip, newSlip))
else:
self.initializeslip()
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Split a patch in 4
def splitPatch(self, patch):
'''
Splits a patch in 4 patches and returns 4 new patches.
Args:
* patch : item of the list of patch
Returns:
* p1, p2, p3, p4: Four patches
'''
# Gets the 4 corners
c1, c2, c3, c4 = patch
if type(c1) is not list:
c1 = c1.tolist()
c2 = c2.tolist()
c3 = c3.tolist()
c4 = c4.tolist()
# Compute the center
xc, yc, zc = self.getcenter(patch)
center = [xc, yc, zc]
# Compute middle of segments
c12 = [c1[0] + (c2[0]-c1[0])/2.,
c1[1] + (c2[1]-c1[1])/2.,
c1[2] + (c2[2]-c1[2])/2.]
c23 = [c2[0] + (c3[0]-c2[0])/2.,
c2[1] + (c3[1]-c2[1])/2.,
c2[2] + (c3[2]-c2[2])/2.]
c34 = [c3[0] + (c4[0]-c3[0])/2.,
c3[1] + (c4[1]-c3[1])/2.,
c3[2] + (c4[2]-c3[2])/2.]
c41 = [c4[0] + (c1[0]-c4[0])/2.,
c4[1] + (c1[1]-c4[1])/2.,
c4[2] + (c1[2]-c4[2])/2.]
# make patches
p1 = np.array([c1, c12, center, c41])
p2 = np.array([c12, c2, c23, center])
p3 = np.array([center, c23, c3, c34])
p4 = np.array([c41, center, c34, c4])
# All done
return p1, p2, p3, p4
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Split all patches in 4
def splitPatches(self):
'''
Split all patches in 4 patches.
Returns:
* None
'''
# New slip vector
slip = []
# New patch list
patches = []
for ipatch,patch in enumerate(self.patch):
# Split
for p in self.splitPatch(patch): patches.append(p)
# Save slip
for p in range(4): slip.append(self.slip[ipatch,:])
# Save slip and patches
self.patch = patches
self.slip = np.array(slip)
# Clean up
self.patch2ll()
self.computeEquivRectangle()
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Merge patches into a common patch
def mergePatches(self, p1, p2, eps=1e-6, verbose=True):
'''
Merges 2 patches that have common corners. This modifies directly the attribute patch
Args:
* p1 : index of the patch #1.
* p2 : index of the patch #2.
Kwargs:
* eps : tolerance value for the patch corners (in km)
* verbose : Speak to me (default is True)
'''
if verbose:
print('Merging patches {} and {} into patch {}'.format(p1,p2,p1))
newpatch,newpatchll = self._mergePatches(p1, p2, eps=eps)
# Replace the patch 1 by the new patch
self.patch[p1] = newpatch
self.patchll[p1] = newpatchll
# Delete the patch 2
self.deletepatch(p2)
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Simple linear extrapolating routine (why is it here?)
def extrap1d(self,interpolator):
'''
Linear extrapolation routine. Found on StackOverflow by sastanin.
Args:
* interpolator : An instance of scipy.interpolation.interp1d
Returns:
* ufunc : An extrapolating method
'''
# import a bunch of stuff
from scipy import arange, array, exp
xs = interpolator.x
ys = interpolator.y
def pointwise(x):
if x < xs[0]:
return ys[0]+(x-xs[0])*(ys[1]-ys[0])/(xs[1]-xs[0])
elif x > xs[-1]:
return ys[-1]+(x-xs[-1])*(ys[-1]-ys[-2])/(xs[-1]-xs[-2])
else:
return interpolator(x)
def ufunclike(xs):
return pointwise(xs) #array(map(pointwise, array(xs)))
return ufunclike
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Convert the shallowest patches into a surface trace
def surfacePatches2Trace(self):
'''
Takes the shallowest patches of the fault and use them to build a
fault trace. Direclty modifies attributes xf, yf, lonf and latf
Returns:
* None
'''
# Create lists
xf = []
yf = []
# Iterate on patches
for p in self.patch:
for c in p:
if c[2]==0.0:
xf.append(c[0])
yf.append(c[1])
# Make arrays
xf = np.array(xf)
yf = np.array(yf)
# Compute lonlat
lonf, latf = self.xy2ll(xf, yf)
# Set values
self.xf = xf
self.yf = yf
self.lonf = lonf
self.latf = latf
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Compute the area of all patches and store them in {area}
def computeArea(self):
'''
Computes the area of all patches. Stores that in {self.area}
Returns:
* None
'''
# Area
self.area = []
# Loop
for p in self.equivpatch:
self.area.append(self.patchArea(p))
# all done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Get the area of a patch
def patchArea(self, p):
'''
Computes the area of one patch.
Args:
* p : One item of self.patch
Returns:
* area : The area of the patch
'''
# get points
p1 = p[0]
p2 = p[1]
p3 = p[2]
# computes distances
d1 = np.sqrt( (p1[0]-p2[0])**2 + (p1[1]-p2[1])**2 + (p1[2]-p2[2])**2 )
d2 = np.sqrt( (p3[0]-p2[0])**2 + (p3[1]-p2[1])**2 + (p3[2]-p2[2])**2 )
area = d1*d2
# All done
return area
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Conversion method
def patchesUtm2LonLat(self):
'''
Perform the utm to lonlat conversion for all patches.
Returns:
* None
'''
# iterate over the patches
for i in range(len(self.patch)):
# Get the patch
patch = self.patch[i]
# Get x, y
x1 = patch[0][0]
y1 = patch[0][1]
x2 = patch[1][0]
y2 = patch[1][1]
zu = patch[0][2]
zd = patch[2][2]
# Convert
lon1, lat1 = self.xy2ll(x1, y1)
lon2, lat2 = self.xy2ll(x2, y2)
# Build a ll patch
patchll = [ [lon1, lat1, zu],
[lon2, lat2, zu],
[lon2, lat2, zd],
[lon1, lat1, zd] ]
# Put it in the list
self.patchll[i] = patchll
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Compute equivalent rectangles
def computeEquivRectangle(self):
'''
In the case where patches are not exactly rectangular, this method
computes the best rectangle that fits within patches. Stores all the
equivalent rectangles in equivpatch and equivpatchll.
Returns:
* None
'''
# Initialize the equivalent structure
self.equivpatch = []
self.equivpatchll = []
# Loop on the patches
for u in range(len(self.patch)):
# Get the patch
p = self.patch[u]
p1, p2, p3, p4 = self.patch[u]
# 1. Get the two top points
pt1 = p[0]; x1, y1, z1 = pt1
pt2 = p[1]; x2, y2, z2 = pt2
# 2. Get the strike of this patch
vs = p2-p1
vd = p4-p1
assert vs[2]==0., 'p1 and p2 must be located at the same depth: {}'.format(vs[2])
vnz = vs[1]*vd[0]-vs[0]*vd[1]
if vnz<0.:
vs *= -1.
strike = np.arctan2( vs[0],vs[1] )
# 3. Get the dip of this patch
dip1 = np.arcsin((p4[2] - p1[2]) / np.sqrt((p1[0] - p4[0])**2
+ (p1[1] - p4[1])**2 + (p1[2] - p4[2])**2))
dip2 = np.arcsin((p3[2] - p2[2]) / np.sqrt( (p2[0] - p3[0])**2
+ (p2[1] - p3[1])**2 + (p2[2] - p3[2])**2))
dip = 0.5 * (dip1 + dip2)
# 4. compute the position of the bottom corners
width = np.sqrt((p1[0] - p4[0])**2 + (p1[1] - p4[1])**2 + (p1[2] - p4[2])**2)
wc = width * np.cos(dip)
ws = width * np.sin(dip)
halfPi = 0.5 * np.pi
x3 = x2 + wc * np.cos(strike)
y3 = y2 - wc * np.sin(strike)
z3 = z2 + ws
x4 = x1 + wc * np.cos(strike)
y4 = y1 - wc * np.sin(strike)
z4 = z1 + ws
pt3 = [x3, y3, z3]
pt4 = [x4, y4, z4]
# set up the patch
self.equivpatch.append(np.array([pt1, pt2, pt3, pt4]))
# Deal with the lon lat
lon1, lat1 = self.xy2ll(x1, y1)
lon2, lat2 = self.xy2ll(x2, y2)
lon3, lat3 = self.xy2ll(x3, y3)
lon4, lat4 = self.xy2ll(x4, y4)
pt1 = [lon1, lat1, z1]
pt2 = [lon2, lat2, z2]
pt3 = [lon3, lat3, z3]
pt4 = [lon4, lat4, z4]
# set up the patchll
self.equivpatchll.append(np.array([pt1, pt2, pt3, pt4]))
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Create a patch
def lonlat2patch(self, lon, lat, depth, strike, dip, length, width):
'''
Builds a patch from its longitude {lon}, latitude {lat},
depths {depth}, strike angles {strike}, dip angles {dip},
patch length {length} and patch width {width}
Args:
* lon : Longitude of the center of the patch
* lat : Latitude of the center of the patch
* depth : Depth of the center of the patch (km)
* strike : Strike of the patch (degree)
* dip : Dip of the patch (degree)
* length : Length of the patch (km)
* width : Width of the patch (km)
Returns:
* patch : a list for patch corners
* patchll : a list patch corners in lonlat
'''
# Convert angles
dip *= np.pi/180.
strike *= np.pi/180.
# Convert Lon Lat to X Y
xc, yc = self.ll2xy(lon,lat)
zc = -1.0*depth
# Calculate the center of the upper segment
xcu = xc - width/2.*np.cos(dip)*np.cos(strike)
ycu = yc + width/2.*np.cos(dip)*np.sin(strike)
zcu = zc + width/2.*np.sin(dip)
# Calculate the center of the lower segment
xcd = xc + width/2.*np.cos(dip)*np.cos(strike)
ycd = yc - width/2.*np.cos(dip)*np.sin(strike)
zcd = zc - width/2.*np.sin(dip)
# Calculate the 2 upper corners
x1 = xcu - length/2.*np.sin(strike)
y1 = ycu - length/2.*np.cos(strike)
z1 = zcu
p1 = [x1, y1, -1.0*z1]
lon1, lat1 = self.xy2ll(x1, y1)
pll1 = [lon1, lat1, -1.0*z1]
x2 = xcu + length/2.*np.sin(strike)
y2 = ycu + length/2.*np.cos(strike)
z2 = zcu
p2 = [x2, y2, -1.0*z2]
lon2, lat2 = self.xy2ll(x2, y2)
pll2 = [lon2, lat2, -1.0*z2]
# Calculate the 2 lower corner
x3 = xcd + length/2.*np.sin(strike)
y3 = ycd + length/2.*np.cos(strike)
z3 = zcd
p3 = [x3, y3, -1.0*z3]
lon3, lat3 = self.xy2ll(x3, y3)
pll3 = [lon3, lat3, -1.0*z3]
x4 = xcd - length/2.*np.sin(strike)
y4 = ycd - length/2.*np.cos(strike)
z4 = zcd
p4 = [x4, y4, -1.0*z4]
lon4, lat4 = self.xy2ll(x4, y4)
pll4 = [lon4, lat4, -1.0*z4]
# Set up patch
patch = np.array([p1, p2, p3, p4])
patchll = np.array([pll1, pll2, pll3, pll4])
# All done
return patch, patchll
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Given lon, lat, etc arrays, builds patches
def geometry2patch(self, Lon, Lat, Depth, Strike, Dip, Length, Width,
initializeSlip=True):
'''
Builds the list of patches from lists of lon, lat, depth, strike, dip,
length and width
Args:
* Lon : List of longitudes
* Lat : List of Latitudes
* Depth : List of depths (km)
* Strike : List of strike angles (degree)
* Dip : List of dip angles (degree)
* Length : List of length (km)
* Width : List of width (km)
Kwargs:
* initializeSlip : Set slip values to zero
'''
# Create the patch lists
self.patch = []
self.patchll = []
# Iterate
for lon, lat, depth, strike, dip, length, width in zip(Lon, Lat, Depth, Strike, Dip, Length, Width):
patch, patchll = self.lonlat2patch(lon, lat, depth, strike, dip, length, width)
self.patch.append(patch)
self.patchll.append(patchll)
# Initialize Slip
if initializeSlip:
self.initializeslip()
# Depth things
depth = [[p[2] for p in patch] for patch in self.patch]
depth = np.unique(np.array(depth).flatten())
self.z_patches = depth.tolist()
self.top = np.min(depth)
self.depth = np.max(depth)
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Relax type method
def importPatches(self, filename, origin=[45.0, 45.0]):
'''
Builds a patch geometry and the corresponding files from a relax
co-seismic file type.
Args:
* filename : Input from Relax (See Barbot and Cie on the CIG website).
Kwargs:
* origin : Origin of the reference frame used by relax. [lon, lat]
Returns:
* None
'''
# Create lists
self.patch = []
self.patchll = []
self.slip = []
# origin
x0, y0 = self.ll2xy(origin[0], origin[1])
# open/read/close the input file
fin = open(filename, 'r')
Text = fin.readlines()
fin.close()
# Depth array
D = []
# Loop over the patches
for text in Text:
# split
text = text.split()
# check if continue
if not text[0]=='#':
# Get values
slip = float(text[1])
xtl = float(text[2]) + x0
ytl = float(text[3]) + y0
depth = float(text[4])
length = float(text[5])
width = float(text[6])
strike = float(text[7])*np.pi/180.
rake = float(text[9])*np.pi/180.
D.append(depth)
# Build a patch with that
x1 = xtl + length*np.cos(strike)
y1 = ytl + length*np.sin(strike)
z1 = depth
#z1 = depth + width
x2 = xtl
y2 = ytl
z2 = depth
x3 = xtl
y3 = ytl
z3 = depth + width
#x3 = xtl + length*np.cos(strike)
#y3 = ytl + length*np.sin(strike)
#z3 = depth
x4 = xtl + length*np.cos(strike)
y4 = ytl + length*np.sin(strike)
z4 = depth + width
# Convert to lat lon
lon1, lat1 = self.xy2ll(x1, y1)
lon2, lat2 = self.xy2ll(x2, y2)
lon3, lat3 = self.xy2ll(x3, y3)
lon4, lat4 = self.xy2ll(x4, y4)
# Fill the patch
p = np.zeros((4, 3))
pll = np.zeros((4, 3))
p[0,:] = [x1, y1, z1]
p[1,:] = [x2, y2, z2]
p[2,:] = [x3, y3, z3]
p[3,:] = [x4, y4, z4]
pll[0,:] = [lon1, lat1, z1]
pll[1,:] = [lon2, lat2, z2]
pll[2,:] = [lon3, lat3, z3]
pll[3,:] = [lon4, lat4, z4]
self.patch.append(p)
self.patchll.append(pll)
# Slip
ss = slip*np.cos(rake)
ds = slip*np.sin(rake)
ts = 0.
self.slip.append([ss, ds, ts])
# Translate slip to np.array
self.slip = np.array(self.slip)
# Depth
D = np.unique(np.array(D))
self.z_patches = D
self.depth = D.max()
# Create a trace
dmin = D.min()
self.lon = []
self.lat = []
for p in self.patchll:
d = p[1][2]
if d==dmin:
self.lon.append(p[1][0])
self.lat.append(p[1][1])
self.lon = np.array(self.lon)
self.lat = np.array(self.lat)
# All done
return
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Read patches
def readPatchesFromFile(self, filename, Cm=None, readpatchindex=True,
inputCoordinates='lonlat',
donotreadslip=False, increasingy=True):
'''
Read patches from a GMT formatted file. This means the file is a
list of patches separated by '>'.
Args:
* filename : Name of the file.
Kwargs:
* Cm : Posterior covariances (array nslip x nslip)
* readpatchindex : Read the index of the patch and organize them
* inputCoordinates : lonlat or utm
* donotreadslip : Do not read slip values in the file
* increasingy : if you don't want csi to set your patches corners according to increasing y, set increasingy = False
'''
# create the lists
self.patch = []
self.patchll = []
if readpatchindex:
self.index_parameter = []
self.Cm = []
if not donotreadslip:
Slip = []
# open the files
fin = open(filename, 'r')
# Assign posterior covariances
if Cm!=None: # Slip
self.Cm = np.array(Cm)
# read all the lines
A = fin.readlines()
# depth