diff --git a/geomagio/edge/RawInputClient.py b/geomagio/edge/RawInputClient.py index f50a93cd24ab445216232828e6cbacb740235c0b..7259c2798b40805e5534b90c0d8df61281e1f2af 100644 --- a/geomagio/edge/RawInputClient.py +++ b/geomagio/edge/RawInputClient.py @@ -6,6 +6,7 @@ import struct import sys from datetime import datetime from ..TimeseriesFactoryException import TimeseriesFactoryException +import logging from obspy.core import UTCDateTime from time import sleep @@ -425,12 +426,24 @@ class RawInputClient: secs: int usecs: int """ + usecs = time.microsecond / 100000 + # round microseconds to nearest microsecond(whole number) + rounded_usecs = int(round(usecs, 0)) + # add warning to log if residual microsecond values are received + if rounded_usecs != usecs: + logging.warning( + "residual microsecond values encountered, rounding to nearest microsecond" + ) + # in the case that microseconds rounds up, account for next second + if rounded_usecs == 10: + time += 1 + rounded_usecs = 0 + # establish rest of date yr = time.year doy = time.datetime.timetuple().tm_yday secs = time.hour * 3600 + time.minute * 60 + time.second - usecs = time.microsecond - return (yr, doy, secs, usecs) + return (yr, doy, secs, rounded_usecs) def _open_socket(self): """Open a socket diff --git a/test/edge_test/RawInputClient_test.py b/test/edge_test/RawInputClient_test.py index 850f1a7dfff04cc18d0dc45ffc860d334ba36bd8..5996fd79ef0e665b15f59a94088385c3e85b1205 100644 --- a/test/edge_test/RawInputClient_test.py +++ b/test/edge_test/RawInputClient_test.py @@ -1,6 +1,8 @@ """Tests for RawInputClient.py""" import numpy +from datetime import datetime +import logging from obspy.core import Stats, Trace, UTCDateTime from geomagio.edge import EdgeFactory, RawInputClient from numpy.testing import assert_equal @@ -56,7 +58,7 @@ def test_raw_input_client(): def test__get_tag(): - """edge_test.RawInputClient_test.test_raw_input_client()""" + """edge_test.RawInputClient_test.test__get_tag()""" network = "NT" station = "BOU" channel = "MVH" @@ -72,3 +74,36 @@ def test__get_tag(): ) tag_send = client._get_tag() assert_equal(tag_send is not None, True) + + +def test__get_time_values(caplog): + """edge_test.RawInputClient_test.test__get_time_values()""" + network = "NT" + station = "BOU" + channel = "MVH" + location = "R0" + client = MockRawInputClient( + tag="tag", + host="host", + port="port", + station=station, + channel=channel, + location=location, + network=network, + ) + # define expected date from _get_time_values + expected_time = UTCDateTime(2020, 10, 7, 0, 0, 0, 0) + # define date with residual microseconds + residual_time = UTCDateTime(2020, 10, 6, 23, 59, 59, 999999) + r_yr, r_doy, r_secs, r_usecs = client._get_time_values(residual_time) + # check if input microsecond value changes within function + assert_equal( + caplog.text, + "WARNING root:RawInputClient.py:434 residual microsecond values encountered, rounding to nearest microsecond\n", + ) + e_yr, e_doy, e_secs, e_usecs = client._get_time_values(expected_time) + # test if residual result matches expected result + assert_equal(e_yr, r_yr) + assert_equal(e_doy, r_doy) + assert_equal(e_secs, r_secs) + assert_equal(e_usecs, r_usecs)