-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_dad_ch.py
119 lines (114 loc) · 2.45 KB
/
parse_dad_ch.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
#*******************************************************************************
# Copyright (c) 2015 Dr. Philip Wenig
#
# All rights reserved.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v1.0 which accompanies this distribution,
# and is available at http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Dr. Philip Wenig
#*******************************************************************************/
import struct
import math
#
# Parse all listed files.
#
for file in ['DAD1A.ch','DAD1B.ch','DAD1C.ch']:
#
# Create the binary file handle.
#
f = open(file, 'rb')
#
# Wavelength
#
f.seek(0x108B)
bytes = struct.unpack('>6B', f.read(6))
print "---------------------------"
print "File: ", file
print "Wavelength: ", chr(bytes[1]) + chr(bytes[3]) + chr(bytes[5])
print "---------------------------"
#
# Data
#
f.seek(0x1800)
f.read(2)
#
intensity = 0
marker = 0
toggle = 0
#
readData = 1
readFile = 1
#
while(readFile == 1):
try:
#
# Pre-evaluate
#
delta = struct.unpack('>h', f.read(2))[0]
if delta == 0x1019 or delta == 0x101A or delta == 0x1033:
readData = 0
if delta == 0x101A:
deltaNext = struct.unpack('>h', f.read(2))[0]
if abs(delta - deltaNext) <= 0xFF:
#
# Add 2x
#
intensity += delta
print intensity
intensity += deltaNext
print intensity
else:
#
# Skip
#
f.seek(f.tell() - 2)
else:
readData = 1
#
# Data
#
if readData == 1:
if delta == 0x1032:
deltaNext = struct.unpack('>h', f.read(2))[0]
if deltaNext == 0x10FF and abs(delta - deltaNext) <= 0xFF:
#
# Add 2x
#
intensity += delta
print intensity
intensity += deltaNext
print intensity
else:
#
# Skip
#
f.seek(f.tell() - 2)
else:
#
# 80 00 (-32768)
#
if delta == -32768:
delta = struct.unpack('>i', f.read(4))[0]
intensity = delta
print intensity
else:
deltaNext = struct.unpack('>h', f.read(2))[0]
if deltaNext == 0x1032 and abs(delta - deltaNext) <= 0xFF:
#
# Add 2x
#
intensity += delta
print intensity
intensity += deltaNext
print intensity
else:
#
# Add 1x
#
f.seek(f.tell() - 2)
intensity += delta
print intensity
except struct.error:
readFile = 0