-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
486 lines (386 loc) · 18 KB
/
main.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import serial, serial.tools.list_ports
import numpy as np
import json
import kivy
import sys
import os
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.config import Config
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt
import pyaudio
from datetime import datetime
from obspy import read, Trace, Stream, UTCDateTime
from obspy.core import AttribDict
from obspy.io.segy.segy import SEGYTraceHeader, SEGYBinaryFileHeader
from obspy.io.segy.core import _read_segy
plt.style.use('bmh')
p = pyaudio.PyAudio()
BANDWITH = 20000
MEDIUMSPEED = 300000000
TAO = 1
SAMPLESIZE = 500 # number of data points to read at a time default 4096
SAMPLERATE = 100000 # time resolution of the recording self.device (Hz) 44100
TIMESAMPLE = 0.01 #in second
DISTANCE = 10
DEFTOPGAIN = 1.0
DEFBOTTOMGAIN = 2.0
# DEFMINGRAPH = 0
# DEFMAXGRAPH = 1
Window.clearcolor = (.9, .9, .9, 1)
Window.fullscreen = 'auto'
kivy.require('2.1.0')
# Designate Our .kv design file
kv = Builder.load_file('main.kv')
data_stream = Stream()
class MainWindow(BoxLayout):
checks_waveform = []
checks_method = []
flag_signal = False
flag_graph = False
flag_map = False
flag_move = False
dt_waveform = ""
dt_method = ""
lon = 107.6213
lat = -6.8775
zoom = 19
dt_slider_distance = 0
dt_distance = 0
last_distance = 0
dt_delay = 0
dt_interval = 10
# min_graph = 0
# max_graph = 50
top_gain = 5
bottom_gain = 20
data_signal = np.zeros(50)
data_colormap = np.zeros((SAMPLESIZE, DISTANCE))
data_wiggles = np.zeros((SAMPLESIZE, 1))
data_samples = np.zeros((SAMPLESIZE, 1))
data_adc = np.zeros(20)
sequence = 0
def __init__(self):
super(MainWindow, self).__init__()
self.fig1, self.ax = plt.subplots()
# self.fig2, (self.ax1, self.ax2, self.ax3) = plt.subplots(nrows=1, ncols=3, gridspec_kw={'width_ratios': [10, 1, 1]})
self.fig2, (self.ax1, self.ax2) = plt.subplots(nrows=1, ncols=2, gridspec_kw={'width_ratios': [10, 2]})
# self.ids.slider_min_graph.value = DEFMINGRAPH
# self.ids.slider_max_graph.value = DEFMAXGRAPH
self.ids.slider_top_gain.value = DEFTOPGAIN
self.ids.slider_bottom_gain.value = DEFBOTTOMGAIN
self.ids.slider_distance.max = DISTANCE
self.graph_screen()
def hide_widget(self, wid, dohide=True):
if hasattr(wid, 'saved_attrs'):
if not dohide:
wid.height, wid.size_hint_x, wid.size_hint_y, wid.opacity, wid.disabled = wid.saved_attrs
del wid.saved_attrs
elif dohide:
wid.saved_attrs = wid.height, wid.size_hint_x, wid.size_hint_y, wid.opacity, wid.disabled
wid.height, wid.size_hint_x, wid.size_hint_y, wid.opacity, wid.disabled = 0, None, None, 0, True
def graph_screen(self):
# self.ids.layout_graph_carrier.clear_widgets()
self.hide_widget(self.ids.layout_graph_setting, False)
self.hide_widget(self.ids.layout_graph, False)
self.hide_widget(self.ids.layout_map, True)
# self.ids.bt_screen_setting.disabled = False
self.ids.bt_screen_graph.disabled = True
self.ids.bt_screen_map.disabled = False
self.ids.bt_save_data.disabled = False
self.ids.bt_save_graph.disabled = False
self.ids.bt_update_map.disabled = True
self.ids.label_page.text = "GRAPH SCREEN"
if("CONTINUOUS" in self.dt_method):
self.ids.bt_update_graph.disabled = True
Clock.schedule_interval(self.update_graph_time, 2)
elif("MANUAL" in self.dt_method):
self.ids.bt_update_graph.disabled = False
Clock.unschedule(self.update_graph_time)
else:
self.ids.bt_update_graph.disabled = False
Clock.unschedule(self.update_graph_time)
def map_screen(self):
# self.ids.layout_graph_carrier.clear_widgets()
self.ids.layout_graph_signal.clear_widgets()
self.hide_widget(self.ids.layout_graph_setting, True)
self.hide_widget(self.ids.layout_graph, True)
self.hide_widget(self.ids.layout_map, False)
# self.ids.bt_screen_setting.disabled = False
self.ids.bt_screen_graph.disabled = False
self.ids.bt_screen_map.disabled = True
self.ids.bt_update_graph.disabled = True
self.ids.bt_save_data.disabled = True
self.ids.bt_save_graph.disabled = True
self.ids.bt_update_map.disabled = False
self.ids.label_page.text = "MAP SCREEN"
Clock.unschedule(self.update_graph_time)
def update_graph_time(self, interval):
self.update_graph()
self.dt_slider_distance += 1000
self.ids.slider_distance.value = float(self.dt_slider_distance / 1000)
self.dt_distance = int(self.dt_slider_distance / 1000)
def update_graph(self):
# self.min_graph = self.ids.slider_min_graph.value
# self.max_graph = self.ids.slider_max_graph.value
self.top_gain = self.ids.slider_top_gain.value
self.bottom_gain = self.ids.slider_bottom_gain.value
stream = p.open(format=pyaudio.paFloat32,
channels = 1,
rate = SAMPLERATE,
frames_per_buffer = SAMPLESIZE,
input = True)
x = np.linspace(0, SAMPLESIZE-1, SAMPLESIZE)
for i in range(0, int(SAMPLERATE / SAMPLESIZE * TIMESAMPLE)):
self.data_signal = np.frombuffer(stream.read(SAMPLESIZE), dtype=np.float32)
x_spec = np.fft.fftfreq(SAMPLESIZE, d = 1.0 / SAMPLERATE)
y_temp = np.fft.fft(self.data_signal[0:SAMPLESIZE])
y_spec = np.asarray([np.sqrt(c.real ** 2 + c.imag ** 2) for c in y_temp])
y_gain = np.array([SAMPLESIZE])
y_intime = np.fft.ifft(y_spec).real
y_wiggle = np.asarray([np.sqrt(c.real ** 2 + c.imag ** 2) for c in y_intime])
# add data to obspy trace
conv_y_wiggle = np.require(y_wiggle, dtype=np.float32)
trace = Trace(data=conv_y_wiggle)
# Attributes in trace.stats will overwrite everything in
# trace.stats.segy.trace_header
trace.stats.delta = 0.01
# SEGY does not support microsecond precision! Any microseconds will
# be discarded.
year = int(datetime.now().strftime("%Y"))
month = int(datetime.now().strftime("%m"))
date = int(datetime.now().strftime("%d"))
hour = int(datetime.now().strftime("%H"))
minute = int(datetime.now().strftime("%M"))
second = int(datetime.now().strftime("%S"))
trace.stats.starttime = UTCDateTime(year, month, date, hour, minute, second)
# If you want to set some additional attributes in the trace header,
# add one and only set the attributes you want to be set. Otherwise the
# header will be created for you with default values.
self.sequence += 1
if not hasattr(trace.stats, 'segy.trace_header'):
trace.stats.segy = {}
trace.stats.segy.trace_header = SEGYTraceHeader()
trace.stats.segy.trace_header.trace_sequence_number_within_line = self.sequence
trace.stats.segy.trace_header.receiver_group_elevation = 444
# Add trace to stream
data_stream.append(trace)
self.dt_distance = int(self.dt_slider_distance / 1000)
x_gain = np.linspace(0, SAMPLESIZE-1, SAMPLESIZE)
y_gain = ( x_gain * (self.bottom_gain - self.top_gain) / SAMPLESIZE) + self.top_gain
# for i in range (0, SAMPLESIZE):
# y_spec[i] = y_spec[i] * y_gain[i]
for i in range (0, SAMPLESIZE):
y_wiggle[i] = y_wiggle[i] * y_gain[i]
if(self.dt_distance >= DISTANCE):
# temp_data_signal = y_spec
temp_data_signal = y_wiggle
temp_data_signal.resize([SAMPLESIZE, 1])
data_stream.append(trace)
self.data_colormap = np.concatenate([self.data_colormap, temp_data_signal], axis=1)
self.data_wiggles = np.concatenate([self.data_wiggles, temp_data_signal], axis=1)
temp_data_samples = -x_spec
temp_data_samples.resize([SAMPLESIZE, 1])
self.data_samples = np.concatenate([self.data_samples, temp_data_samples], axis=1)
self.ids.slider_distance.max += 1
else:
# self.data_colormap[: , self.dt_distance] = y_spec
self.data_colormap[: , self.dt_distance] = y_wiggle
temp_data_signal = y_wiggle
temp_data_signal.resize([SAMPLESIZE, 1])
data_stream.append(trace)
self.data_wiggles = np.concatenate([self.data_wiggles, temp_data_signal], axis=1)
temp_data_samples = -x_spec
temp_data_samples.resize([SAMPLESIZE, 1])
self.data_samples = np.concatenate([self.data_samples, temp_data_samples], axis=1)
stream.stop_stream()
stream.close()
# self.fig2, (self.ax1, self.ax2, self.ax3) = plt.subplots(nrows=1, ncols=3, gridspec_kw={'width_ratios': [10, 2, 1]})
self.fig2, (self.ax1, self.ax2) = plt.subplots(nrows=1, ncols=2, gridspec_kw={'width_ratios': [10, 2]})
self.fig2.tight_layout()
self.fig2.set_facecolor((.9,.9,.9))
# #plot color
# self.ax1.axis([0,DISTANCE, (SAMPLESIZE/2),0])
# #self.ax1.set_ylabel('frequency (Hz)', fontsize=20)
# # self.ax1.set_ylabel('time (ms)', fontsize=20)
# self.ax1.yaxis.set_ticks_position('right')
# self.ax1.set_xlabel('distance (m)', fontsize=20)
# # self.ax1.set_ylabel('depth (m)', fontsize=20)
# secax = self.ax1.secondary_yaxis('left', functions=(self.freq_to_depth, self.depth_to_freq))
# secax.set_ylabel('depth (m)', fontsize=20)
# #self.ax1.yaxis.set_ticks(np.arange(self.freq_to_depth(0), self.freq_to_depth(250), 100000))
# self.ax1.set_xlim(0, self.ids.slider_distance.max)
# self.ax1.grid(False)
# clrmesh = self.ax1.pcolor(self.data_colormap, cmap='turbo', vmin=self.min_graph, vmax=self.max_graph)
# self.fig2.colorbar(clrmesh, ax=self.ax1, format='%3.2f')
#plot wiggle
# Some example data
# self.ax1.axis([0,DISTANCE, (SAMPLESIZE/2),0])
self.ax1.set_xlabel('distance (m)', fontsize=20)
self.ax1.set_ylabel('depth', fontsize=20)
offsets = np.linspace(0, self.dt_distance-1, self.dt_distance)
for offset, wiggle, sample in zip(offsets, self.data_wiggles.T, self.data_samples.T):
x = offset + wiggle
y = sample
self.ax1.plot(x, y, lw=1, linestyle='-', color='k')
self.ax1.fill_betweenx(y, offset, x, where=(x > offset), color='k')
self.ax1.set_xlim(0, self.ids.slider_distance.max)
self.ax1.set_ylim(SAMPLESIZE/TIMESAMPLE, 0)
# self.ax2.axis([self.min_graph, self.max_graph, SAMPLESIZE/TIMESAMPLE, 0])
self.ax2.set_ylim(SAMPLESIZE/TIMESAMPLE, 0)
self.ax2.set_xlabel('amplitude', fontsize=20)
# self.ax2.set_ylabel('frequency (Hz)', fontsize=20)
# self.ax2.plot(y_spec, -x_spec, lw=1,marker= 'o', linestyle='-')
# self.ax2.plot(y_wiggle, -x_spec, lw=1,marker= 'o', linestyle='-')
self.ax2.plot(y_wiggle, -x_spec, lw=1, linestyle='-', color='k')
# self.ax3.axis([0, np.max(y_gain), -SAMPLESIZE, 0])
# self.ax3.set_xlabel('gain', fontsize=20)
# self.ax3.plot(y_gain, -x_gain, lw=1, linestyle='-', color='k')
self.ids.layout_graph_signal.clear_widgets()
self.ids.layout_graph_signal.add_widget(FigureCanvasKivyAgg(self.fig2))
def freq_to_depth(self, freq):
return (freq * (MEDIUMSPEED * TAO) / (2 * BANDWITH))
def depth_to_freq(self, depth):
return (depth * (2 * BANDWITH) / (MEDIUMSPEED * TAO))
def update_enco(self, interval):
try:
data_json = json.dumps({"type":"ENCO"})
str_send = data_json + "\n"
self.device.write(str_send.encode())
print(str_send)
except:
print("error updating encoder")
self.ids.label_notif.text = "error updating encoder"
self.ids.label_notif.color = 1,0,0
def update_map(self, interval):
try:
data_json = json.dumps({"type":"GPS"})
str_send = data_json + "\n"
self.device.write(str_send.encode())
print(str_send)
except:
print("error updating map")
self.ids.label_notif.text = "error updating map"
self.ids.label_notif.color = 1,0,0
def map_mark(self):
self.lon = float(self.ids.txt_lon.text)
self.lat = float(self.ids.txt_lat.text)
self.zoom = int(self.ids.slider_zoom.value)
self.ids.map_view.lon = self.lon
self.ids.map_view.lat = self.lat
self.ids.map_view.zoom = self.zoom
self.ids.map_marker.lon = self.lon
self.ids.map_marker.lat = self.lat
print("long:" + str(self.lon) + ", lat:" + str(self.lat) + ", zoom:" + str(self.zoom))
def request_map(self):
if(self.flag_map):
Clock.unschedule(self.update_map)
self.ids.bt_screen_setting.disabled = False
self.ids.bt_screen_graph.disabled = False
self.ids.bt_update_map.text = "UPDATE MAP"
self.flag_map = False
else:
Clock.schedule_interval(self.update_map, 2)
self.ids.bt_screen_setting.disabled = True
self.ids.bt_screen_graph.disabled = True
self.ids.bt_update_map.text = "UPDATING MAP..."
self.flag_map = True
def request_graph(self):
self.update_graph()
self.dt_slider_distance += 1000
self.ids.slider_distance.value = float(self.dt_slider_distance / 1000)
self.dt_distance = int(self.dt_slider_distance / 1000)
def save_data(self):
try:
name_file_now = datetime.now().strftime("%d_%m_%Y_%H_%M_%S.sgy")
# A SEGY file has file wide headers. This can be attached to the stream
# object. If these are not set, they will be autocreated with default
# values.
data_stream.stats = AttribDict()
data_stream.stats.textual_file_header = 'Textual Header!'
data_stream.stats.binary_file_header = SEGYBinaryFileHeader()
data_stream.stats.binary_file_header.trace_sorting_code = 5
print("Stream object before writing...")
print(data_stream)
data_stream.write(name_file_now, format="SEGY", data_encoding=1,
byteorder=sys.byteorder)
print("Stream object after writing. Will have some segy attributes...")
print(data_stream)
print("Reading using obspy.io.segy...")
st1 = _read_segy(name_file_now)
print(st1)
print("Reading using obspy.core...")
st2 = read(name_file_now)
print(st2)
print("Just to show that the values are written...")
print([tr.stats.segy.trace_header.receiver_group_elevation for tr in data_stream])
print([tr.stats.segy.trace_header.receiver_group_elevation for tr in st2])
print(data_stream.stats.binary_file_header.trace_sorting_code)
print(st1.stats.binary_file_header.trace_sorting_code)
# with open(now,"wb") as f:
# np.savetxt(f, self.data_colormap, fmt="%f")
# # write_segy(f, self.data_colormap, endian=">")
print("sucessfully save data")
self.ids.label_notif.text = "sucessfully save data"
self.ids.label_notif.color = 0,0,1
except Exception as e:
print("error saving data")
print(e)
self.ids.label_notif.text = "error saving data"
self.ids.label_notif.color = 1,0,0
def save_graph(self):
try:
now = datetime.now().strftime("%d_%m_%Y_%H_%M_%S.jpg")
self.fig2.savefig(now)
print("sucessfully save graph")
self.ids.label_notif.text = "sucessfully save graph"
self.ids.label_notif.color = 0,0,1
except:
print("error saving graph")
self.ids.label_notif.text = "error saving graph"
self.ids.label_notif.color = 1,0,0
def reset_graph(self):
# self.ids.slider_min_graph.value = DEFMINGRAPH
# self.ids.slider_max_graph.value = DEFMAXGRAPH
self.min_graph = self.ids.slider_min_graph.value
self.max_graph = self.ids.slider_max_graph.value
def reset_gain(self):
self.ids.slider_top_gain.value = DEFTOPGAIN
self.ids.slider_bottom_gain.value = DEFBOTTOMGAIN
self.top_gain = self.ids.slider_top_gain.value
self.bottom_gain = self.ids.slider_bottom_gain.value
def exec_exit(self):
quit()
def exec_shutdown(self):
os.system("shutdown /s /t 1")
def checkbox_method_click(self, instance, value, meths):
if value == True:
self.checks_method.append(meths)
methods = ''
for x in self.checks_method:
methods = f'{methods} {x}'
else:
self.checks_method.remove(meths)
methods = ''
for x in self.checks_method:
methods = f'{methods} {x}'
self.dt_method = methods
if("CONTINUOUS" in self.dt_method):
self.ids.bt_update_graph.disabled = True
Clock.schedule_interval(self.update_graph_time, 2)
elif("MANUAL" in self.dt_method):
self.ids.bt_update_graph.disabled = False
Clock.unschedule(self.update_graph_time)
else:
self.ids.bt_update_graph.disabled = False
Clock.unschedule(self.update_graph_time)
class GPRApp(App):
def build(self):
self.icon = 'asset/logo.ico'
return MainWindow()
if __name__ == '__main__':
GPRApp().run()