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

root/adcp/trunk/adcp/nortek/nortek.py

Revision 95 (checked in by gdusek, 16 years ago)

Adding RDI waves processing and Nortek processing.

Line 
1 """This code takes the raw data from the Nortek AWAC and prepares it
2 for matlab  You input a raw binary *.WPR file from the AWAC and it outs three Currents files with all of the currents
3 data, as well as a waves orbital velocity, range, pressure and sysinfo file for each wave burst"""
4
5
6 import sys, struct, math, shutil, os
7
8 try:
9     infileName = sys.argv[1]
10
11 except:
12     print 'error'
13     sys.exit(1)
14
15 ifile = open(infileName, 'rb')
16 testOfile=open('test','w')
17 currentsOfile = open('currents', 'w')
18 currentsHeaderOfile = open('currentsHeader','w')
19 currentsTiltOfile = open('currentsTilt','w')
20
21 currentsOfile.write('%this is the Nortek AWAC Currents data \n')
22 currentsHeaderOfile.write('%this is the Header data for the Nortek AWAC Currents data \n %it includes Header Info, Date info, Pressure and Temp \n')
23 currentsTiltOfile.write('%this is the Tilt readings (heading pitch and roll) for the Nortek AWAC Currents data \n')
24
25 # HARDWARE CONFIGURATION
26
27 #read header and make sure it is valid
28 HWconfID= 0x05a5
29
30 readHeaderID=ifile.read(2)
31 headerID=struct.unpack('H',readHeaderID)[0]
32 if headerID != HWconfID:
33     print 'error with Hardware Config Header'
34     sys.exit(1)
35
36 #size of structure in words (1 word = 2 bytes)
37 readSize=ifile.read(2)
38 size=struct.unpack('H',readSize)[0]
39 #compute the length in bytes - the checksum
40 length=(size*2)-2
41
42 #Serial Number (not sure what to do with this)
43 readSerial=ifile.read(14)
44
45 #Board Configuration
46 readBoardC=ifile.read(2)
47
48 #Board Frequency (kHz)
49 readBoardF=ifile.read(2)
50
51 #PIC Code Version #
52 readPIC=ifile.read(2)
53
54 #Hardware Revision
55 readHardRev=ifile.read(2)
56
57 #Recorder Size (*65536 bytes)
58 readRecSize=ifile.read(2)
59
60 #Status
61 readStatus=ifile.read(2)
62
63 #Spare
64 ifile.read(12)
65
66 #Firmware Version
67 readFirmVersion=ifile.read(4)
68 firmVersion=struct.unpack('I',readFirmVersion)[0]
69 currentsHeaderOfile.write(str(firmVersion))
70 currentsHeaderOfile.write('        %Firmware Version \n')
71
72 #Checksum
73 readCheckSum=ifile.read(2)
74
75
76
77 # HEAD CONFIGURATION
78
79 #read header and make sure it is valid
80 HeadConfID= 0x04a5
81
82 readHeaderID=ifile.read(2)
83 headerID=struct.unpack('H',readHeaderID)[0]
84 if headerID != HeadConfID:
85     print 'error with Head Config Header'
86     sys.exit(1)
87
88 #size of structure in words (1 word = 2 bytes)
89 readSize=ifile.read(2)
90 size=struct.unpack('H',readSize)[0]
91 #compute the length in bytes - the checksum
92 length=(size*2)-2
93
94 #Head Configuration
95 readHeadConfig=ifile.read(2)
96
97 #Head Frequency (kHz)
98 readHeadFreq=ifile.read(2)
99 headFreq=struct.unpack('H',readHeadFreq)[0]
100 currentsHeaderOfile.write(str(headFreq))
101 currentsHeaderOfile.write('        %head frequency in khz \n')
102
103 #Head Type
104 readHeadType=ifile.read(2)
105
106 #Head Serial Number
107 readHeadSerial=ifile.read(12)
108
109 #System Data
110 ifile.read(176)
111
112 #Spare
113 ifile.read(22)
114
115 #Number of beams
116 readNbeams=ifile.read(2)
117 Nbeams=struct.unpack('H',readNbeams)[0]
118 currentsHeaderOfile.write(str(Nbeams))
119 currentsHeaderOfile.write('        %number of beams \n')
120
121 #Checksum
122 readChecksum=ifile.read(2)
123
124
125
126 # USER CONFIGURATION
127
128 #read header and make sure it is valid
129 UserConfID= 0x00a5
130
131 readHeaderID=ifile.read(2)
132 headerID=struct.unpack('H',readHeaderID)[0]
133 if headerID != UserConfID:
134     print 'error with User Config Header'
135     sys.exit(1)
136
137 #size of structure in words (1 word = 2 bytes)
138 readSize=ifile.read(2)
139 size=struct.unpack('H',readSize)[0]
140 #compute the length in bytes - the checksum
141 length=(size*2)-2
142
143 #Transmit Pulse Length (counts)
144 readPulse=ifile.read(2)
145 pulse=struct.unpack('H',readPulse)[0]
146 currentsHeaderOfile.write(str(pulse))
147 currentsHeaderOfile.write('        %transmit pulse length (counts) \n')
148
149 #Blanking Distance (counts)
150 readBlanking=ifile.read(2)
151 blanking=struct.unpack('H',readBlanking)[0]
152
153 #Receive Length (counts)
154 readReceive=ifile.read(2)
155 receive=struct.unpack('H',readReceive)[0]
156 currentsHeaderOfile.write(str(receive))
157 currentsHeaderOfile.write('        %recieve length (counts) \n')
158
159 #Time Between Pings (counts)
160 readTimePing=ifile.read(2)
161 timePing=struct.unpack('H',readTimePing)[0]
162 currentsHeaderOfile.write(str(timePing))
163 currentsHeaderOfile.write('        %time between pings (counts) \n')
164                      
165 #Time Between Burst Sequence (counts)
166 readTimeBurst=ifile.read(2)
167 timeBurst=struct.unpack('H',readTimeBurst)[0]
168 currentsHeaderOfile.write(str(timeBurst))
169 currentsHeaderOfile.write('        %time between burst sequence (counts) \n')
170
171 #Number of Beam Sequences per Burst
172 readBeamSeq=ifile.read(2)
173 beamSeq=struct.unpack('H',readBeamSeq)[0]
174 currentsHeaderOfile.write(str(beamSeq))
175 currentsHeaderOfile.write('        %number of beam sequences per burst \n')
176
177 #Average interval (seconds)
178 readAvgInt=ifile.read(2)
179 avgInt=struct.unpack('H',readAvgInt)[0]
180 currentsHeaderOfile.write(str(avgInt))
181 currentsHeaderOfile.write('        %average interval (seconds) \n')
182
183
184 #Number of Beams (alread have this from before)
185 readNbeams=ifile.read(2)
186
187 #Timing Controller Mode
188 readTimeCont=ifile.read(2)
189
190 #Power Control Register
191 readPowerCont=ifile.read(2)
192
193 #Spare
194 ifile.read(6)
195
196 #Compass Update Rate
197 readCompRate=ifile.read(2)
198
199 #Coordinate System (0=ENU, 1=XYZ, 2=Beam)
200 readCoord=ifile.read(2)
201 coord=struct.unpack('H',readCoord)[0]
202 currentsHeaderOfile.write(str(coord))
203 currentsHeaderOfile.write('        %coord system, 0=ENU, 1=XYZ, 2=Beam \n')
204
205 #Number of Cells or Bins
206 readNcells=ifile.read(2)
207 Ncells=struct.unpack('H',readNcells)[0]
208 currentsHeaderOfile.write(str(Ncells))
209 currentsHeaderOfile.write('        %number of bins or cells \n')
210
211
212 #Cell Size
213 readCellSize=ifile.read(2)
214 cCellSize=struct.unpack('H',readCellSize)[0]
215
216 #compute the currents cell size
217 cell=cCellSize*100.0*(750/((headFreq*1000.0*4)/255.0))
218 cellint = math.floor(cell)
219 rawCellSize=0.01*cellint/256.0
220 cellSize=math.cos(25*(math.pi/180))*rawCellSize
221 currentsHeaderOfile.write(str('%3.2f' %cellSize))
222 currentsHeaderOfile.write('         %bin cell size (m) \n')
223
224 #compute the blanking distance
225 pLength=rawCellSize*100.0
226 cellBlanking=blanking*100.0*(750.0/32768.0)-pLength
227 cellBlanking=0.01*math.floor(cellBlanking)
228 cellBlanking=math.cos(25*(math.pi/180))*cellBlanking
229 currentsHeaderOfile.write(str('%3.2f' %cellBlanking))
230 currentsHeaderOfile.write('        %blanking distance (m) \n')
231
232 #compute and write the cell depths
233 currentsHeaderOfile.write(' % cell heights from bottom (meters) \n')
234 depths=[]
235 for i in range(1,Ncells+1):
236     depths.append(cellBlanking+cellSize*i)
237     currentsHeaderOfile.write(str('%3.2f' %depths[i-1]))
238     currentsHeaderOfile.write('\n')
239
240 #Measurement Interval
241 readMeasInt=ifile.read(2)
242 measInt=struct.unpack('H',readMeasInt)[0]
243 currentsHeaderOfile.write(str(measInt))
244 currentsHeaderOfile.write('        %measurement interval (seconds) \n')
245
246 #Recorder Deployment Name
247 readDepName=ifile.read(6)
248
249 #Recorder Wrap Mode
250 readRecWrap=ifile.read(2)
251
252 #Deployment Start Time (we can use the start time of the samples instead)
253 readStarTime=ifile.read(6)
254
255 #Time Between Diagnostic Measurements (seconds)
256 readDiagTime=ifile.read(4)
257 diagTime=struct.unpack('I',readDiagTime)[0]
258
259 #Mode
260 readMode=ifile.read(2)
261
262 #User Input Sound Speed Adjustment Factor
263 readAdjSound=ifile.read(2)
264
265 # Number of Samples in Diagnostic Mode
266 readNsamples=ifile.read(2)
267 Nsamples=struct.unpack('H',readNsamples)[0]
268
269 # Beam and Cell Number to Measure in Diagnostic Mode
270 readNbeamCell=ifile.read(2)
271
272 #Number Pings in Diagnostic Mode
273 readNpingsDiag=ifile.read(2)
274
275 #Mode Test
276 readModeTest=ifile.read(2)
277
278 #Analog Input Address
279 readAnalAddr=ifile.read(2)
280
281 #Software Version
282 readSoftVers=ifile.read(2)
283
284 #Spare
285 ifile.read(2)
286
287 #Velocity Adjustment Table
288 readVelAdjTable=ifile.read(180)
289
290 #File Comments
291 readFileComments=ifile.read(180)
292
293 #Mode for data rate and wave cell position
294 readMode=ifile.read(2)
295 byteList=struct.unpack('2B', readMode)
296
297 def int2msbits(n, count=8):
298     """returns the most-significant binary of integer n, using count number of digits"""
299     return "".join([str((n >> y) & 1) for y in range(0, count)])
300
301 bytemap = ''
302 bitmap = ''
303 for ind in range(len(byteList)):
304     byte = struct.pack('B', byteList[ind])
305     bytemap += byte
306     bits = int2msbits(byteList[ind])
307     bitmap += bits
308
309 #Percentage for Wave Cell Positioning
310 readPercWave=ifile.read(2)
311 PercWave=struct.unpack('H',readPercWave)[0]
312 PercWave=(PercWave/32767)*100
313
314 #Wave Transmit Pulse
315 readWavePulse=ifile.read(2)
316 wavePulse=struct.unpack('H',readWavePulse)[0]
317
318 #Fixed Wave Blanking Distance (counts)
319 readFixedBlanking=ifile.read(2)
320 fixedBlanking=struct.unpack('H',readFixedBlanking)[0]
321
322 #Wave Measurement Cell Size
323 readWaveCellSize=ifile.read(2)
324 waveCellSize=struct.unpack('H',readWaveCellSize)[0]
325
326 #Number of Diagnostic/Wave Samples
327 readWaveSamples=ifile.read(2)
328 waveSamples=struct.unpack('H',readWaveSamples)[0]
329
330 #Spare
331 ifile.read(8)
332
333 #Analog Output Scale Factor
334 readAnalogScale=ifile.read(2)
335
336 #Correlation Threshold
337 readCorrThresh=ifile.read(2)
338
339 #Spare
340 ifile.read(2)
341
342 #Transmit Pulse Length Second Lag (counts)
343 readPulseLag=ifile.read(2)
344
345 #Spare
346 ifile.read(30)
347
348 #Stage Match Filter Constants
349 readStageMatch=ifile.read(16)
350
351 #Checksum
352 readChecksum=ifile.read(2)
353
354
355
356 #  VELOCITY PROFILE DATA
357
358 #first create the lists that we can write the data to and then write to the file
359 currentsTime=[]
360 currentsPress=[]
361 currentsTemp=[]
362 currentsHeading=[]
363 currentsPitch=[]
364 currentsRoll=[]
365 allVel1=[]
366 allVel2=[]
367 allVel3=[]
368 allAmp1=[]
369 allAmp2=[]
370 allAmp3=[]
371
372 VelID= 0x20a5
373
374 readHeaderID=ifile.read(2)
375 headerID=struct.unpack('H',readHeaderID)[0]
376 if headerID != VelID:
377     print 'error with Velocity Profile Header'
378     sys.exit(1)
379    
380 while headerID == VelID:
381
382
383     #size in words
384     readSize=ifile.read(2)
385     if len(readSize) == 0:
386         break
387     size=struct.unpack('H',readSize)[0]
388     #compute the length in bytes - the checksum
389     length=(size*2)-2
390
391     # READ AND COMPUTE THE START TIME FOR THE CURRENT VEL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
392     #minute
393     readMinute=ifile.read(1)
394     minute=struct.unpack('B',readMinute)[0]
395     minute=str(hex(minute))[2:4]
396     #Second
397     readSecond=ifile.read(1)
398     second=struct.unpack('B',readSecond)[0]
399     second=str(hex(second))[2:4]
400     #day
401     readDay=ifile.read(1)
402     day=struct.unpack('B',readDay)[0]
403     day=str(hex(day))[2:4]
404     #hour
405     readHour=ifile.read(1)
406     hour=struct.unpack('B',readHour)[0]
407     hour=str(hex(hour))[2:4]
408     #year
409     readYear=ifile.read(1)
410     year=struct.unpack('B',readYear)[0]
411     year=str(hex(year))[2:4]
412     #month
413     readMonth=ifile.read(1)
414     month=struct.unpack('B',readMonth)[0]
415     month=str(hex(month))[2:4]
416     #century
417     century = str(20)
418
419     startTime=century+(year.zfill(2))+(month.zfill(2))+(day.zfill(2))+(hour.zfill(2))+(minute.zfill(2))+(second.zfill(2))
420
421     #create a time stamp for the file names that only includes from year up to minutes
422     FileStamp=(year.zfill(2))+(month.zfill(2))+(day.zfill(2))+(hour.zfill(2))+(minute.zfill(2))
423     currentsTime.append(FileStamp)
424    
425
426     #error code
427     readError=ifile.read(2)
428     error=struct.unpack('H',readError)[0]
429
430     #analog input 1
431     readAnalog1=ifile.read(2)
432     analog1=struct.unpack('H',readAnalog1)[0]
433
434     #Battery Voltage (.1 V)
435     readBattery=ifile.read(2)
436     battery=struct.unpack('H',readBattery)[0]
437
438     #Speed of Sound or Analog 2 (.1 m/s)
439     readSound=ifile.read(2)
440     sound=struct.unpack('H',readSound)[0]
441
442     #Heading (.1 deg)
443     readHeading=ifile.read(2)
444     heading=struct.unpack('H',readHeading)[0]
445     currentsHeading.append(heading)
446
447     #Pitch (.1 deg)
448     readPitch=ifile.read(2)
449     pitch=struct.unpack('h',readPitch)[0]
450     currentsPitch.append(pitch)
451
452     #Roll (.1 deg)
453     readRoll=ifile.read(2)
454     roll=struct.unpack('h',readRoll)[0]
455     currentsRoll.append(roll)
456
457     #pressure MSB in mm
458     readPressMSB=ifile.read(1)
459     pressureMSB=struct.unpack('B',readPressMSB)[0]
460
461     #status code
462     readStatus=ifile.read(1)
463
464     #pressure LSW in mm
465     readPressLSW=ifile.read(2)
466     pressureLSW=struct.unpack('H',readPressLSW)[0]
467
468     #compute the Pressure
469     pressure= 65536*pressureMSB + pressureLSW
470     currentsPress.append(pressure)
471
472     #Temperature (0.01 deg C)
473     readTemp=ifile.read(2)
474     temp=struct.unpack('h',readTemp)[0]
475     currentsTemp.append(temp)
476
477     #Spare
478     readSpare=ifile.read(88)
479    
480
481     #Read in the velocity data for beam 1-3 (mm/s) from cell 1(bottom) to n
482     numCells=3*Ncells
483     vel1=[]
484     vel2=[]
485     vel3=[]
486     while numCells > 0:
487         readVel=ifile.read(2)
488         vel=struct.unpack('h',readVel)[0]
489         if numCells > 2*Ncells:
490             vel1.append(vel)
491         elif numCells > Ncells:
492             vel2.append(vel)
493         else:
494             vel3.append(vel)
495         numCells -=1
496     allVel1.append(vel1)
497     allVel2.append(vel2)
498     allVel3.append(vel3)
499
500     #Read in the amplitude data for beam 1-3 (counts) from cell 1(bottom) to n
501  
502     numCells=3*Ncells
503     amp1=[]
504     amp2=[]
505     amp3=[]
506     while numCells > 0:
507         readAmp=ifile.read(1)
508         amp=struct.unpack('B',readAmp)[0]
509         if numCells > 2*Ncells:
510             amp1.append(amp)
511         elif numCells > Ncells:
512             amp2.append(amp)
513         else:
514             amp3.append(amp)
515         numCells -=1
516     allAmp1.append(amp1)
517     allAmp2.append(amp2)
518     allAmp3.append(amp3)       
519
520     #Add a fill byte if there are an odd number of cells
521     if Ncells/2 != 0:
522         ifile.read(1)
523
524     else:
525         ifile.read(0)
526
527     #Compute the Checksum
528     ifile.seek(-(length+1),1)
529     dataStruct=ifile.read(length)
530
531     total =0   
532     for byte in dataStruct:
533         value = struct.unpack('B', byte)[0]
534         total += value
535     #add 0xb58c in hex to put the cs in the right format
536     cs=total
537
538     #Read in the Checksum
539     readChecksum=ifile.read(2)
540     checksum=struct.unpack('H',readChecksum)[0]
541
542
543
544
545     # WAVE DATA HEADER
546
547     WdataID=0x31a5
548
549     #Read the Sync and ID as a 2 byte hex instead of seperately
550     readHeaderID=ifile.read(2)
551     if len(readHeaderID) == 0:
552         break
553     headerID=struct.unpack('H',readHeaderID)[0]
554
555      
556
557     while headerID == WdataID:
558
559         if headerID != WdataID:
560             break
561
562         pressureOfile = open('pressure', 'w')
563         rangeOfile = open('range', 'w')
564         orbitOfile = open('orbit', 'w')
565         sysinfoOfile = open('sysinfo', 'w')
566
567
568         #write the data from the hardware/user config to the sysinfoOfile
569         sysinfoOfile.write(str(coord))
570         sysinfoOfile.write('        %coord system, 0=ENU, 1=XYZ, 2=Beam \n')
571
572         sysinfoOfile.write(str(bitmap))
573         sysinfoOfile.write('         % bit1:data rate 0=1Hz,1=2Hz bit2:wave cell position 0=fixed,1=dynamic bit3=type of pos 0=% of mean press, 1=% of min press \n')
574    
575         sysinfoOfile.write(str(PercWave))
576         sysinfoOfile.write('         %percentage for wave cell position \n')
577
578    
579         #size in words
580         readSize=ifile.read(2)
581         size=struct.unpack('H',readSize)[0]
582         #compute the length in bytes - the checksum
583         length=(size*2)-2
584
585         # READ AND COMPUTE THE START TIME FOR THE WAVE HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
586         #minute
587         readMinute=ifile.read(1)
588         minute=struct.unpack('B',readMinute)[0]
589         minute=str(hex(minute))[2:4]
590         #Second
591         readSecond=ifile.read(1)
592         second=struct.unpack('B',readSecond)[0]
593         second=str(hex(second))[2:4]
594         #day
595         readDay=ifile.read(1)
596         day=struct.unpack('B',readDay)[0]
597         day=str(hex(day))[2:4]
598         #hour
599         readHour=ifile.read(1)
600         hour=struct.unpack('B',readHour)[0]
601         hour=str(hex(hour))[2:4]
602         #year
603         readYear=ifile.read(1)
604         year=struct.unpack('B',readYear)[0]
605         year=str(hex(year))[2:4]
606         #month
607         readMonth=ifile.read(1)
608         month=struct.unpack('B',readMonth)[0]
609         month=str(hex(month))[2:4]
610         #century
611         century = str(20)
612
613         startTime=century+(year.zfill(2))+(month.zfill(2))+(day.zfill(2))+(hour.zfill(2))+(minute.zfill(2))+(second.zfill(2))
614
615         #create a time stamp for the file names that only includes from year up to minutes
616         FileStamp=(year.zfill(2))+(month.zfill(2))+(day.zfill(2))+(hour.zfill(2))+(minute.zfill(2))
617
618         sysinfoOfile.write(startTime)
619         sysinfoOfile.write('        % this is the start time \n')
620
621
622
623         #Number of Wave Data records to follow
624         readWaveRecords=ifile.read(2)
625         waveRecords=struct.unpack('H',readWaveRecords)[0]
626
627         #Blanking Distance (counts)
628         readBlanking=ifile.read(2)
629         blanking=struct.unpack('H',readBlanking)[0]
630
631         #Battery Voltage
632         readBattery=ifile.read(2)
633
634         #Sound Speed
635         readSound=ifile.read(2)
636         sound=struct.unpack('H',readSound)[0]
637         sysinfoOfile.write(str(sound))
638         sysinfoOfile.write('         %speed of sound (0.1m/s) \n')
639
640         #Heading
641         readHeading=ifile.read(2)
642         heading=struct.unpack('H',readHeading)[0]
643         sysinfoOfile.write(str(heading))
644         sysinfoOfile.write('         %heading \n')
645
646         #Pitch
647         readPitch=ifile.read(2)
648         pitch=struct.unpack('h',readPitch)[0]
649         sysinfoOfile.write(str(pitch))
650         sysinfoOfile.write('         %pitch \n')
651
652         #Roll
653         readRoll=ifile.read(2)
654         roll=struct.unpack('h',readRoll)[0]
655         sysinfoOfile.write(str(roll))
656         sysinfoOfile.write('         %roll \n')
657
658         #Min Pressure Value (mm)
659         readMinPress=ifile.read(2)
660         minPress=struct.unpack('H',readMinPress)[0]
661         sysinfoOfile.write(str(minPress))
662         sysinfoOfile.write('         %min press (mm) \n')
663
664         #Max Pressure Value (mm)
665         readMaxPress=ifile.read(2)
666
667         #Temperature
668         readTemp=ifile.read(2)
669
670         #hCell Size (counts)
671         readCellSize=ifile.read(2)
672         hCellSize=struct.unpack('H',readCellSize)[0]
673
674      
675         #compute the wave cell size
676         cell=hCellSize*100.0*(750/((headFreq*1000.0*4)/255.0))
677         cellint = math.floor(cell)
678         cellSize=0.01*cellint/256.0
679         sysinfoOfile.write(str('%3.2f' %cellSize))
680         sysinfoOfile.write('         %wave cell size (m) \n')
681    
682         #compute the wave cell position
683         pLength=cellSize*100.0
684         cellDist=blanking*100.0*(750.0/32768.0)-pLength
685         cellDist=0.01*math.floor(cellDist)
686         beamCellPos=cellDist+cellSize
687         cellPos=math.cos(25*(math.pi/180))*beamCellPos
688         sysinfoOfile.write(str('%3.2f' %cellPos))
689         sysinfoOfile.write('         %wave cell position (m) \n')
690         testOfile.write(str('%3.2f' %cellPos))
691         testOfile.write(' \n')
692        
693         #Noise Amplitude Beam1
694         readAmpBeam1=ifile.read(1)
695
696         #Noise Amplitude Beam2
697         readAmpBeam2=ifile.read(1)
698
699         #Noise Amplitude Beam3
700         readAmpBeam3=ifile.read(1)
701
702         #Noise Amplitude Beam4
703         readAmpBeam4=ifile.read(1)
704
705         #Processing Mag Beam1
706         readMagBeam1=ifile.read(2)
707
708         #Processing Mag Beam2
709         readMagBeam2=ifile.read(2)
710
711         #Processing Mag Beam3
712         readMagBeam3=ifile.read(2)
713
714         #Processing Mag Beam4
715         readMagBeam4=ifile.read(2)
716
717         #Spare
718         ifile.read(14)
719
720         #Checksum
721         readChecksum=ifile.read(2)
722
723
724
725
726         #WAVE DATA
727
728
729
730         #what is the wave ID or the extra data structure ID
731         waveID= 0x30a5
732         extraID= 0x42a5
733        
734
735         headerID=waveID
736
737         while (headerID == waveID) or (headerID == extraID):
738
739             #Read the Sync and ID as a 2 byte hex instead of seperately
740             readHeaderID=ifile.read(2)
741             if len(readHeaderID) == 0:
742                 break
743             headerID=struct.unpack('H',readHeaderID)[0]
744
745             if (headerID != waveID) and (headerID != extraID):
746                 break
747
748
749             #if the extra data structure exists, read through it           
750             if headerID == extraID:
751                 readSize=ifile.read(2)
752                 size=struct.unpack('H',readSize)[0]
753                 ifile.read(size*2-2)
754                
755                
756             #size in words
757             readSize=ifile.read(2)
758             size=struct.unpack('H',readSize)[0]
759             #compute the length in bytes - the checksum
760             length=(size*2)-2
761
762             #Pressure
763             readPressure=ifile.read(2)
764             pressure=struct.unpack('H',readPressure)[0]
765             pressureOfile.write(str(pressure))
766             pressureOfile.write('\n')
767    
768             #AST 1
769             readAST1=ifile.read(2)
770             AST1=struct.unpack('H',readAST1)[0]
771             rangeOfile.write(str('%06d' % AST1))
772             rangeOfile.write(' \n')
773
774             #Analog input
775             aInput=ifile.read(2)
776
777             #Velocity Beam 1 (mm/s)
778             readVel1=ifile.read(2)
779             vel1=struct.unpack('h',readVel1)[0]
780             orbitOfile.write(str('%06d' % vel1))
781             orbitOfile.write('  ')
782
783             #Velocity Beam 2 (mm/s)
784             readVel2=ifile.read(2)
785             vel2=struct.unpack('h',readVel2)[0]
786             orbitOfile.write(str('%06d' % vel2))
787             orbitOfile.write('  ')
788
789             #Velocity Beam 3 (mm/s)
790             readVel3=ifile.read(2)
791             vel3=struct.unpack('h',readVel3)[0]
792             orbitOfile.write(str('%06d' % vel3))
793             orbitOfile.write('\n')   
794
795             #AST 2
796             readAST2=ifile.read(2)
797             AST2=struct.unpack('H',readAST2)[0]
798             rangeOfile.write(str('%06d' % AST2))
799             rangeOfile.write(' \n')
800
801             #Noise Amplitude for Beam 1
802             readAmp1=ifile.read(1)
803             amp1=struct.unpack('B',readAmp1)[0]
804
805             #Noise Amplitude for Beam 2
806             readAmp2=ifile.read(1)
807             amp2=struct.unpack('B',readAmp2)[0]
808
809             #Noise Amplitude for Beam 3
810             readAmp3=ifile.read(1)
811             amp3=struct.unpack('B',readAmp3)[0]
812
813             #AST quality
814             readASTQuality=ifile.read(1)
815             ASTQuality=struct.unpack('B',readASTQuality)[0]
816
817             #CheckSum
818             readChecksum=ifile.read(2)
819
820    
821
822         pressureOfile.close()
823         rangeOfile.close()
824         orbitOfile.close()
825         sysinfoOfile.close()
826
827         shutil.copy('pressure','pressure_'+FileStamp+'.txt')
828         shutil.copy('orbit','orbit_'+FileStamp+'.txt')
829         shutil.copy('range','range_'+FileStamp+'.txt')       
830         shutil.copy('sysinfo','sysinfo_'+FileStamp+'.txt')
831
832         os.remove('pressure')
833         os.remove('orbit')
834         os.remove('range')
835         os.remove('sysinfo')
836
837
838 #write the rest of the data to the currents file and rename
839
840 currentsHeaderOfile.write('%List of time records for this data set, YYMMDDHHMM \n')
841
842 for date in currentsTime:
843     currentsHeaderOfile.write(str(date))
844     currentsHeaderOfile.write('\n')
845
846 currentsHeaderOfile.write('%List of pressure readings for each current burst \n')
847
848 for press in currentsPress:
849     currentsHeaderOfile.write(str(press/1000.000))
850     currentsHeaderOfile.write('\n')
851
852 currentsHeaderOfile.write('%List of temp readings in degrees C for each current burst \n')
853
854 for temp in currentsTemp:
855     currentsHeaderOfile.write(str(temp/100.00))
856     currentsHeaderOfile.write('\n')
857
858 currentsTiltOfile.write('%List heading,pitch and roll values in degrees for each current burst \n')
859
860 for value in range(len(currentsHeading)):
861     currentsTiltOfile.write(str('%- 8.1f' %(currentsHeading[value]/10.0)))
862     currentsTiltOfile.write(str('%- 8.1f' %(currentsPitch[value]/10.0)))
863     currentsTiltOfile.write(str('%- 8.1f' %(currentsRoll[value]/10.0)))
864     currentsTiltOfile.write('\n')
865
866 #write the vel and amplitude data to the file
867
868 currentsOfile.write('% velocities and amplitudes with lines of Ncells for each burst \n')
869 currentsOfile.write('% velocities from lowest cell up, in m/s ---- amplitudes in counts \n')
870 currentsOfile.write('% vel1(beam1, x or u) vel2(beam2, y or v) vel3(beam3, z or w) ---- amp1(beam1) amp2(beam2) amp3(beam3) \n')
871
872
873 for list in range(len(allVel1)):
874     for i in range(0,Ncells):
875         currentsOfile.write(str('%- 8.3f' % (allVel1[list][i]/1000.000)))
876         currentsOfile.write(str('%- 8.3f' % (allVel2[list][i]/1000.000)))
877         currentsOfile.write(str('%- 8.3f' % (allVel3[list][i]/1000.000)))
878         currentsOfile.write(str('%-8d' % allAmp1[list][i]))
879         currentsOfile.write(str('%-8d' % allAmp2[list][i]))
880         currentsOfile.write(str('%-8d' % allAmp3[list][i]))
881         currentsOfile.write( '\n')
882
883    
884 currentsOfile.close()
885 currentsHeaderOfile.close()
886 currentsTiltOfile.close()
887 shutil.copy('currents','currents_'+currentsTime[0]+'.txt')
888 shutil.copy('currentsHeader','currentsHeader_'+currentsTime[0]+'.txt')
889 shutil.copy('currentsTilt','currentsTilt_'+currentsTime[0]+'.txt')
890
891 print 'End Of File, Processing Complete'
892
893 currentsOfile.close()
894 currentsHeaderOfile.close()
895 currentsTiltOfile.close()
896 pressureOfile.close()
897 rangeOfile.close()
898 orbitOfile.close()
899 sysinfoOfile.close()
900
901 os.remove('currents')
902 os.remove('currentsHeader')
903 os.remove('currentsTilt')
904
905 ifile.close()
906 testOfile.close()
Note: See TracBrowser for help on using the browser.