import mf4_rs
import math
def main():
try:
writer = mf4_rs.PyMdfWriter("example.mf4")
print("Created MDF writer")
writer.init_mdf_file()
print("Initialized MDF file")
group_id = writer.add_channel_group("Test Group")
print(f"Added channel group: {group_id}")
time_ch_id = writer.add_time_channel(group_id, "Time")
print(f"Added time channel: {time_ch_id} (automatic: FloatLE 32-bit, master channel)")
temp_ch_id = writer.add_float_channel(group_id, "Temperature")
print(f"Added temperature channel: {temp_ch_id} (automatic: FloatLE 32-bit, linked)")
speed_ch_id = writer.add_int_channel(group_id, "Speed")
print(f"Added speed channel: {speed_ch_id} (automatic: UnsignedIntegerLE 32-bit, linked)")
writer.start_data_block(group_id)
print("Started data block")
print("Writing data records...")
for i in range(100):
time = i * 0.01 temperature = 20.0 + 5.0 * math.sin(time * 2.0) speed = int(math.sin(time * 10.0) * 50.0 + 60.0)
values = [
mf4_rs.create_float_value(time),
mf4_rs.create_float_value(temperature),
mf4_rs.create_uint_value(speed)
]
writer.write_record(group_id, values)
if i % 20 == 0:
print(f" Wrote record {i}: time={time:.3f}, temp={temperature:.2f}, speed={speed}")
writer.finish_data_block(group_id)
print("Finished data block")
writer.finalize()
print("MDF file 'example.mf4' created successfully!")
print("\nTesting the created file:")
test_read_created_file()
except mf4_rs.MdfException as e:
print(f"MDF Error: {e}")
except Exception as e:
print(f"Error: {e}")
def test_read_created_file():
try:
mdf = mf4_rs.PyMDF("example.mf4")
groups = mdf.channel_groups()
print(f"Created file has {len(groups)} channel groups")
if groups:
group = groups[0]
print(f"Group has {group.channel_count} channels and {group.record_count} records")
names = mdf.get_all_channel_names()
print(f"Channel names: {names}")
for name in names[:3]: values = mdf.get_channel_values(name)
if values and len(values) > 0:
print(f"Channel '{name}': first value = {values[0]}, last value = {values[-1]}")
except Exception as e:
print(f"Error reading created file: {e}")
if __name__ == "__main__":
main()