Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/moment_tensor_viewer.py: 0%

78 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-06 06:59 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6from .qt_compat import qc, qw 

7 

8from .util import make_QPolygonF, LinValControl 

9from .pile_viewer import Projection 

10 

11from pyrocko import beachball, moment_tensor as mtm 

12from pyrocko import plot 

13 

14 

15class BeachballView(qw.QWidget): 

16 

17 def __init__(self, *args): 

18 qw.QWidget.__init__(self, *args) 

19 mt = mtm.MomentTensor(m=mtm.symmat6(1., -1., 2., 0., -2., 1.)) 

20 self._mt = mt 

21 self.set_moment_tensor(mt) 

22 

23 def set_moment_tensor(self, mt): 

24 self._mt = mt 

25 self.update() 

26 

27 def paintEvent(self, paint_ev): 

28 ''' 

29 Called by QT whenever widget needs to be painted. 

30 ''' 

31 

32 painter = qw.QPainter(self) 

33 painter.setRenderHint(qw.QPainter.Antialiasing) 

34 self.drawit(painter) 

35 

36 def drawit(self, p): 

37 ''' 

38 Draw beachball into painter. 

39 ''' 

40 

41 h = self.height() 

42 w = self.width() 

43 

44 s = min(h, w)*0.9 

45 

46 xproj = Projection() 

47 xproj.set_in_range(-1., 1.) 

48 xproj.set_out_range((w-s)/2., w-(w-s)/2.) 

49 

50 yproj = Projection() 

51 yproj.set_in_range(-1., 1.) 

52 yproj.set_out_range(h-(h-s)/2., (h-s)/2.) 

53 

54 # m = mtm.symmat6(*(num.random.random(6)*2.-1.)) 

55 # mtm.MomentTensor(m=m) 

56 

57 mt = self._mt 

58 

59 mt_devi = mt.deviatoric() 

60 eig = mt_devi.eigensystem() 

61 

62 group_to_color = { 

63 'P': plot.graph_colors[0], 

64 'T': plot.graph_colors[1]} 

65 

66 for (group, patches, patches_lower, patches_upper, 

67 lines, lines_lower, lines_upper) in beachball.eig2gx(eig): 

68 

69 color = group_to_color[group] 

70 brush = qw.QBrush(qw.QColor(*color)) 

71 p.setBrush(brush) 

72 

73 pen = qw.QPen(qw.QColor(*color)) 

74 pen.setWidth(1) 

75 p.setPen(pen) 

76 

77 for poly in patches_lower: 

78 px, py, pz = poly.T 

79 points = make_QPolygonF(xproj(px), yproj(py)) 

80 p.drawPolygon(points) 

81 

82 color = (0, 0, 0) 

83 pen = qw.QPen(qw.QColor(*color)) 

84 pen.setWidth(2) 

85 p.setPen(pen) 

86 

87 for poly in lines_lower: 

88 px, py, pz = poly.T 

89 points = make_QPolygonF(xproj(px), yproj(py)) 

90 p.drawPolyline(points) 

91 

92 

93class MomentTensorEditor(qw.QFrame): 

94 

95 moment_tensor_changed = qc.pyqtSignal(object) 

96 

97 def __init__(self, *args): 

98 qw.QFrame.__init__(self, *args) 

99 

100 self._mt = mtm.MomentTensor(m=mtm.symmat6(1., -1., 2., 0., -2., 1.)) 

101 

102 setupdata = [ 

103 (LinValControl, 'Strike 1', 0., 360., 0., 0), 

104 (LinValControl, 'Dip 1', 0., 90., 0., 1), 

105 (LinValControl, 'Slip-Rake 1', -180., 180., 0., 2), 

106 (LinValControl, 'Strike 2', 0., 360., 0., 3), 

107 (LinValControl, 'Dip 2', 0., 90., 0., 4), 

108 (LinValControl, 'Slip-Rake 2', -180., 180., 0., 5)] 

109 

110 layout = qw.QGridLayout() 

111 self.setLayout(layout) 

112 

113 val_controls = [] 

114 for irow, (typ, name, vmin, vmax, vcur, ind) in enumerate(setupdata): 

115 val_control = typ() 

116 val_control.setup(name, vmin, vmax, vcur, ind) 

117 val_controls.append(val_control) 

118 for icol, widget in enumerate(val_control.widgets()): 

119 layout.addWidget(widget, irow, icol) 

120 val_control.valchanged.connect( 

121 self.valchange) 

122 

123 self.val_controls = val_controls 

124 self.adjust_values() 

125 

126 def adjust_values(self): 

127 

128 ((strike1, dip1, rake1), 

129 (strike2, dip2, rake2)) = self._mt.both_strike_dip_rake() 

130 

131 for val_control, value in zip( 

132 self.val_controls, [ 

133 strike1, dip1, rake1, strike2, dip2, rake2]): 

134 val_control.set_value(value) 

135 

136 def valchange(self, val, ind): 

137 strike, dip, rake = [ 

138 val_control.get_value() for val_control in self.val_controls[:3]] 

139 

140 self._mt = mtm.MomentTensor( 

141 strike=strike, dip=dip, rake=rake) 

142 

143 self.adjust_values() 

144 

145 self.moment_tensor_changed.emit( 

146 self._mt)