danielhrisca / asammdf

Fast Python reader and editor for ASAM MDF / MF4 (Measurement Data Format) files
GNU Lesser General Public License v3.0
648 stars 226 forks source link

Access to header labels on MDF v4.1 file #78

Closed fizcris closed 6 years ago

fizcris commented 6 years ago

Python version

Please run the following snippet and write the output here


('python=3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 16:13:55) '
 '[MSC v.1900 64 bit (AMD64)]')
'os=Windows-7-6.1.7601-SP1'
'numpy=1.14.2'

Code

MDF version

'asammdf=4.0.0.dev'

Description

Cannot find a way to modify the header properties (Author, Organisation, Project, Subject) of MDF V4.1 files (picture attached). Because target properties are not in the header as in MDF V3 files.

Extract from v3 file header:

{'id': b'HD', 'block_len': 164, 'first_dg_addr': 0, 'comment_addr': 0, 'program_addr': 0, 'dg_nr': 0, 'date': b'30:05:2018', 'time': b'10:16:17', 'author': b'AL\x00', 'organization': b'\x00', 'project': b'\x00\', 'subject': b'\x00'}

Extract from v4.1 file header:

{'id': b'##HD', 'reserved0': 0, 'block_len': 104, 'links_nr': 6, 'first_dg_addr': 0, 'file_history_addr': 0, 'channel_tree_addr': 0, 'first_attachment_addr': 0, 'first_event_addr': 0, 'comment_addr': 0, 'abs_time': 1527675300087600896, 'tz_offset': 0, 'daylight_save_time': 0, 'time_flags': 2, 'time_quality': 0, 'flags': 0, 'reserved1': 0, 'start_angle': 0, 'start_distance': 0}

image

danielhrisca commented 6 years ago

Hello Cristian,

this info is stored as XML in the header's comment. Please try the latest development code and see if it works. You should access the author, department, project and subject attributes of the header object.

fizcris commented 6 years ago

I am testing it with a file stacked from a v3.0 and v4.1 with output v4.10 and it is not working. I cannont access the set of atributes of the header object, neither they are being represented in the header dictionary.

Kind regards.

danielhrisca commented 6 years ago

This works for me

from asammdf import MDF, Signal

s = Signal(
    samples=[],
    timestamps=[],
    name='s1',
    )

mdf = MDF(version='4.10')

mdf.append([s])

mdf.header.author = 'me'
mdf.header.project = 'asammdf'
mdf.header.department = 'python'

mdf.save('test_header.mf4', overwrite=True)

image

danielhrisca commented 6 years ago

Now you have similar attributes for MDF3 and MDF4 measurements.

fizcris commented 6 years ago

For me it works partially, as you can see the metadata it is not shown on the CANApe side window but it appears when you try to delete the file. The metadata is in the file, so probably extra fields needs to be modified.

As a suggestion, it also would be benefitial to appear in the header dictionary as individual properties.

image

image

image

danielhrisca commented 6 years ago

I can see the proper info in CANape (right clcik the loaded files on the left side and select properties)

image

danielhrisca commented 6 years ago

I would like the key-value pairs to be exactly the block fields, and have related things as attibutes.

fizcris commented 6 years ago

Now I am experiencing that the comment is not being stored into the measurement:

outputFile.file_comment = ' This is my comment

image

danielhrisca commented 6 years ago

Hello Cristian,

there were some problems indeed. I've fixed them and it appear to work correctly now.

The proper way to change the comment is

outfile.header.comment = 'my new comment'
fizcris commented 6 years ago

Sorry but after using the line posted above I am getting the following error:

outputFile.header.comment = ' my comment'
outputFile.save(dst='test.mf4', overwrite=True,compression=2)

AttributeError Traceback (most recent call last)

in () ----> 1 outputFile.save(dst=fileOutFullName, overwrite=True,compression=2)

C:\ProgramData\Anaconda3\lib\site-packages\asammdf-3.5.1.dev0-py3.6.egg\asammdf\mdf_v4.py in save(self, dst, overwrite, compression) 5538 dst, 5539 overwrite, -> 5540 compression, 5541 ) 5542

C:\ProgramData\Anaconda3\lib\site-packages\asammdf-3.5.1.dev0-py3.6.egg\asammdf\mdf_v4.py in _save_with_metadata(self, dst, overwrite, compression) 5625 5626 write(bytes(self.identification)) -> 5627 self.header.tostream(dst) 5628 5629 dst_.flush()

C:\ProgramData\Anaconda3\lib\site-packages\asammdf-3.5.1.dev0-py3.6.egg\asammdf\v4_blocks.py in to_stream(self, stream) 3920 def to_stream(self, stream): 3921 address = stream.tell() -> 3922 if self.comment.startswith('<HDcomment'): 3923 comment = self.comment.replace(' xmlns="http://www.asam.net/mdf/v4"', '') 3924 comment = ET.fromstring(comment)

AttributeError: 'dict' object has no attribute 'startswith'

danielhrisca commented 6 years ago

Are you sure you are assigning a string to the comment attribute? It looks like it had been assigned with a dict or possibly a TextBlock object.

fizcris commented 6 years ago

Sorry my bad. I forgot to stringify a JSON doc object.