Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding start times to nearest sample rather than previous sample #75

Open
ebeauce opened this issue May 10, 2022 · 0 comments
Open

Rounding start times to nearest sample rather than previous sample #75

ebeauce opened this issue May 10, 2022 · 0 comments

Comments

@ebeauce
Copy link

ebeauce commented May 10, 2022

Hi,

For my applications, I need sample precision when I extract subsets of the data at a given time using the get_waveforms method of an ASDFDataSet instance. However, I observed discrepancies of up to 1 sample between the start time I requested and the start time that was returned.

I tracked the issue down to _get_idx_and_size_estimate of asdf_data_set.py. The line:

offset = max(0, int((starttime - data_starttime) // dt ))

produces an undesirable output when (starttime - data_starttime) / dt = X + 0.999999. Instead of returning offset = X + 1 samples, it returns offset = X samples due to the floating point number imprecision. This happens of course because of the behavior of int, which rounds to the nearest lower integer number.

A solution that works for me is to add a number a < 1 to (starttime - data_starttime) / dt before converting it to an integer. If this number is a = 0.5, the following line:

offset = max(0, int((starttime - data_starttime) / dt + 0.5))

actually rounds the time to the nearest (lower or upper) integer number. See this commit: ebeauce@4437051

I guess that some people would want to be able to always round to the nearest upper or the nearest lower integer, so it may be worth adding a key-word argument to get_waveforms similarly to the slice method of Obspy Stream. My opinion is that rounding to the nearest sample would be the preferable default behavior.

Thank you,
Eric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant