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 import mmap
112 import numpy
113 import os
114 import struct
115 import traceback
116 from math import *
117
118 ACQUIRE_FORMAT = 1
119 ACQUIRE_BLOCK_SIZE = 1024
120 ACQUIRE_FILE_COMMENT_SIZE = 256
121 ACQUIRE_COMMENT_SIZE = 80
122 ACQUIRE_FILE_TECHNIQUE_SIZE = 256
123 ACQUIRE_TECHNIQUE_SIZE = 80
124 ACQUIRE_SCREEN_SIZE = 80
125 ACQUIRE_LABEL_SIZE = 16
126 ACQUIRE_UNITS_SIZE = 16
127 ACQUIRE_SIGNAL_SIZE = 32
128 ACQUIRE_CONTROL_NAME_SIZE = 80
129 ACQUIRE_USER_NAME_SIZE = 64
130 ACQUIRE_COMPUTER_NAME_SIZE = 64
131
132 try:
133 from qubx.fast.data import decode_acquirefile_samples
134 except:
136 i_dest = 0
137 i_src = 0
138 val = 0
139 while i_src < size:
140 c = src[i_src]
141 if c == -128:
142 i_src += 1
143 if i_src % 2:
144 val = src_16s[i_src/2]
145 else:
146 val = src_16[i_src/2]
147 i_src += 2
148 else:
149 val += c
150 i_src += 1
151 dest[i_dest] = val
152 i_dest += 1
153
154
155
157 next = loc_bytes + count
158 s = str(buf[loc_bytes:next])
159 terminator = s.find(chr(0))
160 if terminator >= 0:
161 s = s[:terminator]
162 return s, next
163
165 next = loc_bytes+2*count
166 s = unicode(buf[loc_bytes:next], encoding='UTF-16')
167 terminator = s.find(chr(0))
168 if terminator >= 0:
169 s = s[:terminator]
170 return s, next
171
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
195 self.loc_bytes = loc_bytes
196 self.vendor, loc_bytes = read_string8(buf, loc_bytes, 8)
197 self.format, self.version, self.minor_version, r1, r2, r3, self.next_loc_bytes = read_struct(buf, loc_bytes, 'iiqqqq')
198 self.reserved = (r1, r2, r3)
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
231 self.loc_bytes = loc_bytes
232 self.date_time, self.file_tag, loc_bytes = read_struct(buf, loc_bytes, 'qq')
233 self.comment, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_FILE_COMMENT_SIZE)
234 self.technique, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_FILE_TECHNIQUE_SIZE)
235 (self.clock_rate,
236 self.frame_interval,
237 self.analog_in_channel_count,
238 self.analog_in_maximum_block_size,
239 self.analog_in_maximum_data_count,
240 self.aperiodic_channel_count,
241 self.aperiodic_maximum_block_size,
242 self.series_count,
243 self.file_size,
244 self.control_size,
245 self.control_position,
246 self.directory_position,
247 loc_bytes) = read_struct(buf, loc_bytes, 'dqqqqqqqqqqq')
248 self.user_name, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_USER_NAME_SIZE)
249 self.computer_name, self.next_loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_USER_NAME_SIZE)
250
251
252
253
254
255
256
257
258
259
260
261
262
263
266 self.loc_bytes = loc_bytes
267 self.comment, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_COMMENT_SIZE)
268 self.technique, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_TECHNIQUE_SIZE)
269 self.start_time, self.sweep_count, self.next_loc_bytes = read_struct(buf, loc_bytes, 'qq')
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
293 self.loc_bytes = loc_bytes
294 (self.maximum_voltage, self.minimum_voltage,
295 self.maximum_value, self.minimum_value,
296 self.gain, self.offset,
297 self.next_loc_bytes) = read_struct(buf, loc_bytes, 'ddqqdd')
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
315 self.loc_bytes = loc_bytes
316 self.value, r1, r2, r3, r4, self.next_loc_bytes = read_struct(buf, loc_bytes, 'dqqqq')
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
337 self.loc_bytes = loc_bytes
338 self.comment, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_COMMENT_SIZE)
339 self.technique, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_TECHNIQUE_SIZE)
340 self.screen, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_SCREEN_SIZE)
341 (self.start_time, self.segment_count,
342 self.duration, self.control_data_location,
343 self.segment_location, self.next_loc_bytes) = read_struct(buf, loc_bytes, 'qqqqq')
344
345
346
347
348
349
350
351
352
353
354
355
360
361
362
363
364
365
366
367
368
369
370
373 self.loc_bytes = loc_bytes
374 self.block_count, self.next_loc_bytes = read_struct(buf, loc_bytes, 'q')
375
376
377
378
379
380
381
382
383
384
385
386
387
388
391 self.loc_bytes = loc_bytes
392 self.size, self.data_count, self.data_size, self.location, self.next_loc_bytes = read_struct(buf, loc_bytes, 'qqqq')
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
414 self.loc_bytes = loc_bytes
415 self.label, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_LABEL_SIZE)
416 self.signal, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_SIGNAL_SIZE)
417 self.units, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_UNITS_SIZE)
418 self.natural_scale, self.next_loc_bytes = read_struct(buf, loc_bytes, 'q')
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
438 self.loc_bytes = loc_bytes
439 self.type, loc_bytes = read_struct(buf, loc_bytes, 'q')
440 self.name, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_CONTROL_NAME_SIZE)
441 self.description, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_COMMENT_SIZE)
442 self.signal, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_SIGNAL_SIZE)
443 self.units, loc_bytes = read_string16(buf, loc_bytes, ACQUIRE_UNITS_SIZE)
444 ints = read_struct(buf, loc_bytes, '9q')
445 self.natural_scale = ints[0]
446 self.reserved = ints[1:-1]
447 self.next_loc_bytes = ints[-1]
448
449
450
451
452
453
454
455
456
457 ACQUIRE_APERIODIC_CHANNEL_CONTROL = 0
458 ACQUIRE_APERIODIC_CHANNEL_IMAGE = 1
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
482 - def __init__(self, buf, loc_bytes):
483 self.loc_bytes = loc_bytes
484 (self.entry_size, self.control_channel,
485 self.frame, self.value,
486 self.text_size, self.data_size,
487 self.extended_data_size, self.extended_block_index,
488 self.next_loc_bytes) = read_struct(buf, loc_bytes, 'qqqdqqqq')
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
512 self.loc_bytes = loc_bytes
513 (self.file_tag, self.block_index,
514 self.block_type, self.block_size,
515 self.data_count, self.data_size,
516 self.channel, self.frame,
517 self.next_loc_bytes) = read_struct(buf, loc_bytes, 'qqqqqqqq')
518
519
520
521
522
523
524
525
526
527 ACQUIRE_DATA_BLOCK = 1
528 ACQUIRE_SWEEP_BLOCK = 2
529 ACQUIRE_CONTROL_BLOCK = 3
530 ACQUIRE_SERIES_BLOCK = 4
531 ACQUIRE_FILE_PREFIX_BLOCK = 5
532 ACQUIRE_SWEEP_PREFIX_BLOCK = 6
533
534
535
536
538 return divisor * int(ceil(i*1.0/divisor))
539
549
553
555 ai_ch, ap_ch = [], []
556 for i in xrange(header.analog_in_channel_count):
557 chan = AcquireAnalogInChannelRecord(buf, loc_bytes)
558 ai_ch.append(chan)
559 loc_bytes = chan.next_loc_bytes
560 for i in xrange(header.aperiodic_channel_count):
561 chan = AcquireAperiodicChannelRecord(buf, loc_bytes)
562 ap_ch.append(chan)
563 loc_bytes = chan.next_loc_bytes
564 block.analog_in_channel_records = ai_ch
565 block.aperiodic_channel_records = ap_ch
566
568 ai_ch_pre, ap_ch_pre = [], []
569 for i in xrange(header.analog_in_channel_count):
570 chan = AcquireSweepPrefixAnalogChannel(buf, loc_bytes)
571 ai_ch_pre.append(chan)
572 loc_bytes = chan.next_loc_bytes
573 for i in xrange(header.aperiodic_channel_count):
574 chan = AcquireSweepPrefixAperiodicChannel(buf, loc_bytes)
575 ap_ch_pre.append(chan)
576 loc_bytes = chan.next_loc_bytes
577 block.analog_in_channel_prefixes = ai_ch_pre
578 block.aperiodic_channel_prefixes = ap_ch_pre
579
580 -def ReadSweep(buf, loc_bytes, block, header):
581 sweep = AcquireSweepDescriptor(buf, loc_bytes)
582 sweep.ctrl_channels = []
583 ctrl_channels = AcquireChannelDescriptor(buf, sweep.next_loc_bytes)
584 loc_bytes = ctrl_channels.next_loc_bytes
585 for i in xrange(ctrl_channels.block_count):
586 chan = AcquireBlockDescriptor(buf, loc_bytes)
587 loc_bytes = chan.next_loc_bytes
588 sweep.ctrl_channels.append(chan)
589 sweep.ctrl_channels_ext = []
590 ctrl_channels_ext = AcquireChannelDescriptor(buf, loc_bytes)
591 loc_bytes = ctrl_channels_ext.next_loc_bytes
592 for i in xrange(ctrl_channels_ext.block_count):
593 chan = AcquireBlockDescriptor(buf, loc_bytes)
594 loc_bytes = chan.next_loc_bytes
595 sweep.ctrl_channels_ext.append(chan)
596 ints = read_struct(buf, loc_bytes, '%iq'%sweep.segment_count)
597 sweep.segment_dir, loc_bytes = ints[:-1], ints[-1]
598 ints = read_struct(buf, loc_bytes, '%iq'%header.analog_in_channel_count)
599 sweep.channel_dir, loc_bytes = ints[:-1], ints[-1]
600 sweep.segments = []
601 for i in xrange(sweep.segment_count):
602 seg = AcquireSegmentDescriptor(buf, loc_bytes)
603 loc_bytes = seg.next_loc_bytes
604 seg.channels = []
605 for j in xrange(header.analog_in_channel_count):
606 chan_d = AcquireChannelDescriptor(buf, loc_bytes)
607 loc_bytes = chan_d.next_loc_bytes
608 blocks = []
609 for k in xrange(chan_d.block_count):
610 block_desc = AcquireBlockDescriptor(buf, loc_bytes)
611 loc_bytes = block_desc.next_loc_bytes
612 blocks.append(block_desc)
613 seg.channels.append(blocks)
614 sweep.segments.append(seg)
615 block.sweep = sweep
616
623
625 tag = AcquireBlockTag(buf, loc_bytes)
626 block = Block(tag)
627 if tag.block_type == ACQUIRE_FILE_PREFIX_BLOCK:
628 ReadFilePrefix(buf, tag.next_loc_bytes, block, header)
629 elif tag.block_type == ACQUIRE_SWEEP_PREFIX_BLOCK:
630 ReadSweepPrefix(buf, tag.next_loc_bytes, block, header)
631
632 elif tag.block_type == ACQUIRE_SWEEP_BLOCK:
633 ReadSweep(buf, tag.next_loc_bytes, block, header)
634 elif tag.block_type == ACQUIRE_SERIES_BLOCK:
635 ReadSeries(buf, tag.next_loc_bytes, block, header)
636
637 return block, round_up(loc_bytes + tag.block_size, ACQUIRE_BLOCK_SIZE)
638
639
640
641
642
645 size = os.stat(path)[6]
646 self.fileno = os.open(path, os.O_RDONLY)
647 self.mmap = mmap.mmap(self.fileno, size, access=mmap.ACCESS_READ)
648 self.ai_channels = []
649 self.series = []
650 self.data_by_loc_bytes = {}
651 self.header = None
652 self.file_prefix = None
653 self.control = None
654 try:
655 self.read_blocks()
656 except:
657 if not (self.header and self.file_prefix and self.series):
658 raise
659 else:
660 traceback.print_exc()
661 self.sampling_sec = self.header.frame_interval * 1.0 / self.header.clock_rate
665 if self.mmap:
666 self.mmap.close()
667 self.mmap = None
668 os.close(self.fileno)
670 self.header, loc_bytes = ReadHeader(self.mmap)
671
672 self.sweep_prefix = None
673 self.sweeps = []
674 while True:
675 block, next_loc_bytes = ReadBlock(self.mmap, loc_bytes, self.header)
676 if block.tag.block_type == ACQUIRE_FILE_PREFIX_BLOCK:
677 self.file_prefix = block
678 self.ai_channels = self.file_prefix.analog_in_channel_records
679 elif block.tag.block_type == ACQUIRE_SWEEP_PREFIX_BLOCK:
680 self.sweep_prefix = block
681 elif block.tag.block_type == ACQUIRE_DATA_BLOCK:
682 self.data_by_loc_bytes[loc_bytes] = block
683 elif block.tag.block_type == ACQUIRE_SWEEP_BLOCK:
684 self.sweeps.append(block)
685 block.prefix = self.sweep_prefix
686 self.sweep_prefix = None
687 elif block.tag.block_type == ACQUIRE_SERIES_BLOCK:
688 self.series.append(block)
689 block.sweeps = self.sweeps
690 self.sweeps = []
691 elif block.tag.block_type == ACQUIRE_CONTROL_BLOCK:
692 self.control = block
693 break
694 loc_bytes = next_loc_bytes
695 - def read_data_int16(self, series, sweep, segment, channel, offset, count):
696 data = numpy.zeros(shape=(count,), dtype='int16')
697 ser = self.series[series]
698 swp = ser.sweeps[sweep]
699 seg = swp.sweep.segments[segment]
700 chn = seg.channels[channel]
701 at = 0
702 b = 0
703 while b < len(chn):
704 if offset < (at + chn[b].data_count):
705 break
706 else:
707 at += chn[b].data_count
708 b += 1
709 into = 0
710 while count and (b < len(chn)):
711 start = offset - at
712 count_here = min(count, chn[b].data_count - start)
713 segdata = self.decode(chn[b])
714 data[into:into+count_here] = segdata[start:start+count_here]
715 into += count_here
716 count -= count_here
717 offset += count_here
718 at += chn[b].data_count
719 b += 1
720 return data
722 ser = self.series[series]
723 swp = ser.sweeps[sweep]
724 ch_fi = self.file_prefix.analog_in_channel_records[channel]
725 ch_pre = swp.prefix.analog_in_channel_prefixes[channel]
726 idata = self.read_data_int16(series, sweep, segment, channel, offset, count)
727 fdata = idata.astype('float32')
728 fdata *= ( (1.0 / (ch_pre.gain * pow(1e3, ch_fi.natural_scale)))
729 * (ch_pre.maximum_voltage - ch_pre.minimum_voltage) / (ch_pre.maximum_value - ch_pre.minimum_value))
730 return fdata
731 - def decode(self, block_desc):
732 count = block_desc.data_count
733 size = block_desc.data_size
734 block = self.data_by_loc_bytes[block_desc.location]
735 loc = block.tag.next_loc_bytes
736 src = numpy.frombuffer(self.mmap, 'int8', size, loc)
737 src_16 = numpy.frombuffer(self.mmap, numpy.dtype('>i2'), size/2, loc)
738 src_16s = numpy.frombuffer(self.mmap, numpy.dtype('>i2'), size/2, loc+1)
739 dest = numpy.zeros(shape=(count,), dtype='int16')
740 decode_acquirefile_samples(src, src_16, src_16s, dest, size, count)
741 return dest
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
765 """Returns a scaled value of "val" as function return with a single character unit
766 scaler from the metric scale set as a tuple of the form (Scaled Value, Scaler Char+sUnit)"""
767 if value:
768 SuffixIndex=int(floor(log10(abs(value))/3))
769 if SuffixIndex!=0:
770
771
772
773
774
775
776
777 SuffixIndex = EnsureRange(SuffixIndex,-6,4)
778 return (value*10**(-3*SuffixIndex),'afpnum%KMGT'[SuffixIndex+6]+sUnit);
779 return (value,sUnit)
780
781
782
783 if __name__ == '__main__':
784 import sys
785 from itertools import *
786
787 f = AcquireFile(sys.argv[1])
788 for r, series in enumerate(f.series):
789
790 for w, sweep in enumerate(series.sweeps):
791
792 for g, seg in enumerate(sweep.sweep.segments):
793
794 at = 0
795 for chans in izip(*seg.channels):
796 bufs = [f.read_data_float32(r, w, g, c, at, block.data_count) for c, block in enumerate(chans)]
797
798 at += len(bufs[0])
799 for nums in izip(*bufs):
800 print '\t'.join([str(x) for x in nums])
801 print
802