Merge branch 'develop' into update_waveformwidget

This commit is contained in:
2017-08-29 15:57:04 +02:00
9 changed files with 146 additions and 34 deletions

View File

@@ -9,6 +9,9 @@ import subprocess
import numpy as np
from obspy import UTCDateTime, read
from obspy.signal.rotate import rotate2zne
from obspy.io.xseed.utils import SEEDParserException
from pylot.core.io.inputs import PylotParameter
from scipy.interpolate import splrep, splev
@@ -696,6 +699,94 @@ def get_stations(data):
return stations
def check4rotated(data, metadata=None):
def rotate_components(wfstream, metadata=None):
"""rotates components if orientation code is numeric.
azimut and dip are fetched from metadata"""
try:
# indexing fails if metadata is None
metadata[0]
except:
msg = 'Warning: could not rotate traces since no metadata was given\nset Inventory file!'
print(msg)
return wfstream
if metadata[0] is None:
# sometimes metadata is (None, (None,))
msg = 'Warning: could not rotate traces since no metadata was given\nCheck inventory directory!'
print(msg)
return wfstream
else:
parser = metadata[1]
def get_dip_azimut(parser, trace_id):
"""gets azimut and dip for a trace out of the metadata parser"""
dip = None
azimut = None
try:
blockettes = parser._select(trace_id)
except SEEDParserException as e:
print(e)
raise ValueError
for blockette_ in blockettes:
if blockette_.id != 52:
continue
dip = blockette_.dip
azimut = blockette_.azimuth
break
if dip is None or azimut is None:
error_msg = 'Dip and azimuth not available for trace_id {}'.format(trace_id)
raise ValueError(error_msg)
return dip, azimut
trace_ids = [trace.id for trace in wfstream]
for trace_id in trace_ids:
orientation = trace_id[-1]
if orientation.isnumeric():
# misaligned channels have a number as orientation
azimuts = []
dips = []
for trace_id in trace_ids:
try:
dip, azimut = get_dip_azimut(parser, trace_id)
except ValueError as e:
print(e)
print('Failed to rotate station {}, no azimuth or dip available in metadata'.format(trace_id))
return wfstream
azimuts.append(azimut)
dips.append(dip)
# to rotate all traces must have same length
wfstream = trim_station_components(wfstream, trim_start=True, trim_end=True)
z, n, e = rotate2zne(wfstream[0], azimuts[0], dips[0],
wfstream[1], azimuts[1], dips[1],
wfstream[2], azimuts[2], dips[2])
print('check4rotated: rotated station {} to ZNE'.format(trace_id))
z_index = dips.index(min(dips)) # get z-trace index (dip is measured from 0 to -90
wfstream[z_index].data = z
wfstream[z_index].stats.channel = wfstream[z_index].stats.channel[0:-1] + 'Z'
del trace_ids[z_index]
for trace_id in trace_ids:
dip, az = get_dip_azimut(parser, trace_id)
trace = wfstream.select(id=trace_id)[0]
if az > 315 and az <= 45 or az > 135 and az <= 225:
trace.data = n
trace.stats.channel = trace.stats.channel[0:-1] + 'N'
elif az > 45 and az <= 135 or az > 225 and az <= 315:
trace.data = e
trace.stats.channel = trace.stats.channel[0:-1] + 'E'
break
else:
continue
return wfstream
stations = get_stations(data)
for station in stations:
wf_station = data.select(station=station)
wf_station = rotate_components(wf_station, metadata)
return data
def scaleWFData(data, factor=None, components='all'):
"""
produce scaled waveforms from given waveform data and a scaling factor,

View File

@@ -23,7 +23,7 @@ except:
from matplotlib.figure import Figure
from pylot.core.util.utils import find_horizontals, identifyPhase, loopIdentifyPhase, trim_station_components, \
identifyPhaseID
identifyPhaseID, check4rotated
try:
from matplotlib.backends.backend_qt4agg import FigureCanvas
@@ -440,6 +440,7 @@ class WaveformWidgetPG(QtGui.QWidget):
self.plotWidget.showGrid(x=False, y=True, alpha=0.2)
self.plotWidget.hideAxis('bottom')
self.plotWidget.hideAxis('left')
self.wfstart, self.wfend = 0, 0
self.reinitMoveProxy()
self._proxy = pg.SignalProxy(self.plotWidget.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
@@ -457,8 +458,9 @@ class WaveformWidgetPG(QtGui.QWidget):
# if x > 0:# and index < len(data1):
wfID = self._parent.getWFID(y)
station = self._parent.getStationName(wfID)
abstime = self.wfstart + x
if self._parent.get_current_event():
self.label.setText("station = {}, t = {} [s]".format(station, x))
self.label.setText("station = {}, T = {}, t = {} [s]".format(station, abstime, x))
self.vLine.setPos(mousePoint.x())
self.hLine.setPos(mousePoint.y())
@@ -482,7 +484,7 @@ class WaveformWidgetPG(QtGui.QWidget):
component='*', nth_sample=1, iniPick=None, verbosity=0):
self.title = title
self.clearPlotDict()
wfstart, wfend = full_range(wfdata)
self.wfstart, self.wfend = full_range(wfdata)
nmax = 0
settings = QSettings()
@@ -510,7 +512,7 @@ class WaveformWidgetPG(QtGui.QWidget):
try:
self.plotWidget.getPlotItem().vb.setLimits(xMin=float(0),
xMax=float(wfend - wfstart),
xMax=float(self.wfend - self.wfstart),
yMin=-0.5,
yMax=len(nsc) + 0.5)
except:
@@ -528,7 +530,7 @@ class WaveformWidgetPG(QtGui.QWidget):
if verbosity:
msg = 'plotting %s channel of station %s' % (channel, station)
print(msg)
stime = trace.stats.starttime - wfstart
stime = trace.stats.starttime - self.wfstart
time_ax = prepTimeAxis(stime, trace)
if time_ax is not None:
if not scaleddata:
@@ -538,9 +540,9 @@ class WaveformWidgetPG(QtGui.QWidget):
data = [datum + n for index, datum in enumerate(trace.data) if not index % nth_sample]
plots.append((times, data))
self.setPlotDict(n, (station, channel, network))
self.xlabel = 'seconds since {0}'.format(wfstart)
self.xlabel = 'seconds since {0}'.format(self.wfstart)
self.ylabel = ''
self.setXLims([0, wfend - wfstart])
self.setXLims([0, self.wfend - self.wfstart])
self.setYLims([-0.5, nmax + 0.5])
return plots
@@ -2291,6 +2293,8 @@ class TuneAutopicker(QWidget):
wfdat = self.data.getWFData() # all available streams
# trim station components to same start value
trim_station_components(wfdat, trim_start=True, trim_end=False)
# rotate misaligned stations to ZNE
wfdat = check4rotated(wfdat, self.parent.metadata)
self.stationBox.clear()
stations = []
for trace in self.data.getWFData():