Bugfixxes, spectogram tab wip

This commit is contained in:
Jeldrik Gaal 2023-06-14 13:11:54 +02:00
parent e4e7afa996
commit a1f6c5ffca
5 changed files with 155 additions and 104 deletions

View File

@ -84,7 +84,7 @@ from pylot.core.util.widgets import FilterOptionsDialog, NewEventDlg, \
PylotCanvas, WaveformWidgetPG, PropertiesDlg, HelpForm, createAction, PickDlg, \
ComparisonWidget, TuneAutopicker, PylotParaBox, AutoPickDlg, CanvasWidget, AutoPickWidget, \
CompareEventsWidget, ProgressBarWidget, AddMetadataWidget, SingleTextLineDialog, LogWidget, PickQualitiesFromXml, \
SourceSpecWindow, ChooseWaveFormWindow
SourceSpecWindow, ChooseWaveFormWindow, SpectrogramTab
from pylot.core.util.array_map import Array_map
from pylot.core.util.structure import DATASTRUCTURE
from pylot.core.util.thread import Thread, Worker
@ -496,7 +496,6 @@ class MainWindow(QMainWindow):
icon=eventlist_xml_icon,
tip='Create an Eventlist from a XML File')
self.eventlist_xml_action.setEnabled(False)
printAction = self.createAction(self, "&Print event ...",
self.show_event_information, QKeySequence.Print,
print_icon,
@ -698,14 +697,17 @@ class MainWindow(QMainWindow):
wf_tab = QtWidgets.QWidget(self)
array_tab = QtWidgets.QWidget(self)
events_tab = QtWidgets.QWidget(self)
spectro_tab = QtWidgets.QWidget(self)
# init main widgets layouts
self.wf_layout = QtWidgets.QVBoxLayout()
self.array_layout = QtWidgets.QVBoxLayout()
self.events_layout = QtWidgets.QVBoxLayout()
self.spectro_layout = QtWidgets.QVBoxLayout()
wf_tab.setLayout(self.wf_layout)
array_tab.setLayout(self.array_layout)
events_tab.setLayout(self.events_layout)
spectro_tab.setLayout(self.spectro_layout)
# tighten up layouts inside tabs
for layout in [self.wf_layout, self.array_layout, self.events_layout]:
@ -716,12 +718,14 @@ class MainWindow(QMainWindow):
self.tabs.addTab(wf_tab, 'Waveform Plot')
self.tabs.addTab(array_tab, 'Array Map')
self.tabs.addTab(events_tab, 'Eventlist')
self.tabs.addTab(spectro_tab, 'Spectro')
self.wf_layout.addWidget(self.no_data_label)
self.wf_layout.addWidget(self.wf_scroll_area)
self.wf_scroll_area.setWidgetResizable(True)
self.init_array_tab()
self.init_event_table()
self.init_spectro_tab()
self.tabs.setCurrentIndex(0)
self.eventLabel = QLabel()
@ -734,16 +738,13 @@ class MainWindow(QMainWindow):
_widget.setLayout(self._main_layout)
_widget.showFullScreen()
self.logwidget = LogWidget(parent=None)
if use_logwidget:
self.logwidget = LogWidget(parent=None)
self.logwidget.show()
self.stdout = self.logwidget.stdout
self.stderr = self.logwidget.stderr
# Not sure why but the lines above kept messing with the Ouput even with use_logwidget disabled
sys.stdout = self.stdout
sys.stderr = self.stderr
self.setCentralWidget(_widget)
# Need to store PickQualities Window somewhere so it doesnt disappear
@ -1678,20 +1679,20 @@ class MainWindow(QMainWindow):
def pickQualities(self):
path = self.get_current_event_path()
(_, _, plot) = getQualitiesfromxml(path, self._inputs.get('timeerrorsP'), self._inputs.get('timeerrorsS'),plotflag=1)
(_, plot) = getQualitiesfromxml(path, self._inputs.get('timeerrorsP'), self._inputs.get('timeerrorsS'),plotflag=1)
self.pickQualitiesWindow = PickQualitiesFromXml(figure=plot, path=self.get_current_event_path(),inputVar=self._inputs)
self.pickQualitiesWindow.showUI()
return
# WIP JG
def eventlistXml2(self):
def eventlistXml(self):
path = self._inputs['rootpath'] + '/' + self._inputs['datapath'] + '/' + self._inputs['database']
outpath = self.project.location[:self.project.location.rfind('/')]
geteventlistfromxml(path, outpath)
return
# WIP JG
def eventlistXml(self):
# WIP JG
def spectogramView(self):
global test
stations = []
names = []
@ -1706,12 +1707,25 @@ class MainWindow(QMainWindow):
for tr in self.get_data().wfdata.select(component=ch).traces:
traces[tr.stats.station][ch] = tr
names.sort()
a = self.get_current_event()
test = ChooseWaveFormWindow(WaveForms=names, traces=traces, stream=self.get_data())
#self.get_data().wfdata.spectrogram()
test.show()
print (self.get_data().wfdata.traces[0])
test = SpectrogramTab(traces, self.get_data().wfdata)
height = self.tabs.widget(0).height()
width = self.tabs.widget(0).width()
self.tabs.setCurrentIndex(3)
figCanvas = test.makeSpecFig(direction=self.dispComponent, height = height, width = width, parent = self.tabs.widget)
return figCanvas
#self.spectro_layout.addWidget()
# self.get_data().wfdata.spectrogram()
# self.tabs.addTab(figCanvas, 'Spectrogram')
# self.tabs[3] = figCanvas
# self.refreshTabs()
# test.show()
def compareMulti(self):
if not self.compareoptions:
@ -1898,6 +1912,13 @@ class MainWindow(QMainWindow):
self.newWF(plot=False)
self.update_obspy_dmt()
self.refresh_array_map()
if self.tabs.currentIndex() == 3:
if self.spectroWidget != None:
self.spectro_layout.removeWidget(self.spectroWidget)
newSpectroWidget = self.spectogramView()
self.spectro_layout.addWidget(newSpectroWidget)
self.spectroWidget = newSpectroWidget
def newWF(self, event=None, plot=True):
'''
@ -2155,6 +2176,7 @@ class MainWindow(QMainWindow):
self.locateEventAction.setEnabled(True)
self.qualities_action.setEnabled(True)
self.eventlist_xml_action.setEnabled(True)
if True in self.comparable.values():
self.compare_action.setEnabled(True)
self.draw()
@ -3128,7 +3150,7 @@ class MainWindow(QMainWindow):
phasefile = os.path.join(obsdir, filename + '.obs')
lt.modify_inputs(ctrfile, locroot, filename, phasefile, ttt)
try:
lt.locate(ctrfile, self.obspy_dmt)
lt.locate(ctrfile, self._inputs)
except RuntimeError as e:
print(e.message)
# finally:
@ -3182,7 +3204,7 @@ class MainWindow(QMainWindow):
'''
if checked: pass # dummy argument for QAction trigger signal
self.tabs.setCurrentIndex(1)
# if there is no metadata (invetories is an empty list), just initialize the default empty tab
# if there is no metadata (inventories is an empty list), just initialize the default empty tab
if not self.metadata.inventories:
self.init_array_tab()
return
@ -3204,6 +3226,15 @@ class MainWindow(QMainWindow):
self.tabs.setCurrentIndex(index)
self.refresh_array_map()
def init_spectro_tab(self):
'''
Init spectrogram tab with currently selected event.
'''
self.spectroWidget = None
#self.spectro_layout.addWidget( self.spectogramView() )
pass
def array_map_thread(self):
'''
Start modal thread to init the array_map object.

View File

@ -260,11 +260,6 @@ class Data(object):
can be a str or a list of strings of ['manual', 'auto', 'origin', 'magnitude']
"""
from pylot.core.util.defaults import OUTPUTFORMATS
import sys
sys.stdout = sys.__stdout__
print ( fnext )
if not type(fcheck) == list:
fcheck = [fcheck]
@ -325,18 +320,25 @@ class Data(object):
if lendiff != 0:
print("Manual as well as automatic picks available. Prefered the {} manual ones!".format(lendiff))
no_uncertainties_p = []
no_uncertainties_s = []
if upperErrors:
# check for pick uncertainties exceeding adjusted upper errors
# Picks with larger uncertainties will not be saved in output file!
for j in range(len(picks)):
for i in range(len(picks_copy)):
if picks_copy[i].phase_hint[0] == 'P':
print(picks_copy[i].time_errors)
# Testing if exporting works without upper_uncertatinty
# Skipping pick if no upper_uncertainty is found and warning user
if picks_copy[i].time_errors['upper_uncertainty'] is None:
break
#print("{1} P-Pick of station {0} does not have upper_uncertainty and cant be checked".format(
# picks_copy[i].waveform_id.station_code,
# picks_copy[i].method_id))
if not picks_copy[i].waveform_id.station_code in no_uncertainties_p:
no_uncertainties_p.append(picks_copy[i].waveform_id.station_code)
continue
#print ("checking for upper_uncertainty")
if (picks_copy[i].time_errors['uncertainty'] is None) or \
(picks_copy[i].time_errors['upper_uncertainty'] >= upperErrors[0]):
print("Uncertainty exceeds or equal adjusted upper time error!")
@ -345,14 +347,19 @@ class Data(object):
print("{1} P-Pick of station {0} will not be saved in outputfile".format(
picks_copy[i].waveform_id.station_code,
picks_copy[i].method_id))
print("#######")
del picks_copy[i]
break
if picks_copy[i].phase_hint[0] == 'S':
# Testing if exporting works without upper_uncertatinty
# Skipping pick if no upper_uncertainty is found and warning user
if picks_copy[i].time_errors['upper_uncertainty'] is None:
break
#print("{1} S-Pick of station {0} does not have upper_uncertainty and cant be checked".format(
#picks_copy[i].waveform_id.station_code,
#picks_copy[i].method_id))
if not picks_copy[i].waveform_id.station_code in no_uncertainties_s:
no_uncertainties_s.append(picks_copy[i].waveform_id.station_code)
continue
if (picks_copy[i].time_errors['uncertainty'] is None) or \
(picks_copy[i].time_errors['upper_uncertainty'] >= upperErrors[1]):
@ -362,9 +369,12 @@ class Data(object):
print("{1} S-Pick of station {0} will not be saved in outputfile".format(
picks_copy[i].waveform_id.station_code,
picks_copy[i].method_id))
print("#")
del picks_copy[i]
break
for s in no_uncertainties_p:
print("P-Pick of station {0} does not have upper_uncertainty and cant be checked".format(s))
for s in no_uncertainties_s:
print("S-Pick of station {0} does not have upper_uncertainty and cant be checked".format(s))
if fnext == '.obs':
try:
@ -788,12 +798,9 @@ class GenericDataStructure(object):
"""
expandList = []
for item in self.getExpandFields():
print ( item )
expandList.append(self.getFieldValue(item))
if self.hasSuffix():
expandList.append('*%s' % self.getFieldValue('suffix'))
print ( expandList )
print ( self.getFields() )
return os.path.join(*expandList)
def getCatalogName(self):

View File

@ -1014,7 +1014,6 @@ def chooseArrivals(arrivals):
:return: arrivals but with the manual picks prefered if possible
"""
# If len of arrivals is greater than 2 it comes from autopicking so only autopicks are available
print("=== CHOOSE ===")
if len(arrivals) > 2:
return arrivals
if arrivals['auto'] and arrivals['manual']:

View File

@ -41,7 +41,8 @@ class Thread(QThread):
exctype, value = sys.exc_info()[:2]
self._executedErrorInfo = '{} {} {}'. \
format(exctype, value, traceback.format_exc())
sys.stdout = self.old_stdout
if self.redirect_stdout:
sys.stdout = self.old_stdout
def showProgressbar(self):
if self.progressText:

View File

@ -5667,9 +5667,8 @@ class ChooseWaveFormWindow(QWidget):
# self.chooseBoxTraces.currentText()[3:]][self.chooseBoxComponent.currentText()].spectrogram(show=False, title=t)
#self.currentSpectro.show()
applyFFT()
def applyFFT(self, trace):
sys.stdout = sys.__stdout__
tra = self.traces[self.chooseBoxTraces.currentText()[3:]]['Z']
transformed = abs(np.fft.rfft(tra.data))
print ( transformed )
@ -5677,36 +5676,11 @@ class ChooseWaveFormWindow(QWidget):
matplotlib.pyplot.show()
def applyFFTs(self, tra):
sys.stdout = sys.__stdout__
transformed = abs(np.fft.rfft(tra.data))
print ( transformed )
matplotlib.pyplot.plot ( transformed )
matplotlib.pyplot.show()
def applyFFT2(self, trace):
sys.stdout = sys.__stdout__
tra = self.traces[self.chooseBoxTraces.currentText()[3:]]['Z']
from pylot.core.pick.utils import select_for_phase
zdat = select_for_phase(tra, "P")
freq = zdat[0].stats.sampling_rate
# fft
fny = freq / 2
# l = len(xdat) / freq
# number of fft bins after Bath
# n = freq * l
# find next power of 2 of data length
m = pow(2, np.ceil(np.log(len(zdat)) / np.log(2)))
N = min(int(np.power(m, 2)), 16384)
# N = int(np.power(m, 2))
y = dt * np.fft.fft(zdat, N)
Y = abs(y[: int(N / 2)])
#L = (N - 1) / freq
#f = np.arange(0, fny, 1 / L)
matplotlib.pyplot.plot (Y)
matplotlib.pyplot.show()
def submitN(self):
matplotlib.pyplot.close(self.currentSpectro)
t = self.chooseBoxTraces.currentText() + " " + self.chooseBoxComponent.currentText()
@ -5714,54 +5688,13 @@ class ChooseWaveFormWindow(QWidget):
self.chooseBoxTraces.currentText()[3:]]['N'].spectrogram(show=False, title=t)
self.currentSpectro.show()
def submitE2(self):
def submitE(self):
matplotlib.pyplot.close(self.currentSpectro)
t = self.chooseBoxTraces.currentText() + " " + self.chooseBoxComponent.currentText()
self.currentSpectro = self.traces[
self.chooseBoxTraces.currentText()[3:]]['E'].spectrogram(show=False, title=t)
self.currentSpectro.show()
def submitE(self):
sys.stdout = sys.__stdout__
matplotlib.pyplot.close(self.currentSpectro)
#t = self.chooseBoxTraces.currentText() + " " + self.chooseBoxComponent.currentText()
#self.currentSpectro = self.traces[
# self.chooseBoxTraces.currentText()[3:]]['Z'].spectrogram(show=False, title=t)
#self.currentSpectro.show()
#self.applyFFT('s')
i = 0
figure, axis = matplotlib.pyplot.subplots(len(self.traces), sharex=True)
for t in self.traces:
tra = self.traces[t]['Z']
transformed = abs(np.fft.rfft(tra.data))
axis[i].plot(transformed, label = t)
#axis[i].tick_params(labelbottom=False)
axis[i].spines['top'].set_visible(False)
axis[i].spines['right'].set_visible(False)
axis[i].spines['left'].set_visible(False)
if not (len(self.traces) == i - 1) :
axis[i].spines['bottom'].set_visible(False)
axis[i].set_yticks([])
axis[i].set_ylabel(t,loc='center', rotation='horizontal')
#axis[i].axis('off')
i += 1
#self.applyFFTs(t)
matplotlib.pyplot.margins(0)
return FigureCanvas(figure)
#return figure, axis
matplotlib.pyplot.show()
def submitZ(self):
matplotlib.pyplot.close(self.currentSpectro)
#t = self.chooseBoxTraces.currentText() + " " + self.chooseBoxComponent.currentText()
#self.currentSpectro = self.traces[
# self.chooseBoxTraces.currentText()[3:]]['Z'].spectrogram(show=False, title=t)
#self.currentSpectro.show()
self.applyFFT2('s')
# Creates a QComboBox and adds all traces provided
def createComboBoxTraces(self):
if len(self.wFs) <= 0:
@ -5788,6 +5721,86 @@ class ChooseWaveFormWindow(QWidget):
pass
class SpectrogramTab(QWidget):
def __init__(self, traces, wfdata, parent=None):
super(SpectrogramTab, self).__init__(parent)
self.setupUi()
self.traces = traces
self.wfdata = wfdata
def setupUi(self):
pass
def makeSpecFig(self, direction = 'Z', height = 0, width = 0, parent = None):
i = 0
grams = []
figure, axis = matplotlib.pyplot.subplots(len(self.traces), sharex=True)
start, end = full_range(self.wfdata)
if height != 0 and width != 0:
figure.figsize = (width, height)
figure.set_figwidth = width
figure.set_figheight = height
#figure.tight_layout()
for t in self.traces:
tra = self.traces[t][direction]
#print(start, end)
# Set Title
if i == 0:
if direction == 'Z':
figure.suptitle("section: vertical components")
elif direction == 'E':
figure.suptitle("section: east-west components")
elif direction == 'N':
figure.suptitle("section: north-south components")
axis[i].vlines(0, axis[i].get_ylim()[0], axis[i].get_ylim()[1],
colors='m', linestyles='dashed',
linewidth=2)
# Different axis settings for visual improvements
# axis[i].set_xlim(left=0, right=end - start)
# axis[i].spines['top'].set_visible(False)
# axis[i].spines['right'].set_visible(False)
# # axis[i].spines['left'].set_visible(False)
# axis[i].tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)
# if not (len(self.traces) == i - 1):
# axis[i].spines['bottom'].set_visible(False)
# axis[i].set_yticks([])
# axis[i].set_ylabel(t, loc='center', rotation='horizontal')
#ax.axhline(n, color="0.5", lw=0.5)
grams.append(tra.spectrogram(show=False, axes=axis[i]))
i+=1
#figure.setXLims([0, end - start])
figure.set_tight_layout(True)
fC = FigureCanvas(figure)
return fC
#for t in self.traces:
# tra = self.traces[t]['Z']
# transformed = abs(np.fft.rfft(tra.data))
# axis[i].plot(transformed, label=t)
# # axis[i].tick_params(labelbottom=False)
# axis[i].spines['top'].set_visible(False)
# axis[i].spines['right'].set_visible(False)
# axis[i].spines['left'].set_visible(False)
# if not (len(self.traces) == i - 1):
# axis[i].spines['bottom'].set_visible(False)
# axis[i].set_yticks([])
# axis[i].set_ylabel(t, loc='center', rotation='horizontal')
# # axis[i].axis('off')
# i += 1
# # self.applyFFTs(t)
if __name__ == '__main__':
import doctest