Skip to content
Snippets Groups Projects
Commit 8196df8e authored by Cain, Payton David's avatar Cain, Payton David
Browse files

split streams by interval

parent 64c1940d
No related branches found
No related tags found
2 merge requests!146Release CMO metadata to production,!82Add _pre_process method to MiniSeedInputClient
...@@ -568,3 +568,48 @@ def round_usecs(time): ...@@ -568,3 +568,48 @@ def round_usecs(time):
if rounded_usecs != usecs: if rounded_usecs != usecs:
time = time.replace(microsecond=rounded_usecs) time = time.replace(microsecond=rounded_usecs)
return time return time
def split_streams_by_interval(stream, interval):
"""Splits streams by interval
Parameters:
-----------
stream: obspy.core.Stream
stream of input data
interval: int
interval streams will be split by
Returns:
--------
out_streams: List[obspy.core.Stream]
list of streams split by interval
"""
delta = stream[0].stats.delta
interval_start = stream[0].stats.starttime.timestamp
times = stream[0].times("timestamp")
interval_ends = times[times % interval == 0] - delta
if not interval_ends.any():
return [stream]
out_streams = []
for interval_end in interval_ends:
# should only occur with stream starttime occuring at midnight
if interval_end == interval_start - delta:
continue
stream_copy = stream.copy()
pad_timeseries(
timeseries=stream_copy,
starttime=obspy.core.UTCDateTime(interval_start),
endtime=obspy.core.UTCDateTime(interval_end),
)
out_streams.append(stream_copy)
interval_start = interval_end + delta
# last trim will extend from interval_start to the end of the original input stream
pad_timeseries(
stream,
starttime=obspy.core.UTCDateTime(interval_start),
endtime=stream[0].stats.endtime,
)
out_streams.append(stream)
return out_streams
...@@ -3,6 +3,8 @@ import io ...@@ -3,6 +3,8 @@ import io
import socket import socket
import sys import sys
from ..TimeseriesUtility import split_streams_by_interval
class MiniSeedInputClient(object): class MiniSeedInputClient(object):
"""Client to write MiniSeed formatted data to Edge. """Client to write MiniSeed formatted data to Edge.
...@@ -74,12 +76,43 @@ class MiniSeedInputClient(object): ...@@ -74,12 +76,43 @@ class MiniSeedInputClient(object):
self.connect() self.connect()
# convert stream to miniseed # convert stream to miniseed
buf = io.BytesIO() buf = io.BytesIO()
stream = self._pre_process(stream) streams = self._pre_process(stream)
stream.write(buf, encoding=self.encoding, format="MSEED", reclen=512) for stream in streams:
stream.write(buf, format="MSEED", reclen=512)
# send data # send data
self.socket.sendall(buf.getvalue()) self.socket.sendall(buf.getvalue())
def _pre_process(self, stream): def _pre_process(self, stream):
"""Encodes and splits streams at daily intervals
Paramters:
----------
stream: obspy.core.stream
stream of input data
Returns:
--------
streams: List[obspy.core.stream]
list of encoded streams split at daily intervals
"""
stream = self.__encode_stream(stream)
streams = split_streams_by_interval(stream, interval=86400)
return streams
def __encode_stream(self, stream):
"""Ensures that factory encoding matches output data encoding
Parameters:
-----------
stream: obspy.core.Stream
stream of input data
Returns:
--------
stream: obspy.core.Stream
stream with matching data encoding to factory specification
"""
for trace in stream: for trace in stream:
if trace.data.dtype != self.encoding: if trace.data.dtype != self.encoding:
trace.data = trace.data.astype(self.encoding) trace.data = trace.data.astype(self.encoding)
......
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