diff --git a/etc/filter/10HZ_filter_min.mseed b/etc/filter/10HZ_filter_min.mseed index 659cd228f37a6a64d9bcece556f0a62453f61ce9..28ca40a40b3e146bdc1fcfa8d0fe012993d06fa3 100644 Binary files a/etc/filter/10HZ_filter_min.mseed and b/etc/filter/10HZ_filter_min.mseed differ diff --git a/etc/filter/10HZ_filter_sec.mseed b/etc/filter/10HZ_filter_sec.mseed index ed50ed33e148287ee384eaee7b6bc2ab21d1a68d..28a51c1c71d185c01ac863cf5947292e569649bb 100644 Binary files a/etc/filter/10HZ_filter_sec.mseed and b/etc/filter/10HZ_filter_sec.mseed differ diff --git a/geomagio/Controller.py b/geomagio/Controller.py index 024014ef567d61e0964580d60af10cb96bd568f2..e07874a642dfcc4074863bcd71b21abc60e6b379 100644 --- a/geomagio/Controller.py +++ b/geomagio/Controller.py @@ -333,6 +333,9 @@ def get_input_factory(args): host=args.input_host, port=args.input_port, locationCode=args.locationcode, + convert_channels=args.convert_voltbin, + volt_conv=args.volt_conversion, + bin_conv=args.bin_conversion, **input_factory_args) elif input_type == 'goes': # TODO: deal with other goes arguments @@ -764,6 +767,21 @@ def parse_args(args): 'miniseed', 'pcdcp')) + # conversion factors for volts/bins + parser.add_argument('--volt-conversion', + default=100, + help='Conversion factor for volts') + + parser.add_argument('--bin-conversion', + default=500, + help='Conversion factor for bins') + + # conversion from bins/volts to nT + parser.add_argument('--convert-voltbin', + nargs='*', + default=None, + help='Convert channels from bins/volts to nT') + parser.add_argument('--input-file', help='Read from specified file') parser.add_argument('--input-host', diff --git a/geomagio/algorithm/FilterAlgorithm.py b/geomagio/algorithm/FilterAlgorithm.py index 089e031a15bc3cccf38485a1e0a58f370edbcff2..471892a8340e043d95e398ba5a6ba1b94557ba5c 100644 --- a/geomagio/algorithm/FilterAlgorithm.py +++ b/geomagio/algorithm/FilterAlgorithm.py @@ -113,9 +113,9 @@ class FilterAlgorithm(Algorithm): out : obspy.core.Stream stream containing 1 trace per original trace. """ - # if input stream is 10 Hz, convert data to nT - if self.input_sample_period == 0.1: - stream = self.convert_miniseed(stream) + # # if input stream is 10 Hz, convert data to nT + # if self.input_sample_period == 0.1: + # stream = self.convert_voltbin(stream) output_sample_period = self.output_sample_period input_sample_period = self.input_sample_period @@ -229,37 +229,6 @@ class FilterAlgorithm(Algorithm): filtered_out = np.ma.filled(filtered, np.nan) return filtered_out - def convert_miniseed(self, stream): - """Convert miniseed data from bins and volts to nT. - Converts all traces in stream. - Parameters - ---------- - stream: obspy.core.Stream - stream of data to convert - Returns - ------- - out : obspy.core.Stream - stream containing 1 trace per 2 original traces. - """ - out = Stream() # output stream - # selects volts from input Stream and sorts by channel name - volts = stream.select(channel="?*V*").sort(['channel']) - # selects bins from input Stream and sorts by channel name - bins = stream.select(channel="?*B*").sort(['channel']) - for i in range(0, len(volts)): - # copy stats from input trace - stats = Stats(stream[i].stats) - # set output trace's channel to U, V, or W - stats.channel = str(volts[i].stats.channel)[0] - # convert volts and bins readings into nT data - data = int(self.volt_conv) * \ - volts[i].data + int(self.bin_conv) * bins[i].data - # create output trace with adapted channel and data - trace_out = self.create_trace(stats.channel, stats, data) - out += trace_out - - return out - def get_input_interval(self, start, end, observatory=None, channels=None): """Get Input Interval start : UTCDateTime @@ -320,15 +289,6 @@ class FilterAlgorithm(Algorithm): parser.add_argument('--filter-coefficients', help='File storing custom filter coefficients') - # conversion factors for volts/bins - parser.add_argument('--volt-conversion', - default=100, - help='Conversion factor for volts') - - parser.add_argument('--bin-conversion', - default=500, - help='Conversion factor for bins') - def configure(self, arguments): """Configure algorithm using comand line arguments. Parameters diff --git a/geomagio/edge/MiniSeedFactory.py b/geomagio/edge/MiniSeedFactory.py index d0f7704978469ad5b02283f1d74066a8322c2b19..5c192c9bd940ef1fbc44edc10c3c4a493344fafa 100644 --- a/geomagio/edge/MiniSeedFactory.py +++ b/geomagio/edge/MiniSeedFactory.py @@ -64,7 +64,8 @@ class MiniSeedFactory(TimeseriesFactory): def __init__(self, host='cwbpub.cr.usgs.gov', port=2061, write_port=7981, observatory=None, channels=None, type=None, interval=None, - observatoryMetadata=None, locationCode=None): + observatoryMetadata=None, locationCode=None, + convert_channels=None, volt_conv=100, bin_conv=500): TimeseriesFactory.__init__(self, observatory, channels, type, interval) self.client = miniseed.Client(host, port) @@ -75,6 +76,9 @@ class MiniSeedFactory(TimeseriesFactory): self.host = host self.port = port self.write_port = write_port + self.convert_channels = convert_channels + self.volt_conv = volt_conv + self.bin_conv = bin_conv def get_timeseries(self, starttime, endtime, observatory=None, channels=None, type=None, interval=None): @@ -108,6 +112,7 @@ class MiniSeedFactory(TimeseriesFactory): """ observatory = observatory or self.observatory channels = channels or self.channels + convert_channels = self.convert_channels type = type or self.type interval = interval or self.interval @@ -127,6 +132,7 @@ class MiniSeedFactory(TimeseriesFactory): data = self._get_timeseries(starttime, endtime, observatory, channel, type, interval) timeseries += data + # restore stdout finally: output = temp_stdout.getvalue() @@ -134,8 +140,19 @@ class MiniSeedFactory(TimeseriesFactory): sys.stderr.write(str(output)) temp_stdout.close() sys.stdout = original_stdout - self._post_process(timeseries, starttime, endtime, channels) + if len(self.convert_channels) != 0: + out = obspy.core.Stream() + for i in range(1, len(timeseries), 2): + _in_ = obspy.core.Stream() + if timeseries[i].stats.channel[0] in convert_channels: + if timeseries[i - 1].stats.channel[0] in convert_channels: + _in_ += timeseries[i] + _in_ += timeseries[i - 1] + out += self.convert_voltbin(_in_) + timeseries = out + + self._post_process(timeseries, starttime, endtime, channels) return timeseries def put_timeseries(self, timeseries, starttime=None, endtime=None, @@ -448,6 +465,39 @@ class MiniSeedFactory(TimeseriesFactory): observatory, channel, type, interval) return data + def convert_voltbin(self, stream): + """Convert miniseed data from bins and volts to nT. + Converts all traces in stream. + Parameters + ---------- + stream: obspy.core.Stream + stream of data to convert + Returns + ------- + out : obspy.core.Trace + Trace containing 1 trace per 2 original traces. + """ + out = obspy.core.Trace() + # selects volts from input Trace + volts = stream.select(channel="*V*") + # selects bins from input Trace + bins = stream.select(channel="*B*") + # copy stats from original Trace + stats = obspy.core.Stats(volts[0].stats) + # set channel parameter to U, V, or W + stats.channel = volts[0].stats.channel[0] + # conversion from bins/volts to nT + data = int(self.volt_conv) * \ + volts[0].data + int(self.bin_conv) * bins[0].data + # create empty trace with adapted stats + out = TimeseriesUtility.create_empty_trace(stats.starttime, + stats.endtime, stats.station, stats.channel, + stats.data_type, stats.data_interval, + stats.network, stats.station, stats.location) + # set data for empty trace as nT converted data + out.data = data + return out + def _post_process(self, timeseries, starttime, endtime, channels): """Post process a timeseries stream after the raw data is is fetched from querymom. Specifically changes diff --git a/test/algorithm_test/FilterAlgorithm_test.py b/test/algorithm_test/FilterAlgorithm_test.py index a6f016be747eba3859dee0f8870fb8f2f443cc8e..d972a0d84d28b5fd323688ce28a1dc56d22644fe 100644 --- a/test/algorithm_test/FilterAlgorithm_test.py +++ b/test/algorithm_test/FilterAlgorithm_test.py @@ -17,7 +17,8 @@ def test_process(): # starttime = UTCDateTime('2020-01-06T00:00:00Z') # endtime = UTCDateTime('2020-01-07T04:00:00Z') # starttime, endtime = f.get_input_interval(starttime, endtime) - # m = MiniSeedFactory(port=2061, host='...') + # m = MiniSeedFactory(port=2061, host='...', + # convert_channels=['U', 'V', 'W'], volt_conv=100, bin_conv=500) # llo_for_filter = m.get_timeseries(observatory='LLO', # channels=['U_Bin', 'U_Volt', 'V_Bin', # 'V_Volt', 'W_Bin', 'W_Volt'], @@ -25,7 +26,7 @@ def test_process(): # interval= 'tenhertz', # starttime=starttime, # endtime=endtime) - # filtered_sec = f.process(llo_for_filter) + # llo_for_filter.write('/Users/pcain/geomag-algorithms/etc/filter/10HZ_filter_sec.mseed',format='MSEED') llo_sec = read('etc/filter/10HZ_filter_sec.mseed') filtered_sec = f.process(llo_sec) @@ -36,15 +37,16 @@ def test_process(): # starttime = UTCDateTime('2020-01-06T00:00:00Z') # endtime = UTCDateTime('2020-01-07T04:00:00Z') # starttime, endtime = f.get_input_interval(starttime, endtime) - # m = MiniSeedFactory(port=2061, host='...') + # m = MiniSeedFactory(port=2061, host='...', + # convert_channels=['U', 'V', 'W'], volt_conv=100, bin_conv=500) # llo_for_filter = m.get_timeseries(observatory='LLO', # channels=['U_Bin', 'U_Volt', 'V_Bin', # 'V_Volt', 'W_Bin', 'W_Volt'], - # type='variation', - # interval='tenhertz', + # type= 'variation', + # interval= 'tenhertz', # starttime=starttime, # endtime=endtime) - # filtered_minute = f.process(llo_for_filter) + # llo_for_filter.write('/Users/pcain/geomag-algorithms/etc/filter/10HZ_filter_min.mseed',format='MSEED') llo_min = read('etc/filter/10HZ_filter_min.mseed') f = FilterAlgorithm(filtertype='default', @@ -52,22 +54,21 @@ def test_process(): output_sample_period=60) filtered_min = f.process(llo_min) - # # generation of etc/filter/10HZ_filter_min.mseed + # # generation of etc/filter/10HZ_filter_hor.mseed # f = FilterAlgorithm(filtertype='default', - # input_sample_period=0.1, + # input_sample_period=60, # output_sample_period=3600) # starttime = UTCDateTime('2020-01-06T00:00:00Z') # endtime = UTCDateTime('2020-01-07T04:00:00Z') # starttime, endtime = f.get_input_interval(starttime, endtime) - # m = MiniSeedFactory(port=2061, host='...') - # llo_for_filter = m.get_timeseries(observatory='LLO', - # channels=['U_Bin', 'U_Volt', 'V_Bin', - # 'V_Volt', 'W_Bin', 'W_Volt'], - # type='variation', - # interval='tenhertz', + # e = EdgeFactory() + # bou_for_filter = e.get_timeseries(observatory='BOU', + # channels=['H','E','Z'], + # type= 'variation', + # interval= 'minute', # starttime=starttime, # endtime=endtime) - # filtered_hour = f.process(llo_for_filter) + # bou_for_filter.write('/Users/pcain/geomag-algorithms/etc/filter/10HZ_filter_hor.mseed',format='MSEED') llo_hor = read('etc/filter/10HZ_filter_hor.mseed') f = FilterAlgorithm(filtertype='default',