From e5f5ab8412daf50d7e2b6c7c143a3d6e5aa3dc7b Mon Sep 17 00:00:00 2001 From: Marcel Paffrath Date: Mon, 17 Jul 2017 11:32:38 +0200 Subject: [PATCH] [change] improved code behavior; exportEvent now checks for magnitudes and origins as well --- QtPyLoT.py | 13 +++++++--- pylot/core/io/data.py | 57 +++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/QtPyLoT.py b/QtPyLoT.py index 572bdc97..927b6ff3 100755 --- a/QtPyLoT.py +++ b/QtPyLoT.py @@ -1135,10 +1135,15 @@ class MainWindow(QMainWindow): # export to given path #self.get_data().exportEvent(fbasename, exform, upperErrors=[uppererrorP[3], uppererrorS[3]]) - self.get_data().exportEvent(fbasename, exform[0], fcheck='manual', - upperErrors=[uppererrorP[3], uppererrorS[3]]) - self.get_data().exportEvent(fbasename, exform[1], fcheck='manual', - upperErrors=[uppererrorP[3], uppererrorS[3]]) + try: + self.get_data().exportEvent(fbasename, exform[0], fcheck='manual', + upperErrors=[uppererrorP[3], uppererrorS[3]]) + self.get_data().exportEvent(fbasename, exform[1], fcheck='manual', + upperErrors=[uppererrorP[3], uppererrorS[3]]) + except Exception as e: + QMessageBox.warning(self, "PyLoT Warning", + "Could not save event: {}".format(e)) + return #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], diff --git a/pylot/core/io/data.py b/pylot/core/io/data.py index 926512c2..f1c2df44 100644 --- a/pylot/core/io/data.py +++ b/pylot/core/io/data.py @@ -147,6 +147,44 @@ class Data(object): # handle forbidden filenames especially on windows systems return fnConstructor(str(ID)) + def checkEvent(self, event, fcheck, forceOverwrite=False): + if 'origin' in fcheck: + self.replaceOrigin(event, forceOverwrite) + if 'magnitude' in fcheck: + self.replaceMagnitude(event, forceOverwrite) + if 'auto' in fcheck: + self.replacePicks(event, 'auto') + if 'manual' in fcheck: + self.replacePicks(event, 'manual') + + def replaceOrigin(self, event, forceOverwrite=False): + if self.get_evt_data().origins or forceOverwrite: + if event.origins: + print("Found origin, replace it by new origin." ) + event.origins = self.get_evt_data().origins + + def replaceMagnitude(self, event, forceOverwrite=False): + if self.get_evt_data().magnitudes or forceOverwrite: + if event.magnitudes: + print("Found magnitude, replace it by new magnitude") + event.magnitudes = self.get_evt_data().magnitudes + + def replacePicks(self, event, picktype): + checkflag = 0 + picks = event.picks + #remove existing picks + for j, pick in reversed(list(enumerate(picks))): + if pick.method_id.id.split('/')[1] == picktype: + picks.pop(j) + checkflag = 1 + if checkflag: + print("Found %s pick(s), remove them and append new picks to catalog." % picktype) + + #append new picks + for pick in self.get_evt_data().picks: + if pick.method_id.id.split('/')[1] == picktype: + picks.append(pick) + def exportEvent(self, fnout, fnext='.xml', fcheck='auto', upperErrors=None): """ @@ -157,6 +195,9 @@ class Data(object): """ from pylot.core.util.defaults import OUTPUTFORMATS + if not type(fcheck)==list: + fcheck=list(fcheck) + try: evtformat = OUTPUTFORMATS[fnext] except KeyError as e: @@ -177,21 +218,7 @@ class Data(object): if not event.resource_id == self.get_evt_data().resource_id: raise IOError("Missmatching event resource id's: {} and {}".format(event.resource_id, self.get_evt_data().resource_id)) - checkflag = 0 - picks = event.picks - #remove existing picks - for j, pick in reversed(list(enumerate(picks))): - if pick.method_id.id.split('/')[1] == fcheck: - picks.pop(j) - checkflag = 1 - if checkflag: - print("Found %s pick(s), remove them and append new picks to catalog." % fcheck) - - #append new picks - for pick in self.get_evt_data().picks: - if pick.method_id.id.split('/')[1] == fcheck: - picks.append(pick) - + self.checkEvent(event, fcheck) self.setEvtData(event) self.get_evt_data().write(fnout + fnext, format=evtformat)