-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobserver_waveform.py
More file actions
executable file
·342 lines (284 loc) · 11.6 KB
/
observer_waveform.py
File metadata and controls
executable file
·342 lines (284 loc) · 11.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding: utf-8 -*-
import time
import numpy
import queue
import socket
import struct
import signal
import threading
from queue import Empty
import matplotlib.pyplot
import matplotlib.animation
from matplotlib.artist import Artist
from obspy import UTCDateTime, Stream, Trace
station_tcpaddr = "127.0.0.1" # Observer TCP Forwarder address
station_tcpport = 30000 # Observer TCP Forwarder port
time_span = 120 # Time span in seconds
refresh_time = 1000 # Refresh time in milliseconds
window_size = 2 # Spectrogram window size in seconds
overlap_percent = 86 # Spectrogram overlap in percent
spectrogram_power_range = [20, 140] # Spectrogram power range in dB
processing_queue = queue.Queue(maxsize=1000)
fig, axs = matplotlib.pyplot.subplots(6, 1, num="Observer Waveform", figsize=(9.6, 7.0))
matplotlib.pyplot.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0)
stop_event = threading.Event()
worker_thread: threading.Thread | None = None
ani = None
channel_code = "EHZ"
def get_checksum(message: str) -> int:
fields = message.split(",")
if len(fields) < 8:
raise ValueError("message fields length is less than 8")
data_arr = [int(field) for field in fields[7:-1]]
checksum = 0
for data in data_arr:
bytes_data = struct.pack("<i", data)
for byte in bytes_data:
checksum ^= byte
return checksum
def compare_checksum(message: str):
checksum_index = message.find("*")
if checksum_index == -1:
raise ValueError("checksum not found in message")
msg_checksum = int(message[checksum_index + 1 : checksum_index + 3], 16)
calc_checksum = get_checksum(message)
return msg_checksum == calc_checksum
def make_trace(net, stn, loc, channel, sps, counts_list, timestamp):
trace = Trace(data=numpy.ma.MaskedArray(counts_list, dtype=numpy.float64))
trace.stats.network = net
trace.stats.station = stn
trace.stats.location = loc
trace.stats.channel = channel
trace.stats.sampling_rate = sps
trace.stats.starttime = UTCDateTime(timestamp)
return trace
def resample_trace(trace, target_sampling_rate):
if trace.stats.sampling_rate != target_sampling_rate:
trace.interpolate(target_sampling_rate)
return trace
def get_data(host, port):
global channel_code
buffer = ""
while not stop_event.is_set():
client_socket = None
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.settimeout(1.0) # 短超时,方便及时退出
client_socket.connect((host, port))
print(f"Connected to {host}:{port}")
while not stop_event.is_set():
try:
recv_data = client_socket.recv(16384)
except socket.timeout:
continue
if not recv_data:
print("No data received, connection lost.")
break
buffer += recv_data.decode("utf-8", errors="ignore")
while "\r\n" in buffer:
line, buffer = buffer.split("\r\n", 1)
if not line.strip():
continue
try:
if compare_checksum(line):
msg = line.split("*")[0].rstrip(",")
fields = msg.split(",")
index = int(fields[0][1:])
network_code = fields[1]
station_code = fields[2]
location_code = fields[3]
channel_code = fields[4]
timestamp = int(fields[5]) / 1000
sample_rate = int(fields[6])
samples = list(map(int, fields[7:]))
if index in [1, 2, 3] and channel_code[-1] in [
"E",
"N",
"Z",
]:
trace = make_trace(
network_code,
station_code,
location_code,
channel_code,
sample_rate,
samples,
timestamp,
)
st_data = Stream(traces=[trace])
if not processing_queue.empty():
try:
existing_stream = processing_queue.get_nowait()
st_data += existing_stream
except Empty:
pass
try:
processing_queue.put_nowait(st_data)
except queue.Full:
try:
processing_queue.get_nowait()
except Empty:
pass
try:
processing_queue.put_nowait(st_data)
except queue.Full:
pass
except Exception as ex:
if not stop_event.is_set():
print(f"Error processing line: {ex}")
except Exception as e:
if not stop_event.is_set():
print(f"TCP Error: {e}. Reconnecting..")
finally:
if client_socket is not None:
try:
client_socket.close()
except Exception:
pass
# 不要一次睡太久,拆成短等待,便于及时响应退出
for _ in range(10):
if stop_event.is_set():
break
time.sleep(0.1)
def update(frame) -> tuple[Artist, ...]:
if stop_event.is_set():
return ()
try:
st_data = processing_queue.get_nowait()
if len(st_data) < 3:
return ()
components = {tr.stats.channel[-1]: tr for tr in st_data}
bhe_resampled = resample_trace(components["E"], bhe_stream.stats.sampling_rate)
bhn_resampled = resample_trace(components["N"], bhn_stream.stats.sampling_rate)
bhz_resampled = resample_trace(components["Z"], bhz_stream.stats.sampling_rate)
for stream, new_data in zip(
[bhe_stream, bhn_stream, bhz_stream],
[bhe_resampled, bhn_resampled, bhz_resampled],
):
new_samples = int(new_data.stats.npts)
stream_length = int(stream.stats.sampling_rate * time_span)
if len(stream.data) >= stream_length:
stream.data = numpy.roll(stream.data, -new_samples)
stream.data[-new_samples:] = new_data.data
else:
stream.data = numpy.concatenate((stream.data, new_data.data))
if len(stream.data) > stream_length:
stream.data = stream.data[-stream_length:]
stream.stats.starttime = stream.stats.starttime + 1.0
for i, (stream, component) in enumerate(
zip(
[bhe_stream, bhn_stream, bhz_stream],
[
f"{channel_code[0:2]}E",
f"{channel_code[0:2]}N",
f"{channel_code[0:2]}Z",
],
)
):
axs[i * 2].clear()
axs[i * 2 + 1].clear()
times = numpy.arange(stream.stats.npts) / stream.stats.sampling_rate
waveform_data = (
stream.copy()
.filter("bandpass", freqmin=0.1, freqmax=10.0, zerophase=True)
.data
)
if not numpy.any(numpy.isnan(waveform_data)) and not numpy.any(
numpy.isinf(waveform_data)
):
axs[i * 2].plot(times, waveform_data, label=component, color="blue")
axs[i * 2].legend(loc="upper left")
axs[i * 2].xaxis.set_visible(False)
axs[i * 2].yaxis.set_visible(False)
axs[i * 2].set_xlim([times[0], times[-1]])
ymin = float(numpy.min(waveform_data))
ymax = float(numpy.max(waveform_data))
if ymin == ymax:
ymin -= 1.0
ymax += 1.0
axs[i * 2].set_ylim([ymin, ymax])
NFFT = int(stream.stats.sampling_rate * window_size)
noverlap = int(NFFT * (overlap_percent / 100))
spec_data = stream.copy().filter("highpass", freq=0.1, zerophase=True).data
if not numpy.any(numpy.isnan(spec_data)) and not numpy.any(
numpy.isinf(spec_data)
):
axs[i * 2 + 1].specgram(
spec_data,
NFFT=NFFT,
Fs=stream.stats.sampling_rate,
noverlap=noverlap,
cmap="jet",
vmin=spectrogram_power_range[0],
vmax=spectrogram_power_range[1],
)
axs[i * 2 + 1].set_ylim(0, 15)
axs[i * 2 + 1].yaxis.set_visible(False)
axs[i * 2 + 1].xaxis.set_visible(False)
except Empty:
return ()
except Exception as e:
if not stop_event.is_set():
print(f"Error plotting data: {e}")
return ()
def shutdown(*_args):
global ani
if stop_event.is_set():
return
print("Shutting down...")
stop_event.set()
if ani is not None:
try:
ani.event_source.stop()
except Exception:
pass
try:
matplotlib.pyplot.close("all")
except Exception:
pass
if __name__ == "__main__":
worker_thread = threading.Thread(
target=get_data,
args=(station_tcpaddr, station_tcpport),
name="observer-reader",
daemon=True, # 先设成 daemon,避免极端情况下解释器卡死
)
worker_thread.start()
signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)
fig.canvas.mpl_connect("close_event", shutdown)
# 初始化等待首批数据,但不要无限阻塞
st_data = None
while not stop_event.is_set():
try:
st_data = processing_queue.get(timeout=0.5)
if len(st_data) >= 3:
break
except Empty:
continue
if st_data is None or len(st_data) < 3:
print("No initial data received, exiting.")
else:
components = {tr.stats.channel[-1]: tr for tr in st_data}
bhe_stream = components["E"].copy()
bhn_stream = components["N"].copy()
bhz_stream = components["Z"].copy()
stream_length = int(bhe_stream.stats.sampling_rate * time_span)
bhe_stream.data = numpy.zeros(stream_length)
bhn_stream.data = numpy.zeros(stream_length)
bhz_stream.data = numpy.zeros(stream_length)
ani = matplotlib.animation.FuncAnimation(
fig,
update,
interval=refresh_time,
cache_frame_data=False,
blit=False,
)
try:
matplotlib.pyplot.show()
except KeyboardInterrupt:
shutdown()
finally:
shutdown()
if worker_thread is not None and worker_thread.is_alive():
worker_thread.join(timeout=2.0)