1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import os 

7import sys 

8import signal 

9import logging 

10import time 

11 

12from pyrocko import hamster_pile, util 

13from pyrocko.streaming import serial_hamster 

14from optparse import OptionParser 

15pjoin = os.path.join 

16 

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

18 

19 

20def main(args=None): 

21 if args is None: 

22 args = sys.argv[1:] 

23 

24 parser = OptionParser( 

25 usage='hamster [options] datadir', 

26 description=''' 

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

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

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

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

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

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

33sampling rate are compensated by adding or removing single samples when 

34necessary. 

35'''.strip()) 

36 

37 parser.add_option( 

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

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

40 

41 parser.add_option( 

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

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

44 

45 parser.add_option( 

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

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

48 

49 parser.add_option( 

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

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

52 

53 parser.add_option( 

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

55 help='network name (%default)') 

56 parser.add_option( 

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

58 help='station name (%default)') 

59 parser.add_option( 

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

61 help='location name (%default)') 

62 parser.add_option( 

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

64 help='channel name (%default)') 

65 

66 parser.add_option( 

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

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

69 

70 parser.add_option( 

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

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

73 

74 parser.add_option( 

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

76 help='enable debugging output') 

77 

78 options, args = parser.parse_args(args) 

79 if len(args) < 2: 

80 parser.error('required argument missing') 

81 directory = args[1] 

82 

83 if options.debug: 

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

85 else: 

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

87 

88 pile = hamster_pile.HamsterPile() 

89 pile.set_fixation_length(float(options.filelength)) 

90 

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

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

93 

94 pile.set_save_path(pjoin(directory, fn)) 

95 

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

97 

98 while True: 

99 try: 

100 hamster = serial_hamster.Acquisition( 

101 port=options.port, 

102 baudrate=int(options.baudrate), 

103 timeout=float(options.timeout), 

104 network=options.network, 

105 station=options.station, 

106 location=options.location, 

107 channel=options.channel, 

108 buffersize=options.buffersize, 

109 lookback=options.lookback, 

110 # in_file=testsource.stdout, 

111 ) 

112 

113 hamster.add_listener(pile) 

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

115 hamster.start() 

116 pile.fixate_all() 

117 sys.exit() 

118 

119 except serial_hamster.SerialHamsterError as e: 

120 

121 pile.fixate_all() 

122 hamster.stop() 

123 hamster.clear_listeners() 

124 logger.error(str(e)) 

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

126 time.sleep(5) 

127 if not hamster.sun_is_shining(): 

128 sys.exit() 

129 

130 

131if __name__ == '__main__': 

132 main()