mf4 to csv conversion in python

10.1k views Asked by At

I have a dataset of mf4 and I want to convert it into CSV in python. I have read the data from mf4 and convert it into csv (as given below) but I am getting an error, as I am new to python, so unable to find an appropriate method to convert.

from asammdf import MDF
import pandas as pd

efficient = MDF('./Data.mf4')

df = efficient.to_csv()


df.append(efficient)

mdf.save('output.csv')

I am getting this error:

MdfException: "Data.mf4" is not a valid ASAM MDF file: magic header is b'timestam'

Kindly suggest. Thank you!

3

There are 3 answers

2
Thuấn Đào Minh On BEST ANSWER

You already have mf4 file. Is this file converted from an mdf file? If it is as I thought, you should use the mdf file to convert it into a CSV file. Here it is anyway and hopefully you or someone else can come up with a better method.

import mdfreader
import os

extension = [".mdf"]   
for root, dirs, files in os.walk(input_path):
    for file in files:
        ext = os.path.splitext(file)[-1].lower()
        f_name = os.path.splitext(file)[0]
        if ext in extension:
            yop=mdfreader.Mdf(os.path.join(root, file))
            yop=mdfreader.Mdf(os.path.join(root, file), channel_list=['channel1', 'channel2'], convert_after_read=False)
            yop=mdfreader.Mdf(os.path.join(root, file), compression=True)
            yop=mdfreader.Mdf(os.path.join(root, file), no_data_loading=True)
            yop=mdfreader.Mdf(os.path.join(root, file), metadata=0)  
            yop.get_channel_name4('channelName', 'source path or name')
            yop.get_channel('channelName')
            yop.get_channel_data('channelName')
            yop.MDFVersionNumber
            info=mdfreader.MdfInfo()
            info.list_channels('NameOfFile') 
            info.read_info('NameOfFile') 
            yop.info 
            yop.keys()
            yop.masterChannelList
            yop.plot(['channel1',['channel2','channel3']])
            yop.resample(0.1)
            yop.resample(master_channel='master3')
            yop.cut(begin=10, end=15)
            yop.export_to_csv(sampling=0.01)
            yop.export_to_NetCDF()
            yop.export_to_hdf5()
            yop.export_to_matlab()
            yop.export_to_xlsx()
            yop.export_to_parquet()
            yop.return_pandas_dataframe('master_channel_name')
            yop.convert_to_pandas()
            yop.keep_channels({'channel1','channel2','channel3'})
            yop2=mdfreader.Mdf('NameOfFile_2')
            yop.merge_mdf(yop2)
            yop.write('NewNameOfFile')
            yop.write4('NameOfFile', compression=True)
            yop.write3()
            yop.attachments 

I used lib mdfreader to do this. You can refer below link for more detail.

mdfreader

Thanks for your feedback. I got it your issue, you can try:

pip install https://github.com/danielhrisca/asammdf/archive/development.zip -I --no-deps 

to install the development branch code and then

mdf = MDF('file.mf4')
mdf.export('csv', 'export.csv', single_time_base=True, raster=0.1) 

About your case (use anaconda). Please try:

conda install https://github.com/danielhrisca/asammdf/archive/development.zip -I --no-deps 

Lib mdfreader. In you want to install it.

conda install mdfreader
3
danielhrisca On

You can use the asammdf package, either by using the API or the GUI.

Install the package and the GUI dependencies:

pip install asammdf[gui]

API usage

from asammdf import MDF
mdf = MDF('input.mf4')
mdf.export(fmt='csv', filename='output.csv')

There are other export options that you can use, please check the documentation https://asammdf.readthedocs.io/en/latest/api.html#asammdf.mdf.MDF.export

GUI

Start the GUI from the python_installation_folder\Scripts\asammdfgui.exe or by running this script

from asammdf.gui.asammdfgui import main
main()

enter image description here

0
Ranjeet R Patil On

The ASAM MDF (Measurement Data Format) is a binary file format for recording e.g. CAN, CAN FD, and LIN bus data. Today, the MDF format is the industry standard - ensuring interoperability across many CAN tools.

The CANedge supports the latest MF4 file format - and below we list benefits, basics, and software/API tools. We also provide sample MF4 data (J1939, OBD2).

from asammdf import MDF, Signal #import asammdf
f_in = input ("Enter Input MDF4 FIle :") 
mdf = MDF(f_in) #read mf4 data
f_out = f_in.replace(".MF4", ".csv")
mdf.export(fmt='csv', filename= f_out) #write to csv

MDF4 files are comprised of "blocks" - most of which are in practice arbitrarily ordered. Via a system of internal "links", these blocks form a hierarchy.

This structure enables more flexible log files vs. e.g. CSV - ideal for e.g. mixing CAN, CAN FD & LIN, or supporting both raw/physical data. enter image description here

Source: https://www.csselectronics.com/pages/mf4-mdf4-measurement-data-format