Skip to content

Commit

Permalink
Examples: Add HDF5 example
Browse files Browse the repository at this point in the history
  • Loading branch information
sanssecours committed Sep 9, 2024
1 parent ec8a329 commit 58eafb6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions Documentation/API/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ For code examples, please check out the `examples directory <https://github.com/

- `Read STH Name <https://github.com/MyTooliT/ICOc/tree/master/mytoolit/examples/sth_name.py>`_: Read the name of the „first“ available STH (device id `0`).
- `Read Data Points <https://github.com/MyTooliT/ICOc/tree/master/mytoolit/examples/read_data.py>`_: Read five acceleration messages (5 · 3 = 15 values) and print their string representation (value, timestamp and message counter)
- `Store Data as HDF5 file <https://github.com/MyTooliT/ICOc/tree/master/mytoolit/examples/store_data.py>`_: Read five seconds of acceleration data and store it as HDF5 file
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pytest-test-no-hardware:
--ignore-glob='*commander.py' \
--ignore-glob='*read_data.t' \
--ignore-glob='*sth_name.t' \
--ignore-glob='*store_data.t' \
--ignore-glob='*measure.t' \
--ignore='Documentation'

Expand Down
21 changes: 21 additions & 0 deletions Test/store_data.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Setup -----------------------------------------------------------------------

$ cd "$TESTDIR"
$ EXAMPLEDIR=../mytoolit/examples

-- Check Read Data Example -----------------------------------------------------

Read and store data for five seconds

$ python $EXAMPLEDIR/store_data.py

The file should approximately store 47620 (9524 · 5) values

$ h5dump -d acceleration -H test.hdf5 |
> grep 'DATASPACE SIMPLE' |
> sed -E 's/.*DATASPACE SIMPLE \{ \( ([0-9]+) .*/\1/'
4[5-9]\d{3} (re)

-- Cleanup ---------------------------------------------------------------------

$ rm test.hdf5
48 changes: 48 additions & 0 deletions mytoolit/examples/store_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Read and store some acceleration data of STH with device name Test-STH"""

# -- Imports ------------------------------------------------------------------

from asyncio import run
from functools import partial
from pathlib import Path
from time import time

from mytoolit.can import Network
from mytoolit.can.streaming import StreamingConfiguration
from mytoolit.measurement import convert_raw_to_g, Storage

# -- Functions ----------------------------------------------------------------


async def store_streaming_data(identifier):
async with Network() as network:
await network.connect_sensor_device(identifier)

sensor_range = await network.read_acceleration_sensor_range_in_g()
conversion_to_g = partial(convert_raw_to_g, max_value=sensor_range)

# Read data for five seconds
start = time()
end = start + 5
filepath = Path("test.hdf5")
stream_first = StreamingConfiguration(first=True)

with Storage(filepath, channels=stream_first) as storage:
# Store acceleration range as metadata
storage.add_acceleration_meta(
"Sensor_Range", f"± {sensor_range / 2} g₀"
)
async with network.open_data_stream(first=True) as stream:
async for data, _ in stream:
# Convert from ADC bit value into multiples of g
data.apply(conversion_to_g)
# Store in data file
storage.add_streaming_data(data)
if time() > end:
break


# -- Main ---------------------------------------------------------------------

if __name__ == "__main__":
run(store_streaming_data(identifier="Test-STH"))

0 comments on commit 58eafb6

Please sign in to comment.