Skip to content

Commit c0b556d

Browse files
committed
Add single register binary file reader
1 parent e5b430f commit c0b556d

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

harp/io.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
_SECONDS_PER_TICK = 32e6
5+
payloadtypes = {
6+
1 : np.dtype(np.uint8),
7+
2 : np.dtype(np.uint16),
8+
4 : np.dtype(np.uint32),
9+
8 : np.dtype(np.uint64),
10+
129 : np.dtype(np.int8),
11+
130 : np.dtype(np.int16),
12+
132 : np.dtype(np.int32),
13+
136 : np.dtype(np.int64),
14+
68 : np.dtype(np.float32)
15+
}
16+
17+
def read(file: str, columns=None):
18+
'''
19+
Read single-register Harp data from the specified file.
20+
21+
:param str file: The path to a Harp binary file containing data from a single device register.
22+
:param str or array-like names: The optional column labels to use for the data values.
23+
:return: A pandas data frame containing harp event data, sorted by time.
24+
'''
25+
data = np.fromfile(file, dtype=np.uint8)
26+
if len(data) == 0:
27+
return pd.DataFrame(
28+
columns=columns,
29+
index=pd.Index([], dtype=np.float64, name='time'))
30+
31+
stride = data[1] + 2
32+
length = len(data) // stride
33+
payloadsize = stride - 12
34+
payloadtype = payloadtypes[data[4] & ~0x10]
35+
elementsize = payloadtype.itemsize
36+
payloadshape = (length, payloadsize // elementsize)
37+
seconds = np.ndarray(length, dtype=np.uint32, buffer=data, offset=5, strides=stride)
38+
micros = np.ndarray(length, dtype=np.uint16, buffer=data, offset=9, strides=stride)
39+
seconds = micros * _SECONDS_PER_TICK + seconds
40+
payload = np.ndarray(
41+
payloadshape,
42+
dtype=payloadtype,
43+
buffer=data, offset=11,
44+
strides=(stride, elementsize))
45+
time = pd.Series(seconds)
46+
time.name = 'time'
47+
return pd.DataFrame(payload, index=time, columns=columns)

0 commit comments

Comments
 (0)