Skip to content
Snippets Groups Projects
Commit f31057f8 authored by Hal Simpson's avatar Hal Simpson
Browse files

Fixed odds and ends, and made convert_stream_to_masked, and...

Fixed odds and ends,  and made convert_stream_to_masked, and _convert_trace_to_int non destrutive calls
parent caf1c02f
No related branches found
No related tags found
No related merge requests found
...@@ -79,7 +79,6 @@ class EdgeFactory(TimeseriesFactory): ...@@ -79,7 +79,6 @@ class EdgeFactory(TimeseriesFactory):
self.port = port self.port = port
self.cwbhost = cwbhost or '' self.cwbhost = cwbhost or ''
self.cwbport = cwbport self.cwbport = cwbport
self.ric = None
def get_timeseries(self, starttime, endtime, observatory=None, def get_timeseries(self, starttime, endtime, observatory=None,
channels=None, type=None, interval=None): channels=None, type=None, interval=None):
...@@ -159,7 +158,7 @@ class EdgeFactory(TimeseriesFactory): ...@@ -159,7 +158,7 @@ class EdgeFactory(TimeseriesFactory):
type = type or self.type or stats.data_type type = type or self.type or stats.data_type
interval = interval or self.interval or stats.data_interval interval = interval or self.interval or stats.data_interval
if (starttime == None or endtime == None): if (starttime is None or endtime is None):
starttime, endtime = self._get_stream_start_end_times(timeseries) starttime, endtime = self._get_stream_start_end_times(timeseries)
for channel in channels: for channel in channels:
...@@ -168,8 +167,6 @@ class EdgeFactory(TimeseriesFactory): ...@@ -168,8 +167,6 @@ class EdgeFactory(TimeseriesFactory):
'Missing channel "%s" for output' % channel) 'Missing channel "%s" for output' % channel)
for channel in channels: for channel in channels:
self._convert_stream_to_masked(timeseries=timeseries,
channel=channel)
self._put_channel(timeseries, observatory, channel, type, self._put_channel(timeseries, observatory, channel, type,
interval, starttime, endtime) interval, starttime, endtime)
...@@ -206,34 +203,44 @@ class EdgeFactory(TimeseriesFactory): ...@@ -206,34 +203,44 @@ class EdgeFactory(TimeseriesFactory):
numpy.full(cnt, numpy.nan, dtype=numpy.float64)]) numpy.full(cnt, numpy.nan, dtype=numpy.float64)])
trace.stats.endttime = endtime trace.stats.endttime = endtime
def _convert_trace_to_decimal(self, stream): def _convert_timeseries_to_decimal(self, stream):
"""convert geomag edge traces stored as ints, to decimal by dividing """convert geomag edge timeseries data stored as ints, to decimal by
by 1000.00 dividing by 1000.00
Parameters Parameters
---------- ----------
stream : obspy.core.stream stream : obspy.core.stream
a stream retrieved from a geomag edge representing one channel. a stream retrieved from a geomag edge representing one channel.
Notes
-----
This routine changes the values in the timeseries. The user should
make a copy before calling if they don't want that side effect.
""" """
for trace in stream: for trace in stream:
trace.data = numpy.divide(trace.data, 1000.00) trace.data = numpy.divide(trace.data, 1000.00)
def _convert_trace_to_int(self, trace): def _convert_trace_to_int(self, trace_in):
"""convert geomag edge traces stored as decimal, to ints by multiplying """convert geomag edge traces stored as decimal, to ints by multiplying
by 1000 by 1000
Parameters Parameters
---------- ----------
stream : obspy.core.stream trace : obspy.core.trace
a stream retrieved from a geomag edge representing one channel. a trace retrieved from a geomag edge representing one channel.
Returns
-------
obspy.core.trace
a trace converted to ints
Notes Notes
----- -----
this doesn't work on ndarray with nan's in it. this doesn't work on ndarray with nan's in it.
the trace must be a masked array. the trace must be a masked array.
""" """
trace = trace_in.copy()
trace.data = numpy.multiply(trace.data, 1000.00) trace.data = numpy.multiply(trace.data, 1000.00)
trace.data = trace.data.astype(int) trace.data = trace.data.astype(int)
return trace
def _convert_stream_to_masked(self, timeseries, channel): def _convert_stream_to_masked(self, timeseries, channel):
"""convert geomag edge traces in a timeseries stream to a MaskedArray """convert geomag edge traces in a timeseries stream to a MaskedArray
This allows for gaps and splitting. This allows for gaps and splitting.
...@@ -243,9 +250,15 @@ class EdgeFactory(TimeseriesFactory): ...@@ -243,9 +250,15 @@ class EdgeFactory(TimeseriesFactory):
a stream retrieved from a geomag edge representing one channel. a stream retrieved from a geomag edge representing one channel.
channel: string channel: string
the channel to be masked. the channel to be masked.
Returns
-------
obspy.core.stream
a stream with all traces converted to masked arrays.
""" """
for trace in timeseries.select(channel=channel): stream = timeseries.copy()
for trace in stream.select(channel=channel):
trace.data = numpy.ma.masked_invalid(trace.data) trace.data = numpy.ma.masked_invalid(trace.data)
return stream
def _create_missing_channel(self, starttime, endtime, observatory, def _create_missing_channel(self, starttime, endtime, observatory,
channel, type, interval, network, station, location): channel, type, interval, network, station, location):
...@@ -285,11 +298,11 @@ class EdgeFactory(TimeseriesFactory): ...@@ -285,11 +298,11 @@ class EdgeFactory(TimeseriesFactory):
if interval == 'second': if interval == 'second':
stats.sampling_rate = 1. stats.sampling_rate = 1.
elif interval == 'minute': elif interval == 'minute':
stats.sampling_rate = 1 / 60. stats.sampling_rate = 1. / 60.
elif interval == 'hourly': elif interval == 'hourly':
stats.sampling_rate = 1 / 3600 stats.sampling_rate = 1. / 3600.
elif interval == 'daily': elif interval == 'daily':
stats.sampling_rate = 1 / 86400 stats.sampling_rate = 1. / 86400.
length = int((endtime - starttime) * stats.sampling_rate) length = int((endtime - starttime) * stats.sampling_rate)
stats.npts = length + 1 stats.npts = length + 1
...@@ -530,7 +543,7 @@ class EdgeFactory(TimeseriesFactory): ...@@ -530,7 +543,7 @@ class EdgeFactory(TimeseriesFactory):
Notes: the original timeseries object is changed. Notes: the original timeseries object is changed.
""" """
self._convert_trace_to_decimal(timeseries) self._convert_timeseries_to_decimal(timeseries)
for trace in timeseries: for trace in timeseries:
if isinstance(trace.data, numpy.ma.MaskedArray): if isinstance(trace.data, numpy.ma.MaskedArray):
trace.data.set_fill_value(numpy.nan) trace.data.set_fill_value(numpy.nan)
...@@ -583,19 +596,22 @@ class EdgeFactory(TimeseriesFactory): ...@@ -583,19 +596,22 @@ class EdgeFactory(TimeseriesFactory):
host = self.host host = self.host
port = self.port port = self.port
self.ric = RawInputClient(self.tag, host, port, station, ric = RawInputClient(self.tag, host, port, station,
edge_channel, location, network) edge_channel, location, network)
for trace in timeseries.select(channel=channel).split(): stream = self._convert_stream_to_masked(timeseries=timeseries,
channel=channel)
for trace in stream.select(channel=channel).split():
trace_send = trace.copy() trace_send = trace.copy()
trace_send.trim(starttime, endtime) trace_send.trim(starttime, endtime)
if channel == 'D': if channel == 'D':
trace_send.data = ChannelConverter.get_minutes_from_radians( trace_send.data = ChannelConverter.get_minutes_from_radians(
trace_send.data) trace_send.data)
self._convert_trace_to_int(trace_send) trace_send = self._convert_trace_to_int(trace_send)
self.ric.send_trace(interval, trace_send) ric.send_trace(interval, trace_send)
self.ric.forceout() ric.forceout()
self.ric.close() ric.close()
def _set_metadata(self, stream, observatory, channel, type, interval): def _set_metadata(self, stream, observatory, channel, type, interval):
"""set metadata for a given stream/channel """set metadata for a given stream/channel
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment