Source code for pyrocko.gui.snuffler.snuffler

# https://pyrocko.org - GPLv3
#
# The Pyrocko Developers, 21st Century
# ---|P------/S----------~Lg----------

'''
Effective seismological trace viewer.
'''

import os
import sys
import logging
import gc
import tempfile
import shutil
import glob

from os.path import join as pjoin
from optparse import OptionParser


from pyrocko import deps
from pyrocko import pile as pile_mod
from pyrocko import util
from pyrocko import model
from pyrocko import config
from pyrocko import io
from pyrocko.io import stationxml

from . import marker


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


def extend_paths(paths):
    paths_r = []
    for p in paths:
        paths_r.extend(glob.glob(p))
    return paths_r


[docs] def snuffle(pile=None, **kwargs): ''' View pile in a snuffler window. :param pile: :py:class:`~pyrocko.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.snuffler.util.Marker` objects or ``None`` :param ntracks: float, number of tracks to be shown initially (default: 12) :param marker_editor_sortable: bool, whether to allow sorting in marker table (default True). Disabling this will give better performance when working with many markers. :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: ``None`` - automatic choice). :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 :param instant_close: bool, whether to bypass close window confirmation dialog ''' from .snuffler_app import SnufflerWindow, \ setup_acquisition_sources, PollInjector if pile is None: pile = pile_mod.make_pile() from pyrocko.gui import util as gui_util app = gui_util.get_app() 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) app.set_main_window(win) 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_ms)s.yaff') elif os.path.isdir(store_path): store_path = pjoin( store_path, 'trace-%(network)s.%(station)s.%(location)s.%(channel)s.' '%(tmin_ms)s.yaff') interval = min( source.get_wanted_poll_interval() for source in sources) pollinjector = PollInjector( pile, fixation_length=store_interval, path=store_path, interval=interval) 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() try: app.exec_() finally: app.uninstall_sigint_handler() app.set_main_window(None) 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 ... Input waveforms can be any combination of the following data sources: * waveform files of supported formats * directories, which are recursively searched for waveform files * pseudo URLs, which are used to open real-time data streams from different sources To read real-time streams, the following pseudo URLs are supported: seedlink://<host>[:<port>]/<pattern> Acquire data through SeedLink from given <host>. The specified <pattern> is matched against strings of the form <network>.<station>.<location>.<channel>, where the placeholders stand for the corresponding IDs of each SeedLink data stream. To use this feature, slinktool must be installed. school://<device>?rate=<rate>[&gain=<gain>][&station=<station>] Acquire data through serial line from "school seismometer" or Lego Mindsets seismometer. Older variants have a chip labeled LPC1343F (https://www.mindsetsonline.co.uk/Downloads/SEP_064.pdf), newer have a RP2040 (https://mindsetsonline.co.uk/Downloads/SeismicStreamTechnical_V1.pdf). The LPC1343F variant supports sampling rates of 20.032, 39.860, and 79.719 Hz, selected with rate=lego1_20, lego1_40, or lego1_80, and gains of 1, 2, and 4, selected with gain=lego1_1, lego1_2, or lego1_4. The RP2040 variant supports sampling rates of 25, 50, and 100 Hz, selected with rate=lego2_25, lego2_50, or lego2_100, and gains of 1, 2, 4, 8, and 16, selected with gain=lego2_1, lego2_2, lego2_4, lego2_8, or lego2_16. The rate must be given, the gain defaults to 4. The station code defaults to TEST. The code does not check which variant is attached, using an invalid setting leads to undefined behaviour. For example to select 39.860 Hz and a gain of 2 with the older variant appearing on device /dev/ttyACM0, use 'school:///dev/ttyACM0?rate=lego1_40&gain=lego1_2'. hb628://<device>[?rate=<rate>[&channels=<channels>]] Acquire data from eight-channel USB-HB628 module. Example: to read channels 0, 3 and 5 from serial device /dev/ttyS0 at default rate (50Hz), use snuffler 'hb628:///dev/ttyS0?channels=035'. By default all channels are shown. cam://<device> Acquire data from Hamburg University Wiechert camera module. cube://<device> Acquire data from DiGOS DATA-CUBE recorder connected via serial interface. Example: snuffler 'cube:///dev/ttyUSB0' pulse:// Acquire data from the system's default microphone using PulseAudio. '''.strip() 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, this option can ' 'be given more than once') parser.add_option( '--stationxml', dest='stationxml_fns', action='append', default=[], metavar='STATIONSXML', help='read station information from XML file STATIONSXML, this ' 'option can be given more than once') parser.add_option( '--event', '--events', dest='event_fns', action='append', default=[], metavar='EVENT', help='read event information from file EVENT, this option can be ' 'given more than once') parser.add_option( '--markers', dest='marker_fns', action='append', default=[], metavar='MARKERS', help='read marker information from file MARKERS, this option can be ' 'given more than once') 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( '--disable-marker-sorting', action='store_false', dest='marker_editor_sortable', default=True, help='disable sorting in marker table for improved performance with ' '100000+ markers') parser.add_option( '--hptime', choices=('on', 'off', 'config'), dest='hp_time', default='config', metavar='on|off|config', help='set high precision time mode [default: %default]') parser.add_option( '--opengl', dest='opengl', action='store_true', default=None, help='use OpenGL for drawing') parser.add_option( '--no-opengl', dest='opengl', action='store_false', default=None, help='do not use OpenGL for drawing') 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', 'info') if options.hp_time in ('on', 'off'): util.use_high_precision_time(options.hp_time == 'on') this_pile = pile_mod.Pile() stations = [] for stations_fn in extend_paths(options.station_fns): stations.extend(model.station.load_stations(stations_fn)) station_xmls = [] for stationxml_fn in extend_paths(options.stationxml_fns): station_xml = stationxml.load_xml(filename=stationxml_fn) stations.extend(station_xml.get_pyrocko_stations()) station_xmls.append(station_xml) 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)) import pyrocko try: return pyrocko.snuffle( this_pile, stations=stations, station_xmls=station_xmls, events=events, markers=markers, ntracks=options.ntracks, marker_editor_sortable=options.marker_editor_sortable, 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) except deps.MissingPyrockoDependency as e: logger.fatal(str(e)) sys.exit(1) if __name__ == '__main__': snuffler_from_commandline()