diff --git a/geomagio/algorithm/FilterAlgorithm.py b/geomagio/algorithm/FilterAlgorithm.py index 6cd104487ae05b0f5a0aa69ce3ca8fdd3930209b..48763ee17a5a548ff37683d0fef54ddaa0520d21 100644 --- a/geomagio/algorithm/FilterAlgorithm.py +++ b/geomagio/algorithm/FilterAlgorithm.py @@ -64,24 +64,29 @@ STEPS = [ def get_nearest_time(step, output_time, left=True): + + # nearest allowed timestamp "left" of output_time interval_start = output_time - ( output_time.timestamp % step["output_sample_period"] ) - # shift interval right if needed - if interval_start != output_time and not left: - interval_start += step["output_sample_period"] - # position center of filter, data around interval + half_width = get_step_time_shift(step) + if step["type"] == "average": - filter_center = interval_start + half_width - data_start = interval_start - data_end = (interval_start + step["output_sample_period"]) - step[ - "input_sample_period" - ] + interval_start += half_width + + # shift interval if needed + if left: + if interval_start > output_time: + interval_start -= step["output_sample_period"] else: - filter_center = interval_start - data_start = filter_center - half_width - data_end = filter_center + half_width + if interval_start < output_time: + interval_start += step["output_sample_period"] + + filter_center = interval_start + data_start = filter_center - half_width + data_end = filter_center + half_width + return { "time": filter_center, "data_start": data_start,