Skip to content

Instantly share code, notes, and snippets.

@glopesdev
Created May 12, 2021 02:41
Show Gist options
  • Select an option

  • Save glopesdev/140c39281953584c4d53fbfb03777c1b to your computer and use it in GitHub Desktop.

Select an option

Save glopesdev/140c39281953584c4d53fbfb03777c1b to your computer and use it in GitHub Desktop.
Short utility functions for loading HARP files
import numpy as np
import pandas as pd
payloadtypes = {
1 : np.dtype(np.uint8),
2 : np.dtype(np.uint16),
4 : np.dtype(np.uint32),
8 : np.dtype(np.uint64),
129 : np.dtype(np.int8),
130 : np.dtype(np.int16),
132 : np.dtype(np.int32),
136 : np.dtype(np.int64),
68 : np.dtype(np.float32)
}
def read(file, names=None):
'''
Read single-register Harp data from the specified file.
:param str file: The path to a Harp binary file containing data from a single device register.
:param str or array-like names: The optional column labels to use for the data values.
:return: A pandas data frame containing harp event data, sorted by time.
'''
data = np.fromfile(file, dtype=np.uint8)
stride = data[1] + 2
length = len(data) // stride
payloadsize = stride - 12
payloadtype = payloadtypes[data[4] & ~0x10]
elementsize = payloadtype.itemsize
payloadshape = (length, payloadsize // elementsize)
seconds = np.ndarray(length, dtype=np.uint32, buffer=data, offset=5, strides=stride)
micros = np.ndarray(length, dtype=np.uint16, buffer=data, offset=9, strides=stride)
seconds = micros * 32e-6 + seconds
payload = np.ndarray(
payloadshape,
dtype=payloadtype,
buffer=data, offset=11,
strides=(stride, elementsize))
time = pd.Series(seconds)
time.name = 'time'
return pd.DataFrame(payload, index=time, columns=names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment