From 8d37e9299c2f35537f4d25661563bf9061d93b4e 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 41cfd590..8ec44a92 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 db9a3888..8daeaefa 100644 --- a/pylot/core/util/utils.py +++ b/pylot/core/util/utils.py @@ -205,6 +205,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