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

Save 'arm_time', 'start_time' and 'hw_time_offset_ns' to file #81

Merged
merged 17 commits into from
Mar 15, 2024

Conversation

dmgav
Copy link
Contributor

@dmgav dmgav commented Feb 29, 2024

Saving the timing parameters arm_time, start_time and hw_time_offset_ns passed as part of the header from PandA Box to IOC in as root attributes of the HDF5 file.

Issue: #80

@dmgav
Copy link
Contributor Author

dmgav commented Feb 29, 2024

This is initial working implementation of the code. Missing parameters are assigned default value "" (empty string). The code was initially tested on PandA with standard v3.0 firmware, which records NTP timestamp. The parameter hw_time_offset_ns is missing in the header and has default value of "". The timestamps received from PandA are saved as strings without modification (in the same format). The following attributes were saved to the HDF5 file:

$ h5dump panda_rbdata_20240229_143311.h5
HDF5 "panda_rbdata_20240229_143311.h5" {
GROUP "/" {
   ATTRIBUTE "arm_time" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_UTF8;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "2024-02-29T19:33:11.212Z"
      }
   }
   ATTRIBUTE "hw_time_offset_ns" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_UTF8;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): ""
      }
   }
   ATTRIBUTE "start_time" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_UTF8;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "2024-02-29T19:33:11.212Z"
      }
   }

@dmgav
Copy link
Contributor Author

dmgav commented Mar 1, 2024

The result of testing with Clock Source set as sfp3_recovered_clock and Timestamp Source to sfp3:

image

$ h5dump panda_rbdata_20240305_152712.h5
HDF5 "panda_rbdata_20240305_152712.h5" {
GROUP "/" {
   ATTRIBUTE "arm_time" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_UTF8;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "2024-03-05T20:27:12.607841574Z"
      }
   }
   ATTRIBUTE "hw_time_offset_ns" {
      DATATYPE  H5T_STD_I64LE
      DATASPACE  SCALAR
      DATA {
      (0): 4002155797
      }
   }
   ATTRIBUTE "start_time" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_UTF8;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "2024-03-05T20:27:08.605729480Z"
      }
   }

@dmgav
Copy link
Contributor Author

dmgav commented Mar 1, 2024

@coretl Do you think we need to convert hw_time_offset_ns to the same time format? Or we can leave it as integer?

@coretl
Copy link
Contributor

coretl commented Mar 4, 2024

@coretl Do you think we need to convert hw_time_offset_ns to the same time format? Or we can leave it as integer?

It is an int64 with units of nanoseconds so probably best to store it in the HDF file as such. This is only used for debugging really...

@dmgav
Copy link
Contributor Author

dmgav commented Mar 4, 2024

Yes, I agree with keeping hw_time_offset_ns as int64 (it is saved as a string). But my understanding is that it is still going to be used for time computation (start_time + hw_time_offset_ns). But this depends on how the firmware is implemented and it is not important for the IOC unless IOC is responsible for computing the absolute time.

@coretl
Copy link
Contributor

coretl commented Mar 5, 2024

At the moment the as yet unmerged docs from PandABlocks/PandABlocks-server#45 read:

name description
arm_time System timestamp when ARM command was sent
start_time Timestamp of when PCAP became both armed and enabled
hw_time_offset_ns Offset in ns from hardware timestamp to captured system time, only present when hardware time source is used.

This means that if hw_time_offset_ns is present, start_time will be the hardware time, and start_time + hw_time_offset_ns will give you the system timestamp. This means that start_time should always be used for timestamp computation, whether its source is hardware or software, and hw_time_offset_ns is only used for debugging to work out how close those clocks are.

I will suggest some different words for PandABlocks/PandABlocks-server#45 to try and make that clearer, please comment there if you can think of some improvements for the docs too.

Yes, I agree with keeping hw_time_offset_ns as int64 (it is saved as a string).

Will you put it as an int64 attribute in the HDF file then? I'm not sure what datatypes HDF attributes support...

@dmgav
Copy link
Contributor Author

dmgav commented Mar 5, 2024

I changed the type of hw_time_offset_ns to H5T_STD_I64LE. It is set to zero if there is no hardware timestamps (hw_time_offset_ns is not in the header) and to -1 if the string value in the header can not be interpreted as int64. I guess the latter case should never happen unless there is a serious bug in Panda firmware.

@dmgav dmgav force-pushed the abs-ts branch 3 times, most recently from 8a0919e to f663798 Compare March 10, 2024 19:25
@dmgav dmgav marked this pull request as ready for review March 10, 2024 19:38
tests/conftest.py Outdated Show resolved Hide resolved
Copy link
Contributor

@evalott100 evalott100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one type annotation

src/pandablocks/responses.py Outdated Show resolved Hide resolved
@evalott100
Copy link
Contributor

evalott100 commented Mar 11, 2024

CI won't work until #78 is merged.

Tests pass locally.

Comment on lines 108 to 125
import h5py
import pandas as pd

with h5py.File('data/panda_rbdata_20240305_152712.h5', 'r') as f:
arm_time = f.attrs['arm_time']
start_time = f.attrs['start_time']
hw_time_offset_ns = f.attrs['hw_time_offset_ns']

print(f'Arm time: {arm_time!r}')
print(f'Start time: {start_time!r}')
print(f'Hardware time offset: {hw_time_offset_ns!r} ns')

if start_time:
# Compute and print the start time that includes the offset
ts_start = pd.Timestamp(start_time)
if hw_time_offset_ns > 0:
ts_start += pd.Timedelta(nanoseconds=hw_time_offset_ns)
print(f'Start TS including the offset: {ts_start}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you put snippets of Python into the examples/ directory and include their contents here so they can be easily tested?

E.g.:
https://github.com/PandABlocks/PandABlocks-client/blob/master/docs/user/how-to/library-hdf.rst

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to the documentation were implemented. The example code code was moved to examples/load_abs_timestamps.py.

src/pandablocks/responses.py Outdated Show resolved Hide resolved
src/pandablocks/hdf.py Outdated Show resolved Hide resolved
src/pandablocks/connections.py Outdated Show resolved Hide resolved
src/pandablocks/connections.py Outdated Show resolved Hide resolved
@dmgav
Copy link
Contributor Author

dmgav commented Mar 11, 2024

I tested the package with Panda IOC and Panda again to make sure it works as expected.

@dmgav
Copy link
Contributor Author

dmgav commented Mar 11, 2024

I had an impression that hw_time_offset_ns is always positive or zero, but additional testing showed that it can be negative indeed.

Copy link
Contributor

@coretl coretl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one clarification to the text, start_time will be hardware if available, falling back to software if not. Suggested some changes to make this clear

docs/user/tutorials/commandline-hdf.rst Outdated Show resolved Hide resolved
docs/user/tutorials/commandline-hdf.rst Outdated Show resolved Hide resolved
examples/load_abs_timestamps.py Outdated Show resolved Hide resolved
@dmgav
Copy link
Contributor Author

dmgav commented Mar 14, 2024

It looks like the PR is ready to be merged.

Copy link

codecov bot commented Mar 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.90%. Comparing base (e6f8b67) to head (8d95812).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #81      +/-   ##
==========================================
+ Coverage   96.86%   96.90%   +0.03%     
==========================================
  Files          12       12              
  Lines        1181     1194      +13     
==========================================
+ Hits         1144     1157      +13     
  Misses         37       37              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@evalott100 evalott100 merged commit 9c01c18 into PandABlocks:master Mar 15, 2024
14 checks passed
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

Successfully merging this pull request may close these issues.

3 participants