-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_main.py
380 lines (301 loc) · 13.8 KB
/
gui_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
from Tkinter import *
from obspy.core import *
from obspy.signal.trigger import classicSTALTA, plotTrigger
from obspy.signal import seisSim, cornFreq2Paz
#from obspy.signal import seisSim, cornFreq2Paz
from tkFileDialog import *
#from obspy.gse2.paz import readPaz
#from obspy.xseed import Parser
import Pmw
from obspy.xseed import Parser
import os
class Gui:
def __init__(self, master):
stream_data= {} #dictionary with saved streams
parser_data= {} #dictionary with saved parser data for stations
self.frame = Frame(master)
# Create the Balloon.
self.balloon = Pmw.Balloon(master)
#Create the menu bar
self.makeMenuBar(master, self.balloon,stream_data,parser_data)
# Create and pack the canvas
self.scrolledCanvas = Pmw.ScrolledCanvas(master,
canvas_width = 500,
canvas_height = 215)
self.scrolledCanvas.pack()
#Create the button box
self.buttonBox = Pmw.ButtonBox(master,
labelpos = 'nw',
label_text = 'Data Processing',
frame_borderwidth = 2,
frame_relief = 'groove')
self.buttonBox.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
self.buttonBox.add('Plot Stream Data',
command = lambda: self.plot_stream_options(stream_data))
self.buttonBox.add('Filter Options',
command = lambda: self.filter_options(stream_data))
self.buttonBox.add('Convolution',
command = lambda: self.convolution_options(stream_data, parser_data))
self.frame.pack()
def makeMenuBar(self,master, balloon,stream_data,parser_data):
''' Creates the top menu bar options of the application'''
menuBar = Pmw.MenuBar(master,
hull_relief = 'raised',
hull_borderwidth = 1,balloon = self.balloon)
menuBar.pack(side="top", fill = 'x')
self.menuBar = menuBar
menuBar.addmenu('Load','Select and load seismic data')
menuBar.addmenuitem('Load','command','open a stream file',
command = lambda: self.load_stream(stream_data),
label = 'Stream File')
menuBar.addmenuitem('Load','command','open a dataless file',
command = lambda: self.load_dataless(parser_data),
label = 'Dataless File')
menuBar.addmenu('Edit', 'Data Options')
menuBar.addmenuitem('Edit','command','Delete stream data',
command = lambda: self._del_data(stream_data),
label = 'Stream clearing')
menuBar.addmenuitem('Edit','command','Delete parser data',
command = lambda: self._del_data(parser_data),
label = 'Parser clearing')
menuBar.addmenuitem('Edit','command','Save Stream Data',
command = lambda: self.save_options(stream_data),
label = 'Save Stream Data')
menuBar.addmenu('Options', 'General Options')
menuBar.addmenuitem('Options','command','Check stream stats',
command = lambda: self.display_stats_options(stream_data),
label = 'Stream Stats')
def _del_data(self,data):
'''Clears the dictionary data'''
try:
data.clear()
except:
print "Problem with the data struture"
finally:
print "The stream data was cleared. Ready for more"
def load_stream(self, stream_data):
''' User input: Locate the directory of the waveform file and reads
it using the read method from obspy.
Saves the stream into the stream_data dictionary'''
dir_stream=askopenfilenames()
for dir in dir_stream:
try:
st=read(dir)
except:
print('Problem reading the waveform file, try another one')
else:
stream_data[st[0].stats.station]=st
return stream_data
def load_dataless(self, parser_data):
''' User input: Locate and saves the directory of the dataless file into the
parser_data dictionary.
Saves the parser data using the parser method from obspy,
into parser_data dictionary'''
dir_dl=askopenfilenames()
key_name="" #key name for the dictionary data
for dir in dir_dl:
for c in dir:
if c != '.':
key_name+=c
else:
break
parser=Parser(dir)
parser_data[key_name]=parser
return parser_data
'''try:
parser=Parser(dir_dl)
except:
print('Problem parsing the dataless file, try another one')
else:
parser_data[key_name]=parser'''
def create_selection_box(self,data):
''' Creates a selection box with the stream data. Uses the global dictionary
stream_data
The check list is created using a for loop calling the object selection_box'''
try:
#Check if dictionary stream_data is empty. Raises exception if so.
if data:
pass
except:
print "You haven't loaded the data yet!"
else:
self.button_dic=data.copy()
#Set all values in the button_dic to zero
for key in self.button_dic: self.button_dic[key]=0
for key in sorted(self.button_dic):
self.button_dic[key] = IntVar()
check_button = Checkbutton(self.selection_box, text=key,
variable=self.button_dic[key])
check_button.pack(side="top")
def display_stats_options(self,stream_data):
''' Displays the seleted stream stats inside the canvas component '''
self.selection_box=Toplevel(self.frame)
self.create_selection_box(stream_data)
self.selection_box.title('Select Streams')
display_button=Button(self.selection_box, text="Display Stats",
command= lambda: self.display_stats(stream_data))
display_button.pack()
cancel_button=Button(self.selection_box, text="Cancel",
command=self.selection_box.destroy)
cancel_button.pack()
def display_stats(self,stream_data):
for key, value in self.button_dic.items():
state = value.get()
if state:
st=stream_data[key]
for tr in st:
print tr.stats
self.button_dic[key].set(0)
def convolution_automatic(self,stream_data,parser_data):
#st=stream_data.values().copy()
#for tr in st and key in parser_data:
inst2hz = cornFreq2Paz(float(self.inst2hz.getvalue())) #what the hell is this?
waterLevel=float(self.water_level.getvalue())
parser_keys=parser_data.keys()
for key, st in stream_data.items():
for pr_key in parser_data.keys():
if pr_key.find(key[0:2]):
pr=parser_data.get(pr_key)
for tr in st:
paz=pr.getPAZ(tr.stats)
df = tr.stats.sampling_rate
tr.data = seisSim(tr.data, df, paz_remove=paz, paz_simulate=inst2hz,
water_level=waterLevel)
stream_data[key+'_converted']=tr.copy()
print "Try the manual option"
return stream_data
#save data into a user seleted dirotory
def convolution_options(self,stream_data,parser_data):
self.selection_box=Toplevel(self.frame)
self.create_selection_box(stream_data)
self.selection_box.title('Convolution options')
self.inst2hz=Pmw.EntryField(self.selection_box,
labelpos = 'w',
label_text = 'Inst to Hz:',
value = 2.0,
validate = {'validator' : 'real'})
self.inst2hz.pack()
self.water_level=Pmw.EntryField(self.selection_box,
labelpos = 'w',
label_text = 'Inst to Hz:',
value = 60,
validate = {'validator' : 'real'})
self.water_level.pack()
auto_button=Button(self.selection_box, text="Automatic",
command= lambda: self.convolution_automatic(stream_data,parser_data))
auto_button.pack()
manual_button=Button(self.selection_box, text="Manual",
command=lambda: self.convolution_manual(stream_data,parser_data))
manual_button.pack()
cancel_button=Button(self.selection_box, text="Cancel",
command=self.selection_box.destroy)
cancel_button.pack()
def convolution_manual(self,stream_data):
''' create a cross list type widget to associate the parser file with the
wafeform data'''
pass
def save_options(self,stream_data):
self.selection_box=Toplevel(self.frame)
self.create_selection_box(stream_data)
self.selection_box.title('Select the streams you wish to save')
save_button=Button(self.selection_box, text="Save",
command=lambda: self.save_stream(stream_data))
save_button.pack(side=BOTTOM)
cancel_button=Button(self.selection_box, text="Cancel",
command=self.selection_box.destroy)
cancel_button.pack(side=BOTTOM)
def save_stream(self,stream_data):
''' '''
save_dir=askdirectory()
for key, value in self.button_dic.items():
state = value.get()
if state:
file_name=open(os.path.join(save_dir,key+'.mseed'),"w")
stream_data[key].write(file_name,format='MSEED')
self.button_dic[key].set(0)
def filter_options(self,stream_data):
self.selection_box=Toplevel(self.frame)
self.create_selection_box(stream_data)
self.selection_box.title('Butterworth Filter options')
self.freq_min=Pmw.EntryField(self.selection_box,
labelpos = 'w',
label_text = 'Freq Min (Hz):',
value = 1,
validate = {'validator' : 'numeric'})
self.freq_min.pack()
self.freq_max=Pmw.EntryField(self.selection_box,
labelpos = 'w',
label_text = 'Freq Max (Hz):',
value = 20,
validate = {'validator' : 'numeric'})
self.freq_max.pack()
self.freq=Pmw.EntryField(self.selection_box,
labelpos = 'w',
label_text = 'Corner Freq (Hz):',
value = 1,
validate = {'validator' : 'numeric'})
self.freq.pack()
b_pass='bandpass'; b_stop='bandstop'; h_pass='highpass'; l_pass='lowpass';
bandpass_button=Button(self.selection_box, text="Bandpass",
command=lambda: self.apply_filter(b_pass, stream_data))
bandpass_button.pack()
bandstop_button=Button(self.selection_box, text="Bandstop",
command=lambda: self.apply_filter(b_stop, stream_data))
bandstop_button.pack()
highpass_button=Button(self.selection_box, text="Highpass",
command=lambda: self.apply_filter(h_pass, stream_data))
highpass_button.pack()
lowpass_button=Button(self.selection_box, text="Lowpass",
command=lambda: self.apply_filter(l_pass, stream_data))
lowpass_button.pack()
cancel_button=Button(self.selection_box, text="Cancel",
command=self.selection_box.destroy)
cancel_button.pack(side=BOTTOM)
def apply_filter(self, filter_key, stream_data):
''' Apply's the given filtered by keyword filter_key.
Saves the filtered stream into stream_data with the
keyword: [station name]_filtered'''
for key, value in self.button_dic.items():
state = value.get()
if state:
temp=stream_data[key].copy()
#Detrend the stream object
temp.detrend()
#Apply the bandpass or stop filter
if filter_key.startswith('band'):
temp.filter(filter_key,freqmin=int(self.freq_min.getvalue()),
freqmax=int(self.freq_max.getvalue()))
stream_data[key+'_filtered']=temp.copy()
stream_data[key+'_filtered'].plot()
else:
#Apply the high or low pass filter
temp.filter(filter_key,freq=int(self.freq.getvalue()))
stream_data[key+'_filtered']=temp.copy()
stream_data[key+'_filtered'].plot()
self.button_dic[key].set(0)
return stream_data
def plot_stream_options(self, stream_data):
'''Plots the stream file that was previsouly loaded by the user'''
self.selection_box=Toplevel(self.frame)
self.create_selection_box(stream_data)
self.selection_box.title('Selecting Stations')
plot_button = Button(self.selection_box, text="Plot",
command=lambda: self.plot_stream(stream_data))
plot_button.pack()
cancel_button=Button(self.selection_box, text="Cancel",
command=self.selection_box.destroy)
cancel_button.pack()
def plot_stream(self, stream_data):
for key, value in self.button_dic.items():
state = value.get()
if state:
stream_data[key].plot()
self.button_dic[key].set(0)
#############################################
root = Tk()
root.geometry("600x400")
root.title('Gorbatikov Method')
exitButton = Button(root, text = 'Exit', command =root.destroy)
exitButton.pack(side=BOTTOM)
gui = Gui(root)
root.mainloop()