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