diff --git a/geomagio/algorithm/FilterAlgorithm.py b/geomagio/algorithm/FilterAlgorithm.py index af226d742ee5a8c1f3379becda3655cb6d288364..36580e900c8a5b5a7ff49a68bf1fdadd572eb236 100644 --- a/geomagio/algorithm/FilterAlgorithm.py +++ b/geomagio/algorithm/FilterAlgorithm.py @@ -136,34 +136,46 @@ class FilterAlgorithm(Algorithm): out : obspy.core.Stream stream containing 1 trace per original trace. """ - # intitialize dictionary object for filter + # intitialize step array for filter steps = self.get_filter_steps() - out_stream = Stream() - out_stream = stream.copy() - # while dictionary is not empty for step in steps: - # gather variables from single step - window = np.array(step['window']) - numtaps = len(window) - input_sample_period = step['input_sample_period'] - output_sample_period = step['output_sample_period'] - decimation = int(output_sample_period / input_sample_period) - out = Stream() - # from original algorithm - for trace in out_stream: - data = trace.data - filtered = self.firfilter(data, - window / sum(window), decimation) - stats = Stats(trace.stats) - stats.starttime = stats.starttime + \ - input_sample_period * (numtaps // 2) - stats.sampling_rate = 1 / output_sample_period - stats.delta = output_sample_period - stats.npts = filtered.shape[0] - trace_out = self.create_trace(stats.channel, stats, filtered) - out += trace_out - # set out_stream to filtered output and continue while loop - out_stream = out.copy() + stream = self.process_step(step, stream.copy()) + + return stream + + def process_step(self, step, stream): + """Filters stream for one step. + Filters all traces in stream. + Parameters + ---------- + step : array element + step holding variables for one filtering operation + stream : obspy.core.Stream + stream of data to filter + Returns + ------- + out : obspy.core.Stream + stream containing 1 trace per original trace. + """ + # gather variables from step + input_sample_period = step['input_sample_period'] + output_sample_period = step['output_sample_period'] + window = np.array(step['window']) + decimation = int(output_sample_period / input_sample_period) + numtaps = len(window) + window = window / sum(window) + + out = Stream() + for trace in stream: + filtered = self.firfilter(trace.data, + window, decimation) + stats = Stats(trace.stats) + stats.starttime = stats.starttime + \ + input_sample_period * (numtaps // 2) + stats.delta = output_sample_period + stats.npts = len(filtered) + trace_out = self.create_trace(stats.channel, stats, filtered) + out += trace_out return out @staticmethod @@ -240,8 +252,9 @@ class FilterAlgorithm(Algorithm): # calculate start/end from step array for step in steps: half = len(step["window"]) // 2 - start = start - half * step["input_sample_period"] - end = end + half * step["input_sample_period"] + half_step = half * step["input_sample_period"] + start = start - half_step + end = end + half_step return (start, end) @classmethod diff --git a/geomagio/edge/MiniSeedFactory.py b/geomagio/edge/MiniSeedFactory.py index c4a9e05e3615176ef12ff60ae12a34b7877b3555..aabf673b2d79d5791ab203ced018825659b7c864 100644 --- a/geomagio/edge/MiniSeedFactory.py +++ b/geomagio/edge/MiniSeedFactory.py @@ -477,16 +477,16 @@ class MiniSeedFactory(TimeseriesFactory): """ out = obspy.core.Trace() # selects volts from input Trace - volts = stream.select(channel=channel + "_Volt") + volts = stream.select(channel=channel + "_Volt")[0] # selects bins from input Trace - bins = stream.select(channel=channel + "_Bin") + bins = stream.select(channel=channel + "_Bin")[0] # copy stats from original Trace - stats = obspy.core.Stats(volts[0].stats) + stats = obspy.core.Stats(volts.stats) # set channel parameter to U, V, or W - stats.channel = channel[0] + stats.channel = channel # conversion from bins/volts to nT - data = self.volt_conv * \ - volts[0].data + self.bin_conv * bins[0].data + data = self.volt_conv * volts.data \ + + self.bin_conv * bins.data # create empty trace with adapted stats out = TimeseriesUtility.create_empty_trace(stats.starttime, stats.endtime, stats.station, stats.channel,