NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/DPWP/trunk/DPWP/ADCP_splitter/adcpwaves.py

Revision 315 (checked in by gdusek, 14 years ago)

changes to mulitple files due to recent merge

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