moved static methods as functions to the util package for flexibility reasons

This commit is contained in:
Sebastian Wehling-Benatelli 2015-07-02 10:29:50 +02:00
parent a32f6fc037
commit e99d2630a1
2 changed files with 58 additions and 32 deletions

View File

@ -10,6 +10,7 @@ import hashlib
import numpy as np import numpy as np
from obspy.core import UTCDateTime from obspy.core import UTCDateTime
import obspy.core.event as ope import obspy.core.event as ope
from pylot.core.pick.utils import getnoisewin
def runProgram(cmd, parameter=None): def runProgram(cmd, parameter=None):
""" """
@ -87,6 +88,60 @@ def prepTimeAxis(stime, trace):
return time_ax return time_ax
def scaleWFData(data, factor=None, components='all'):
"""
produce scaled waveforms from given waveform data and a scaling factor,
waveform may be selected by their components name
:param data: waveform data to be scaled
:type data: `~obspy.core.stream.Stream` object
:param factor: scaling factor
:type factor: float
:param components: components labels for the traces in data to be scaled by
the scaling factor (optional, default: 'all')
:type components: tuple
:return: scaled waveform data
:rtype: `~obspy.core.stream.Stream` object
"""
if components is not 'all':
for comp in components:
if factor is None:
max_val = np.max(np.abs(data.select(component=comp)[0].data))
data.select(component=comp)[0].data /= 2 * max_val
else:
data.select(component=comp)[0].data /= 2 * factor
else:
for tr in data:
if factor is None:
max_val = float(np.max(np.abs(tr.data)))
tr.data /= 2 * max_val
else:
tr.data /= 2 * factor
return data
def demeanWFData(data, t0, win, gap):
"""
returns the DATA where each trace is demean by the average value within
a desired time window WIN before T0 and a GAP
:param data: waveform stream object
:type data: `~obspy.core.stream.Stream`
:param t0: time before which the noise
:type t0: float
:param win: time window for average calculation
:type win: tuple
:param gap: gap window to avoid polluting the average
:type gap: float
:return: data
:rtype: `~obspy.core.stream.Stream`
"""
starttime = getGlobalTimes(data)[0]
for tr in data:
stime = tr.stats.starttime - starttime
t = prepTimeAxis(stime, tr)
inoise = getnoisewin(t, t0, win, gap)
tr.data -= tr.data[inoise].mean()
return data
def getGlobalTimes(stream): def getGlobalTimes(stream):
min_start = UTCDateTime() min_start = UTCDateTime()
max_end = None max_end = None

View File

@ -27,7 +27,8 @@ from pylot.core.read import FilterOptions
from pylot.core.pick.utils import getSNR, earllatepicker, getnoisewin,\ from pylot.core.pick.utils import getSNR, earllatepicker, getnoisewin,\
getResolutionWindow getResolutionWindow
from pylot.core.util.defaults import OUTPUTFORMATS, FILTERDEFAULTS from pylot.core.util.defaults import OUTPUTFORMATS, FILTERDEFAULTS
from pylot.core.util import prepTimeAxis, getGlobalTimes from pylot.core.util import prepTimeAxis, getGlobalTimes, scaleWFData, \
demeanWFData
def createAction(parent, text, slot=None, shortcut=None, icon=None, def createAction(parent, text, slot=None, shortcut=None, icon=None,
@ -386,40 +387,10 @@ class PickDlg(QDialog):
wfdata = self.getWFData().select(component=component) wfdata = self.getWFData().select(component=component)
return wfdata return wfdata
@staticmethod
def demeanWFData(data, t0, win, gap):
"""
returns the DATA where each trace is demean by the average value within
a desired time window WIN before T0 and a GAP
:param data: waveform stream object
:type data: `~obspy.core.stream.Stream`
:param t0: time before which the noise
:type t0: float
:param win: time window for average calculation
:type win: tuple
:param gap: gap window to avoid polluting the average
:type gap: float
:return: data
:rtype: `~obspy.core.stream.Stream`
"""
starttime = getGlobalTimes(data)[0]
for tr in data:
stime = tr.stats.starttime - starttime
t = prepTimeAxis(stime, tr)
inoise = getnoisewin(t, t0, win, gap)
tr.data -= tr.data[inoise].mean()
return data
def getPicks(self): def getPicks(self):
return self.picks return self.picks
def setIniPick(self, gui_event): def setIniPick(self, gui_event):
if self.selectPhase.currentText().upper().startswith('P'):
self.setIniPickP(gui_event)
elif self.selectPhase.currentText().upper().startswith('S'):
self.setIniPickS(gui_event)
def setIniPickP(self, gui_event):
trace_number = round(gui_event.ydata) trace_number = round(gui_event.ydata)
@ -448,7 +419,7 @@ class PickDlg(QDialog):
x_res = getResolutionWindow(snr) x_res = getResolutionWindow(snr)
data = self.demeanWFData(data=self.getWFData().copy(), t0=ini_pick, data = demeanWFData(data=self.getWFData().copy(), t0=ini_pick,
win=noise_win, gap=gap_win) win=noise_win, gap=gap_win)
self.setXLims([ini_pick - x_res, ini_pick + x_res]) self.setXLims([ini_pick - x_res, ini_pick + x_res])