Writing data from scratch

When your data is not based on existing data, you will have to create a metadata object yourself.

import numpy as np
import tsdf

data_dir = "data"
# Generate random data
rs = np.random.RandomState(seed=42)
data_1 = rs.rand(17, 1).astype(np.float32)
data_2 = rs.rand(15, 2).astype(np.int16)
data_3 = rs.rand(10, 3).astype(np.int16)

# Define the metadata
basic_metadata = {
    "subject_id": "example",
    "study_id": "example",
    "device_id": "example",
    "endianness": "little",
    "metadata_version": "0.1",
    "start_datetime_unix_ms": 1571135957025,
    "start_iso8601": "2019-10-15T10:39:17.025000+00:00",
    "end_datetime_unix_ms": 1571168851826,
    "end_iso8601": "2019-10-15T19:47:31.826000+00:00",
    "channels": ["x", "y", "z"],
    "units": ["m/s/s", "m/s/s", "m/s/s"]
}

# Write the three binary files based on the provided metadata.
# The new_meta variables will contain the basic_metadata, combined
# with the fields derived from the binary data (data type, bit depth, etc.)
file_prefix = "tmp_test"
new_meta_1 = tsdf.write_binary_file(data_dir, f"{file_prefix}_1.bin", data_1, basic_metadata)
new_meta_2 = tsdf.write_binary_file(data_dir, f"{file_prefix}_2.bin", data_2, basic_metadata)
new_meta_3 = tsdf.write_binary_file(data_dir, f"{file_prefix}_3.bin", data_3, basic_metadata)

# Write the metadata file, which combines the metadata, in turn referencing the three binary files
tsdf.write_metadata([new_meta_1, new_meta_2, new_meta_3], f"{file_prefix}_meta.json")
print(f"Files written to {data_dir}/")
Files written to data/