1 |
"""This code takes the raw waves portion of the adcp data from the splitter |
---|
2 |
and outputs pressure, range, orbital velocity and system info text files that |
---|
3 |
can be read into matlab. |
---|
4 |
|
---|
5 |
NOTE: Transducer height above bottom is hardwired into this code at .4m. |
---|
6 |
Need to update code in the future to be a user selected option. It is also |
---|
7 |
hardwired into the Matlab code radialtouvw.m and radial.m""" |
---|
8 |
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 |
import sys, struct, math, shutil, os |
---|
13 |
|
---|
14 |
try: |
---|
15 |
infileName = sys.argv[1] |
---|
16 |
|
---|
17 |
except: |
---|
18 |
print 'error' |
---|
19 |
sys.exit(1) |
---|
20 |
|
---|
21 |
|
---|
22 |
ifile = open(infileName, 'rb') |
---|
23 |
|
---|
24 |
# set the transducer height above bottom |
---|
25 |
thab = 0.4 |
---|
26 |
|
---|
27 |
|
---|
28 |
#header |
---|
29 |
firstHeader=31103 |
---|
30 |
Header=firstHeader |
---|
31 |
while Header == firstHeader: |
---|
32 |
readFirstHeader=ifile.read(2) |
---|
33 |
if len(readFirstHeader) == 0: |
---|
34 |
print('\n End of file') |
---|
35 |
break |
---|
36 |
|
---|
37 |
pressureOfile = open('pressure', 'w') |
---|
38 |
rangeOfile = open('range', 'w') |
---|
39 |
orbitOfile = open('orbit', 'w') |
---|
40 |
sysinfoOfile = open('sysinfo', 'w') |
---|
41 |
|
---|
42 |
Header =struct.unpack('H',readFirstHeader)[0] |
---|
43 |
#print 'headerID=', hex(Header) |
---|
44 |
if Header != 31103: |
---|
45 |
print(' \n error with file, check to see that file is a waves file') |
---|
46 |
break |
---|
47 |
|
---|
48 |
firstLeaderHeader=259 |
---|
49 |
wavePingHeader=515 |
---|
50 |
lastLeaderHeader=771 |
---|
51 |
HPRheader=1027 |
---|
52 |
|
---|
53 |
#read the checksum offset |
---|
54 |
csOffset=ifile.read(2) |
---|
55 |
csOffset=struct.unpack('H',csOffset)[0] |
---|
56 |
|
---|
57 |
#read 4 bytes before the leader ID |
---|
58 |
spare=ifile.read(1) |
---|
59 |
|
---|
60 |
readDatatypes=ifile.read(1) |
---|
61 |
datatypes=struct.unpack('B',readDatatypes)[0] |
---|
62 |
|
---|
63 |
readOffset=ifile.read(2) |
---|
64 |
offset=struct.unpack('H',readOffset)[0] |
---|
65 |
|
---|
66 |
#this is added in to account for extra bytes in the header |
---|
67 |
dataOffsetDif=offset-8 |
---|
68 |
ifile.read(dataOffsetDif) |
---|
69 |
|
---|
70 |
|
---|
71 |
#THIS SECTION IS FOR THE FIRST DATA TYPE |
---|
72 |
|
---|
73 |
|
---|
74 |
readLeaderHeader=ifile.read(2) |
---|
75 |
leaderHeader =struct.unpack('H',readLeaderHeader)[0] |
---|
76 |
#print hex(leaderHeader) |
---|
77 |
|
---|
78 |
if leaderHeader != firstLeaderHeader: |
---|
79 |
print('error with first leader header') |
---|
80 |
print hex(leaderHeader) |
---|
81 |
break |
---|
82 |
#read and write sysinfo |
---|
83 |
|
---|
84 |
#firmware version |
---|
85 |
readFwVersion=ifile.read(2) |
---|
86 |
fwVersion=struct.unpack('H',readFwVersion)[0] |
---|
87 |
sysinfoOfile.write(str(fwVersion)) |
---|
88 |
sysinfoOfile.write('\n') |
---|
89 |
|
---|
90 |
#Configuration (not sure what to do with this) |
---|
91 |
ifile.read(2) |
---|
92 |
|
---|
93 |
# number of depth cells |
---|
94 |
readNbins=ifile.read(1) |
---|
95 |
Nbins= struct.unpack('B',readNbins)[0] |
---|
96 |
sysinfoOfile.write(str(Nbins)) |
---|
97 |
sysinfoOfile.write('\n') |
---|
98 |
|
---|
99 |
#Samples per wave burst |
---|
100 |
readNumSamples=ifile.read(2) |
---|
101 |
numSamples=struct.unpack('H',readNumSamples)[0] |
---|
102 |
sysinfoOfile.write(str(numSamples)) |
---|
103 |
sysinfoOfile.write('\n') |
---|
104 |
|
---|
105 |
# Size of depth cell |
---|
106 |
readBinLength=ifile.read(2) |
---|
107 |
binLength= struct.unpack('H',readBinLength)[0] |
---|
108 |
#change size to meters |
---|
109 |
binLength=binLength/100.00 |
---|
110 |
sysinfoOfile.write(str(binLength)) |
---|
111 |
sysinfoOfile.write('\n') |
---|
112 |
|
---|
113 |
#Time between wave samples |
---|
114 |
readTBS=ifile.read(2) |
---|
115 |
TBS=struct.unpack('H',readTBS)[0] |
---|
116 |
sysinfoOfile.write(str(TBS)) |
---|
117 |
sysinfoOfile.write('\n') |
---|
118 |
|
---|
119 |
#Time between wave bursts |
---|
120 |
readTBB=ifile.read(2) |
---|
121 |
TBB=struct.unpack('H',readTBB)[0] |
---|
122 |
sysinfoOfile.write(str(TBB)) |
---|
123 |
sysinfoOfile.write('\n') |
---|
124 |
|
---|
125 |
# distance to first bin |
---|
126 |
readDist1=ifile.read(2) |
---|
127 |
dist1= struct.unpack('H',readDist1)[0] |
---|
128 |
#change distance to meters |
---|
129 |
dist1=dist1/100.00 |
---|
130 |
sysinfoOfile.write(str(dist1)) |
---|
131 |
sysinfoOfile.write('\n') |
---|
132 |
|
---|
133 |
|
---|
134 |
# number of bins output |
---|
135 |
readBinsOut=ifile.read(1) |
---|
136 |
binsOut= struct.unpack('B',readBinsOut)[0] |
---|
137 |
sysinfoOfile.write(str(binsOut)) |
---|
138 |
sysinfoOfile.write('\n') |
---|
139 |
|
---|
140 |
#reserved data (?) |
---|
141 |
ifile.read(2) |
---|
142 |
|
---|
143 |
#skip the directional bitmap |
---|
144 |
ifile.read(16) |
---|
145 |
|
---|
146 |
#read and write the bitmap for what bins are output |
---|
147 |
readByteList=ifile.read(16) |
---|
148 |
byteList=struct.unpack('16B',readByteList) |
---|
149 |
|
---|
150 |
def int2msbits(n, count=8): |
---|
151 |
"""returns the most-significant binary of integer n, using count number of digits""" |
---|
152 |
return "".join([str((n >> y) & 1) for y in range(0, count)]) |
---|
153 |
|
---|
154 |
bytemap = '' |
---|
155 |
bitmap = '' |
---|
156 |
for ind in range(len(byteList)): |
---|
157 |
byte = struct.pack('B', byteList[ind]) |
---|
158 |
bytemap += byte |
---|
159 |
bits = int2msbits(byteList[ind]) |
---|
160 |
bitmap += bits |
---|
161 |
|
---|
162 |
# find indexes from bit map |
---|
163 |
# (1) one way to get indices of 1's |
---|
164 |
ind = []; |
---|
165 |
for i in range(len(bitmap)): |
---|
166 |
if bitmap[i]=='1': ind.append(i) |
---|
167 |
|
---|
168 |
# transducer height above the bottom |
---|
169 |
th = thab |
---|
170 |
|
---|
171 |
# heights above transducer |
---|
172 |
hat = [dist1+(binLength*elem) for elem in ind] |
---|
173 |
|
---|
174 |
# heights above bottom |
---|
175 |
hab = [th+elem for elem in hat] |
---|
176 |
|
---|
177 |
#if the bins output = 3 or 4 need to include NaNs |
---|
178 |
|
---|
179 |
if binsOut == 4: |
---|
180 |
sysinfoOfile.write('NaN') |
---|
181 |
sysinfoOfile.write('\n') |
---|
182 |
elif binsOut == 3: |
---|
183 |
sysinfoOfile.write('NaN') |
---|
184 |
sysinfoOfile.write('\n') |
---|
185 |
sysinfoOfile.write('NaN') |
---|
186 |
sysinfoOfile.write('\n') |
---|
187 |
|
---|
188 |
for value in hab: |
---|
189 |
sysinfoOfile.write(str(value)) |
---|
190 |
sysinfoOfile.write('\n') |
---|
191 |
|
---|
192 |
|
---|
193 |
#Read start time and write to file |
---|
194 |
readCentury=ifile.read(1) |
---|
195 |
century =struct.unpack('B',readCentury)[0] |
---|
196 |
|
---|
197 |
readYear=ifile.read(1) |
---|
198 |
year =struct.unpack('B',readYear)[0] |
---|
199 |
|
---|
200 |
readMonth=ifile.read(1) |
---|
201 |
month=struct.unpack('B',readMonth)[0] |
---|
202 |
|
---|
203 |
readDay=ifile.read(1) |
---|
204 |
day=struct.unpack('B',readDay)[0] |
---|
205 |
|
---|
206 |
readHour=ifile.read(1) |
---|
207 |
hour=struct.unpack('B',readHour)[0] |
---|
208 |
|
---|
209 |
readMinute=ifile.read(1) |
---|
210 |
minute=struct.unpack('B',readMinute)[0] |
---|
211 |
|
---|
212 |
readSecond=ifile.read(1) |
---|
213 |
second=struct.unpack('B',readSecond)[0] |
---|
214 |
|
---|
215 |
readHundSec=ifile.read(1) |
---|
216 |
hundSec=struct.unpack('B',readHundSec)[0] |
---|
217 |
|
---|
218 |
startTime=str(century)+str('%02d' % year)+str('%02d' % month)+str('%02d' % day)+str('%02d' % hour)+str('%02d' % minute)+str('%02d' % second)+str('%02d' % hundSec) |
---|
219 |
|
---|
220 |
#create a time stamp for the file names that only includes from year up to minutes |
---|
221 |
FileStamp=str('%02d' % year)+str('%02d' % month)+str('%02d' % day)+str('%02d' % hour)+str('%02d' % minute) |
---|
222 |
|
---|
223 |
sysinfoOfile.write(startTime) |
---|
224 |
sysinfoOfile.write('\n') |
---|
225 |
|
---|
226 |
#Burst number (how to unpack this? dont think we need it) |
---|
227 |
readBurstNum=ifile.read(4) |
---|
228 |
|
---|
229 |
#Serial number (how to unpack this? dont think we need it) |
---|
230 |
readSerial=ifile.read(8) |
---|
231 |
|
---|
232 |
#Temperature (dont need this, use the avg temp instead) |
---|
233 |
readTemp=ifile.read(2) |
---|
234 |
|
---|
235 |
|
---|
236 |
#Reserved space (?) |
---|
237 |
ifile.read(4) |
---|
238 |
|
---|
239 |
cs=ifile.read(2) |
---|
240 |
|
---|
241 |
#should be the packets ID header again |
---|
242 |
test=ifile.read(2) |
---|
243 |
test=struct.unpack('H',test)[0] |
---|
244 |
#print hex(test); |
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 |
# Done with the First Leader Type>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
---|
249 |
|
---|
250 |
|
---|
251 |
#Wave Ping Type |
---|
252 |
|
---|
253 |
leaderHeader = wavePingHeader |
---|
254 |
while leaderHeader == wavePingHeader: |
---|
255 |
|
---|
256 |
|
---|
257 |
#read the checksum offset |
---|
258 |
csOffset=ifile.read(2) |
---|
259 |
if len(csOffset) == 0: |
---|
260 |
print('\n End of file') |
---|
261 |
break |
---|
262 |
csOffset=struct.unpack('H',csOffset)[0] |
---|
263 |
#print csOffset |
---|
264 |
|
---|
265 |
|
---|
266 |
#read the spare and datatype |
---|
267 |
spare=ifile.read(1) |
---|
268 |
|
---|
269 |
datatypes=ifile.read(1) |
---|
270 |
|
---|
271 |
#read the offset until the next datatype |
---|
272 |
offset=ifile.read(2) |
---|
273 |
offset=struct.unpack('H',offset)[0] |
---|
274 |
|
---|
275 |
#this is added in to account for extra bytes in the header |
---|
276 |
dataOffsetDif=offset-8 |
---|
277 |
ifile.read(dataOffsetDif) |
---|
278 |
|
---|
279 |
|
---|
280 |
readLeaderHeader=ifile.read(2) |
---|
281 |
leaderHeader =struct.unpack('H',readLeaderHeader)[0] |
---|
282 |
|
---|
283 |
# if the leader header isnt the wave ping header move on to the next leader |
---|
284 |
if leaderHeader != wavePingHeader: |
---|
285 |
break |
---|
286 |
|
---|
287 |
#read through sample number and time since burst (don't need this) |
---|
288 |
ifile.read(6) |
---|
289 |
|
---|
290 |
#read and write the pressure data |
---|
291 |
pressure=ifile.read(4) |
---|
292 |
pressure=struct.unpack('I',pressure)[0] |
---|
293 |
|
---|
294 |
pressureOfile.write(str(pressure)) |
---|
295 |
pressureOfile.write('\n') |
---|
296 |
|
---|
297 |
#read and write the range data |
---|
298 |
|
---|
299 |
range1=ifile.read(4) |
---|
300 |
range1=struct.unpack('I',range1)[0] |
---|
301 |
rangeOfile.write(str(range1)) |
---|
302 |
rangeOfile.write(' ') |
---|
303 |
|
---|
304 |
range2=ifile.read(4) |
---|
305 |
range2=struct.unpack('I',range2)[0] |
---|
306 |
rangeOfile.write(str(range2)) |
---|
307 |
rangeOfile.write(' ') |
---|
308 |
|
---|
309 |
range3=ifile.read(4) |
---|
310 |
range3=struct.unpack('I',range3)[0] |
---|
311 |
rangeOfile.write(str(range3)) |
---|
312 |
rangeOfile.write(' ') |
---|
313 |
|
---|
314 |
range4=ifile.read(4) |
---|
315 |
range4=struct.unpack('I',range4)[0] |
---|
316 |
rangeOfile.write(str(range4)) |
---|
317 |
rangeOfile.write('\n') |
---|
318 |
|
---|
319 |
|
---|
320 |
#read and write the orbital velocity data |
---|
321 |
|
---|
322 |
bytes=binsOut*8 |
---|
323 |
|
---|
324 |
while bytes > 0: |
---|
325 |
orbit=ifile.read(2) |
---|
326 |
orbit=struct.unpack('h',orbit)[0] |
---|
327 |
orbitOfile.write('%06d' % orbit) |
---|
328 |
orbitOfile.write(' ') |
---|
329 |
bytes=bytes-2 |
---|
330 |
|
---|
331 |
orbitOfile.write('\n') |
---|
332 |
|
---|
333 |
|
---|
334 |
# Done with the Wave Ping Type>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
---|
335 |
|
---|
336 |
# if the ADCP was set up to take heading, pitch and roll measurements |
---|
337 |
# for every ensemble needs to include this |
---|
338 |
|
---|
339 |
ID=ifile.read(2) |
---|
340 |
ID=struct.unpack('H',ID)[0] |
---|
341 |
#print hex(ID) |
---|
342 |
|
---|
343 |
if ID==HPRheader: |
---|
344 |
heading=ifile.read(2) |
---|
345 |
pitch=ifile.read(2) |
---|
346 |
roll=ifile.read(2) |
---|
347 |
spare=ifile.read(2) #not sure what this is |
---|
348 |
cs=ifile.read(2) |
---|
349 |
else: |
---|
350 |
ifile.seek(-2, 1) |
---|
351 |
spare=ifile.read(2) |
---|
352 |
cs=ifile.read(2) |
---|
353 |
|
---|
354 |
|
---|
355 |
ID=ifile.read(2) |
---|
356 |
if len(ID) == 0: |
---|
357 |
break |
---|
358 |
ID=struct.unpack('H',ID)[0] |
---|
359 |
|
---|
360 |
|
---|
361 |
|
---|
362 |
|
---|
363 |
#readLeaderHeader=ifile.read(2) |
---|
364 |
#leaderHeader =struct.unpack('H',readLeaderHeader)[0] |
---|
365 |
|
---|
366 |
if leaderHeader != lastLeaderHeader: |
---|
367 |
break |
---|
368 |
|
---|
369 |
#average depth |
---|
370 |
readAvgDepth= ifile.read(2) |
---|
371 |
avgDepth= struct.unpack('H', readAvgDepth)[0] |
---|
372 |
sysinfoOfile.write(str(avgDepth)) |
---|
373 |
sysinfoOfile.write('\n') |
---|
374 |
|
---|
375 |
#Average speed of sound in m/s |
---|
376 |
readAvgSound=ifile.read(2) |
---|
377 |
avgSound=struct.unpack('H',readAvgSound)[0] |
---|
378 |
sysinfoOfile.write(str(avgSound)) |
---|
379 |
sysinfoOfile.write('\n') |
---|
380 |
|
---|
381 |
#Average Temp in .01 deg C |
---|
382 |
readAvgTemp=ifile.read(2) |
---|
383 |
avgTemp=struct.unpack('H',readAvgTemp)[0] |
---|
384 |
sysinfoOfile.write(str(avgTemp)) |
---|
385 |
sysinfoOfile.write('\n') |
---|
386 |
|
---|
387 |
#heading |
---|
388 |
readHeading= ifile.read(2) |
---|
389 |
heading= struct.unpack('H', readHeading)[0] |
---|
390 |
sysinfoOfile.write(str(heading)) |
---|
391 |
sysinfoOfile.write('\n') |
---|
392 |
|
---|
393 |
#skip the std dev heading |
---|
394 |
ifile.read(2) |
---|
395 |
|
---|
396 |
#pitch |
---|
397 |
readPitch= ifile.read(2) |
---|
398 |
pitch= struct.unpack('h', readPitch)[0] |
---|
399 |
sysinfoOfile.write(str(pitch)) |
---|
400 |
sysinfoOfile.write('\n') |
---|
401 |
|
---|
402 |
#skip the std dev pitch |
---|
403 |
stdPitch=ifile.read(2) |
---|
404 |
|
---|
405 |
#Roll |
---|
406 |
readRoll= ifile.read(2) |
---|
407 |
roll= struct.unpack('h', readRoll)[0] |
---|
408 |
sysinfoOfile.write(str(roll)) |
---|
409 |
sysinfoOfile.write('\n') |
---|
410 |
|
---|
411 |
#skip std dev roll and last 4 bytes (what are they? maybe a checksum?) |
---|
412 |
stdRoll=ifile.read(2) |
---|
413 |
|
---|
414 |
cs=ifile.read(2) |
---|
415 |
|
---|
416 |
spare=ifile.read(2) |
---|
417 |
|
---|
418 |
|
---|
419 |
|
---|
420 |
|
---|
421 |
pressureOfile.close() |
---|
422 |
rangeOfile.close() |
---|
423 |
orbitOfile.close() |
---|
424 |
sysinfoOfile.close() |
---|
425 |
|
---|
426 |
|
---|
427 |
shutil.copy('pressure','pressure_'+FileStamp+'.txt') |
---|
428 |
shutil.copy('orbit','orbit_'+FileStamp+'.txt') |
---|
429 |
shutil.copy('range','range_'+FileStamp+'.txt') |
---|
430 |
shutil.copy('sysinfo','sysinfo_'+FileStamp+'.txt') |
---|
431 |
|
---|
432 |
os.remove('pressure') |
---|
433 |
os.remove('orbit') |
---|
434 |
os.remove('range') |
---|
435 |
os.remove('sysinfo') |
---|
436 |
|
---|
437 |
|
---|
438 |
ifile.close() |
---|
439 |
print 'processing complete' |
---|
440 |
|
---|
441 |
|
---|