From 84739ed8ab104a99f304f01af72efd523f48215e Mon Sep 17 00:00:00 2001 From: Sebastianw Wehling-Benatelli Date: Mon, 19 Sep 2016 11:32:00 +0200 Subject: [PATCH] [new] added new function to find common time window within a stream --- pylot/core/analysis/magnitude.py | 20 ++------------------ pylot/core/util/utils.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/pylot/core/analysis/magnitude.py b/pylot/core/analysis/magnitude.py index a22dbeba..8635fbc9 100644 --- a/pylot/core/analysis/magnitude.py +++ b/pylot/core/analysis/magnitude.py @@ -15,6 +15,7 @@ from scipy.optimize import curve_fit from scipy import integrate, signal from pylot.core.io.data import Data from pylot.core.util.dataprocessing import restitute_data +from pylot.core.util.utils import common_range class Magnitude(object): @@ -360,25 +361,8 @@ def calcsourcespec(wfstream, onset, inventory, vp, delta, azimuth, incidence, qp cordat_copy = cordat.copy() # get equal time stamps and lengths of traces # necessary for rotation of traces - tr0start = cordat_copy[0].stats.starttime - tr0start = tr0start.timestamp - tr0end = cordat_copy[0].stats.endtime - tr0end = tr0end.timestamp - tr1start = cordat_copy[1].stats.starttime - tr1start = tr1start.timestamp - tr1end = cordat_copy[1].stats.endtime - tr1end = tr1end.timestamp - tr2start = cordat_copy[2].stats.starttime - tr2start = tr2start.timestamp - tr2end = cordat_copy[0].stats.endtime - tr2end = tr2end.timestamp - trstart = UTCDateTime(max([tr0start, tr1start, tr2start])) - trend = UTCDateTime(min([tr0end, tr1end, tr2end])) + trstart, trend = common_range(cordat_copy) cordat_copy.trim(trstart, trend) - minlen = min([len(cordat_copy[0]), len(cordat_copy[1]), len(cordat_copy[2])]) - cordat_copy[0].data = cordat_copy[0].data[0:minlen] - cordat_copy[1].data = cordat_copy[1].data[0:minlen] - cordat_copy[2].data = cordat_copy[2].data[0:minlen] try: # rotate into LQT (ray-coordindate-) system using Obspy's rotate # L: P-wave direction diff --git a/pylot/core/util/utils.py b/pylot/core/util/utils.py index 3c210da9..5045d4b1 100644 --- a/pylot/core/util/utils.py +++ b/pylot/core/util/utils.py @@ -206,6 +206,24 @@ def four_digits(year): return year +def common_range(stream): + ''' + takes a stream object and returns the earliest end and the latest start + time of all contained trace objects + :param stream: seismological data stream + :type stream: `~obspy.core.stream.Stream` + :return: maximum start time and minimum end time + ''' + max_start = None + min_end = None + for trace in stream: + if max_start is None or trace.stats.starttime > max_start: + max_start = trace.stats.starttime + if min_end is None or trace.stats.endtime < min_end: + min_end = trace.stats.endtime + return max_start, min_end + + def full_range(stream): ''' takes a stream object and returns the latest end and the earliest start