Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/sparrow/elements/volcanoes.py: 97%

92 statements  

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

1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import numpy as num 

7 

8from pyrocko import table, geometry, cake 

9from pyrocko.guts import Bool, Float 

10from pyrocko.gui.qt_compat import qw, qc 

11 

12from pyrocko.dataset.volcanoes import Volcanoes 

13from pyrocko.gui.vtk_util import ScatterPipe 

14 

15from .base import Element, ElementState 

16 

17guts_prefix = 'sparrow' 

18 

19km = 1e3 

20COLOR_HOLOCENE = (0.98, 0.26, .32) 

21COLOR_PLEISTOCENE = (1., .41, .28) 

22 

23 

24def volcanoes_to_points(volcanoes): 

25 coords = num.zeros((len(volcanoes), 3)) 

26 

27 for i, v in enumerate(volcanoes): 

28 coords[i, :] = v.lat, v.lon, -v.elevation - 10*km 

29 

30 station_table = table.Table() 

31 

32 station_table.add_col(('coords', '', ('lat', 'lon', 'depth')), coords) 

33 

34 return geometry.latlondepth2xyz( 

35 station_table.get_col('coords'), 

36 planetradius=cake.earthradius) 

37 

38 

39def volcanoes_to_color(volcanoes): 

40 colors = [] 

41 for v in volcanoes: 

42 if v.age == 'holocene': 

43 colors.append(COLOR_HOLOCENE) 

44 else: 

45 colors.append(COLOR_PLEISTOCENE) 

46 return num.array(colors) 

47 

48 

49class VolcanoesState(ElementState): 

50 visible = Bool.T(default=True) 

51 size = Float.T(default=3.0) 

52 

53 def create(self): 

54 element = VolcanoesElement() 

55 return element 

56 

57 

58class VolcanoesElement(Element): 

59 

60 def __init__(self): 

61 Element.__init__(self) 

62 self._pipe = None 

63 self._controls = None 

64 self._volcanoes = None 

65 

66 def bind_state(self, state): 

67 Element.bind_state(self, state) 

68 self.talkie_connect(state, ['visible', 'size'], self.update) 

69 

70 def get_name(self): 

71 return 'Volcanoes' 

72 

73 def set_parent(self, parent): 

74 self._parent = parent 

75 if not self._volcanoes: 

76 self._volcanoes = Volcanoes() 

77 

78 self._parent.add_panel( 

79 self.get_title_label(), 

80 self._get_controls(), 

81 visible=True, 

82 title_controls=[ 

83 self.get_title_control_remove(), 

84 self.get_title_control_visible()]) 

85 

86 self.update() 

87 

88 def unset_parent(self): 

89 self.unbind_state() 

90 if not self._parent: 

91 return 

92 

93 if self._pipe: 

94 self._parent.remove_actor(self._pipe.actor) 

95 self._pipe = None 

96 

97 self._parent.remove_panel(self._controls) 

98 self._controls = None 

99 

100 self._parent.update_view() 

101 self._parent = None 

102 

103 def update(self, *args): 

104 state = self._state 

105 

106 if state.visible: 

107 if self._pipe is None: 

108 points = volcanoes_to_points(self._volcanoes.volcanoes) 

109 self._pipe = ScatterPipe(points) 

110 

111 colors = volcanoes_to_color(self._volcanoes.volcanoes) 

112 self._pipe.set_colors(colors) 

113 

114 self._pipe.set_size(state.size) 

115 self._pipe.set_symbol('sphere') 

116 self._parent.add_actor(self._pipe.actor) 

117 

118 else: 

119 if self._pipe is not None: 

120 self._parent.remove_actor(self._pipe.actor) 

121 

122 self._parent.update_view() 

123 

124 def _get_controls(self): 

125 if not self._controls: 

126 from ..state import state_bind_slider 

127 

128 frame = qw.QFrame() 

129 layout = qw.QGridLayout() 

130 frame.setLayout(layout) 

131 

132 layout.addWidget(qw.QLabel('Size'), 0, 0) 

133 

134 slider = qw.QSlider(qc.Qt.Horizontal) 

135 slider.setSizePolicy( 

136 qw.QSizePolicy( 

137 qw.QSizePolicy.Expanding, qw.QSizePolicy.Fixed)) 

138 slider.setMinimum(0) 

139 slider.setMaximum(10) 

140 slider.setSingleStep(1) 

141 slider.setPageStep(1) 

142 layout.addWidget(slider, 0, 1) 

143 state_bind_slider(self, self._state, 'size', slider) 

144 

145 layout.addWidget(qw.QFrame(), 1, 0, 1, 2) 

146 

147 self._controls = frame 

148 

149 return self._controls 

150 

151 

152__all__ = [ 

153 'VolcanoesElement', 

154 'VolcanoesState' 

155]