1#!/usr/bin/env python
2# http://pyrocko.org - GPLv3
3#
4# The Pyrocko Developers, 21st Century
5# ---|P------/S----------~Lg----------
6from __future__ import absolute_import, division
8import os
9import sys
10import signal
11import logging
12import time
14from pyrocko import hamster_pile, util
15from pyrocko.streaming import serial_hamster
16from optparse import OptionParser
17pjoin = os.path.join
19logger = logging.getLogger('pyrocko.apps.hamster')
22def main(args=None):
23 if args is None:
24 args = sys.argv[1:]
26 parser = OptionParser(
27 usage='hamster [options] datadir',
28 description='''
29Datalogger program for the A/D converter of the "School Seismometer" connected
30to a serial port. This program expects whitespace-separated ascii numbers on a
31serial interface and stores the received data as MSEED files in the directory
32datadir. It automatically detects the sampling rate and uses the system clock
33as reference for time synchronization. If a change, gap, or jump of the
34sampling rate is detected, a new data trace is started. Small drifts of the
35sampling rate are compensated by adding or removing single samples when
36necessary.
37'''.strip())
39 parser.add_option(
40 '--port', dest='port', default='/dev/ttyS0', metavar='STR',
41 help='device name of the serial port to be used (%default)')
43 parser.add_option(
44 '--baudrate', dest='baudrate', default='9600', metavar='INT',
45 help='baudrate for serial port (%default)')
47 parser.add_option(
48 '--timeout', dest='timeout', default='5', metavar='FLOAT',
49 help='serial port timeout in seconds (%default)')
51 parser.add_option(
52 '--filelength', dest='filelength', default='3600', metavar='FLOAT',
53 help='approx file length in seconds (%default)')
55 parser.add_option(
56 '--network', dest='network', default='', metavar='STR',
57 help='network name (%default)')
58 parser.add_option(
59 '--station', dest='station', default='STA', metavar='STR',
60 help='station name (%default)')
61 parser.add_option(
62 '--location', dest='location', default='', metavar='STR',
63 help='location name (%default)')
64 parser.add_option(
65 '--channel', dest='channel', default='N', metavar='STR',
66 help='channel name (%default)')
68 parser.add_option(
69 '--blocksize', dest='buffersize', default=128, metavar='INT',
70 help='block size for time synchronization (%default)')
72 parser.add_option(
73 '--lookback', dest='lookback', default=5, metavar='INT',
74 help='number of previous blocks to consider (%default)')
76 parser.add_option(
77 '--debug', dest='debug', action='store_true', default=False,
78 help='enable debugging output')
80 options, args = parser.parse_args(args)
81 if len(args) < 2:
82 parser.error('required argument missing')
83 directory = args[1]
85 if options.debug:
86 util.setup_logging('hamster', 'debug')
87 else:
88 util.setup_logging('hamster', 'warning')
90 pile = hamster_pile.HamsterPile()
91 pile.set_fixation_length(float(options.filelength))
93 fn = 'data_%(network)s_%(station)s_%(location)s_%(channel)s_' \
94 '%(tmin)s_%(tmax)s.mseed'
96 pile.set_save_path(pjoin(directory, fn))
98 # testsource = Popen(['./test_datasource.py'], stdout=PIPE)
100 while True:
101 try:
102 hamster = serial_hamster.Acquisition(
103 port=options.port,
104 baudrate=int(options.baudrate),
105 timeout=float(options.timeout),
106 network=options.network,
107 station=options.station,
108 location=options.location,
109 channel=options.channel,
110 buffersize=options.buffersize,
111 lookback=options.lookback,
112 # in_file=testsource.stdout,
113 )
115 hamster.add_listener(pile)
116 signal.signal(signal.SIGINT, hamster.quit_soon)
117 hamster.start()
118 pile.fixate_all()
119 sys.exit()
121 except serial_hamster.SerialHamsterError as e:
123 pile.fixate_all()
124 hamster.stop()
125 hamster.clear_listeners()
126 logger.error(str(e))
127 logger.error('Acquistion terminated, restart in 5 s')
128 time.sleep(5)
129 if not hamster.sun_is_shining():
130 sys.exit()
133if __name__ == '__main__':
134 main(sys.argv[1:])