forked from enokjun/2D-and-3D-LEM-Slope-Analysis-Python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3D_backend_analysis.py
3083 lines (2507 loc) · 108 KB
/
3D_backend_analysis.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
'''3D back end'''
def csv2txtlistSlide3(fileName):
import csv
with open(fileName, 'r') as f:
reader = csv.reader(f)
csvListTxt = list(reader)
csvList = []
tempList = []
for idR in range(len(csvListTxt)):
csvListTxt[idR] = [x for x in csvListTxt[idR] if x != '']
for i in range(len(csvListTxt[idR])):
if csvListTxt[idR][i] == '':
continue
elif i==0 and (idR==0 or idR%22 ==0):
indList = list(csvListTxt[idR][i])
blankInx = indList.index(' ')
columnID = ''.join(csvListTxt[idR][i][(blankInx+1):])
tempList.append(int(columnID))
elif i!=0 and csvListTxt[idR][i].find(',') == -1:
tempList.append(float(csvListTxt[idR][i]))
elif i!=0 and csvListTxt[idR][i].find(',') != -1:
commaIdx = csvListTxt[idR][i].index(',')
tempList.append(float(csvListTxt[idR][i][0:commaIdx]))
tempList.append(float(csvListTxt[idR][i][(commaIdx+2):]))
if idR!=0 and len(tempList)==22:
csvList.append(tempList)
tempList = []
return csvList
def csv2list(fileName):
import csv
#with open(fileName, 'w', encoding='UTF-8', newline='') as f:
with open(fileName, 'r') as f:
reader = csv.reader(f)
csvListTxt = list(reader)
csvListNum = []
for idR in range(len(csvListTxt)):
csvListTxt[idR] = [x for x in csvListTxt[idR] if x != '']
tempList = [float(i) for i in csvListTxt[idR]]
csvListNum.append(tempList)
return csvListNum
def making_float_list(startNum, endNum, spacing):
result = []
length = 1 + abs(startNum - endNum)/abs(spacing)
for i in range(int(length)):
x = startNum + spacing*i
result.append(float(x))
return result
def concatenate_lists(parentList, addingList):
result = parentList[:]
for i in range(len(addingList)):
result.append(float(addingList[i]))
return result
def listAtColNum(listName,colNum):
result = []
for i in range(len(listName)):
result.append(float(listName[i][colNum]))
return result
def listAtColNumTxt(listName,colNum):
result = []
for i in range(len(listName)):
result.append(listName[i][colNum])
return result
def arrayAtColNum(arrayName,colNum):
import numpy as np
result = []
for i in range(len(arrayName)):
result.append(float(arrayName[i][colNum]))
result = np.array(result)
return result
# csv_file = filename of csv exported from list
# csv_column = column titles
# data_list = list data
def exportList2CSV(csv_file,data_list,csv_columns=None):
# export files
import csv
with open(csv_file, 'w',newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
if csv_columns != None:
writer.writerow(csv_columns)
for data in data_list:
writer.writerow(data)
'''
purpose: create slices with information at sides
Input: DEM surface, slice parameters, slip surfaces
Output: slices points
Interpolation methods used:
1. scipy interpolation interp2d
> linear - a1
2. kriging ordinary
> linear - b1
> power - b2
> gaussian - b3
> spherical - b4
> exponentail - b5
> hole-effect - b6
3. kriging universal
> linear - c1
> power - c2
> gaussian - c3
> spherical - c4
> exponentail - c5
> hole-effect - c6
'''
# interpolation method
def interpolation3D(interpolType, edgeXCoords, edgeYCoords, DEMname, stdMax, exportOption=0):
import numpy as np
#print(DEMname)
inputCSV = csv2list(DEMname)
tempArrayX, tempArrayY, tempArrayZ = np.array(inputCSV).T
csvXYZ = []
''' interpolation method '''
#library import
if interpolType[0] == 'a':
from scipy.interpolate import interp2d
elif interpolType[0] == 'b':
from pykrige.ok import OrdinaryKriging
elif interpolType[0] == 'c':
from pykrige.uk import UniversalKriging
# scipy interpol1d
if interpolType == 'a1':
tempInterpolated = interp2d(tempArrayX, tempArrayY, tempArrayZ, bounds_error=False)
interpolZ = tempInterpolated(edgeXCoords, edgeYCoords)
#print(interpolZ)
#interpolZ = interpolZ[0,:]
# pykrige ordinary kriging
elif interpolType == 'b1':
tempInterpolated = OrdinaryKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='linear')
interpolZ, stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'b2':
tempInterpolated = OrdinaryKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='power')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'b3':
tempInterpolated = OrdinaryKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='gaussian')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'b4':
tempInterpolated = OrdinaryKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='spherical')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'b5':
tempInterpolated = OrdinaryKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='exponential')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'b6':
tempInterpolated = OrdinaryKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='hole-effect')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
# pykrige universal kriging
elif interpolType == 'c1':
tempInterpolated = UniversalKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='linear')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'c2':
tempInterpolated = UniversalKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='power')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'c3':
tempInterpolated = UniversalKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='gaussian')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'c4':
tempInterpolated = UniversalKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='spherical')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'c5':
tempInterpolated = UniversalKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='exponential')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
elif interpolType == 'c6':
tempInterpolated = UniversalKriging(tempArrayX, tempArrayY, tempArrayZ, variogram_model='hole-effect')
interpolZ,stdZ = tempInterpolated.execute('grid', edgeXCoords, edgeYCoords)
# for pykrige, eliminate points that has a large standard deviation
#print(interpolZ)
#print(len(interpolZ[0])*len(interpolZ))
'''
if interpolType[0] in ['b','c']:
for loopZPredrow in range(len(interpolZ)):
for loopZPredcol in range(len(interpolZ[0])):
#print(stdZ[loopZPredrow][loopZPredcol])
#print(stdZ[loopZPredrow][loopZPredcol] > 1000)
#print(interpolZ[loopZPredrow][loopZPredcol])
if stdZ[loopZPredrow][loopZPredcol] > stdMax:
interpolZ[loopZPredrow][loopZPredcol] = np.nan
else:
continue
'''
#print(interpolZ)
#print(stdZ)
#print(len(edgeXCoords))
#print(len(edgeYCoords))
#print(len(interpolZ))
#print(len(interpolZ[0]))
if len(interpolZ[0]) < len(interpolZ):
interpolZ = interpolZ.tolist()
elif len(interpolZ[0]) >= len(interpolZ):
interpolZ = np.transpose(interpolZ)
interpolZ = interpolZ.tolist()
#print(len(interpolZ))
#print(len(interpolZ[0]))
for loopXYrow in range(len(interpolZ)):
for loopXYcol in range(len(interpolZ[0])):
if (len(interpolZ)) <= (len(interpolZ[0])):
csvXYZ.append([edgeXCoords[loopXYrow], edgeYCoords[loopXYcol], interpolZ[loopXYrow][loopXYcol]])
elif (len(interpolZ)) > (len(interpolZ[0])):
csvXYZ.append([edgeXCoords[loopXYcol], edgeYCoords[loopXYrow], interpolZ[loopXYrow][loopXYcol]])
#print(csvXYZ)
# export the interpolated data into csv file
if exportOption==0:
exportList2CSV('interpolated_'+DEMname, csvXYZ)
return interpolZ, csvXYZ
# function that will calculate z-coordinates (from csv files) for given XY for each layer
# at column edge
def DEM_3D_columnEdge(DEMNameList, DEMTypeList, DEMInterpolTypeList, canvasRange, colXmax, colYmax, stdMax=150):
# import python libraries
import numpy as np
#import scipy
#import matplotlib.pyplot as plt
# column X and Y coordinates edge
initialSpaceX = abs(np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[1] - np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[0])
initialSpaceY = abs(np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[1] - np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[0])
if initialSpaceX == initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0], canvasRange[1], colXmax+1)
edgeYCoords = np.linspace(canvasRange[2], canvasRange[3], colYmax+1)
elif initialSpaceX < initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0], canvasRange[1], colXmax+1)
edgeYCoords = np.arange(canvasRange[2], canvasRange[3], initialSpaceX)
elif initialSpaceX > initialSpaceY:
edgeXCoords = np.arange(canvasRange[0], canvasRange[1], initialSpaceY)
edgeYCoords = np.linspace(canvasRange[2], canvasRange[3], colYmax+1)
#print(initialSpaceX)
#print(initialSpaceY)
#print(edgeXCoords)
#Input file sorting
Z_pred = []
for loopFile in range(len(DEMNameList)):
interpolZ,csvXYZ = interpolation3D(DEMInterpolTypeList[loopFile], edgeXCoords, edgeYCoords, DEMNameList[loopFile], stdMax, exportOption=1)
Z_pred.append(interpolZ)
#print(Z_pred)
edgesXY = {}
# find XYZ for each material layer for given XY
for loopX in range(len(edgeXCoords)):
for loopY in range(len(edgeYCoords)):
tempPtZ = [float(canvasRange[4])]
tempPtM = ['bb']
# input information to dictionary of edgesX
for loopFile in range(len(DEMNameList)):
#print(len(Z_pred[loopFile]))
#print(len(Z_pred[loopFile][0]))
#print(loopX)
#print(loopY)
interpolZ_edge = Z_pred[loopFile][loopX][loopY]
#print(interpolY_edge)
if not(np.isnan(interpolZ_edge)):
if DEMTypeList[loopFile] == 'tt' and interpolZ_edge > canvasRange[5]:
tempPtZ.append(float(canvasRange[5]))
tempPtM.append('tt')
elif DEMTypeList[loopFile] == 'tt' and interpolZ_edge <= canvasRange[5]:
tempPtZ.append(interpolZ_edge)
tempPtM.append('tt')
elif DEMTypeList[loopFile] == 'rr' and interpolZ_edge > canvasRange[4]:
tempPtZ[0] = interpolZ_edge
tempPtM[0] = 'rr'
else:
tempPtZ.append(interpolZ_edge)
tempPtM.append(DEMTypeList[loopFile])
edgesXY[edgeXCoords[loopX],edgeYCoords[loopY]] = [tempPtZ, tempPtM]
# find XY for each column edge
colNedge = {}
totalColNum = colXmax*colYmax
count = 1
rowCount = 0
colCount = 0
rowCountMax = len(edgeXCoords)-1
colCountMax = len(edgeYCoords)-1
while count <= totalColNum:
tempList = []
#print('rowCount=%f'%rowCount)
#print('colCount=%f'%colCount)
edge1 = edgesXY[edgeXCoords[rowCount],edgeYCoords[colCount]]
tempList.append([edgeXCoords[rowCount],edgeYCoords[colCount]])
tempList.append(edge1)
edge2 = edgesXY[edgeXCoords[rowCount+1],edgeYCoords[colCount]]
tempList.append([edgeXCoords[rowCount+1],edgeYCoords[colCount]])
tempList.append(edge2)
edge3 = edgesXY[edgeXCoords[rowCount+1],edgeYCoords[colCount+1]]
tempList.append([edgeXCoords[rowCount+1],edgeYCoords[colCount+1]])
tempList.append(edge3)
edge4 = edgesXY[edgeXCoords[rowCount],edgeYCoords[colCount+1]]
tempList.append([edgeXCoords[rowCount],edgeYCoords[colCount+1]])
tempList.append(edge4)
colNedge[count] = tempList
if (rowCount+1) == rowCountMax and (colCount+1) == colCountMax: # last column
break
elif (rowCount+1) == rowCountMax and (colCount+1) != colCountMax: # move to next row
rowCount = 0
colCount += 1
elif (rowCount+1) != rowCountMax: # move along the row
rowCount += 1
count += 1
return colNedge
# function that will calculate z-coordinates (from csv files) for given XY for each layer
# at column center
def DEM_3D_columnCenter(colNedge, DEMNameList, DEMTypeList, DEMInterpolTypeList, canvasRange, colXmax, colYmax, stdMax=150):
# import python libraries
import numpy as np
#import scipy
#import matplotlib.pyplot as plt
# column X and Y coordinates edge
initialSpaceX = abs(np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[1] - np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[0])
initialSpaceY = abs(np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[1] - np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[0])
#print(initialSpaceX)
#print(initialSpaceY)
if initialSpaceX == initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0]+initialSpaceX, canvasRange[1]-initialSpaceX, colXmax)
edgeYCoords = np.linspace(canvasRange[2]+initialSpaceY, canvasRange[3]-initialSpaceY, colYmax)
elif initialSpaceX < initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0]+initialSpaceX, canvasRange[1]-initialSpaceX, colXmax)
edgeYCoords = np.arange(canvasRange[2]+initialSpaceX, canvasRange[3]-initialSpaceX, initialSpaceX)
elif initialSpaceX > initialSpaceY:
edgeXCoords = np.arange(canvasRange[0]+initialSpaceY, canvasRange[1]-initialSpaceY, initialSpaceY)
edgeYCoords = np.linspace(canvasRange[2]+initialSpaceY, canvasRange[3]-initialSpaceY, colYmax)
'''
if len(edgeXCoords) < len(edgeYCoords):
edgeXCoords = np.arange(canvasRange[0]+initialSpaceY, canvasRange[1]-initialSpaceY, initialSpaceY)
elif len(edgeXCoords) > len(edgeYCoords):
edgeYCoords = np.arange(canvasRange[0]+initialSpaceY, canvasRange[1]-initialSpaceY, initialSpaceY)
'''
#print(edgeXCoords)
#print(len(edgeXCoords))
#print(edgeYCoords)
#print(len(edgeYCoords))
#Input file sorting
Z_pred = []
for loopFile in range(len(DEMNameList)):
interpolZ, csvXYZ = interpolation3D(DEMInterpolTypeList[loopFile], edgeXCoords, edgeYCoords, DEMNameList[loopFile], stdMax, exportOption=0)
Z_pred.append(interpolZ)
#print(Z_pred)
edgesXY = {}
# find XYZ for each material layer for given XY
for loopX in range(len(edgeXCoords)):
for loopY in range(len(edgeYCoords)):
tempPtZ = [float(canvasRange[4])]
tempPtM = ['bb']
# input information to dictionary of edgesX
for loopFile in range(len(DEMNameList)):
#print(Z_pred[loopFile][loopX][loopY])
interpolZ_edge = Z_pred[loopFile][loopX][loopY]
#print(interpolY_edge)
if not(np.isnan(interpolZ_edge)):
if DEMTypeList[loopFile] == 'tt' and interpolZ_edge > canvasRange[5]:
tempPtZ.append(float(canvasRange[5]))
tempPtM.append('tt')
elif DEMTypeList[loopFile] == 'tt' and interpolZ_edge <= canvasRange[5]:
tempPtZ.append(interpolZ_edge)
tempPtM.append('tt')
elif DEMTypeList[loopFile] == 'rr' and interpolZ_edge > canvasRange[4]:
tempPtZ[0] = interpolZ_edge
tempPtM[0] = 'rr'
else:
tempPtZ.append(interpolZ_edge)
tempPtM.append(DEMTypeList[loopFile])
edgesXY[edgeXCoords[loopX],edgeYCoords[loopY]] = [tempPtZ, tempPtM]
# find XY for each column edge
rowCount = 0
colCount = 0
rowCountMax = colXmax
colCountMax = colYmax
for colN in colNedge.keys():
tempList = colNedge[colN]
center = edgesXY[edgeXCoords[rowCount],edgeYCoords[colCount]]
tempList.append([edgeXCoords[rowCount],edgeYCoords[colCount]])
tempList.append(center)
colNedge[colN] = tempList
if rowCount+1 == rowCountMax and colCount+1 == colCountMax: # last column
break
elif rowCount+1 == rowCountMax and colCount+1 != colCountMax: # move to next row
rowCount = 0
colCount += 1
elif rowCount+1 != rowCountMax: # move along the row
rowCount += 1
return colNedge
''' DEM points of 3D slip surface '''
'''
SSTypeList = 1 -> user-defined surface [DEMname, interpolType]
SSTypeList = 2 -> grid eplitical search [pt0x, pt0y, pt0z, xr, yr, zr]
output
#colNedge format = [[X1, Y1], [[Z..],[type...]...], an exmaple below
#1: [[1795700.0, 498790.0], [[700.0, 758.2936221107052, 758.2936221107052], ['bb', 'w1', 'tt']], [1795726.5, 498790.0], [[700.0, 758.3716389809305, 758.3716389809305], ['bb', 'w1', 'tt']], [1795726.5, 498791.0], [[700.0, 708.1436968600685, 708.1436968600685], ['bb', 'w1', 'tt']], [1795700.0, 498791.0], [[700.0, 706.7219523927594, 706.7219523927594], ['bb', 'w1', 'tt']]]
'''
# find base y-coordinates for a given slip surface
def SS_3D_columnsEdge(SSTypeList, inputPara, canvasRange, colXmax, colYmax, colNedge, stdMax=150):
# import python libraries
import numpy as np
#import scipy
#from scipy.interpolate import interp1d
#from pykrige.ok import OrdinaryKriging
#from pykrige.uk import UniversalKriging
#import matplotlib.pyplot as plt
newColEdge = {}
ss_csv = []
# column X and Y coordinates edge
initialSpaceX = abs(np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[1] - np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[0])
initialSpaceY = abs(np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[1] - np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[0])
if initialSpaceX == initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0], canvasRange[1], colXmax+1)
edgeYCoords = np.linspace(canvasRange[2], canvasRange[3], colYmax+1)
elif initialSpaceX < initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0], canvasRange[1], colXmax+1)
edgeYCoords = np.arange(canvasRange[2], canvasRange[3], initialSpaceX)
elif initialSpaceX > initialSpaceY:
edgeXCoords = np.arange(canvasRange[0], canvasRange[1], initialSpaceY)
edgeYCoords = np.linspace(canvasRange[2], canvasRange[3], colYmax+1)
#print(edgeXCoords)
'''Input SS for each XY coordiantes'''
ss_Z_pred = {} #keys = XY, value = Z
if SSTypeList == 2: # ellipsoid grid search
# extract inputPara
pt0X = inputPara[0]
pt0Y = inputPara[1]
pt0Z = inputPara[2]
xr = inputPara[3]
yr = inputPara[4]
zr = inputPara[5]
for loopX in range(len(edgeXCoords)):
for loopY in range(len(edgeYCoords)):
if (1 - ((edgeXCoords[loopX] - pt0X)**2)/(xr**2) - ((edgeYCoords[loopY] - pt0Y)**2)/(yr**2)) >= 0:
tempPtZSS = pt0Z - zr*np.sqrt(1 - ((edgeXCoords[loopX] - pt0X)**2)/(xr**2) - ((edgeYCoords[loopY] - pt0Y)**2)/(yr**2))
else:
tempPtZSS = np.nan
ss_Z_pred[edgeXCoords[loopX], edgeYCoords[loopY]] = tempPtZSS
elif SSTypeList == 1: # user-defined surface
# extract inputPara
DEMname = inputPara[0]
interpolType = inputPara[1]
tempPtZSS,csvFile = interpolation3D(interpolType, edgeXCoords, edgeYCoords, DEMname, stdMax, exportOption=1)
for loopX in range(len(edgeXCoords)):
for loopY in range(len(edgeYCoords)):
ss_Z_pred[edgeXCoords[loopX], edgeYCoords[loopY]] = tempPtZSS[loopX][loopY]
colNumberMax = colXmax*colYmax
for loopCol in range(1,colNumberMax+1):
# extract XY of each corner of the columns
corner1 = colNedge[loopCol][0]
corner2 = colNedge[loopCol][2]
corner3 = colNedge[loopCol][4]
corner4 = colNedge[loopCol][6]
# slip surface z coordinates
corner1z_ss = ss_Z_pred[corner1[0],corner1[1]]
corner2z_ss = ss_Z_pred[corner2[0],corner2[1]]
corner3z_ss = ss_Z_pred[corner3[0],corner3[1]]
corner4z_ss = ss_Z_pred[corner4[0],corner4[1]]
# check if isnan
corner1_check = 0
if not(np.isnan(corner1z_ss)):
if corner1z_ss <= colNedge[loopCol][1][0][-1]:
corner1_check = 1
corner2_check = 0
if not(np.isnan(corner2z_ss)):
if corner2z_ss <= colNedge[loopCol][3][0][-1]:
corner2_check = 1
corner3_check = 0
if not(np.isnan(corner3z_ss)):
if corner3z_ss <= colNedge[loopCol][5][0][-1]:
corner3_check = 1
corner4_check = 0
if not(np.isnan(corner4z_ss)):
if corner4z_ss <= colNedge[loopCol][7][0][-1]:
corner4_check = 1
if corner1_check!=1 or corner2_check!=1 or corner3_check!=1 or corner4_check!=1:
continue
elif corner1_check==1 and corner2_check==1 and corner3_check==1 and corner4_check==1:
# list of Z coordinates
corner1z = colNedge[loopCol][1][0]
corner2z = colNedge[loopCol][3][0]
corner3z = colNedge[loopCol][5][0]
corner4z = colNedge[loopCol][7][0]
# list of Z coorindate types
corner1z_type = colNedge[loopCol][1][1]
corner2z_type = colNedge[loopCol][3][1]
corner3z_type = colNedge[loopCol][5][1]
corner4z_type = colNedge[loopCol][7][1]
# corner 1
nslice1 = [0]
nslice1_type = [0]
tempZss = []
tempZssType = []
for loopLayer in range(len(corner1z_type)):
if corner1z_type[loopLayer][0] in ['t','m','g']:
nslice1.append(corner1z[loopLayer])
nslice1_type.append(corner1z_type[loopLayer])
elif corner1z_type[loopLayer][0] in ['r','w','b']:
if loopLayer == 0:
tempZss.append(corner1z_ss)
tempZssType.append('ss')
if corner1z_ss <= corner1z[loopLayer]:
tempZss.append(corner1z[loopLayer])
tempZssType.append(corner1z_type[loopLayer])
maxEdgeBottom = max(tempZss)
maxEdgeBottomIDX = tempZss.index(maxEdgeBottom)
maxEdgeBottomType = tempZssType[maxEdgeBottomIDX]
nslice1[0] = maxEdgeBottom
nslice1_type[0] = maxEdgeBottomType
# corner 2
nslice2 = [0]
nslice2_type = [0]
tempZss = []
tempZssType = []
for loopLayer in range(len(corner2z_type)):
if corner2z_type[loopLayer][0] in ['t','m','g']:
nslice2.append(corner2z[loopLayer])
nslice2_type.append(corner2z_type[loopLayer])
elif corner2z_type[loopLayer][0] in ['r','w','b']:
if loopLayer == 0:
tempZss.append(corner2z_ss)
tempZssType.append('ss')
if corner2z_ss <= corner2z[loopLayer]:
tempZss.append(corner2z[loopLayer])
tempZssType.append(corner2z_type[loopLayer])
maxEdgeBottom = max(tempZss)
maxEdgeBottomIDX = tempZss.index(maxEdgeBottom)
maxEdgeBottomType = tempZssType[maxEdgeBottomIDX]
nslice2[0] = maxEdgeBottom
nslice2_type[0] = maxEdgeBottomType
# corner 3
nslice3 = [0]
nslice3_type = [0]
tempZss = []
tempZssType = []
for loopLayer in range(len(corner3z_type)):
if corner3z_type[loopLayer][0] in ['t','m','g']:
nslice3.append(corner3z[loopLayer])
nslice3_type.append(corner3z_type[loopLayer])
elif corner3z_type[loopLayer][0] in ['r','w','b']:
if loopLayer == 0:
tempZss.append(corner3z_ss)
tempZssType.append('ss')
if corner3z_ss <= corner3z[loopLayer]:
tempZss.append(corner3z[loopLayer])
tempZssType.append(corner3z_type[loopLayer])
maxEdgeBottom = max(tempZss)
maxEdgeBottomIDX = tempZss.index(maxEdgeBottom)
maxEdgeBottomType = tempZssType[maxEdgeBottomIDX]
nslice3[0] = maxEdgeBottom
nslice3_type[0] = maxEdgeBottomType
# corner 4
nslice4 = [0]
nslice4_type = [0]
tempZss = []
tempZssType = []
for loopLayer in range(len(corner4z_type)):
if corner4z_type[loopLayer][0] in ['t','m','g']:
nslice4.append(corner4z[loopLayer])
nslice4_type.append(corner4z_type[loopLayer])
elif corner4z_type[loopLayer][0] in ['r','w','b']:
if loopLayer == 0:
tempZss.append(corner4z_ss)
tempZssType.append('ss')
if corner4z_ss <= corner4z[loopLayer]:
tempZss.append(corner4z[loopLayer])
tempZssType.append(corner4z_type[loopLayer])
maxEdgeBottom = max(tempZss)
maxEdgeBottomIDX = tempZss.index(maxEdgeBottom)
maxEdgeBottomType = tempZssType[maxEdgeBottomIDX]
nslice4[0] = maxEdgeBottom
nslice4_type[0] = maxEdgeBottomType
# slip surface XYZ csv file
ss_csv.append([corner1[0], corner1[1], nslice1[0]])
ss_csv.append([corner2[0], corner2[1], nslice2[0]])
ss_csv.append([corner3[0], corner3[1], nslice3[0]])
ss_csv.append([corner4[0], corner4[1], nslice4[0]])
newColEdge[loopCol] = [corner1, [nslice1, nslice1_type], corner2, [nslice2, nslice2_type], corner3, [nslice3, nslice3_type], corner4, [nslice4, nslice4_type], colNedge[loopCol][8], colNedge[loopCol][9]]
#print(ss_csv)
if len(newColEdge.keys()) == 0:
return None
else:
#exportList2CSV('interpolated_ss_type'+str(SSTypeList)+'.csv', ss_csv)
return newColEdge, ss_csv
''' DEM points of 3D slip surface '''
'''
SSTypeList = 1 -> user-defined surface [DEMname, interpolType]
SSTypeList = 2 -> grid eplitical search [pt0x, pt0y, pt0z, xr, yr, zr]
output
#colNedge format = [[X1, Y1], [[Z..],[type...]..., [Xc, Yc],[[Z...],[type...]]]
'''
# find base z-coordinates for a given slip surface
def SS_3D_columnsCenter(SSTypeList, inputPara, canvasRange, colXmax, colYmax, newColEdge, stdMax=150):
# import python libraries
import numpy as np
#import scipy
#from scipy.interpolate import interp1d
#from pykrige.ok import OrdinaryKriging
#from pykrige.uk import UniversalKriging
#import matplotlib.pyplot as plt
ss_csv = []
# column X and Y coordinates center
initialSpaceX = abs(np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[1] - np.linspace(canvasRange[0], canvasRange[1], colXmax+1)[0])
initialSpaceY = abs(np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[1] - np.linspace(canvasRange[2], canvasRange[3], colYmax+1)[0])
if initialSpaceX == initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0]+initialSpaceX, canvasRange[1]-initialSpaceX, colXmax)
edgeYCoords = np.linspace(canvasRange[2]+initialSpaceY, canvasRange[3]-initialSpaceY, colYmax)
elif initialSpaceX < initialSpaceY:
edgeXCoords = np.linspace(canvasRange[0]+initialSpaceX, canvasRange[1]-initialSpaceX, colXmax)
edgeYCoords = np.arange(canvasRange[2]+initialSpaceX, canvasRange[3]-initialSpaceX, initialSpaceX)
elif initialSpaceX > initialSpaceY:
edgeXCoords = np.arange(canvasRange[0]+initialSpaceY, canvasRange[1]-initialSpaceY, initialSpaceY)
edgeYCoords = np.linspace(canvasRange[2]+initialSpaceY, canvasRange[3]-initialSpaceY, colYmax)
#colNumberMax = colXmax*colYmax
#print(edgeXCoords)
'''Input SS for each XY coordiantes'''
ss_Z_pred = {} #keys = XY, value = Z
if SSTypeList == 2: # ellipsoid grid search
# extract inputPara
pt0X = inputPara[0]
pt0Y = inputPara[1]
pt0Z = inputPara[2]
xr = inputPara[3]
yr = inputPara[4]
zr = inputPara[5]
for loopX in range(len(edgeXCoords)):
for loopY in range(len(edgeYCoords)):
if (1 - ((edgeXCoords[loopX] - pt0X)**2)/(xr**2) - ((edgeYCoords[loopY] - pt0Y)**2)/(yr**2)) >= 0:
tempPtZSS = pt0Z - zr*np.sqrt(1 - ((edgeXCoords[loopX] - pt0X)**2)/(xr**2) - ((edgeYCoords[loopY] - pt0Y)**2)/(yr**2))
else:
tempPtZSS = np.nan
ss_Z_pred[edgeXCoords[loopX], edgeYCoords[loopY]] = tempPtZSS
elif SSTypeList == 1: # user-defined surface
# extract inputPara
DEMname = inputPara[0]
interpolType = inputPara[1]
tempPtZSS,csvFile = interpolation3D(interpolType, edgeXCoords, edgeYCoords, DEMname, stdMax, exportOption=0)
for loopX in range(len(edgeXCoords)):
for loopY in range(len(edgeYCoords)):
ss_Z_pred[edgeXCoords[loopX], edgeYCoords[loopY]] = tempPtZSS[loopX][loopY]
for loopCol in newColEdge.keys():
# extract XY of each corner of the columns
#print(loopCol)
#print((newColEdge[loopCol]))
center = newColEdge[loopCol][8]
# slip surface z coordinates
center_ss = ss_Z_pred[center[0],center[1]]
# list of Z coordinates
centerz = newColEdge[loopCol][9][0]
# list of Z coorindate types
centerz_type = newColEdge[loopCol][9][1]
# corner 1
ncenterz = [0]
ncenterz_type = [0]
tempZss = []
tempZssType = []
for loopLayer in range(len(centerz_type)):
#print(centerz_type[loopLayer])
if centerz_type[loopLayer][0] in ['t','m','g']:
ncenterz.append(centerz[loopLayer])
ncenterz_type.append(centerz_type[loopLayer])
elif centerz_type[loopLayer][0] in ['r','w','b']:
if loopLayer == 0:
tempZss.append(center_ss)
tempZssType.append('ss')
if center_ss <= centerz[loopLayer]:
tempZss.append(centerz[loopLayer])
tempZssType.append(centerz_type[loopLayer])
maxEdgeBottom = max(tempZss)
maxEdgeBottomIDX = tempZss.index(maxEdgeBottom)
maxEdgeBottomType = tempZssType[maxEdgeBottomIDX]
ncenterz[0] = maxEdgeBottom
ncenterz_type[0] = maxEdgeBottomType
# slip surface XYZ csv file
ss_csv.append([center[0], center[1], ncenterz[0]])
newColEdge[loopCol][9] = [ncenterz, ncenterz_type]
#print(ss_csv)
if len(newColEdge.keys()) == 0:
return None
else:
exportList2CSV('interpolated_ss_type'+str(SSTypeList)+'.csv', ss_csv)
return newColEdge, ss_csv
# find center of rotation and radius of user-defined slip surface
def findpt0nR_approxSphere_3D(ss_csv):
import numpy as np
midpt = np.random.choice(np.arange(1,len(ss_csv)-1),2)
if midpt[0] == midpt[1]:
midpt[1] += 1
P1x = ss_csv[0][0]
P1y = ss_csv[0][1]
P1z = ss_csv[0][2]
P2x = ss_csv[midpt[0]][0]
P2y = ss_csv[midpt[0]][1]
P2z = ss_csv[midpt[0]][2]
P3x = ss_csv[midpt[1]][0]
P3y = ss_csv[midpt[1]][1]
P3z = ss_csv[midpt[1]][2]
P4x = ss_csv[-1][0]
P4y = ss_csv[-1][1]
P4z = ss_csv[-1][2]
DistSq1 = -(P1x**2 + P1y**2 + P1z**2)
DistSq2 = -(P2x**2 + P2y**2 + P2z**2)
DistSq3 = -(P3x**2 + P3y**2 + P3z**2)
DistSq4 = -(P4x**2 + P4y**2 + P4z**2)
Mx = np.array([[DistSq1, P1y, P1z, 1], [DistSq2, P2y, P2z, 1], [DistSq1, P3y, P3z, 1], [DistSq4, P4y, P4z, 1]])
My = np.array([[P1x, DistSq1, P1z, 1], [P2x, DistSq2, P2z, 1], [P3x, DistSq3, P3z, 1], [P4x, DistSq4, P4z, 1]])
Mz = np.array([[P1x, P1y, DistSq1, 1], [P2x, P2y, DistSq2, 1], [P3x, P3y, DistSq3, 1], [P4x, P4y, DistSq4, 1]])
Mr = np.array([[P1x, P1y, P1z, DistSq1], [P2x, P2y, P2z, DistSq2], [P3x, P3y, P3z, DistSq3], [P4x, P4y, P4z, DistSq4]])
T = np.array([[P1x, P1y, P1z, 1], [P2x, P2y, P2z, 1], [P3x, P3y, P3z, 1], [P4x, P4y, P4z, 1]])
pt0X = -0.5*np.linalg.det(Mx)/np.linalg.det(T)
pt0Y = -0.5*np.linalg.det(My)/np.linalg.det(T)
pt0Z = -0.5*np.linalg.det(Mz)/np.linalg.det(T)
R = 0.5*np.sqrt(pt0X**2 + pt0Y**2 + pt0Z**2 - 4*(np.linalg.det(Mr)/np.linalg.det(T)))
return [round(pt0X,3), round(pt0Y,3), round(pt0Z,3), round(R,3)]
# The appropriate input for this function is a list of tuples in the format
# [(x1, y1, z1), (x2, y2, z2), (x3, y3, z3)]
# output = dip, dipDirection, strike
def dip_dipDirection_from3pts(pts):
import math
#print(pts)
ptA, ptB, ptC = pts[0], pts[1], pts[2]
x1, y1, z1 = float(ptA[0]), float(ptA[1]), float(ptA[2])
x2, y2, z2 = float(ptB[0]), float(ptB[1]), float(ptB[2])
x3, y3, z3 = float(ptC[0]), float(ptC[1]), float(ptC[2])
u1 = float(((y1 - y2) * (z3 - z2) - (y3 - y2) * (z1 - z2)))
u2 = float((-((x1 - x2) * (z3 - z2) - (x3 - x2) * (z1 - z2))))
u3 = float(((x1 - x2) * (y3 - y2) - (x3 - x2) * (y1 - y2)))
# determine dip
#print(strike, 'strike')
if abs(z3-z1) < 0.01 and abs(z2-z1)<0.01 and abs(z2-z3)<0.01:
dip = 0
else:
part1_dip = math.sqrt(u2**2 + u1**2)
part2_dip = math.sqrt(u1**2 + u2**2 + u3**2)
dip = math.degrees(math.asin(part1_dip / part2_dip))
'''
Calculate pseudo eastings and northings from origin
these are actually coordinates of a new point that represents
the normal from the plane's origin defined as (0,0,0).
If the z value (u3) is above the plane we first reverse the easting
then we check if the z value (u3) is below the plane, if so
we reverse the northing.
This is to satisfy the right hand rule in geology where dip is always
to the right if looking down strike.
'''
if dip == 0:
dipDirection = 999
strike = 999
else:
'''
if u3 < 0:
easting = u2
else:
easting = -u2
if u3 > 0:
northing = u1
else:
northing = -u1
'''
easting = u2
northing = -u1
if easting >= 0:
partA_strike = (easting**2) + (northing**2)
strike = math.degrees(math.acos(northing / math.sqrt(partA_strike)))
else:
partA_strike = northing / math.sqrt((easting**2) + (northing**2))
strike = math.degrees(2 * math.pi - math.acos(partA_strike))
dipDirection = (strike+90)%360
return round(dip,2), round(dipDirection,2), round(strike,2)
'''calculate area in 3D plane'''
# pts = [(x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4)]
def area_3d(pts):
import numpy as np
p1 = pts[0]
p2 = pts[1]
p3 = pts[2]
p4 = pts[3]
vector21 = [p2[0]-p1[0], p2[1]-p1[1], p2[2]-p1[2]]
vector41 = [p4[0]-p1[0], p4[1]-p1[1], p4[2]-p1[2]]
vector23 = [p2[0]-p3[0], p2[1]-p3[1], p2[2]-p3[2]]
vector43 = [p4[0]-p3[0], p4[1]-p3[1], p4[2]-p3[2]]
area = 0.5*np.linalg.norm(np.cross(vector21,vector41)) + 0.5*np.linalg.norm(np.cross(vector23,vector43))
return area
'''calculate tetrahedron volume'''
def vol_tetra(pt1, pt2, pt3, pt4):
# based on vector points method
# import modules
import numpy as np
row1 = pt1[:]
row1.append(1)
row2 = pt2[:]
row2.append(1)
row3 = pt3[:]
row3.append(1)
row4 = pt4[:]
row4.append(1)
volM = np.array([row1, row2, row3, row4])
v = abs(np.linalg.det(volM))/6
return v
'''calculate a volume of box (can be irregular) from 8 tetrahedron volumes'''
def vol_box_from_tetras(base4Pts, top4Pts):
# calculate center of gravity point of base point 4 base points and 4 top points
baseCenPt = [0.5*(base4Pts[0][0]+base4Pts[1][0]), 0.5*(base4Pts[0][1]+base4Pts[3][1]), 0]
baseCenPt[2] = 0.5*(0.5*(base4Pts[2][2]+base4Pts[3][2])+0.5*(base4Pts[0][2]+base4Pts[1][2]))
topCenPt = [0.5*(top4Pts[0][0]+top4Pts[1][0]), 0.5*(top4Pts[0][1]+top4Pts[3][1]), 0]
topCenPt[2] = 0.5*(0.5*(top4Pts[2][2]+top4Pts[3][2])+0.5*(top4Pts[0][2]+top4Pts[1][2]))
v1 = vol_tetra(base4Pts[0], base4Pts[1], base4Pts[3], top4Pts[0])
v2 = vol_tetra(base4Pts[2], base4Pts[1], base4Pts[3], top4Pts[2])
v3 = vol_tetra(top4Pts[1], top4Pts[0], top4Pts[2], base4Pts[1])
v4 = vol_tetra(top4Pts[3], top4Pts[0], top4Pts[2], base4Pts[3])
v5 = vol_tetra(baseCenPt, topCenPt, top4Pts[0], base4Pts[1])
v6 = vol_tetra(baseCenPt, topCenPt, top4Pts[0], base4Pts[3])
v7 = vol_tetra(baseCenPt, topCenPt, top4Pts[2], base4Pts[1])
v8 = vol_tetra(baseCenPt, topCenPt, top4Pts[2], base4Pts[3])
return v1+v2+v3+v4+v5+v6+v7+v8
# GW level, pwp
def GW_3D(inputFile, water_unitWeight):
import math
dictKeys = inputFile.keys()
output = {}
for loopCol in dictKeys:
# check presence of GW level
checkGW_1 = 0
if 'gw' in inputFile[loopCol][1][1]:
gw1index = inputFile[loopCol][1][1]
gw1index = gw1index.index('gw')
gwZ1 = inputFile[loopCol][1][0][gw1index]
x1 = inputFile[loopCol][0][0]
y1 = inputFile[loopCol][0][1]
zt1 = inputFile[loopCol][1][0][-1]
zb1 = inputFile[loopCol][1][0][0]
checkGW_1 = 1
checkGW_2 = 0