[bugfix] origins object will be created independent to notes.txt,

[update] pycharm code restructuring (accidently)
This commit is contained in:
Marcel Paffrath 2017-07-14 16:25:59 +02:00
parent a017c4deb4
commit 897b915eff

View File

@ -25,6 +25,7 @@ https://www.iconfinder.com/iconsets/flavour
import os
import sys
import platform
import argparse
import matplotlib
@ -41,7 +42,7 @@ from PySide.QtGui import QMainWindow, QInputDialog, QIcon, QFileDialog, \
QTreeView, QComboBox, QTabWidget, QPushButton, QGridLayout
import numpy as np
from obspy import UTCDateTime
from obspy.core.event import Magnitude
from obspy.core.event import Magnitude, Origin
from obspy.core.util import AttribDict
try:
@ -82,6 +83,7 @@ from pylot.core.util.map_projection import map_projection
from pylot.core.util.structure import DATASTRUCTURE
from pylot.core.util.thread import AutoPickThread, Thread
from pylot.core.util.version import get_git_version as _getVersionString
if sys.version_info.major == 3:
import icons_rc_3 as icons_rc
elif sys.version_info.major == 2:
@ -103,7 +105,7 @@ class MainWindow(QMainWindow):
if not infile:
infile = os.path.join(os.path.expanduser('~'), '.pylot', 'pylot.in')
print('Using default input file {}'.format(infile))
if os.path.isfile(infile)== False:
if os.path.isfile(infile) == False:
infile = QFileDialog().getOpenFileName(caption='Choose PyLoT-input file')
if not os.path.exists(infile[0]):
QMessageBox.warning(self, "PyLoT Warning",
@ -225,7 +227,7 @@ class MainWindow(QMainWindow):
self._event_layout.addWidget(self.eventBox)
self._event_layout.addWidget(self.ref_event_button)
self._event_layout.addWidget(self.test_event_button)
self._event_layout.setStretch(1,1) #set stretch of item 1 to 1
self._event_layout.setStretch(1, 1) # set stretch of item 1 to 1
self._main_layout.addLayout(self._event_layout)
self.eventBox.activated.connect(self.refreshEvents)
@ -254,7 +256,7 @@ class MainWindow(QMainWindow):
array_tab.setLayout(self.array_layout)
events_tab.setLayout(self.events_layout)
#add tabs to main tab widget
# add tabs to main tab widget
self.tabs.addTab(wf_tab, 'Waveform Plot')
self.tabs.addTab(array_tab, 'Array Map')
self.tabs.addTab(events_tab, 'Eventlist')
@ -557,7 +559,7 @@ class MainWindow(QMainWindow):
def init_wfWidget(self):
settings = QSettings()
xlab = self.startTime.strftime('seconds since %Y/%m/%d %H:%M:%S (%Z)')
plottitle = None#"Overview: {0} components ".format(self.getComponent())
plottitle = None # "Overview: {0} components ".format(self.getComponent())
self.disconnectWFplotEvents()
if str(settings.value('pyqtgraphic')) == 'false' or not pg:
self.pg = False
@ -578,9 +580,9 @@ class MainWindow(QMainWindow):
'''
self.ref_event_button = QtGui.QPushButton('Ref')
self.test_event_button = QtGui.QPushButton('Test')
self.ref_event_button.setToolTip('Set manual picks of current '+
self.ref_event_button.setToolTip('Set manual picks of current ' +
'event as reference picks for autopicker tuning.')
self.test_event_button.setToolTip('Set manual picks of current '+
self.test_event_button.setToolTip('Set manual picks of current ' +
'event as test picks for autopicker testing.')
self.ref_event_button.setCheckable(True)
self.test_event_button.setCheckable(True)
@ -607,12 +609,10 @@ class MainWindow(QMainWindow):
def metadata(self):
return self._metadata
@metadata.setter
def metadata(self, value):
self._metadata = value
def updateFileMenu(self):
self.fileMenu.clear()
@ -684,16 +684,16 @@ class MainWindow(QMainWindow):
def load_multiple_data(self, type='manual'):
if not self.okToContinue():
return
refresh=False
refresh = False
events = self.project.eventlist
fext = '.xml'
for event in events:
path = event.path
eventname = path.split('/')[-1]
filename = os.path.join(path, 'PyLoT_'+eventname+fext)
filename = os.path.join(path, 'PyLoT_' + eventname + fext)
if os.path.isfile(filename):
self.load_data(filename, draw=False, event=event, overwrite=True)
refresh=True
refresh = True
if not refresh:
return
if self.get_current_event().pylot_picks:
@ -711,7 +711,7 @@ class MainWindow(QMainWindow):
if not fname:
return
self.set_fname(fname, type)
#data = dict(auto=self.autodata, manual=self.data)
# data = dict(auto=self.autodata, manual=self.data)
if not event:
event = self.get_current_event()
data = Data(self, event)
@ -745,7 +745,7 @@ class MainWindow(QMainWindow):
def add_recentfile(self, event):
self.recentfiles.insert(0, event)
def set_button_color(self, button, color = None):
def set_button_color(self, button, color=None):
'''
Set background color of a button.
button: type = QtGui.QAbstractButton
@ -859,13 +859,23 @@ class MainWindow(QMainWindow):
print('No project found.')
return
#get path from first event in list and split them
# get path from first event in list and split them
path = eventlist[0]
try:
system_name = platform.system()
if system_name in ["Linux", "Darwin"]:
dirs = {
'database': path.split('/')[-2],
'datapath': path.split('/')[-3],
'rootpath': '/'+os.path.join(*path.split('/')[:-3])
'rootpath': '/' + os.path.join(*path.split('/')[:-3])
}
elif system_name == "Windows":
rootpath = path.split('/')[:-3]
rootpath[0] += '/'
dirs = {
'database': path.split('/')[-2],
'datapath': path.split('/')[-3],
'rootpath': os.path.join(*rootpath)
}
except Exception as e:
dirs = {
@ -880,15 +890,15 @@ class MainWindow(QMainWindow):
settings.sync()
if not self.project.eventlist:
#init parameter object
# init parameter object
self.setParameter(show=False)
#hide all parameter (show all needed parameter later)
# hide all parameter (show all needed parameter later)
self.paraBox.hide_parameter()
for directory in dirs.keys():
#set parameter
# set parameter
box = self.paraBox.boxes[directory]
self.paraBox.setValue(box, dirs[directory])
#show needed parameter in box
# show needed parameter in box
self.paraBox.show_parameter(directory)
dirs_box = self.paraBox.get_groupbox_dialog('Directories')
if not dirs_box.exec_():
@ -948,8 +958,8 @@ class MainWindow(QMainWindow):
'''
if not eventBox:
eventBox = self.eventBox
index=eventBox.currentIndex()
tv=QtGui.QTableView()
index = eventBox.currentIndex()
tv = QtGui.QTableView()
header = tv.horizontalHeader()
header.setResizeMode(QtGui.QHeaderView.ResizeToContents)
header.setStretchLastSection(True)
@ -960,12 +970,12 @@ class MainWindow(QMainWindow):
eventBox.setView(tv)
eventBox.clear()
model = eventBox.model()
plmax=0
#set maximum length of path string
plmax = 0
# set maximum length of path string
for event in self.project.eventlist:
pl = len(event.path)
if pl > plmax:
plmax=pl
plmax = pl
for id, event in enumerate(self.project.eventlist):
event_path = event.path
@ -989,8 +999,8 @@ class MainWindow(QMainWindow):
item_nmp.setIcon(self.manupicksicon_small)
item_nap = QtGui.QStandardItem(str(event_nautopicks))
item_nap.setIcon(self.autopicksicon_small)
item_ref = QtGui.QStandardItem()#str(event_ref))
item_test = QtGui.QStandardItem()#str(event_test))
item_ref = QtGui.QStandardItem() # str(event_ref))
item_test = QtGui.QStandardItem() # str(event_test))
if event_ref:
item_ref.setBackground(self._colors['ref'])
if event_test:
@ -1020,8 +1030,8 @@ class MainWindow(QMainWindow):
'{} unequal {}.'
.format(event.path, self.eventBox.itemText(id)))
raise ValueError(message)
#not working with obspy events
#eventBox.setItemData(id, event)
# not working with obspy events
# eventBox.setItemData(id, event)
eventBox.setCurrentIndex(index)
self.refreshRefTestButtons()
@ -1061,20 +1071,20 @@ class MainWindow(QMainWindow):
else:
dlgflag = 0
eventname = self.get_current_event_name()
filename = 'PyLoT_'+eventname
filename = 'PyLoT_' + eventname
outpath = os.path.join(directory, filename)
title = 'Save pick data ...'
#if not outformat:
# if not outformat:
# outformat = settings.value('output/Format')
# outformat = outformat[0:4]
#else:
# else:
# selected_filter = "NonLinLoc observation file (*.obs)"
# fname = outpath
#if outformat == '.obs':
# if outformat == '.obs':
# file_filter = "NonLinLoc observation file (*.obs)"
#elif outformat == '.cnv':
# elif outformat == '.cnv':
# file_filter = "VELEST observation file format (*.cnv)"
#elif outformat == '.xml':
# elif outformat == '.xml':
file_filter = "(*.xml *.obs *.cnv)"
if dlgflag == 1:
@ -1083,10 +1093,10 @@ class MainWindow(QMainWindow):
outpath,
file_filter)
#fbasename, exform = os.path.splitext(fname)
# fbasename, exform = os.path.splitext(fname)
fbasename = fname
exform = ['.obs', '.xml', '.cnv']
#if not exform and selected_filter or not exform in OUTPUTFORMATS:
# if not exform and selected_filter or not exform in OUTPUTFORMATS:
# exform = selected_filter.split('*')[1][:-1]
# return fname, exform
return fbasename, exform
@ -1099,7 +1109,7 @@ class MainWindow(QMainWindow):
uppererrorS = self._inputs['timeerrorsS']
try:
self.get_data().applyEVTData(self.get_current_event(), typ='event')#getPicks())
self.get_data().applyEVTData(self.get_current_event(), typ='event') # getPicks())
except OverwriteError:
# msgBox = QMessageBox()
# msgBox.setText("Picks have been modified!")
@ -1138,10 +1148,10 @@ class MainWindow(QMainWindow):
# return False
# export to given path
#self.get_data().exportEvent(fbasename, exform, upperErrors=[uppererrorP[3], uppererrorS[3]])
# self.get_data().exportEvent(fbasename, exform, upperErrors=[uppererrorP[3], uppererrorS[3]])
self.get_data().exportEvent(fbasename, exform[0], upperErrors=[uppererrorP[3], uppererrorS[3]])
self.get_data().exportEvent(fbasename, exform[1], upperErrors=[uppererrorP[3], uppererrorS[3]])
#self.get_data().exportEvent(fbasename, exform[2], upperErrors=[uppererrorP[3], uppererrorS[3]])
# self.get_data().exportEvent(fbasename, exform[2], upperErrors=[uppererrorP[3], uppererrorS[3]])
# all files save (ui clean)
self.update_status('Picks saved as %s, %s, and %s' % (fbasename + exform[0], fbasename + exform[1],
fbasename + exform[2]))
@ -1222,7 +1232,8 @@ class MainWindow(QMainWindow):
def okToContinue(self):
if self.dirty:
qmb = QMessageBox(self, icon=QMessageBox.Question, text='Do you wish to save changes in the current project?')
qmb = QMessageBox(self, icon=QMessageBox.Question,
text='Do you wish to save changes in the current project?')
qmb.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
qmb.setDefaultButton(QMessageBox.Save)
ret = qmb.exec_()
@ -1299,7 +1310,7 @@ class MainWindow(QMainWindow):
# Loading an existing project from array_tab leads to two calls of newWF
# which will read in data input twice. Therefore current tab is changed to 0
# in loadProject before calling this function.
plotted=False
plotted = False
# only refresh first/second tab when an event was changed.
if self._eventChanged[0] or self._eventChanged[1]:
event = self.get_current_event()
@ -1321,7 +1332,7 @@ class MainWindow(QMainWindow):
if len(self.project.eventlist) > 0:
self.newWF()
# keep track whether event was already plotted
plotted=True
plotted = True
# if current tab is array_map-tab and the data in this tab was not yet refreshed
if self.tabs.currentIndex() == 1:
if self._eventChanged[1]:
@ -1442,7 +1453,7 @@ class MainWindow(QMainWindow):
plotWidget = self.getPlotWidget()
plotDict = plotWidget.getPlotDict()
pos = plotDict.keys()
labels = [plotDict[n][2]+'.'+plotDict[n][0] for n in pos]
labels = [plotDict[n][2] + '.' + plotDict[n][0] for n in pos]
plotWidget.setYTickLabels(pos, labels)
try:
plotWidget.figure.tight_layout()
@ -1516,13 +1527,14 @@ class MainWindow(QMainWindow):
# wfst += self.get_data().getWFData().select(component=alter_comp)
plotWidget = self.getPlotWidget()
self.adjustPlotHeight()
plots = plotWidget.plotWFData(wfdata=wfst, title=title, mapping=False, component=comp, nth_sample=int(nth_sample))
plots = plotWidget.plotWFData(wfdata=wfst, title=title, mapping=False, component=comp,
nth_sample=int(nth_sample))
return plots
def adjustPlotHeight(self):
if self.pg:
return
height_need = len(self.data.getWFData())*self.height_factor
height_need = len(self.data.getWFData()) * self.height_factor
plotWidget = self.getPlotWidget()
if self.tabs.widget(0).frameSize().height() < height_need:
plotWidget.setMinimumHeight(height_need)
@ -1598,7 +1610,7 @@ class MainWindow(QMainWindow):
def getFilters(self):
return self.filteroptions
def setFilterOptions(self, filterOptions):#, seismicPhase=None):
def setFilterOptions(self, filterOptions): # , seismicPhase=None):
# if seismicPhase is None:
# self.getFilterOptions()[self.getSeismicPhase()] = filterOptions
# else:
@ -1691,12 +1703,12 @@ class MainWindow(QMainWindow):
if vbar.maximum():
vbar.setValue(vbar.value() + up_down[button])
if self._ctrl:
factor = {'up': 5./4.,
'down': 4./5.}
factor = {'up': 5. / 4.,
'down': 4. / 5.}
self.height_factor *= factor[button]
self.adjustPlotHeight()
if self._shift:
factor = {'up': 1./2.,
factor = {'up': 1. / 2.,
'down': 2.}
xlims = self.dataPlot.getXLims()
xdiff = xlims[1] - xlims[0]
@ -1839,7 +1851,7 @@ class MainWindow(QMainWindow):
receventid = self.get_current_event_path()
self.thread = AutoPickThread(parent=self,
func=autoPyLoT,
infile = self.infile,
infile=self.infile,
fnames=self.fnames,
eventid=receventid,
savepath=self.autosave)
@ -1858,7 +1870,7 @@ class MainWindow(QMainWindow):
if not stat_picks:
rval = False
else:
#set picks (ugly syntax?)
# set picks (ugly syntax?)
self.getPicks(type=type)[station] = picks
rval = True
return rval
@ -1917,8 +1929,10 @@ class MainWindow(QMainWindow):
dashed = QtCore.Qt.DashLine
dotted = QtCore.Qt.DotLine
phase_col = {
'P': (pg.mkPen('c'), pg.mkPen((0, 255, 255, 100), style=dashed), pg.mkPen('b', style=dashed), pg.mkPen('b', style=dotted)),
'S': (pg.mkPen('m'), pg.mkPen((255, 0, 255, 100), style=dashed), pg.mkPen('r', style=dashed), pg.mkPen('r', style=dotted))
'P': (pg.mkPen('c'), pg.mkPen((0, 255, 255, 100), style=dashed), pg.mkPen('b', style=dashed),
pg.mkPen('b', style=dotted)),
'S': (pg.mkPen('m'), pg.mkPen((255, 0, 255, 100), style=dashed), pg.mkPen('r', style=dashed),
pg.mkPen('r', style=dotted))
}
else:
phase_col = {
@ -1956,7 +1970,8 @@ class MainWindow(QMainWindow):
pw.plot([lpp, lpp], ylims,
alpha=.25, pen=colors[0], name='LPP')
if spe:
spe_l = pg.PlotDataItem([mpp - spe, mpp - spe], ylims, pen=colors[1], name='{}-SPE'.format(phase))
spe_l = pg.PlotDataItem([mpp - spe, mpp - spe], ylims, pen=colors[1],
name='{}-SPE'.format(phase))
spe_r = pg.PlotDataItem([mpp + spe, mpp + spe], ylims, pen=colors[1])
pw.addItem(spe_l)
pw.addItem(spe_r)
@ -2040,7 +2055,7 @@ class MainWindow(QMainWindow):
obsdir = os.path.join(locroot, 'obs')
self.saveData(directory=obsdir, outformat='.obs')
eventname = self.get_current_event_name()
filename = 'PyLoT_'+eventname
filename = 'PyLoT_' + eventname
locpath = os.path.join(locroot, 'loc', filename)
phasefile = os.path.join(obsdir, filename + '.obs')
phasepath = os.path.join(locroot, 'obs', phasefile)
@ -2053,7 +2068,7 @@ class MainWindow(QMainWindow):
os.remove(phasepath)
self.get_data().applyEVTData(lt.read_location(locpath), typ='event')
#self.get_data().applyEVTData(self.calc_magnitude(), typ='event')
# self.get_data().applyEVTData(self.calc_magnitude(), typ='event')
def init_array_tab(self):
'''
@ -2108,7 +2123,7 @@ class MainWindow(QMainWindow):
self.am_canvas = FigureCanvas(self.am_figure)
self.am_toolbar = NavigationToolbar(self.am_canvas, self)
self.array_map = map_projection(self)
#self.array_map_thread()
# self.array_map_thread()
self.array_layout.addWidget(self.array_map)
self.tabs.setCurrentIndex(index)
self.refresh_array_map()
@ -2128,8 +2143,8 @@ class MainWindow(QMainWindow):
'''
self.array_map = self.amt.data
self.array_layout.addWidget(self.array_map)
#self.tabs.setCurrentIndex(index)
#self.refresh_array_map()
# self.tabs.setCurrentIndex(index)
# self.refresh_array_map()
def refresh_array_map(self):
'''
@ -2151,6 +2166,7 @@ class MainWindow(QMainWindow):
'''
Build and initiate event table (3rd tab [index=2]) containing information of every event.
'''
def set_enabled(item, enabled=True, checkable=False):
# modify item flags depending on case needed
if enabled and not checkable:
@ -2171,7 +2187,7 @@ class MainWindow(QMainWindow):
table = self.project._table
event = self.project.getEventFromPath(table[row][0].text())
if column == 8 or column == 9:
#toggle checked states (exclusive)
# toggle checked states (exclusive)
item_ref = table[row][8]
item_test = table[row][9]
if column == 8 and item_ref.checkState():
@ -2186,7 +2202,7 @@ class MainWindow(QMainWindow):
event.setTestEvent(False)
self.fill_eventbox()
elif column == 5:
#update event notes
# update event notes
notes = table[row][5].text()
event.addNotes(notes)
self.fill_eventbox()
@ -2270,7 +2286,7 @@ class MainWindow(QMainWindow):
else:
item_test.setCheckState(QtCore.Qt.Unchecked)
column=[item_path, item_time, item_lat, item_lon, item_depth, item_mag,
column = [item_path, item_time, item_lat, item_lon, item_depth, item_mag,
item_nmp, item_nap, item_ref, item_test, item_notes]
self.project._table.append(column)
@ -2352,10 +2368,12 @@ class MainWindow(QMainWindow):
# if not rest_flag:
# raise ProcessingError('Restitution of waveform data failed!')
if type == 'ML':
local_mag = LocalMagnitude(corr_wf, self.get_data().get_evt_data(), self.inputs.get('sstop'), verbosity = True)
local_mag = LocalMagnitude(corr_wf, self.get_data().get_evt_data(), self.inputs.get('sstop'),
verbosity=True)
return local_mag.updated_event()
elif type == 'Mw':
moment_mag = MomentMagnitude(corr_wf, self.get_data().get_evt_data(), self.inputs.get('vp'), self.inputs.get('Qp'), self.inputs.get('rho'), verbosity = True)
moment_mag = MomentMagnitude(corr_wf, self.get_data().get_evt_data(), self.inputs.get('vp'),
self.inputs.get('Qp'), self.inputs.get('rho'), verbosity=True)
return moment_mag.updated_event()
else:
return None
@ -2435,7 +2453,7 @@ class MainWindow(QMainWindow):
self.project = Project()
self.init_events(new=True)
self.setDirty(False)
self.project.parameter=self._inputs
self.project.parameter = self._inputs
self.saveProjectAsAction.setEnabled(True)
self.update_status('Created new project...', duration=1000)
return True
@ -2477,7 +2495,7 @@ class MainWindow(QMainWindow):
return False
if not filename.split('.')[-1] == 'plp':
filename = fnm[0] + '.plp'
self.project.parameter=self._inputs
self.project.parameter = self._inputs
self.project.save(filename)
self.setDirty(False)
self.saveProjectAsAction.setEnabled(True)
@ -2494,7 +2512,7 @@ class MainWindow(QMainWindow):
self.setDirty(True)
return False
else:
self.project.parameter=self._inputs
self.project.parameter = self._inputs
self.project.save()
if not self.project.dirty:
self.update_status('Saved back project to file:\n{}'.format(self.project.location), duration=5000)
@ -2502,7 +2520,7 @@ class MainWindow(QMainWindow):
return True
else:
# if still dirty because saving failed
qmb = QMessageBox.warning(self,'Could not save project',
qmb = QMessageBox.warning(self, 'Could not save project',
'Could not save back to original file.\nChoose new file')
self.setDirty(True)
return self.saveProjectAs(exists=True)
@ -2558,6 +2576,7 @@ class Project(object):
'''
Pickable class containing information of a QtPyLoT project, like event lists and file locations.
'''
def __init__(self):
self.eventlist = []
self.location = None
@ -2583,7 +2602,7 @@ class Project(object):
self.setDirty()
else:
print('Skipping event with path {}. Already part of project.'.format(event.path))
#self.search_eventfile_info()
self.search_eventfile_info()
def read_eventfile_info(self, filename, separator=','):
'''
@ -2594,14 +2613,14 @@ class Project(object):
infile = open(filename, 'r')
for line in infile.readlines():
event, date, time, mag, lat, lon, depth = line.split(separator)[:7]
#skip first line
# skip first line
try:
month, day, year = date.split('/')
except:
continue
year = int(year)
#hardcoded, if year only consists of 2 digits (e.g. 16 instead of 2016)
if year<100:
# hardcoded, if year only consists of 2 digits (e.g. 16 instead of 2016)
if year < 100:
year += 2000
datetime = '{}-{}-{}T{}'.format(year, month, day, time)
try:
@ -2610,13 +2629,17 @@ class Project(object):
print(e, datetime, filename)
continue
for event in self.eventlist:
if not event.origins:
continue
origin = event.origins[0] #should have only one origin
if event.origins:
origin = event.origins[0] # should have only one origin
if origin.time == datetime:
origin.latitude = float(lat)
origin.longitude = float(lon)
origin.depth = float(depth)
elif not event.origins:
origin = Origin(resource_id=event.resource_id,
time=datetime, latitude=float(lat),
longitude=float(lon), depth=float(depth))
event.origins.append(origin)
event.magnitudes.append(Magnitude(resource_id=event.resource_id,
mag=float(mag),
mag_type='M'))
@ -2627,7 +2650,7 @@ class Project(object):
and try to read event info from it
'''
datapaths = []
fext='.csv'
fext = '.csv'
for event in self.eventlist:
if not event.datapath in datapaths:
datapaths.append(event.datapath)
@ -2704,6 +2727,7 @@ class getExistingDirectories(QFileDialog):
'''
File dialog with possibility to select multiple folders.
'''
def __init__(self, *args):
super(getExistingDirectories, self).__init__(*args)
self.setOption(self.DontUseNativeDialog, True)
@ -2717,7 +2741,7 @@ class getExistingDirectories(QFileDialog):
def create_window():
app_created = False
app = QCoreApplication.instance()
#check for existing app (when using ipython)
# check for existing app (when using ipython)
if app is None:
app = QApplication(sys.argv)
app_created = True
@ -2725,8 +2749,8 @@ def create_window():
app.setOrganizationDomain("rub.de");
app.setApplicationName("RUB");
app.references = set()
#app.references.add(window)
#window.show()
# app.references.add(window)
# window.show()
return app, app_created
@ -2741,7 +2765,7 @@ def main(args=None):
# create the Qt application
pylot_app, app_created = create_window()
#pylot_app = QApplication(sys.argv)
# pylot_app = QApplication(sys.argv)
pixmap = QPixmap(":/splash/splash.png")
splash = QSplashScreen(pixmap)
splash.show()