diff --git a/QtPyLoT.py b/QtPyLoT.py index 6daaffa6..6deb6f16 100755 --- a/QtPyLoT.py +++ b/QtPyLoT.py @@ -51,7 +51,8 @@ from pylot.core.util.errors import FormatError, DatastructureError, \ OverwriteError from pylot.core.util.connection import checkurl from pylot.core.util.utils import fnConstructor, createEvent, getLogin, \ - createCreationInfo, getGlobalTimes + getGlobalTimes +from pylot.core.io.location import create_creation_info, create_event from pylot.core.util.widgets import FilterOptionsDialog, NewEventDlg, \ MPLWidget, PropertiesDlg, HelpForm, createAction, PickDlg from pylot.core.util.structure import DATASTRUCTURE @@ -829,8 +830,8 @@ class MainWindow(QMainWindow): new = NewEventDlg() if new.exec_() != QDialog.Rejected: evtpar = new.getValues() - cinfo = createCreationInfo(agency_id=self.agency) - event = createEvent(evtpar['origintime'], cinfo) + cinfo = create_creation_info(agency_id=self.agency) + event = create_event(evtpar['origintime'], cinfo) self.data = Data(self, evtdata=event) self.setDirty(True) diff --git a/pylot/core/io/location.py b/pylot/core/io/location.py new file mode 100644 index 00000000..667b7f91 --- /dev/null +++ b/pylot/core/io/location.py @@ -0,0 +1,222 @@ +from obspy import UTCDateTime +from obspy.core import event as ope + +from pylot.core.util.utils import getLogin, getHash + + +def create_amplitude(pickID, amp, unit, category, cinfo): + ''' + + :param pickID: + :param amp: + :param unit: + :param category: + :param cinfo: + :return: + ''' + amplitude = ope.Amplitude() + amplitude.creation_info = cinfo + amplitude.generic_amplitude = amp + amplitude.unit = ope.AmplitudeUnit(unit) + amplitude.type = ope.AmplitudeCategory(category) + amplitude.pick_id = pickID + return amplitude + + +def create_arrival(pickresID, cinfo, phase, azimuth=None, dist=None): + ''' + create_arrival - function to create an Obspy Arrival + + :param pickresID: Resource identifier of the created pick + :type pickresID: :class: `~obspy.core.event.ResourceIdentifier` object + :param cinfo: An ObsPy :class: `~obspy.core.event.CreationInfo` object + holding information on the creation of the returned object + :type cinfo: :class: `~obspy.core.event.CreationInfo` object + :param phase: name of the arrivals seismic phase + :type phase: str + :param azimuth: azimuth between source and receiver + :type azimuth: float or int, optional + :param dist: distance between source and receiver + :type dist: float or int, optional + :return: An ObsPy :class: `~obspy.core.event.Arrival` object + ''' + arrival = ope.Arrival() + arrival.creation_info = cinfo + arrival.pick_id = pickresID + arrival.phase = phase + if azimuth is not None: + arrival.azimuth = float(azimuth) if azimuth > -180 else azimuth + 360. + else: + arrival.azimuth = azimuth + arrival.distance = dist + return arrival + + +def create_creation_info(agency_id=None, creation_time=None, author=None): + ''' + + :param agency_id: + :param creation_time: + :param author: + :return: + ''' + if author is None: + author = getLogin() + if creation_time is None: + creation_time = UTCDateTime() + return ope.CreationInfo(agency_id=agency_id, author=author, + creation_time=creation_time) + + +def create_event(origintime, cinfo, originloc=None, etype=None, resID=None, + authority_id=None): + ''' + create_event - funtion to create an ObsPy Event + + :param origintime: the events origintime + :type origintime: :class: `~obspy.core.utcdatetime.UTCDateTime` object + :param cinfo: An ObsPy :class: `~obspy.core.event.CreationInfo` object + holding information on the creation of the returned object + :type cinfo: :class: `~obspy.core.event.CreationInfo` object + :param originloc: tuple containing the location of the origin + (LAT, LON, DEP) affiliated with the event which is created + :type originloc: tuple, list + :param etype: Event type str object. converted via ObsPy to a valid event + type string. + :type etype: str + :param resID: Resource identifier of the created event + :type resID: :class: `~obspy.core.event.ResourceIdentifier` object, str + :param authority_id: name of the institution carrying out the processing + :type authority_id: str + :return: An ObsPy :class: `~obspy.core.event.Event` object + ''' + etype = ope.EventType(etype) + if originloc is not None: + o = create_origin(origintime, cinfo, + originloc[0], originloc[1], originloc[2]) + else: + o = None + if etype is None: + etype = ope.EventType('earthquake') # defaults to 'earthquake' + if not resID: + resID = create_resourceID(origintime, etype, authority_id) + elif isinstance(resID, str): + resID = create_resourceID(origintime, etype, authority_id, resID) + elif not isinstance(resID, ope.ResourceIdentifier): + raise TypeError("unsupported type(resID) for resource identifier " + "generation: %s" % type(resID)) + event = ope.Event(resource_id=resID) + event.creation_info = cinfo + event.event_type = etype + if o: + event.origins = [o] + return event + + +def create_magnitude(originID, cinfo): + ''' + create_magnitude - function to create an ObsPy Magnitude object + :param originID: + :type originID: + :param cinfo: + :type cinfo: + :return: + ''' + magnitude = ope.Magnitude() + magnitude.creation_info = cinfo + magnitude.origin_id = originID + return magnitude + + +def create_origin(origintime, cinfo, latitude, longitude, depth): + ''' + create_origin - function to create an ObsPy Origin + :param origintime: the origins time of occurence + :type origintime: :class: `~obspy.core.utcdatetime.UTCDateTime` object + :param cinfo: + :type cinfo: + :param latitude: latitude in decimal degree of the origins location + :type latitude: float + :param longitude: longitude in decimal degree of the origins location + :type longitude: float + :param depth: hypocentral depth of the origin + :type depth: float + :return: An ObsPy :class: `~obspy.core.event.Origin` object + ''' + + assert isinstance(origintime, UTCDateTime), "origintime has to be " \ + "a UTCDateTime object, but " \ + "actually is of type " \ + "'%s'" % type(origintime) + + origin = ope.Origin() + origin.time = origintime + origin.creation_info = cinfo + origin.latitude = latitude + origin.longitude = longitude + origin.depth = depth + return origin + + +def create_pick(origintime, picknum, picktime, eventnum, cinfo, phase, station, + wfseedstr, authority_id): + ''' + create_pick - function to create an ObsPy Pick + + :param origintime: + :type origintime: + :param picknum: number of the created pick + :type picknum: int + :param picktime: + :type picktime: + :param eventnum: human-readable event identifier + :type eventnum: str + :param cinfo: An ObsPy :class: `~obspy.core.event.CreationInfo` object + holding information on the creation of the returned object + :type cinfo: :class: `~obspy.core.event.CreationInfo` object + :param phase: name of the arrivals seismic phase + :type phase: str + :param station: name of the station at which the seismic phase has been + picked + :type station: str + :param wfseedstr: A SEED formatted string of the form + network.station.location.channel in order to set a referenced waveform + :type wfseedstr: str, SEED formatted + :param authority_id: name of the institution carrying out the processing + :type authority_id: str + :return: An ObsPy :class: `~obspy.core.event.Pick` object + ''' + pickID = eventnum + '_' + station.strip() + '/{0:03d}'.format(picknum) + pickresID = create_resourceID(origintime, 'pick', authority_id, pickID) + pick = ope.Pick() + pick.resource_id = pickresID + pick.time = picktime + pick.creation_info = cinfo + pick.phase_hint = phase + pick.waveform_id = ope.ResourceIdentifier(id=wfseedstr, prefix='file:/') + return pick + + +def create_resourceID(timetohash, restype, authority_id=None, hrstr=None): + ''' + + :param timetohash: + :type timetohash + :param restype: type of the resource, e.g. 'orig', 'earthquake' ... + :type restype: str + :param authority_id: name of the institution carrying out the processing + :type authority_id: str, optional + :param hrstr: + :type hrstr: + :return: + ''' + assert isinstance(timetohash, UTCDateTime), "'timetohash' is not an ObsPy" \ + "UTCDateTime object" + hid = getHash(timetohash) + if hrstr is None: + resID = ope.ResourceIdentifier(restype + '/' + hid[0:6]) + else: + resID = ope.ResourceIdentifier(restype + '/' + hrstr + '_' + hid[0:6]) + if authority_id is not None: + resID.convertIDToQuakeMLURI(authority_id=authority_id) + return resID \ No newline at end of file diff --git a/pylot/core/io/phases.py b/pylot/core/io/phases.py index 604ea940..cf2e48b6 100644 --- a/pylot/core/io/phases.py +++ b/pylot/core/io/phases.py @@ -1,16 +1,18 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import os import glob -import warnings -import scipy.io as sio import obspy.core.event as ope +import os +import scipy.io as sio +import warnings from obspy.core import UTCDateTime +from pylot.core.io.location import create_arrival, create_event, \ + create_magnitude, create_origin, create_pick from pylot.core.pick.utils import select_for_phase -from pylot.core.util.utils import getOwner, createPick, createArrival, \ - createEvent, createOrigin, createMagnitude, getGlobalTimes +from pylot.core.util.utils import getOwner, getGlobalTimes + def readPILOTEvent(phasfn=None, locfn=None, authority_id=None, **kwargs): """ @@ -75,14 +77,14 @@ def readPILOTEvent(phasfn=None, locfn=None, authority_id=None, **kwargs): stations = [stat for stat in phases['stat'][0:-1:3]] - event = createEvent(eventDate, loccinfo, etype='earthquake', resID=eventNum, - authority_id=authority_id) + event = create_event(eventDate, loccinfo, etype='earthquake', resID=eventNum, + authority_id=authority_id) lat = float(loc['LAT']) lon = float(loc['LON']) dep = float(loc['DEP']) - origin = createOrigin(eventDate, loccinfo, lat, lon, dep) + origin = create_origin(eventDate, loccinfo, lat, lon, dep) for n, pick in enumerate(phases['Ptime']): if pick[0] > 0: kwargs = {'year': int(pick[0]), @@ -115,15 +117,15 @@ def readPILOTEvent(phasfn=None, locfn=None, authority_id=None, **kwargs): wffn = os.path.join(sdir, '{0}*{1}*'.format( stations[n].strip(), '[ne]')) print(wffn) - pick = createPick(eventDate, np, picktime, eventNum, pickcinfo, - phase, stations[n], wffn, authority_id) + pick = create_pick(eventDate, np, picktime, eventNum, pickcinfo, + phase, stations[n], wffn, authority_id) event.picks.append(pick) pickID = pick.get('id') - arrival = createArrival(pickID, pickcinfo, phase) + arrival = create_arrival(pickID, pickcinfo, phase) origin.arrivals.append(arrival) np += 1 - magnitude = createMagnitude(origin.get('id'), loccinfo) + magnitude = create_magnitude(origin.get('id'), loccinfo) magnitude.mag = float(loc['Mnet']) magnitude.magnitude_type = 'Ml' diff --git a/pylot/core/util/utils.py b/pylot/core/util/utils.py index c0eb0185..f892c6f6 100644 --- a/pylot/core/util/utils.py +++ b/pylot/core/util/utils.py @@ -1,232 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import os -import subprocess -import pwd -import re import hashlib import numpy as np +import os +import pwd +import re +import subprocess from obspy.core import UTCDateTime -import obspy.core.event as ope - - -def createAmplitude(pickID, amp, unit, category, cinfo): - ''' - - :param pickID: - :param amp: - :param unit: - :param category: - :param cinfo: - :return: - ''' - amplitude = ope.Amplitude() - amplitude.creation_info = cinfo - amplitude.generic_amplitude = amp - amplitude.unit = ope.AmplitudeUnit(unit) - amplitude.type = ope.AmplitudeCategory(category) - amplitude.pick_id = pickID - return amplitude - - -def createArrival(pickresID, cinfo, phase, azimuth=None, dist=None): - ''' - createArrival - function to create an Obspy Arrival - - :param pickresID: Resource identifier of the created pick - :type pickresID: :class: `~obspy.core.event.ResourceIdentifier` object - :param cinfo: An ObsPy :class: `~obspy.core.event.CreationInfo` object - holding information on the creation of the returned object - :type cinfo: :class: `~obspy.core.event.CreationInfo` object - :param phase: name of the arrivals seismic phase - :type phase: str - :param azimuth: azimuth between source and receiver - :type azimuth: float or int, optional - :param dist: distance between source and receiver - :type dist: float or int, optional - :return: An ObsPy :class: `~obspy.core.event.Arrival` object - ''' - arrival = ope.Arrival() - arrival.creation_info = cinfo - arrival.pick_id = pickresID - arrival.phase = phase - if azimuth is not None: - arrival.azimuth = float(azimuth) if azimuth > -180 else azimuth + 360. - else: - arrival.azimuth = azimuth - arrival.distance = dist - return arrival - - -def createCreationInfo(agency_id=None, creation_time=None, author=None): - ''' - - :param agency_id: - :param creation_time: - :param author: - :return: - ''' - if author is None: - author = getLogin() - if creation_time is None: - creation_time = UTCDateTime() - return ope.CreationInfo(agency_id=agency_id, author=author, - creation_time=creation_time) - - -def createEvent(origintime, cinfo, originloc=None, etype=None, resID=None, - authority_id=None): - ''' - createEvent - funtion to create an ObsPy Event - - :param origintime: the events origintime - :type origintime: :class: `~obspy.core.utcdatetime.UTCDateTime` object - :param cinfo: An ObsPy :class: `~obspy.core.event.CreationInfo` object - holding information on the creation of the returned object - :type cinfo: :class: `~obspy.core.event.CreationInfo` object - :param originloc: tuple containing the location of the origin - (LAT, LON, DEP) affiliated with the event which is created - :type originloc: tuple, list - :param etype: Event type str object. converted via ObsPy to a valid event - type string. - :type etype: str - :param resID: Resource identifier of the created event - :type resID: :class: `~obspy.core.event.ResourceIdentifier` object, str - :param authority_id: name of the institution carrying out the processing - :type authority_id: str - :return: An ObsPy :class: `~obspy.core.event.Event` object - ''' - etype = ope.EventType(etype) - if originloc is not None: - o = createOrigin(origintime, cinfo, - originloc[0], originloc[1], originloc[2]) - else: - o = None - if etype is None: - etype = ope.EventType('earthquake') # defaults to 'earthquake' - if not resID: - resID = createResourceID(origintime, etype, authority_id) - elif isinstance(resID, str): - resID = createResourceID(origintime, etype, authority_id, resID) - elif not isinstance(resID, ope.ResourceIdentifier): - raise TypeError("unsupported type(resID) for resource identifier " - "generation: %s" % type(resID)) - event = ope.Event(resource_id=resID) - event.creation_info = cinfo - event.event_type = etype - if o: - event.origins = [o] - return event - - -def createMagnitude(originID, cinfo): - ''' - createMagnitude - function to create an ObsPy Magnitude object - :param originID: - :type originID: - :param cinfo: - :type cinfo: - :return: - ''' - magnitude = ope.Magnitude() - magnitude.creation_info = cinfo - magnitude.origin_id = originID - return magnitude - - -def createOrigin(origintime, cinfo, latitude, longitude, depth): - ''' - createOrigin - function to create an ObsPy Origin - :param origintime: the origins time of occurence - :type origintime: :class: `~obspy.core.utcdatetime.UTCDateTime` object - :param cinfo: - :type cinfo: - :param latitude: latitude in decimal degree of the origins location - :type latitude: float - :param longitude: longitude in decimal degree of the origins location - :type longitude: float - :param depth: hypocentral depth of the origin - :type depth: float - :return: An ObsPy :class: `~obspy.core.event.Origin` object - ''' - - assert isinstance(origintime, UTCDateTime), "origintime has to be " \ - "a UTCDateTime object, but " \ - "actually is of type " \ - "'%s'" % type(origintime) - - origin = ope.Origin() - origin.time = origintime - origin.creation_info = cinfo - origin.latitude = latitude - origin.longitude = longitude - origin.depth = depth - return origin - - -def createPick(origintime, picknum, picktime, eventnum, cinfo, phase, station, - wfseedstr, authority_id): - ''' - createPick - function to create an ObsPy Pick - - :param origintime: - :type origintime: - :param picknum: number of the created pick - :type picknum: int - :param picktime: - :type picktime: - :param eventnum: human-readable event identifier - :type eventnum: str - :param cinfo: An ObsPy :class: `~obspy.core.event.CreationInfo` object - holding information on the creation of the returned object - :type cinfo: :class: `~obspy.core.event.CreationInfo` object - :param phase: name of the arrivals seismic phase - :type phase: str - :param station: name of the station at which the seismic phase has been - picked - :type station: str - :param wfseedstr: A SEED formatted string of the form - network.station.location.channel in order to set a referenced waveform - :type wfseedstr: str, SEED formatted - :param authority_id: name of the institution carrying out the processing - :type authority_id: str - :return: An ObsPy :class: `~obspy.core.event.Pick` object - ''' - pickID = eventnum + '_' + station.strip() + '/{0:03d}'.format(picknum) - pickresID = createResourceID(origintime, 'pick', authority_id, pickID) - pick = ope.Pick() - pick.resource_id = pickresID - pick.time = picktime - pick.creation_info = cinfo - pick.phase_hint = phase - pick.waveform_id = ope.ResourceIdentifier(id=wfseedstr, prefix='file:/') - return pick - - -def createResourceID(timetohash, restype, authority_id=None, hrstr=None): - ''' - - :param timetohash: - :type timetohash - :param restype: type of the resource, e.g. 'orig', 'earthquake' ... - :type restype: str - :param authority_id: name of the institution carrying out the processing - :type authority_id: str, optional - :param hrstr: - :type hrstr: - :return: - ''' - assert isinstance(timetohash, UTCDateTime), "'timetohash' is not an ObsPy" \ - "UTCDateTime object" - hid = getHash(timetohash) - if hrstr is None: - resID = ope.ResourceIdentifier(restype + '/' + hid[0:6]) - else: - resID = ope.ResourceIdentifier(restype + '/' + hrstr + '_' + hid[0:6]) - if authority_id is not None: - resID.convertIDToQuakeMLURI(authority_id=authority_id) - return resID def demeanTrace(trace, window):