From acaf592590e602b5180037239572bf1a112af928 Mon Sep 17 00:00:00 2001 From: Marcel Paffrath Date: Mon, 26 Jun 2017 10:47:40 +0200 Subject: [PATCH] [move] class Event moved to pylot.core.util.utils --- QtPyLoT.py | 128 +-------------------------------------- pylot/core/io/data.py | 10 +-- pylot/core/util/utils.py | 125 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 131 deletions(-) diff --git a/QtPyLoT.py b/QtPyLoT.py index 5f005b9a..71a9a0d8 100755 --- a/QtPyLoT.py +++ b/QtPyLoT.py @@ -41,8 +41,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 Event as ObsPyEvent -from obspy.core.event import Origin, Magnitude, ResourceIdentifier +from obspy.core.event import Magnitude from obspy.core.util import AttribDict try: @@ -71,7 +70,7 @@ from pylot.core.util.errors import FormatError, DatastructureError, \ OverwriteError, ProcessingError from pylot.core.util.connection import checkurl from pylot.core.util.dataprocessing import read_metadata, restitute_data -from pylot.core.util.utils import fnConstructor, getLogin, \ +from pylot.core.util.utils import Event, fnConstructor, getLogin, \ full_range from pylot.core.io.location import create_creation_info, create_event from pylot.core.util.widgets import FilterOptionsDialog, NewEventDlg, \ @@ -2561,129 +2560,6 @@ class Project(object): return project -class Event(ObsPyEvent): - ''' - Pickable class derived from ~obspy.core.event.Event containing information on a single event. - ''' - def __init__(self, path): - # initialize super class - super(Event, self).__init__(resource_id=ResourceIdentifier(path.split('/')[-1])) - self.path = path - self.database = path.split('/')[-2] - self.datapath = path.split('/')[-3] - self.rootpath = '/' + os.path.join(*path.split('/')[:-3]) - self.autopicks = {} - self.picks = {} - self.notes = '' - self._testEvent = False - self._refEvent = False - self.get_notes() - - def get_notes_path(self): - notesfile = os.path.join(self.path, 'notes.txt') - return notesfile - - def get_notes(self): - notesfile = self.get_notes_path() - if os.path.isfile(notesfile): - with open(notesfile) as infile: - path = str(infile.readlines()[0].split('\n')[0]) - text = '[eventInfo: '+path+']' - self.addNotes(text) - try: - datetime = UTCDateTime(path.split('/')[-1]) - origin = Origin(resource_id=self.resource_id, time=datetime, latitude=0, longitude=0, depth=0) - self.origins.append(origin) - except: - pass - - def addNotes(self, notes): - self.notes = str(notes) - - def clearNotes(self): - self.notes = None - - def isRefEvent(self): - return self._refEvent - - def isTestEvent(self): - return self._testEvent - - def setRefEvent(self, bool): - self._refEvent = bool - if bool: self._testEvent = False - - def setTestEvent(self, bool): - self._testEvent = bool - if bool: self._refEvent = False - - def addPicks(self, picks): - for station in picks: - self.picks[station] = picks[station] - - def addAutopicks(self, autopicks): - for station in autopicks: - self.autopicks[station] = autopicks[station] - - def setPick(self, station, pick): - if pick: - self.picks[station] = pick - - def setPicks(self, picks): - self.picks = picks - - def getPick(self, station): - if station in self.picks.keys(): - return self.picks[station] - - def getPicks(self): - return self.picks - - def setAutopick(self, station, autopick): - if autopick: - self.autopicks[station] = autopick - - def setAutopicks(self, autopicks): - self.autopicks = autopicks - - def getAutopick(self, station): - if station in self.autopicks.keys(): - return self.autopicks[station] - - def getAutopicks(self): - return self.autopicks - - def save(self, filename): - ''' - Save PyLoT Event to a file. - Can be loaded by using event.load(filename). - ''' - try: - import cPickle - except ImportError: - import _pickle as cPickle - - try: - outfile = open(filename, 'wb') - cPickle.dump(self, outfile, -1) - except Exception as e: - print('Could not pickle PyLoT event. Reason: {}'.format(e)) - - @staticmethod - def load(filename): - ''' - Load project from filename. - ''' - try: - import cPickle - except ImportError: - import _pickle as cPickle - infile = open(filename, 'rb') - event = cPickle.load(infile) - print('Loaded %s' % filename) - return event - - class getExistingDirectories(QFileDialog): ''' File dialog with possibility to select multiple folders. diff --git a/pylot/core/io/data.py b/pylot/core/io/data.py index fb729132..14ed32eb 100644 --- a/pylot/core/io/data.py +++ b/pylot/core/io/data.py @@ -6,11 +6,11 @@ import os from obspy import read_events from obspy.core import read, Stream, UTCDateTime from obspy.io.sac import SacIOError -from obspy.core.event import Event +from obspy.core.event import Event as ObsPyEvent from pylot.core.io.phases import readPILOTEvent, picks_from_picksdict, \ picksdict_from_pilot, merge_picks from pylot.core.util.errors import FormatError, OverwriteError -from pylot.core.util.utils import fnConstructor, full_range +from pylot.core.util.utils import Event, fnConstructor, full_range class Data(object): """ @@ -33,7 +33,7 @@ class Data(object): self.comp = 'Z' self.wfdata = Stream() self._new = False - if isinstance(evtdata, Event): + if isinstance(evtdata, ObsPyEvent) or isinstance(evtdata, Event): pass elif isinstance(evtdata, dict): evt = readPILOTEvent(**evtdata) @@ -49,7 +49,7 @@ class Data(object): if 'Unknown format for file' in e.message: if 'PHASES' in evtdata: picks = picksdict_from_pilot(evtdata) - evtdata = Event() + evtdata = ObsPyEvent() evtdata.picks = picks_from_picksdict(picks) elif 'LOC' in evtdata: raise NotImplementedError('PILOT location information ' @@ -61,7 +61,7 @@ class Data(object): raise e else: # create an empty Event object self.setNew() - evtdata = Event() + evtdata = ObsPyEvent() evtdata.picks = [] self.evtdata = evtdata self.wforiginal = None diff --git a/pylot/core/util/utils.py b/pylot/core/util/utils.py index 46fa699a..06d1d48c 100644 --- a/pylot/core/util/utils.py +++ b/pylot/core/util/utils.py @@ -10,9 +10,134 @@ import re import warnings import subprocess from obspy import UTCDateTime, read +from obspy.core.event import Event as ObsPyEvent +from obspy.core.event import Origin, Magnitude, ResourceIdentifier from pylot.core.io.inputs import PylotParameter +class Event(ObsPyEvent): + ''' + Pickable class derived from ~obspy.core.event.Event containing information on a single event. + ''' + def __init__(self, path): + # initialize super class + super(Event, self).__init__(resource_id=ResourceIdentifier(path.split('/')[-1])) + self.path = path + self.database = path.split('/')[-2] + self.datapath = path.split('/')[-3] + self.rootpath = '/' + os.path.join(*path.split('/')[:-3]) + self.autopicks = {} + self.picks = {} + self.notes = '' + self._testEvent = False + self._refEvent = False + self.get_notes() + + def get_notes_path(self): + notesfile = os.path.join(self.path, 'notes.txt') + return notesfile + + def get_notes(self): + notesfile = self.get_notes_path() + if os.path.isfile(notesfile): + with open(notesfile) as infile: + path = str(infile.readlines()[0].split('\n')[0]) + text = '[eventInfo: '+path+']' + self.addNotes(text) + try: + datetime = UTCDateTime(path.split('/')[-1]) + origin = Origin(resource_id=self.resource_id, time=datetime, latitude=0, longitude=0, depth=0) + self.origins.append(origin) + except: + pass + + def addNotes(self, notes): + self.notes = str(notes) + + def clearNotes(self): + self.notes = None + + def isRefEvent(self): + return self._refEvent + + def isTestEvent(self): + return self._testEvent + + def setRefEvent(self, bool): + self._refEvent = bool + if bool: self._testEvent = False + + def setTestEvent(self, bool): + self._testEvent = bool + if bool: self._refEvent = False + + def addPicks(self, picks): + for station in picks: + self.picks[station] = picks[station] + + def addAutopicks(self, autopicks): + for station in autopicks: + self.autopicks[station] = autopicks[station] + + def setPick(self, station, pick): + if pick: + self.picks[station] = pick + + def setPicks(self, picks): + self.picks = picks + + def getPick(self, station): + if station in self.picks.keys(): + return self.picks[station] + + def getPicks(self): + return self.picks + + def setAutopick(self, station, autopick): + if autopick: + self.autopicks[station] = autopick + + def setAutopicks(self, autopicks): + self.autopicks = autopicks + + def getAutopick(self, station): + if station in self.autopicks.keys(): + return self.autopicks[station] + + def getAutopicks(self): + return self.autopicks + + def save(self, filename): + ''' + Save PyLoT Event to a file. + Can be loaded by using event.load(filename). + ''' + try: + import cPickle + except ImportError: + import _pickle as cPickle + + try: + outfile = open(filename, 'wb') + cPickle.dump(self, outfile, -1) + except Exception as e: + print('Could not pickle PyLoT event. Reason: {}'.format(e)) + + @staticmethod + def load(filename): + ''' + Load project from filename. + ''' + try: + import cPickle + except ImportError: + import _pickle as cPickle + infile = open(filename, 'rb') + event = cPickle.load(infile) + print('Loaded %s' % filename) + return event + + def _pickle_method(m): if m.im_self is None: return getattr, (m.im_class, m.im_func.func_name)