forked from ratal/mdfreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.py
1328 lines (1136 loc) · 42.8 KB
/
channel.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.
Platform and python version
----------------------------------------
With Unix and Windows for python 2.7 and 3.4+
:Author: `Aymeric Rateau <https://github.com/ratal/mdfreader>`__
Created on Wed Oct 04 21:13:28 2017
Dependencies
-------------------
- Python >2.6, >3.4 <http://www.python.org>
- Numpy >1.6 <http://numpy.scipy.org>
Attributes
--------------
PythonVersion : float
Python version currently running, needed for compatibility of both
python 2.6+ and 3.4+
channel module
--------------------------
"""
from struct import Struct
from sys import path, stderr
from os.path import dirname, abspath
_root = dirname(abspath(__file__))
path.append(_root)
from mdfinfo4 import ATBlock
from mdf import _bits_to_bytes, _convertName
from numpy import right_shift, bitwise_and
class channel4(object):
__slots__ = ['channelNumber', 'channelGroup', 'dataGroup',
'type', 'name', 'VLSD_CG_Flag']
""" channel class gathers all about channel structure in a record
Attributes
--------------
name : str
Name of channel
type : str
channel type. Can be 'std', 'NestCA',
'CAN' or 'Inv'
channelNumber : int
channel number corresponding to mdfinfo4.info4 class
channelGroup : int
channel group number corresponding to mdfinfo4.info4 class
dataGroup : int
data group number corresponding to mdfinfo4.info4 class
VLSD_CG_Flag : bool
flag when Channel Group VLSD is used
Methods
------------
__init__()
constructor
__str__()
to print class attributes
attachment(fid, info)
in case of sync channel attached
set(info, dataGroup, channelGroup, channelNumber, recordIDsize)
standard channel initialisation
setCANOpen(info, dataGroup, channelGroup, channelNumber,
recordIDsize, name)
CANOpen channel initialisation
setInvalidBytes(info, dataGroup, channelGroup, recordIDsize, byte_aligned)
Invalid Bytes channel initialisation
recAttributeName : str
Name of channel compatible with python attribute name conventions
unit : str, default empty string
channel unit
desc : str
channel description
conversion : info class
conversion dictionnary
CNBlock : info class
Channel Block info 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
little_endian : Bool
flag to inform of channel data endian
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 : nested tuple of str
dtype format used for numpy.core.records functions
((name_title,name),str_stype)
nativeRecordFormat : nested tuple of str
same as RecordFormat but using recAttributeName instead of name
channelType : int
channel type ; 0 fixed length data, 1 VLSD, 2 master, 3 virtual master,
4 sync, 5 MLSD, 6 virtual data
channelSyncType : int
channel synchronisation type ; 0 None, 1 Time, 2 Angle,
3 Distance, 4 Index
posByteBeg : int
start position in number of byte of channel record in complete record
posByteEnd : int
end position in number of byte of channel record in complete record
posBitBeg : int
start position in number of bit of channel record in complete record
posBitEnd : int
end position in number of bit of channel record in complete record
maxLengthVLSDRecord :
CABlock : CABlock class
contains CABLock
data : int
pointer to data block linked to a channel (VLSD, MLSD)
invalid_bit : dict
dict of invalid bit channels data
invalid_bytes : bytes
byte containing invalid bit for each channel
"""
def __init__(self):
""" channel class constructor
Attributes
--------------
name : str
Name of channel
type : str, default 'std'
channel type. Can be 'std', 'NestCA',
'CAN' or 'Inv'
channelNumber : int, default 0
channel number corresponding to mdfinfo4.info4 class
channelGroup : int, default 0
channel group number corresponding to mdfinfo4.info4 class
dataGroup : int, default 0
data group number corresponding to mdfinfo4.info4 class
VLSD_CG_Flag : bool, default False
flag when Channel Group VLSD is used
"""
self.name = ''
self.type = 'std'
self.channelNumber = 0
self.dataGroup = 0
self.channelGroup = 0
self.VLSD_CG_Flag = False
def __str__(self):
""" channel object attributes print
"""
return '{0} {1} {2} {3} {4} {5}'.format(self.name,
self.type,
self.dataGroup,
self.channelGroup,
self.channelNumber,
self.VLSD_CG_Flag)
def attachment(self, fid, info):
""" In case of sync channel attached to channel
Parameters
----------------
fid : class
file identifier
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
ATBlock class from mdfinfo4 module
"""
try:
return ATBlock(fid, info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_data'])
except KeyError:
print('No Attachment block for this channel')
def data(self, info):
""" returns data block pointer for VLSD, MLD or sync channels"""
try:
return info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['cn_data']
except KeyError:
return None
def CNBlock(self, info):
""" channel block
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
CNBlock class from mdfinfo4 module
"""
try:
return info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]
except KeyError:
return None
def signalDataType(self, info, byte_aligned=True):
""" extract signal data type from info4 class
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
byte_aligned : bool
flag activated if channel is part of a record byte aligned
Returns
-----------
integer corresponding to channel data type
0 unsigned integer little endian
1 unsigned integer big endian
2 signed integer little endian
3 signed integer big endian
4 float little endian
5 float big endian
6 string latin
7 string utf-8
9 string utf-16
10 byte array
11 mime sample
12 mime stream
13 CANopen date
14 CANopen time
"""
if not self.type == 'Inv':
return info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_data_type']
else:
if byte_aligned:
return 10 # byte array
else:
return 0 # uint LE
def bitCount(self, info):
""" calculates channel number of bits
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer corresponding to channel number of bits
"""
if self.type in ('std', 'CA', 'NestCA'):
return info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_bit_count']
elif self.type == 'CAN':
if self.name == 'ms':
if self.signalDataType(info) == 13:
return 16
else:
return 32
elif self.name == 'days':
return 16
else:
return 8
elif self.type == 'Inv':
return self.nBytes(info) * 8
else:
print('Not found channel type')
def channelSyncType(self, info):
""" Extracts channel sync type from info4
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer corresponding to channel sync type
0 no sync, normal data
1 time
2 angle
3 distance
4 index
"""
try:
return info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_sync_type']
except KeyError:
return 0 # in case of invaldi bytes channel
def CABlock(self, info):
""" Extracts channel CA Block from info4
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
CABlock object from mdfinfo4 module
"""
try:
return info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['CABlock']
except KeyError:
print('No CA Block in this channel')
def recordIDsize(self, info):
""" Extracts record id size from info4
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer describing record id size
0 no record id used
1 uint8
2 uint16
4 uint32
8 uint64
"""
return info['DG'][self.dataGroup]['dg_rec_id_size']
def channelType(self, info):
""" Extracts channel type from info4
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer describing channel type
0 normal channel
1 variable length
2 master channel
3 virtual master channel
4 sync channel
5 max length data
6 virtual data channel
"""
try:
return info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['cn_type']
except KeyError:
return 0 # in case of invaldi bytes channel
def nBytes(self, info):
""" calculates channel bytes number
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
number of bytes integer
"""
if not self.type == 'Inv':
nBytes = _bits_to_bytes(info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_bit_count'])
if self.type in ('CA', 'NestCA'):
nBytes *= self.CABlock(info)['PNd']
Block = self.CABlock(info)
while 'CABlock' in Block: # nested array
Block = Block['CABlock']
nBytes *= Block['PNd']
return nBytes
else:
return info['CG'][self.dataGroup][self.channelGroup]['cg_invalid_bytes']
def little_endian(self, info):
""" check if channel is little endian
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
boolean
"""
if info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]\
['cn_data_type'] in (1, 3, 5, 9): # endianness
return False
else:
return True
def recAttributeName(self, info):
""" clean up channel name from unauthorised characters
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
channel name compliant to python attributes names (for recarray)
"""
return _convertName(self.name)
def dataFormat(self, info):
""" channel numpy.core.records data format
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
string data format
"""
if self.type == 'Inv':
dataformat = '{}V'.format(self.nBytes(info))
elif info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['cn_composition'] and \
'CABlock' in info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]: # channel array
CABlock = info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['CABlock']
dataformat = arrayformat4(info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['cn_data_type'],
info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['cn_bit_count'])
# calculates total array size in bytes
array_desc = CABlock['ca_dim_size']
Block = CABlock
while 'CABlock' in Block: # nested array
Block = Block['CABlock']
Block['ca_dim_size']
if isinstance(array_desc, list):
array_desc.append(Block['ca_dim_size'])
else:
array_desc = [array_desc, Block['ca_dim_size']]
if isinstance(array_desc, list):
array_desc = str(tuple(array_desc))
else:
array_desc = str(array_desc)
dataformat = array_desc + dataformat
elif self.type == 'CAN':
if self.name == 'ms':
if self.signalDataType(info) == 13:
dataformat = '<u2'
else:
dataformat = '<u4'
elif self.name == 'days':
dataformat = '<u2'
else:
dataformat = '<u1'
else: # not channel array
dataformat = arrayformat4(info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_data_type'], info['CN'][self.dataGroup]\
[self.channelGroup][self.channelNumber]['cn_bit_count'])
return dataformat
def RecordFormat(self, info, recAttributeName=None):
""" recarray dtype string
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
recarray dtype
"""
if recAttributeName is None:
recAttributeName = self.recAttributeName(info)
return (('{}_title'.format(recAttributeName),
recAttributeName), self.dataFormat(info))
def nativeRecordFormat(self, info):
""" channel numpy.core.records data format without endian (< or >)
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
string data format
"""
recAttributeName = self.recAttributeName(info)
return (('{}_title'.format(recAttributeName), recAttributeName),
self.dataFormat(info).lstrip('<').lstrip('>'))
def Format(self, info):
""" channel data C format
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
string data C format
"""
if self.type in ('std', 'CA', 'NestCA'):
signalDataType = self.signalDataType(info)
if signalDataType not in (13, 14):
if not self.channelType(info) == 1: # if not VSLD
return datatypeformat4(signalDataType, self.bitCount(info))
else: # VLSD
return datatypeformat4(0, self.bitCount(info))
elif self.type == 'CAN':
if self.name == 'ms':
if self.signalDataType == 13:
return 'H'
else:
return 'I'
elif self.name == 'days':
return 'H'
else:
return 'B'
elif self.type == 'Inv':
return '{}s'.format(self.nBytes(info))
else:
print('Not found channel type')
def CFormat(self, info):
""" channel data C format struct object
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
string data C format struct object
"""
return Struct(self.Format(info))
def CANOpenOffset(self, info):
""" CANopen channel bytes offset
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer, channel bytes offset
"""
if self.name == 'ms':
return 0
elif self.name == 'days':
return 4
else:
if self.name == 'minute':
return 2
elif self.name == 'hour':
return 3
elif self.name == 'day':
return 4
elif self.name == 'month':
return 5
elif self.name == 'year':
return 6
else:
print('CANopen type not understood')
def bitOffset(self, info):
""" channel data bit offset in record
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer, channel bit offset
"""
if self.type in ('std', 'CA', 'NestCA'):
return info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_bit_offset']
elif self.type == 'CAN':
return info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_bit_offset'] + self.CANOpenOffset(info)*8
elif self.type == 'Inv':
return 0
else:
print('Not found channel type')
def byteOffset(self, info):
""" channel data bytes offset in record (without record id)
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer, channel bytes offset
"""
if self.type in ('std', 'CA', 'NestCA'):
return info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_byte_offset']
elif self.type == 'CAN':
return info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['cn_byte_offset'] + self.CANOpenOffset(info)
elif self.type == 'Inv':
return info['CG'][self.dataGroup][self.channelGroup]['cg_data_bytes']
else:
print('Not found channel type')
def posByteBeg(self, info):
""" channel data bytes starting position in record
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer, channel bytes starting position
"""
return self.recordIDsize(info) + self.byteOffset(info)
def posByteEnd(self, info):
""" channel data bytes ending position in record
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer, channel bytes ending position
"""
return self.posByteBeg(info) + self.nBytes(info)
def posBitBeg(self, info):
""" channel data bit starting position in record
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer, channel bit starting position
"""
return self.posByteBeg(info) * 8 + self.bitOffset(info)
def posBitEnd(self, info):
""" channel data bit ending position in record
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
integer, channel bit ending position
"""
return self.posBitBeg(info) + self.bitCount(info)
def unit(self, info):
""" channel unit
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
channel unit string
"""
if self.channelNumber not in info['CC'][self.dataGroup][self.channelGroup]:
return ''
if 'unit' in info['CC'][self.dataGroup][self.channelGroup][self.channelNumber]:
unit = info['CC'][self.dataGroup][self.channelGroup][self.channelNumber]['unit']
elif 'unit' in info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]:
unit = info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['unit']
else:
unit = ''
if 'Comment' in unit:
unit = unit['Comment']
return unit
def desc(self, info):
""" channel description
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
channel description string
"""
if not self.type == 'CAN':
if self.channelNumber in info['CN'][self.dataGroup][self.channelGroup]:
if 'Comment' in info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]:
desc = info['CN'][self.dataGroup][self.channelGroup]\
[self.channelNumber]['Comment']
if (desc is not None) and isinstance(desc, dict):
if 'description' in desc:
desc = desc['description']
elif 'name' in desc:
desc = desc['name']
else:
desc = ''
return desc
else:
return 'Invalid Bytes DGgroup {0} CGgroup {1}'.format(self.dataGroup,
self.channelGroup)
else:
return self.name
def conversion(self, info):
""" channel conversion CCBlock
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
CCBlock
"""
try:
return info['CC'][self.dataGroup][self.channelGroup][self.channelNumber]
except KeyError:
return None
def invalid_bit(self, info):
""" extrzcts from info4 the channels valid bits positions
Parameters
----------------
info : mdfinfo4.info4 class
info4 class containing all MDF Blocks
Returns
-----------
dict of channels valid bits positions
"""
invalid_bit = {}
for channelNumber in info['CN'][self.dataGroup][self.channelGroup]:
name = info['CN'][self.dataGroup][self.channelGroup][channelNumber]['name']
invalid_bit[name] = info['CN'][self.dataGroup][self.channelGroup]\
[channelNumber]['cn_invalid_bit_pos']
return invalid_bit
def set(self, info, dataGroup, channelGroup, channelNumber):
""" channel initialisation
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['CN'][dataGroup][channelGroup][channelNumber]['name']
self.channelNumber = channelNumber
self.dataGroup = dataGroup
self.channelGroup = channelGroup
self.type = 'std'
if info['CN'][dataGroup][channelGroup][channelNumber]['cn_composition'] and \
'CABlock' in info['CN'][dataGroup][channelGroup][channelNumber]:
# channel array
self.type = 'CA'
Block = info['CN'][self.dataGroup][self.channelGroup][self.channelNumber]['CABlock']
if 'CABlock' in Block: # nested array
self.type = 'NestCA'
def setCANOpen(self, info, dataGroup, channelGroup, channelNumber, name):
""" CANOpen channel intialisation
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
name : str
name of channel. Should be in ('ms', 'day', 'days', 'hour',
'month', 'minute', 'year')
"""
self.type = 'CAN'
self.name = name
self.channelNumber = channelNumber
self.dataGroup = dataGroup
self.channelGroup = channelGroup
def setInvalidBytes(self, info, dataGroup, channelGroup, channelNumber):
""" invalid_bytes channel initialisation
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
byte_aligned : Bool
Flag for byte alignement
"""
self.type = 'Inv'
self.name = 'invalid_bytes{}'.format(dataGroup)
self.channelNumber = channelNumber
self.dataGroup = dataGroup
self.channelGroup = channelGroup
def validity_channel(self, info, invalid_bytes):
""" extract channel validity bits
Parameters
----------
info : mdfinfo4.info4 class
invalid_bytes : bytes
bytes from where to extract validity bit array
Return
-------
Numpy vector of validity
"""
if self.type == 'Inv':
return bitwise_and(right_shift(invalid_bytes, self.invalid_bit(info)), 1)
else:
print('asking for invalid byte array but channel is not invalid byte type')
def changeChannelName(self, channelGroup):
""" In case of duplicate channel names within several channel groups
for unsorted data, rename channel name
Parameters
------------
channelGroup : int
channelGroup bumber
"""
self.name = '{0}_{1}'.format(self.name, channelGroup)
def arrayformat4(signalDataType, numberOfBits):
""" function returning numpy style string from channel data type and number of bits
Parameters
----------------
signalDataType : int
channel data type according to specification
numberOfBits : int
number of bits taken by channel data in a record
Returns
-----------
dataType : str
numpy dtype format used by numpy.core.records to read channel raw data
"""
if signalDataType == 0: # unsigned, low endian
if numberOfBits <= 8:
dataType = '<u1'
elif numberOfBits <= 16:
dataType = '<u2'
elif numberOfBits <= 32:
dataType = '<u4'
elif numberOfBits <= 64:
dataType = '<u8'
else:
dataType = '<{}V'.format(_bits_to_bytes(numberOfBits) // 8)
elif signalDataType == 2: # signed int, low endian
if numberOfBits <= 8:
dataType = '<i1'
elif numberOfBits <= 16:
dataType = '<i2'
elif numberOfBits <= 32:
dataType = '<i4'
elif numberOfBits <= 64:
dataType = '<i8'
else:
print('Unsupported number of bits for signed int {}'.format(numberOfBits))
elif signalDataType == 4: # floating point, low endian
if numberOfBits == 32:
dataType = '<f4'
elif numberOfBits == 64:
dataType = '<f8'
else:
print('Unsupported number of bit for floating point ' + str(numberOfBits))
elif signalDataType == 1: # unsigned, big endian
if numberOfBits <= 8:
dataType = '>u1'
elif numberOfBits <= 16:
dataType = '>u2'
elif numberOfBits <= 32:
dataType = '>u4'
elif numberOfBits <= 64:
dataType = '>u8'
else:
dataType = '>{}V'.format(_bits_to_bytes(numberOfBits) // 8)
elif signalDataType == 3: # signed int, big endian
if numberOfBits <= 8:
dataType = '>i1'
elif numberOfBits <= 16:
dataType = '>i2'
elif numberOfBits <= 32:
dataType = '>i4'
elif numberOfBits <= 64:
dataType = '>i8'
else:
print('Unsupported number of bits for signed int {}'.format(numberOfBits))
elif signalDataType == 5: # floating point, big endian
if numberOfBits == 32:
dataType = '>f4'
elif numberOfBits == 64:
dataType = '>f8'
else:
print('Unsupported number of bit for floating point ' + str(numberOfBits))
elif signalDataType == 6: # string ISO-8859-1 Latin
dataType = 'S{}'.format(numberOfBits // 8)
elif signalDataType == 7: # UTF-8
dataType = 'S{}'.format(numberOfBits // 8)
elif signalDataType == 8: # UTF-16 low endian
dataType = '<S{}'.format(numberOfBits // 8)
elif signalDataType == 9: # UTF-16 big endian