Skip to content
Snippets Groups Projects
Commit 65436427 authored by Jeremy M Fee's avatar Jeremy M Fee
Browse files

Add get_previous_interval method and test

parent 6355305d
No related branches found
No related tags found
1 merge request!186update interval no longer shrinks with recursion
...@@ -426,11 +426,10 @@ class Controller(object): ...@@ -426,11 +426,10 @@ class Controller(object):
# check for fillable gap at start # check for fillable gap at start
if output_gap[0] == starttime: if output_gap[0] == starttime:
# found fillable gap at start, recurse to previous interval # found fillable gap at start, recurse to previous interval
delta = TimeseriesUtility.get_delta_from_interval(output_interval) recurse_starttime, recurse_endtime = get_previous_interval(
recurse_starttime = ( start=starttime,
starttime - (endtime - starttime) - delta * bool(update_count) end=endtime,
) )
recurse_endtime = starttime - delta
self.run_as_update( self.run_as_update(
algorithm=algorithm, algorithm=algorithm,
observatory=observatory, observatory=observatory,
...@@ -638,6 +637,29 @@ def get_output_factory(args): ...@@ -638,6 +637,29 @@ def get_output_factory(args):
return output_factory return output_factory
def get_previous_interval(
start: UTCDateTime,
end: UTCDateTime,
) -> Tuple[UTCDateTime, UTCDateTime]:
"""Get previous interval for given interval.
Parameters
----------
start
start of interval
end
end of interval
Returns
-------
Previous interval of approximately the same size.
Interval is rounded to nearest second, and ends one microsecond earlier.
"""
# round to nearest second to recover removed microsecond from repeated calls
interval_size = round(end - start)
return (start - interval_size, start - 1e-6)
def get_realtime_interval(interval_seconds: int) -> Tuple[UTCDateTime, UTCDateTime]: def get_realtime_interval(interval_seconds: int) -> Tuple[UTCDateTime, UTCDateTime]:
# calculate endtime/starttime # calculate endtime/starttime
now = UTCDateTime() now = UTCDateTime()
......
...@@ -6,7 +6,7 @@ from geomagio.algorithm import Algorithm ...@@ -6,7 +6,7 @@ from geomagio.algorithm import Algorithm
from geomagio.iaga2002 import IAGA2002Factory from geomagio.iaga2002 import IAGA2002Factory
# needed to emulate geomag.py script # needed to emulate geomag.py script
from geomagio.Controller import _main, parse_args from geomagio.Controller import _main, get_previous_interval, parse_args
# needed to copy SqDistAlgorithm statefile # needed to copy SqDistAlgorithm statefile
from shutil import copy from shutil import copy
...@@ -290,3 +290,23 @@ def test_controller_update_sqdist(): ...@@ -290,3 +290,23 @@ def test_controller_update_sqdist():
) )
expected = expected_factory.get_timeseries(starttime=starttime1, endtime=endtime6) expected = expected_factory.get_timeseries(starttime=starttime1, endtime=endtime6)
assert_allclose(actual, expected) assert_allclose(actual, expected)
def test_get_previous_interval():
"""Test get_previous_interval produces stable interval sizes"""
# initial interval is one hour
start = UTCDateTime("2022-01-05T00:00:00Z")
end = UTCDateTime("2022-01-05T01:00:00Z")
# previous interval starts at beginning of previous hour
previous = get_previous_interval(start=start, end=end)
assert previous == (
UTCDateTime("2022-01-04T23:00:00"),
UTCDateTime("2022-01-04T23:59:59.999999Z"),
)
# previous interval still starts at beginning of previous hour
# even though interval is one microsecond smaller
previous = get_previous_interval(*previous)
assert previous == (
UTCDateTime("2022-01-04T22:00:00"),
UTCDateTime("2022-01-04T22:59:59.999999Z"),
)
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