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-04 09:52 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6from .qt_compat import qc, qw
8from .util import make_QPolygonF, LinValControl
9from .pile_viewer import Projection
11from pyrocko import beachball, moment_tensor as mtm
12from pyrocko import plot
15class BeachballView(qw.QWidget):
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)
23 def set_moment_tensor(self, mt):
24 self._mt = mt
25 self.update()
27 def paintEvent(self, paint_ev):
28 '''
29 Called by QT whenever widget needs to be painted.
30 '''
32 painter = qw.QPainter(self)
33 painter.setRenderHint(qw.QPainter.Antialiasing)
34 self.drawit(painter)
36 def drawit(self, p):
37 '''
38 Draw beachball into painter.
39 '''
41 h = self.height()
42 w = self.width()
44 s = min(h, w)*0.9
46 xproj = Projection()
47 xproj.set_in_range(-1., 1.)
48 xproj.set_out_range((w-s)/2., w-(w-s)/2.)
50 yproj = Projection()
51 yproj.set_in_range(-1., 1.)
52 yproj.set_out_range(h-(h-s)/2., (h-s)/2.)
54 # m = mtm.symmat6(*(num.random.random(6)*2.-1.))
55 # mtm.MomentTensor(m=m)
57 mt = self._mt
59 mt_devi = mt.deviatoric()
60 eig = mt_devi.eigensystem()
62 group_to_color = {
63 'P': plot.graph_colors[0],
64 'T': plot.graph_colors[1]}
66 for (group, patches, patches_lower, patches_upper,
67 lines, lines_lower, lines_upper) in beachball.eig2gx(eig):
69 color = group_to_color[group]
70 brush = qw.QBrush(qw.QColor(*color))
71 p.setBrush(brush)
73 pen = qw.QPen(qw.QColor(*color))
74 pen.setWidth(1)
75 p.setPen(pen)
77 for poly in patches_lower:
78 px, py, pz = poly.T
79 points = make_QPolygonF(xproj(px), yproj(py))
80 p.drawPolygon(points)
82 color = (0, 0, 0)
83 pen = qw.QPen(qw.QColor(*color))
84 pen.setWidth(2)
85 p.setPen(pen)
87 for poly in lines_lower:
88 px, py, pz = poly.T
89 points = make_QPolygonF(xproj(px), yproj(py))
90 p.drawPolyline(points)
93class MomentTensorEditor(qw.QFrame):
95 moment_tensor_changed = qc.pyqtSignal(object)
97 def __init__(self, *args):
98 qw.QFrame.__init__(self, *args)
100 self._mt = mtm.MomentTensor(m=mtm.symmat6(1., -1., 2., 0., -2., 1.))
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)]
110 layout = qw.QGridLayout()
111 self.setLayout(layout)
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)
123 self.val_controls = val_controls
124 self.adjust_values()
126 def adjust_values(self):
128 ((strike1, dip1, rake1),
129 (strike2, dip2, rake2)) = self._mt.both_strike_dip_rake()
131 for val_control, value in zip(
132 self.val_controls, [
133 strike1, dip1, rake1, strike2, dip2, rake2]):
134 val_control.set_value(value)
136 def valchange(self, val, ind):
137 strike, dip, rake = [
138 val_control.get_value() for val_control in self.val_controls[:3]]
140 self._mt = mtm.MomentTensor(
141 strike=strike, dip=dip, rake=rake)
143 self.adjust_values()
145 self.moment_tensor_changed.emit(
146 self._mt)