forked from ratal/mdfreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdf4reader.py
1660 lines (1511 loc) · 71.5 KB
/
mdf4reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
""" Measured Data Format file reader module for version 4.x
Platform and python version
----------------------------------------
With Unix and Windows for python 2.6+ and 3.2+
:Author: `Aymeric Rateau <http://code.google.com/p/mdfreader/>`__
Created on Thu Dec 10 12:57:28 2013
Dependencies
-------------------
- Python >2.6, >3.2 <http://www.python.org>
- Numpy >1.6 <http://numpy.scipy.org>
- zlib to uncompress data block if needed
- Sympy to convert channels with formula if needed
Attributes
--------------
PythonVersion : float
Python version currently running, needed for compatibility of both python 2.6+ and 3.2+
mdf4reader module
--------------------------
"""
from numpy.core.records import fromstring, fromfile
from numpy import array, recarray, append, asarray, empty, zeros, dtype, where
from numpy import arange, right_shift, bitwise_and, all, diff, interp
from struct import unpack, Struct
from math import pow
from sys import platform, version_info
from io import open # for python 3 and 2 consistency
from mdfinfo4 import info4, MDFBlock, ATBlock#, CNBlock
from time import gmtime, strftime
from multiprocessing import Queue, Process
PythonVersion=version_info
PythonVersion=PythonVersion[0]
LINK='<Q'
REAL='<d'
BOOL='<h'
UINT8='<B'
BYTE='<c'
INT16='<h'
UINT16='<H'
UINT32='<I'
INT32='<i'
UINT64='<Q'
INT64='<q'
CHAR='<c'
def DATABlock(record, parent_block, channelList=None, sortedFlag=True):
""" DATABlock converts raw data into arrays
Parameters
----------------
record : class
record class instance describing a channel group record
parent_block : class
MDFBlock class containing at least parent block header
channelList : list of str, optional
defines list of channels to only read, can be slow but saves memory, for big files
sortedFlag : bool, optional
flag to know if data block is sorted (only one Channel Group in block)
or unsorted (several Channel Groups identified by a recordID). As unsorted block can
contain CG records in random order, block is processed iteratively,
not in raw like sorted -> much slower reading
Returns
---------
a recarray containing the channels data
Notes
--------
This function will read DTBlock, RDBlock, DZBlock (compressed), RDBlock (VLSD), sorted or unsorted
"""
if parent_block['id'] in ('##DT', '##RD', b'##DT', b'##RD'): # normal data block
if sortedFlag:
return fromstring(parent_block['data'] , dtype = record.numpyDataRecordFormat, shape = record.numberOfRecords , names=record.dataRecordName)
else: # unsorted reading
print('not implemented yet unsorted data block reading') # to be implemented
elif parent_block['id'] in ('##SD', b'##SD'): #
if record.signalDataType==6:
format='ISO8859'
elif record.signalDataType==7:
format='utf-8'
elif record.signalDataType==8:
format='<utf-16'
elif record.signalDataType==9:
format='>utf-16'
pointer=0
buf=[] # buf=array(record.numberOfRecords,dtype='s'+str(record.maxLengthVLSDRecord))
while pointer<parent_block['length']-24:
VLSDLen=unpack('I', parent_block['data'][pointer:pointer+4])[0] # length of data
pointer+=4
buf.append(parent_block['data'][pointer:pointer+VLSDLen].decode(format).rstrip('\x00')) # to be improved, removing append
#buf[nElement]=fid.read(VLSDLen).decode(format).rstrip('\x00')
pointer+=VLSDLen
buf=equalizeStringLength(buf)
return array(buf)
elif parent_block['id'] in ('##DZ', b'##DZ'): # zipped data block
# uncompress data
try:
from zlib import decompress
except:
raise('zlib module not found or error while uncompressing')
parent_block['data']=decompress(parent_block['data']) # decompress data
if parent_block['dz_zip_type']==1: # data bytes transposed
N = parent_block['dz_zip_parameter']
M = parent_block['dz_org_data_length']//N
temp=array(memoryview(parent_block['data'][:M*N]))
tail=array(memoryview(parent_block['data'][M*N:]))
temp=temp.reshape(N, M).T.ravel()
if len(tail)>0:
temp=append(temp, tail)
parent_block['data']=temp.tostring()
if channelList is None and sortedFlag: # reads all blocks if sorted block and no channelList defined
record.numberOfRecords = parent_block['dz_org_data_length'] // record.recordLength
return fromstring(parent_block['data'] , dtype = record.numpyDataRecordFormat, shape = record.numberOfRecords , names=record.dataRecordName)
elif channelList is not None and sortedFlag: # sorted data but channel list requested
print('not implemented yet sorted compressed data block reading with channelList') # to be implemented
else: # unsorted reading
# reads only the channels using offset functions, channel by channel.
buf={}
position=0
recordIdCFormat=record[list(record.keys())[0]]['record'].recordIDCFormat
recordIDsize=record[list(record.keys())[0]]['record'].recordIDsize
VLSDStruct=Struct('I')
# initialise data structure
for recordID in record:
for channelName in record[recordID]['record'].channelNames:
buf[channelName]=[] # empty(record.numberOfRecords,dtype=record[recordID]['record'].dataFormat)
#index[channelName]=0
# read data
while position<len(parent_block['data']):
recordID=recordIdCFormat.unpack(parent_block['data'][position:position+recordIDsize])[0]
if not record[recordID]['record'].Flags & 0b1: # not VLSD CG)
temp=record.readRecord(recordID, parent_block['data'][position:position+record[recordID]['record'].recordLength+1], channelList)
position += record[recordID]['record'].recordLength+1
for channelName in temp:
buf[channelName].append(temp[channelName]) # to remove append
# buf[channelName][index[recordID]]=temp[channelName]
# index[recordID] += 1
else: # VLSD CG
position+=recordIDsize
VLSDLen=VLSDStruct.unpack(parent_block['data'][position:position+4])[0] # VLSD length
position+=4
temp=parent_block['data'][position:position+VLSDLen-1]
if record[recordID]['record'].VLSD_CG[recordID]['channel'].signalDataType==6:
temp=temp.decode('ISO8859')
elif record[recordID]['record'].VLSD_CG[recordID]['channel'].signalDataType==7:
temp=temp.decode('utf-8')
elif record[recordID]['record'].VLSD_CG[recordID]['channel'].signalDataType==8:
temp=temp.decode('<utf-16')
elif record[recordID]['record'].VLSD_CG[recordID]['channel'].signalDataType==9:
temp=temp.decode('>utf-16')
buf[record[recordID]['record'].VLSD_CG[recordID]['channelName']].append(temp)
position += VLSDLen
# convert list to array
for chan in buf:
buf[chan]=array(buf[chan])
return buf
def equalizeStringLength(buf):
""" Makes all strings in a list having same length by appending spaces strings
Parameters
----------------
buf : list of str
Returns
-----------
list of str elements all having same length
"""
maxlen=len(max(buf, key=len))
for i in range(len(buf)): # resize string to same length, numpy constrain
buf[i]+=' '*(maxlen-len(buf[i]))
return buf
def append_field(rec, name, arr, numpy_dtype=None):
""" append new field in a recarray
Parameters
----------------
rec : numpy recarray
name : str
name of field to be appended
arr : numpy array to be appended
numpy_dtype : numpy dtype, optional
apply same dtype as arr by default but can be modified
Returns
-----------
numpy recarray appended
"""
arr = asarray(arr)
if numpy_dtype is None:
numpy_dtype = arr.dtype
newdtype = dtype(rec.dtype.descr + [(name, numpy_dtype)])
newrec = empty(rec.shape, dtype=newdtype)
for field in rec.dtype.fields:
newrec[field] = rec[field]
newrec[name] = arr
return newrec
def change_field_name(arr, old_name, new_name):
""" modifies name of field in a recarray
Parameters
----------------
arr : numpy recarray
old_name : str
old field
new_name : str
new field
Returns
-----------
numpy recarray with modified field name
"""
names=list(arr.dtype.names)
for n in range(len(names)):
if names[n]==old_name:
break
names[n]=new_name
names=tuple(names)
arr.dtype.names=names
return arr
class DATA(dict):
""" DATA class is organizing record classes itself made of recordchannel.
This class inherits from dict. Keys are corresponding to channel group recordID
A DATA class corresponds to a data block, a dict of record classes (one per channel group)
Each record class contains a list of recordchannel class representing the structure of channel record.
Attributes
--------------
fid : io.open
file identifier
pointerToData : int
position of Data block in mdf file
type : str
'sorted' or 'unsorted' data block
Methods
------------
addRecord(record)
Adds a new record in DATA class dict
read(channelList, zip=None)
Reads data block
load(record, zip=None, nameList=None)
Reads sorted data block from record definition
readRecord(recordID, buf, channelList=None):
read record from a buffer
"""
def __init__(self, fid, pointer):
""" Constructor
Parameters
----------------
fid : float
file identifier
pointer : int
position of data block in file
"""
self.fid=fid
self.pointerTodata=pointer
self.type='sorted'
def addRecord(self, record):
"""Adds a new record in DATA class dict
Parameters
----------------
record class
channel group definition listing record channel classes
"""
self[record.recordID]={}
self[record.recordID]['record']=record
# detects VLSD CG
for recordID in self[record.recordID]['record'].VLSD_CG:
self[recordID]['record'].VLSD_CG=self[record.recordID]['record'].VLSD_CG
def read(self, channelList, zip=None):
"""Reads data block
Parameters
----------------
channelList : list of str
list of channel names
zip : bool, optional
flag to track if data block is compressed
"""
if len(self)==1: #sorted dataGroup
recordID=list(self.keys())[0]
record=self[recordID]['record']
self[recordID]['data']=self.load( record, zip=None, nameList=channelList, sortedFlag=True)
for cn in record.VLSD: # VLSD channels
if channelList is None or record[cn].name in channelList:
temp=DATA(self.fid, record[cn].data) # all channels
rec=self[recordID]['data'] # recarray from data block
# determine maximum length of values in VLSD for array dtype
# record[cn].maxLengthVLSDRecord=max(diff(rec[convertName(record[cn].name)])-4)
temp=temp.load(record[cn], zip=None, nameList=channelList, sortedFlag=True)
rec=change_field_name(rec, convertName(record[cn].name), convertName(record[cn].name)+'_offset')
rec=append_field(rec, convertName(record[cn].name), temp)
self[recordID]['data']=rec.view(recarray)
else: # unsorted DataGroup
self.type='unsorted'
self['data']=self.load( self, zip=None, nameList=channelList, sortedFlag=False)
def load(self, record, zip=None, nameList=None, sortedFlag=True): # reads sorted data
"""Reads data block from record definition
Parameters
----------------
record class
channel group definition listing record channel classes
zip : bool, optional
flag to track if data block is compressed
nameList : list of str, optional
list of channel names
Returns
-----------
numpy recarray of data
"""
temps=MDFBlock()
# block header
temps.loadHeader(self.fid, self.pointerTodata)
if temps['id'] in ('##DL', b'##DL'): # data list block
# link section
temps['dl_dl_next']=temps.mdfblockread(self.fid, LINK, 1)
temps['dl_data']={}
temps['dl_data'][0]=[temps.mdfblockread(self.fid, LINK, 1) for Link in range(temps['link_count']-1)]
# data section
temps['dl_flags']=temps.mdfblockread(self.fid, UINT8, 1)
temps['dl_reserved']=temps.mdfblockreadBYTE(self.fid, 3)
temps['dl_count']=temps.mdfblockread(self.fid, UINT32, 1)
if temps['dl_flags']: # equal length datalist
temps['dl_equal_length']=temps.mdfblockread(self.fid, UINT64, 1)
else: # datalist defined by byte offset
temps['dl_offset']=temps.mdfblockread(self.fid, UINT64, temps['dl_count'])
if temps['dl_dl_next']:
index=1
while temps['dl_dl_next']: # reads pointers to all data blocks (DT, RD, SD, DZ)
temp=MDFBlock()
temp.loadHeader(self.fid, temps['dl_dl_next'])
temps['dl_dl_next']=temp.mdfblockread(self.fid, LINK, 1)
temps['dl_data'][index]=[temp.mdfblockread(self.fid, LINK, 1) for Link in range(temp['link_count']-1)]
index+=1
if temps['dl_count']:
# read and concatenate raw blocks
buf=bytearray()
for DL in temps['dl_data']:
for pointer in temps['dl_data'][DL]:
# read fist data blocks linked by DLBlock to identify data block type
data_block=MDFBlock()
data_block.loadHeader(self.fid, pointer)
if data_block['id'] in ('##DT', '##RD', b'##DT', b'##RD', '##SD', b'##SD'):
buf.extend(self.fid.read(data_block['length']-24))
elif data_block['id'] in ('##DZ', b'##DZ'):
data_block['dz_org_block_type']=data_block.mdfblockreadCHAR(self.fid, 2)
data_block['dz_zip_type']=data_block.mdfblockread(self.fid, UINT8, 1)
data_block['dz_reserved']=data_block.mdfblockreadBYTE(self.fid, 1)
data_block['dz_zip_parameter']=data_block.mdfblockread(self.fid, UINT32, 1)
data_block['dz_org_data_length']=data_block.mdfblockread(self.fid, UINT64, 1)
data_block['dz_data_length']=data_block.mdfblockread(self.fid, UINT64, 1)
buf.extend(self.fid.read( data_block['dz_data_length'] ))
data_block['data']=buf
temps['data']=DATABlock(record, parent_block=data_block, channelList=nameList, sortedFlag=sortedFlag)
else: # empty datalist
temps['data']=None
elif temps['id'] in ('##HL', b'##HL'): # header list block for DZBlock
# link section
temps['hl_dl_first']=temps.mdfblockread(self.fid, LINK, 1)
# data section
temps['hl_flags']=temps.mdfblockread(self.fid, UINT16, 1)
temps['hl_zip_type']=temps.mdfblockread(self.fid, UINT8, 1)
temps['hl_reserved']=temps.mdfblockreadBYTE(self.fid, 5)
self.pointerTodata= temps['hl_dl_first']
temps['data']=self.load(record, zip=temps['hl_zip_type'], nameList=nameList, sortedFlag=sortedFlag)
elif temps['id'] in ('##DT', '##RD', b'##DT', b'##RD'): # normal sorted data block, direct read
temps['data']=record.readSortedRecord(self.fid, self.pointerTodata, channelList=nameList)
elif temps['id'] in ('##SD', b'##SD'): # VLSD
temps['data']=self.fid.read(temps['length']-24)
temps['data']=DATABlock(record, parent_block=temps, channelList=nameList, sortedFlag=sortedFlag)
elif temps['id'] in ('##DZ', b'##DZ'): # zipped data block
temps['dz_org_block_type']=temps.mdfblockreadCHAR(self.fid, 2)
temps['dz_zip_type']=temps.mdfblockread(self.fid, UINT8, 1)
temps['dz_reserved']=temps.mdfblockreadBYTE(self.fid, 1)
temps['dz_zip_parameter']=temps.mdfblockread(self.fid, UINT32, 1)
temps['dz_org_data_length']=temps.mdfblockread(self.fid, UINT64, 1)
temps['dz_data_length']=temps.mdfblockread(self.fid, UINT64, 1)
temps['data']=self.fid.read( temps['dz_data_length'] )
temps['data']=DATABlock(record, parent_block=temps, channelList=nameList, sortedFlag=sortedFlag)
else:
raise('unknown data block')
return temps['data']
def readRecord(self, recordID, buf, channelList=None):
""" read record from a buffer
Parameters
----------------
recordID : int
record identifier
buf : str
buffer of data from file to be converted to channel raw data
channelList : list of str
list of channel names to be read
"""
return self[recordID]['record'].readRecordBuf(buf, channelList)
class recordChannel():
""" recordChannel class gathers all about channel structure in a record
Attributes
--------------
name : str
Name of channel
channelNumber : int
channel number corresponding to mdfinfo3.info3 class
signalDataType : int
signal type according to specification
bitCount : int
number of bits used to store channel record
nBytes : int
number of bytes (1 byte = 8 bits) taken by channel record
dataFormat : str
numpy dtype as string
Format :
C format understood by fread
CFormat : struct class instance
struct instance to convert from C Format
byteOffset : int
position of channel record in complete record in bytes
bitOffset : int
bit position of channel value inside byte in case of channel having bit count below 8
RecordFormat : list of str
dtype format used for numpy.core.records functions ((name,name_title),str_stype)
channelType : int
channel type
posBeg : int
start position in number of bit of channel record in complete record
posEnd : int
end position in number of bit of channel record in complete record
VLSD_CG_Flag : bool
flag when Channel Group VLSD is used
data : int
pointer to data block linked to a channel (VLSD, MLSD)
Methods
------------
__init__(info, dataGroup, channelGroup, channelNumber, recordIDsize)
constructor
__str__()
to print class attributes
"""
def __init__(self, info, dataGroup, channelGroup, channelNumber, recordIDsize):
""" recordChannel class constructor
Parameters
------------
info : mdfinfo4.info4 class
dataGroup : int
data group number in mdfinfo4.info4 class
channelGroup : int
channel group number in mdfinfo4.info4 class
channelNumber : int
channel number in mdfinfo4.info4 class
recordIDsize : int
size of record ID in Bytes
"""
self.name=info['CNBlock'][dataGroup][channelGroup][channelNumber]['name']
self.channelNumber=channelNumber
self.signalDataType = info['CNBlock'][dataGroup][channelGroup][channelNumber]['cn_data_type']
self.bitCount = info['CNBlock'][dataGroup][channelGroup][channelNumber]['cn_bit_count']
self.channelType = info['CNBlock'][dataGroup][channelGroup][channelNumber]['cn_type']
self.dataFormat=arrayformat4( self.signalDataType, self.bitCount )
if self.channelType in (1, 4, 5): # if VSLD or sync or max length channel
self.data=info['CNBlock'][dataGroup][channelGroup][channelNumber]['cn_data']
if self.channelType==1: # if VSLD
self.dataFormat=arrayformat4(0, self.bitCount )
self.maxLengthVLSDRecord=0 # initialises max length of SDBlock elements to 0 for later calculation
if self.channelType in (2, 3): # master channel, add datagroup number to avoid overwriting same sames like time
self.name+=str(dataGroup)
self.RecordFormat=((self.name, convertName(self.name)), self.dataFormat)
if not self.signalDataType in (13, 14): # processed by several channels
if not self.channelType==1: # if VSLD
self.Format=datatypeformat4( self.signalDataType, self.bitCount )
else:
self.Format=datatypeformat4( 0, self.bitCount )
self.CFormat=Struct(self.Format)
if self.bitCount%8==0:
self.nBytes=self.bitCount // 8
else:
self.nBytes=self.bitCount // 8 + 1
self.bitOffset=info['CNBlock'][dataGroup][channelGroup][channelNumber]['cn_bit_offset']
self.byteOffset=info['CNBlock'][dataGroup][channelGroup][channelNumber]['cn_byte_offset']
self.posBeg=recordIDsize+self.byteOffset
self.posEnd=recordIDsize+self.byteOffset+self.nBytes
self.VLSD_CG_Flag=False
def __str__(self):
output=str(self.channelNumber) + ' '
output+=self.name+' '
output+=str(self.signalDataType)+' '
output+=str(self.channelType)+' '
output+=str(self.RecordFormat)+' '
output+=str(self.bitOffset)+' '
output+=str(self.byteOffset)
return output
class invalid_bytes():
"""invalid_bytes class to handle invalid bytes in record if existing
Attributes
-----------
name : str
Name of channel
signalDataType : int
signal type according to specification
bitCount : int
number of bits used to store channel record
nBytes : int
number of bytes (1 byte = 8 bits) taken by channel record
dataFormat : str
numpy dtype as string
Format :
C format understood by fread
CFormat : struct class instance
struct instance to convert from C Format
byteOffset : int
position of channel record in complete record in bytes
bitOffset : int
bit position of channel value inside byte in case of channel having bit count below 8
RecordFormat : list of str
dtype format used for numpy.core.records functions ((name,name_title),str_stype)
channelType : int
channel type
posBeg : int
start position in number of bit of channel record in complete record
posEnd : int
end position in number of bit of channel record in complete record
VLSD_CG_Flag : bool
flag when Channel Group VLSD is used
data : int
pointer to data block linked to a channel (VLSD, MLSD)
Methods
---------
__init__(info, dataGroup, channelGroup, recordIDsize)
constructor
channel_validity(channelName)
returns channel validity bit array
"""
def __init__(self, info, dataGroup, channelGroup, recordIDsize):
""" invalid_bytes class constructor
Parameters
----------
info : mdfinfo4.info4 class
"""
self.name='invalid_bytes'+str(dataGroup)
self.signalDataType = 10 # byte array
self.nBytes=info['CGBlock'][dataGroup][channelGroup]['cg_invalid_bytes']
self.bitCount = self.nBytes*8
self.channelType = 0 # fixed length data
self.dataFormat=arrayformat4( self.signalDataType, self.bitCount )
self.RecordFormat=((self.name, convertName(self.name)), self.dataFormat)
self.Format=datatypeformat4( self.signalDataType, self.bitCount )
self.CFormat=Struct(self.Format)
self.bitOffset=0
self.byteOffset=info['CGBlock'][dataGroup][channelGroup]['cg_data_bytes']
self.posBeg=recordIDsize+self.byteOffset
self.posEnd=recordIDsize+self.byteOffset+self.nBytes
self.VLSD_CG_Flag=False
self.invalid_bit={}
for channelNumber in info['CNBlock'][dataGroup][channelGroup]:
name=info['CNBlock'][dataGroup][channelGroup][channelNumber]['name']
self.invalid_bit[name]=info['CNBlock'][dataGroup][channelGroup][channelNumber]['cn_invalid_bit_pos']
self.invalid_bytes=None
def channel_validity(self, channelName):
""" extract channel validity bits
Parameters
----------
channelName : str
channel name
"""
return bitwise_and(right_shift(self.invalid_bytes, self.invalid_bit[channelName]), 1)
class record(list):
""" record class lists recordchannel classes, it is representing a channel group
Attributes
--------------
recordLength : int
length of record corresponding of channel group in Byte
numberOfRecords : int
number of records in data block
recordID : int
recordID corresponding to channel group
recordIDsize : int
size of recordID
recordIDCFormat : str
record identifier C format string as understood by fread
dataGroup : int:
data group number
channelGroup : int
channel group number
numpyDataRecordFormat : list
list of numpy (dtype) for each channel
dataRecordName : list
list of channel names used for recarray attribute definition
master : dict
define name and number of master channel
recordToChannelMatching : dict
helps to identify nested bits in byte
channelNames : list
channel names to be stored, useful for low memory consumption but slow
Flags : bool
channel flags as from specification
VLSD_CG : dict
dict of Channel Group VLSD, key being recordID
VLSD : list of recordChannel
list of recordChannel being VLSD
MLSD : dict
copy from info['MLSD'] if existing
Methods
------------
addChannel(info, channelNumber)
loadInfo(info)
readSortedRecord(fid, pointer, channelList=None)
readRecordBuf(buf, channelList=None)
"""
def __init__(self, dataGroup, channelGroup):
self.recordLength=0
self.numberOfRecords=0
self.recordID=0
self.recordIDsize=0
self.recordIDCFormat=''
self.dataGroup=dataGroup
self.channelGroup=channelGroup
self.numpyDataRecordFormat=[]
self.dataRecordName=[]
self.master={}
self.Flags=0
self.VLSD_CG={}
self.VLSD=[]
self.MLSD={}
self.recordToChannelMatching={}
self.channelNames=[]
def addChannel(self, info, channelNumber):
""" add a channel in class
Parameters
----------------
info : mdfinfo4.info4 class
channelNumber : int
channel number in mdfinfo4.info4 class
"""
self.append(recordChannel(info, self.dataGroup, self.channelGroup, channelNumber, self.recordIDsize))
self.channelNames.append(self[-1].name)
def loadInfo(self, info):
""" gathers records related from info class
Parameters
----------------
info : mdfinfo4.info4 class
"""
self.recordIDsize=info['DGBlock'][self.dataGroup]['dg_rec_id_size']
if not self.recordIDsize==0: # no record ID
self.dataRecordName.append('RecordID'+str(self.channelGroup))
format=(self.dataRecordName[-1], convertName(self.dataRecordName[-1]))
if self.recordIDsize==1:
self.numpyDataRecordFormat.append( ( format, 'uint8' ) )
self.recordIDCFormat=Struct('B')
elif self.recordIDsize==2:
self.numpyDataRecordFormat.append( ( format, 'uint16' ) )
self.recordIDCFormat='H'
elif self.recordIDsize==3:
self.numpyDataRecordFormat.append( ( format, 'uint32' ) )
self.recordIDCFormat='I'
elif self.recordIDsize==4:
self.numpyDataRecordFormat.append( ( format, 'uint64' ) )
self.recordIDCFormat='L'
self.recordID=info['CGBlock'][self.dataGroup][self.channelGroup]['cg_record_id']
self.recordLength=info['CGBlock'][self.dataGroup][self.channelGroup]['cg_data_bytes']
self.numberOfRecords=info['CGBlock'][self.dataGroup][self.channelGroup]['cg_cycle_count']
self.Flags=info['CGBlock'][self.dataGroup][self.channelGroup]['cg_flags']
if 'MLSD' in info:
self.MLSD=info['MLSD']
for channelNumber in list(info['CNBlock'][self.dataGroup][self.channelGroup].keys()):
channel=recordChannel(info, self.dataGroup, self.channelGroup, channelNumber, self.recordIDsize)
if channel.channelType in (2, 3): # master channel found
self.master['name']=channel.name
self.master['number']=channelNumber
if channel.channelType in (0, 1, 2, 4, 5): # not virtual channel
self.append(channel)
self.channelNames.append(channel.name)
if len(self)>1 and channel.byteOffset>=self[-2].byteOffset and channel.byteOffset<(self[-2].byteOffset+self[-2].nBytes): # several channels in one byte, ubit1 or ubit2
self.recordToChannelMatching[channel.name]=self.recordToChannelMatching[self[-2].name]
else: # adding bytes
self.recordToChannelMatching[channel.name]=channel.name
if channel.signalDataType not in (13, 14):
self.numpyDataRecordFormat.append(channel.RecordFormat)
self.dataRecordName.append(channel.name)
elif channel.signalDataType == 13:
self.dataRecordName.append( 'ms' )
self.numpyDataRecordFormat.append( ( ('ms', 'ms_title'), '<u2') )
self.dataRecordName.append( 'min' )
self.numpyDataRecordFormat.append( ( ('min', 'min_title'), '<u1') )
self.dataRecordName.append( 'hour' )
self.numpyDataRecordFormat.append( ( ('hour', 'hour_title'), '<u1') )
self.dataRecordName.append( 'day' )
self.numpyDataRecordFormat.append( ( ('day', 'day_title'), '<u1') )
self.dataRecordName.append( 'month' )
self.numpyDataRecordFormat.append( ( ('month', 'month_title'), '<u1') )
self.dataRecordName.append( 'year' )
self.numpyDataRecordFormat.append( ( ('year', 'year_title'), '<u1') )
elif channel.signalDataType == 14:
self.dataRecordName.append( 'ms' )
self.numpyDataRecordFormat.append( ( ('ms', 'ms_title'), '<u4') )
self.dataRecordName.append( 'days' )
self.numpyDataRecordFormat.append( ( ('days', 'days_title'), '<u2') )
if 'VLSD_CG' in info: # is there VLSD CG
for recordID in info['VLSD_CG']:# look for VLSD CG Channel
if info['VLSD_CG'][recordID]['cg_cn']==(self.channelGroup, channelNumber):
self.VLSD_CG[recordID]=info['VLSD_CG'][recordID]
self.VLSD_CG[recordID]['channel']=channel
self.VLSD_CG[recordID]['channelName']=channel.name
self[-1].VLSD_CG_Flag=True
break
if channel.channelType==1: # VLSD channel
self.VLSD.append(channel.channelNumber)
elif channel.channelType in (3, 6): # virtual channel
pass # channel calculated based on record index later in conversion function
if info['CGBlock'][self.dataGroup][self.channelGroup]['cg_invalid_bytes']: # invalid bytes existing
self.recordLength += info['CGBlock'][self.dataGroup][self.channelGroup]['cg_invalid_bytes']
invalid_channel=invalid_bytes(info, self.dataGroup, self.channelGroup, self.recordIDsize)
self.append(invalid_channel)
self.channelNames.append(invalid_channel.name)
self.recordToChannelMatching[invalid_channel.name]=invalid_channel.name
self.numpyDataRecordFormat.append(invalid_channel.RecordFormat)
self.dataRecordName.append(invalid_channel.name)
def readSortedRecord(self, fid, pointer, channelList=None):
""" reads record, only one channel group per datagroup
Parameters
----------------
fid : float
file identifier
pointer
position in file of data block beginning
channelList : list of str, optional
list of channel to read
Returns
-----------
rec : numpy recarray
contains a matrix of raw data in a recarray (attributes corresponding to channel name)
Notes
--------
If channelList is None, read data using numpy.core.records.fromfile that is rather quick.
However, in case of large file, you can use channelList to load only interesting channels or
only one channel on demand, but be aware it might be much slower.
"""
if channelList is None: # reads all, quickest but memory consuming
return fromfile( fid, dtype = self.numpyDataRecordFormat, shape = self.numberOfRecords, names=self.dataRecordName)
else: # reads only some channels from a sorted data block
# memory efficient but takes time
if len(list(set(channelList)&set(self.channelNames)))>0: # are channelList in this dataGroup
# check if master channel is in the list
if not self.master['name'] in channelList:
channelList.append(self.master['name']) # adds master channel
rec={}
recChan=[]
numpyDataRecordFormat=[]
for channel in channelList: # initialise data structure
rec[channel]=0
for channel in self: # list of recordChannels from channelList
if channel.name in channelList:
recChan.append(channel)
numpyDataRecordFormat.append(channel.RecordFormat)
rec=zeros((self.numberOfRecords, ), dtype=numpyDataRecordFormat)
recordLength=self.recordIDsize+self.recordLength
for r in range(self.numberOfRecords): # for each record,
buf=fid.read(recordLength)
for channel in recChan:
rec[channel.name][r]=channel.CFormat.unpack(buf[channel.posBeg:channel.posEnd])[0]
return rec.view(recarray)
def readRecordBuf(self, buf, channelList=None):
""" read stream of record bytes
Parameters
----------------
buf : stream
stream of bytes read in file
channelList : list of str, optional
list of channel to read
Returns
-----------
rec : dict
# returns dictionary of channel with its corresponding values
"""
temp={}
if channelList is None:
channelList = self.channelNames
for channel in self: # list of recordChannels from channelList
if channel.name in channelList and not channel.VLSD_CG_Flag:
temp[channel.name] = channel.CFormat.unpack(buf[channel.posBeg:channel.posEnd])[0]
return temp # returns dictionary of channel with its corresponding values
class mdf4(dict):
""" mdf file reader class from version 4.0 to 4.1
Attributes
--------------
fileName : str
file name
VersionNumber : int
mdf file version number
masterChannelList : dict
Represents data structure: a key per master channel with corresponding value containing a list of channels
One key or master channel represents then a data group having same sampling interval.
multiProc : bool
Flag to request channel conversion multi processed for performance improvement.
One thread per data group.
convertAfterRead : bool
flag to convert raw data to physical just after read
filterChannelNames : bool
flag to filter long channel names from its module names separated by '.'
author : str
organisation : str
project : str
subject : str
comment : str
time : str
date : str
Methods
------------
read4( fileName=None, info=None, multiProc=False, channelList=None, convertAfterRead=True)
Reads mdf 4.x file data and stores it in dict
getChannelData4(channelName)
Returns channel numpy array
convertChannel4(channelName)
converts specific channel from raw to physical data according to CCBlock information
convertAllChannel4()
Converts all channels from raw data to converted data according to CCBlock information
"""
def __init__( self, fileName = None, info=None, multiProc = False, channelList=None, convertAfterRead=True):
self.masterChannelList = {}
self.multiProc = False # flag to control multiprocessing, default deactivate, giving priority to mdfconverter
self.convert_tables = False # if True converts raw data with expensive loops, not necessary most cases
self.VersionNumber=400
self.author=''
self.organisation=''
self.project=''
self.subject=''
self.comment=''
self.time=''
self.date=''
# clears class from previous reading and avoid to mess up
self.clear()
if fileName is None and info is not None:
self.fileName=info.fileName
self.read4(self.fileName, info, multiProc, channelList, convertAfterRead)
elif fileName is not None:
self.fileName=fileName
self.read4(self.fileName, info, multiProc, channelList, convertAfterRead)
def read4( self, fileName=None, info = None, multiProc = False, channelList=None, convertAfterRead=True):
""" Reads mdf 4.x file data and stores it in dict
Parameters
----------------
fileName : str, optional
file name
info : mdfinfo4.info4 class
info3 class containing all MDF Blocks
multiProc : bool
flag to activate multiprocessing of channel data conversion
channelList : list of str, optional
list of channel names to be read
If you use channelList, reading might be much slower but it will save you memory. Can be used to read big files
convertAfterRead : bool, optional
flag to convert channel after read, True by default
If you use convertAfterRead by setting it to false, all data from channels will be kept raw, no conversion applied.
If many float are stored in file, you can gain from 3 to 4 times memory footprint
To calculate value from channel, you can then use method .getChannelData()
"""
self.multiProc = multiProc
if platform in ('win32', 'win64'):
self.multiProc=False # no multiprocessing for windows platform
try:
from multiprocessing import Queue, Process
except:
print('No multiprocessing module found')
self.multiProc = False
if self.fileName == None:
self.fileName=info.fileName
else:
self.fileName=fileName
#inttime = time.clock()
## Read information block from file
if info==None:
info = info4( self.fileName, None )
# reads metadata
fileDateTime=gmtime(info['HDBlock']['hd_start_time_ns']/1000000000)
self.date=strftime('%Y-%m-%d', fileDateTime)
self.time=strftime('%H:%M:%S', fileDateTime)
if 'Comment' in list(info['HDBlock'].keys()):
if 'author' in list(info['HDBlock']['Comment'].keys()):
self.author=info['HDBlock']['Comment']['author']
if 'department' in list(info['HDBlock']['Comment'].keys()):
self.organisation=info['HDBlock']['Comment']['department']
if 'project' in list(info['HDBlock']['Comment'].keys()):
self.project=info['HDBlock']['Comment']['project']
if 'subject' in list(info['HDBlock']['Comment'].keys()):
self.subject=info['HDBlock']['Comment']['subject']
if 'TX' in list(info['HDBlock']['Comment'].keys()):
self.comment=info['HDBlock']['Comment']['TX']
try:
fid = open( self.fileName, 'rb' )
except IOError:
print('Can not find file'+self.fileName)
raise
if self.multiProc:
# prepare multiprocessing of dataGroups
proc = []
Q=Queue()
L={}
masterDataGroup={} # datagroup name correspondence with its master channel
for dataGroup in info['DGBlock'].keys():
if not info['DGBlock'][dataGroup]['dg_data']==0: # data exists
#Pointer to data block
pointerToData =info['DGBlock'][dataGroup]['dg_data']
buf=DATA(fid, pointerToData)
for channelGroup in info['CGBlock'][dataGroup].keys():
temp=record(dataGroup, channelGroup) # create record class
temp.loadInfo(info) # load all info related to record
buf.addRecord(temp)
for channel in info['CNBlock'][dataGroup][channelGroup].keys():
if info['CNBlock'][dataGroup][channelGroup][channel]['cn_type'] in (2, 3):
masterDataGroup[dataGroup]=info['CNBlock'][dataGroup][channelGroup][channel]['name']+str(dataGroup)
buf.read(channelList) # reads raw data with DATA and DATABlock classes
# Convert channels to physical values
OkBuf=len(buf)>0 and 'data' in buf[list(buf.keys())[0]] and buf[list(buf.keys())[0]]['data'] is not None
if self.multiProc and OkBuf:
proc.append( Process( target = processDataBlocks4, args = ( Q, buf, info, dataGroup, channelList, self.multiProc) ) )
proc[-1].start()
elif OkBuf: # for debugging purpose, can switch off multiprocessing
L.update(processDataBlocks4( None, buf, info, dataGroup, channelList, self.multiProc))
elif buf.type=='unsorted':
L=buf['data']
else:
print('No data in dataGroup '+ str(dataGroup))
del buf
if self.multiProc:
for p in proc:
L.update(Q.get()) # concatenate results of processes in dict
for p in proc:
p.join()
del Q # free memory
fid.close() # close file
# After all processing of channels,
# prepare final class data with all its keys
for dataGroup in list(info['DGBlock'].keys()):