Skip to content
Snippets Groups Projects
Commit 18cece6d authored by Erin (Josh) Rigler's avatar Erin (Josh) Rigler
Browse files

TimeseriesUtility Updates:

- better handle hour and day intervals in create_empty_trace
- enode_stream now handles "STEIM" encoding
- fixed bug in pad_and_trim_trace
parent 114bbd6f
No related branches found
No related tags found
1 merge request!386Overhaul EdgeFactory and MiniSeedFactory for Edge data migration
......@@ -28,7 +28,7 @@ def create_empty_trace(
type : str
data type {definitive, quasi-definitive, variation}
interval : str
interval length {minute, second}
interval length {day, hour, minute, second, tenhertz}
network: str
the network code
station: str
......@@ -49,17 +49,23 @@ def create_empty_trace(
# Calculate first valid sample time based on interval
trace_starttime = UTCDateTime(numpy.ceil(starttime.timestamp / delta) * delta)
if delta > 60.0:
trace_starttime += (delta - 60) / 2
if trace_starttime > endtime:
sys.stderr.write(
"Starttime greater than endtime, adjusting request to one sample"
# hourly and daily average have mid-interval timestamps, minus 30 seconds
if trace_starttime - starttime < (delta + 60) / 2:
trace_starttime += (delta - 60) / 2
else:
trace_starttime += (delta - 60) / 2 - delta
if starttime > trace_starttime or endtime < trace_starttime:
sys.stdout.write(
'No allowed "%s" timestamps between %s and %s, '
% (interval, starttime, endtime)
+ "returning empty trace\n",
)
endtime = trace_starttime
stats.starttime = trace_starttime
stats.delta = delta
# Calculate number of valid samples up to or before endtime
length = int((endtime - trace_starttime) / delta)
stats.npts = length + 1
stats.npts = max(0, length + 1)
data = numpy.full(stats.npts, numpy.nan, dtype=numpy.float64)
return Trace(data, stats)
......@@ -82,7 +88,10 @@ def encode_stream(stream: Stream, encoding: str) -> Stream:
for trace in stream:
trace_out = trace.copy()
if trace_out.data.dtype != encoding:
trace_out.data = trace_out.data.astype(encoding)
if "STEIM" in encoding.upper():
trace_out.data = trace_out.data.astype(numpy.int32)
else:
trace_out.data = trace_out.data.astype(encoding)
if "mseed" in trace_out.stats:
trace_out.stats.mseed.encoding = encoding.upper()
out_stream += trace_out
......@@ -567,8 +576,9 @@ def pad_and_trim_trace(trace, starttime, endtime):
if trace_endtime > endtime:
# trim to endtime, at least 1 sample to remove
cnt = int(math.ceil(round((trace_endtime - endtime) / trace_delta, 6)))
trace.data = trace.data[:-cnt]
trace.stats.npts = len(trace.data)
if cnt > 0:
trace.data = trace.data[:-cnt]
trace.stats.npts = len(trace.data)
elif trace_endtime < endtime:
# pad to endtime
cnt = int(round((endtime - trace_endtime) / trace.stats.delta, 6))
......
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