Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/apps/hamster.py: 50%

52 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-04 09:52 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6''' 

7Hamster - a simple seismic datalogger. 

8''' 

9 

10import os 

11import sys 

12import signal 

13import logging 

14import time 

15 

16from pyrocko import hamster_pile, util 

17from pyrocko.streaming import serial_hamster 

18from optparse import OptionParser 

19pjoin = os.path.join 

20 

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

22 

23 

24def main(args=None): 

25 ''' 

26 CLI entry point for Pyrocko's ``hamster`` app. 

27 ''' 

28 if args is None: 

29 args = sys.argv[1:] 

30 

31 parser = OptionParser( 

32 usage='hamster [options] datadir', 

33 description=''' 

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

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

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

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

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

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

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

41necessary. 

42'''.strip()) 

43 

44 parser.add_option( 

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

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

47 

48 parser.add_option( 

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

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

51 

52 parser.add_option( 

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

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

55 

56 parser.add_option( 

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

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

59 

60 parser.add_option( 

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

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

63 parser.add_option( 

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

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

66 parser.add_option( 

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

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

69 parser.add_option( 

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

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

72 

73 parser.add_option( 

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

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

76 

77 parser.add_option( 

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

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

80 

81 parser.add_option( 

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

83 help='enable debugging output') 

84 

85 options, args = parser.parse_args(args) 

86 if len(args) < 2: 

87 parser.error('required argument missing') 

88 directory = args[1] 

89 

90 if options.debug: 

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

92 else: 

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

94 

95 pile = hamster_pile.HamsterPile() 

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

97 

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

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

100 

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

102 

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

104 

105 while True: 

106 try: 

107 hamster = serial_hamster.Acquisition( 

108 port=options.port, 

109 baudrate=int(options.baudrate), 

110 timeout=float(options.timeout), 

111 network=options.network, 

112 station=options.station, 

113 location=options.location, 

114 channel=options.channel, 

115 buffersize=options.buffersize, 

116 lookback=options.lookback, 

117 # in_file=testsource.stdout, 

118 ) 

119 

120 hamster.add_listener(pile) 

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

122 hamster.start() 

123 pile.fixate_all() 

124 sys.exit() 

125 

126 except serial_hamster.SerialHamsterError as e: 

127 

128 pile.fixate_all() 

129 hamster.stop() 

130 hamster.clear_listeners() 

131 logger.error(str(e)) 

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

133 time.sleep(5) 

134 if not hamster.sun_is_shining(): 

135 sys.exit() 

136 

137 

138if __name__ == '__main__': 

139 main()