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

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

'''Effective seismological trace viewer.''' 

from __future__ import absolute_import 

from future import standard_library 

standard_library.install_aliases() # noqa 

 

import os 

import sys 

import logging 

import gc 

import tempfile 

import shutil 

import glob 

import urllib.parse # noqa 

 

from os.path import join as pjoin 

from optparse import OptionParser 

 

 

from pyrocko import pile 

from pyrocko import util 

from pyrocko import model 

from pyrocko import config 

from pyrocko import io 

from pyrocko.gui import marker 

from pyrocko.io import stationxml 

 

 

logger = logging.getLogger('pyrocko.gui.snuffler') 

 

app = None 

 

 

def extend_paths(paths): 

paths_r = [] 

for p in paths: 

paths_r.extend(glob.glob(p)) 

return paths_r 

 

 

def snuffle(pile=None, **kwargs): 

'''View pile in a snuffler window. 

 

:param pile: :py:class:`pile.Pile` object to be visualized 

:param stations: list of `pyrocko.model.Station` objects or ``None`` 

:param events: list of `pyrocko.model.Event` objects or ``None`` 

:param markers: list of `pyrocko.gui.util.Marker` objects or ``None`` 

:param ntracks: float, number of tracks to be shown initially (default: 12) 

:param follow: time interval (in seconds) for real time follow mode or 

``None`` 

:param controls: bool, whether to show the main controls (default: 

``True``) 

:param opengl: bool, whether to use opengl (default: ``False``) 

:param paths: list of files and directories to search for trace files 

:param pattern: regex which filenames must match 

:param format: format of input files 

:param cache_dir: cache directory with trace meta information 

:param force_cache: bool, whether to use the cache when attribute spoofing 

is active 

:param store_path: filename template, where to store trace data from input 

streams 

:param store_interval: float, time interval (in seconds) between stream 

buffer dumps 

:param want_markers: bool, whether markers should be returned 

:param launch_hook: callback function called before snuffler window is 

shown 

''' 

from .snuffler_app import Snuffler, SnufflerWindow, \ 

setup_acquisition_sources, PollInjector 

 

if pile is None: 

pile = pile.make_pile() 

 

global app 

if app is None: 

import locale 

locale.setlocale(locale.LC_ALL, 'C') 

app = Snuffler() 

 

kwargs_load = {} 

for k in ('paths', 'regex', 'format', 'cache_dir', 'force_cache'): 

try: 

kwargs_load[k] = kwargs.pop(k) 

except KeyError: 

pass 

 

store_path = kwargs.pop('store_path', None) 

store_interval = kwargs.pop('store_interval', 600) 

want_markers = kwargs.pop('want_markers', False) 

launch_hook = kwargs.pop('launch_hook', None) 

 

win = SnufflerWindow(pile, **kwargs) 

if launch_hook: 

if not isinstance(launch_hook, list): 

launch_hook = [launch_hook] 

for hook in launch_hook: 

hook(win) 

 

sources = [] 

pollinjector = None 

tempdir = None 

if 'paths' in kwargs_load: 

sources.extend(setup_acquisition_sources(kwargs_load['paths'])) 

if sources: 

if store_path is None: 

tempdir = tempfile.mkdtemp('', 'snuffler-tmp-') 

store_path = pjoin( 

tempdir, 

'trace-%(network)s.%(station)s.%(location)s.%(channel)s.' 

'%(tmin)s.mseed') 

elif os.path.isdir(store_path): 

store_path = pjoin( 

store_path, 

'trace-%(network)s.%(station)s.%(location)s.%(channel)s.' 

'%(tmin)s.mseed') 

 

pollinjector = PollInjector( 

pile, 

fixation_length=store_interval, 

path=store_path) 

 

for source in sources: 

source.start() 

pollinjector.add_source(source) 

 

win.get_view().load(**kwargs_load) 

 

if not win.is_closing(): 

app.install_sigint_handler() 

app.exec_() 

app.uninstall_sigint_handler() 

 

for source in sources: 

source.stop() 

 

if pollinjector: 

pollinjector.fixate_all() 

 

ret = win.return_tag() 

 

if want_markers: 

markers = win.get_view().get_markers() 

 

del win 

gc.collect() 

 

if tempdir: 

shutil.rmtree(tempdir) 

 

if want_markers: 

return ret, markers 

else: 

return ret 

 

 

def snuffler_from_commandline(args=None): 

if args is None: 

args = sys.argv[1:] 

 

usage = '''usage: %prog [options] waveforms ...''' 

parser = OptionParser(usage=usage) 

 

parser.add_option( 

'--format', 

dest='format', 

default='detect', 

choices=io.allowed_formats('load'), 

help='assume input files are of given FORMAT. Choices: %s' 

% io.allowed_formats('load', 'cli_help', 'detect')) 

 

parser.add_option( 

'--pattern', 

dest='regex', 

metavar='REGEX', 

help='only include files whose paths match REGEX') 

 

parser.add_option( 

'--stations', 

dest='station_fns', 

action='append', 

default=[], 

metavar='STATIONS', 

help='read station information from file STATIONS') 

 

parser.add_option( 

'--stationxml', 

dest='stationxml_fns', 

action='append', 

default=[], 

metavar='STATIONSXML', 

help='read station information from XML file STATIONSXML') 

 

parser.add_option( 

'--event', '--events', 

dest='event_fns', 

action='append', 

default=[], 

metavar='EVENT', 

help='read event information from file EVENT') 

 

parser.add_option( 

'--markers', 

dest='marker_fns', 

action='append', 

default=[], 

metavar='MARKERS', 

help='read marker information file MARKERS') 

 

parser.add_option( 

'--follow', 

type='float', 

dest='follow', 

metavar='N', 

help='follow real time with a window of N seconds') 

 

parser.add_option( 

'--cache', 

dest='cache_dir', 

default=config.config().cache_dir, 

metavar='DIR', 

help='use directory DIR to cache trace metadata ' 

'(default=\'%default\')') 

 

parser.add_option( 

'--force-cache', 

dest='force_cache', 

action='store_true', 

default=False, 

help='use the cache even when trace attribute spoofing is active ' 

'(may have silly consequences)') 

 

parser.add_option( 

'--store-path', 

dest='store_path', 

metavar='PATH_TEMPLATE', 

help='store data received through streams to PATH_TEMPLATE') 

 

parser.add_option( 

'--store-interval', 

type='float', 

dest='store_interval', 

default=600, 

metavar='N', 

help='dump stream data to file every N seconds [default: %default]') 

 

parser.add_option( 

'--ntracks', 

type='int', 

dest='ntracks', 

default=24, 

metavar='N', 

help='initially use N waveform tracks in viewer [default: %default]') 

 

parser.add_option( 

'--opengl', 

dest='opengl', 

action='store_true', 

default=False, 

help='use OpenGL for drawing') 

 

parser.add_option( 

'--qt5', 

dest='gui_toolkit_qt5', 

action='store_true', 

default=False, 

help='use Qt5 for the GUI') 

 

parser.add_option( 

'--qt4', 

dest='gui_toolkit_qt4', 

action='store_true', 

default=False, 

help='use Qt4 for the GUI') 

 

parser.add_option( 

'--debug', 

dest='debug', 

action='store_true', 

default=False, 

help='print debugging information to stderr') 

 

options, args = parser.parse_args(list(args)) 

 

if options.debug: 

util.setup_logging('snuffler', 'debug') 

else: 

util.setup_logging('snuffler', 'warning') 

 

if options.gui_toolkit_qt4: 

config.override_gui_toolkit = 'qt4' 

 

if options.gui_toolkit_qt5: 

config.override_gui_toolkit = 'qt5' 

 

this_pile = pile.Pile() 

stations = [] 

for stations_fn in extend_paths(options.station_fns): 

stations.extend(model.station.load_stations(stations_fn)) 

 

for stationxml_fn in extend_paths(options.stationxml_fns): 

stations.extend( 

stationxml.load_xml( 

filename=stationxml_fn).get_pyrocko_stations()) 

 

events = [] 

for event_fn in extend_paths(options.event_fns): 

events.extend(model.load_events(event_fn)) 

 

markers = [] 

for marker_fn in extend_paths(options.marker_fns): 

markers.extend(marker.load_markers(marker_fn)) 

 

return snuffle( 

this_pile, 

stations=stations, 

events=events, 

markers=markers, 

ntracks=options.ntracks, 

follow=options.follow, 

controls=True, 

opengl=options.opengl, 

paths=args, 

cache_dir=options.cache_dir, 

regex=options.regex, 

format=options.format, 

force_cache=options.force_cache, 

store_path=options.store_path, 

store_interval=options.store_interval) 

 

 

if __name__ == '__main__': 

snuffler_from_commandline()