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

#!/usr/bin/env python 

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

from __future__ import absolute_import, division 

 

import os 

import sys 

import signal 

import logging 

import time 

 

from pyrocko import hamster_pile, util 

from pyrocko.streaming import serial_hamster 

from optparse import OptionParser 

pjoin = os.path.join 

 

logger = logging.getLogger('pyrocko.apps.hamster') 

 

 

def main(args=None): 

if args is None: 

args = sys.argv[1:] 

 

parser = OptionParser( 

usage='hamster [options] datadir', 

description=''' 

Datalogger program for the A/D converter of the "School Seismometer" connected 

to a serial port. This program expects whitespace-separated ascii numbers on a 

serial interface and stores the received data as MSEED files in the directory 

datadir. It automatically detects the sampling rate and uses the system clock 

as reference for time synchronization. If a change, gap, or jump of the 

sampling rate is detected, a new data trace is started. Small drifts of the 

sampling rate are compensated by adding or removing single samples when 

necessary. 

'''.strip()) 

 

parser.add_option( 

'--port', dest='port', default='/dev/ttyS0', metavar='STR', 

help='device name of the serial port to be used (%default)') 

 

parser.add_option( 

'--baudrate', dest='baudrate', default='9600', metavar='INT', 

help='baudrate for serial port (%default)') 

 

parser.add_option( 

'--timeout', dest='timeout', default='5', metavar='FLOAT', 

help='serial port timeout in seconds (%default)') 

 

parser.add_option( 

'--filelength', dest='filelength', default='3600', metavar='FLOAT', 

help='approx file length in seconds (%default)') 

 

parser.add_option( 

'--network', dest='network', default='', metavar='STR', 

help='network name (%default)') 

parser.add_option( 

'--station', dest='station', default='STA', metavar='STR', 

help='station name (%default)') 

parser.add_option( 

'--location', dest='location', default='', metavar='STR', 

help='location name (%default)') 

parser.add_option( 

'--channel', dest='channel', default='N', metavar='STR', 

help='channel name (%default)') 

 

parser.add_option( 

'--blocksize', dest='buffersize', default=128, metavar='INT', 

help='block size for time synchronization (%default)') 

 

parser.add_option( 

'--lookback', dest='lookback', default=5, metavar='INT', 

help='number of previous blocks to consider (%default)') 

 

parser.add_option( 

'--debug', dest='debug', action='store_true', default=False, 

help='enable debugging output') 

 

options, args = parser.parse_args(args) 

if len(args) < 2: 

parser.error('required argument missing') 

directory = args[1] 

 

if options.debug: 

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

else: 

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

 

pile = hamster_pile.HamsterPile() 

pile.set_fixation_length(float(options.filelength)) 

 

fn = 'data_%(network)s_%(station)s_%(location)s_%(channel)s_' \ 

'%(tmin)s_%(tmax)s.mseed' 

 

pile.set_save_path(pjoin(directory, fn)) 

 

# testsource = Popen(['./test_datasource.py'], stdout=PIPE) 

 

while True: 

try: 

hamster = serial_hamster.Acquisition( 

port=options.port, 

baudrate=int(options.baudrate), 

timeout=float(options.timeout), 

network=options.network, 

station=options.station, 

location=options.location, 

channel=options.channel, 

buffersize=options.buffersize, 

lookback=options.lookback, 

# in_file=testsource.stdout, 

) 

 

hamster.add_listener(pile) 

signal.signal(signal.SIGINT, hamster.quit_soon) 

hamster.start() 

pile.fixate_all() 

sys.exit() 

 

except serial_hamster.SerialHamsterError as e: 

 

pile.fixate_all() 

hamster.stop() 

hamster.clear_listeners() 

logger.error(str(e)) 

logger.error('Acquistion terminated, restart in 5 s') 

time.sleep(5) 

if not hamster.sun_is_shining(): 

sys.exit() 

 

 

if __name__ == '__main__': 

main(sys.argv[1:])