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

root/DPWavesProc/trunk/DPWavesProc/ADCP_splitter/adcpwaves.py

Revision 225 (checked in by gdusek, 15 years ago)

Re-organized directory structure and cleaned up some code.

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