-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyDidson.py
374 lines (318 loc) · 12 KB
/
pyDidson.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import codecs
import struct
import os
import time
import csv
import datetime
# size = struct.calcsize("III?IfIIII?I32c256ciiiiII?IiIIIII120p")
# size = struct.calcsize("?")
# size = struct.calcsize("32c")
# size = struct.calcsize("L")
# size = struct.calcsize("I8cIIIIIIIIIIIIIIIIIIIIffffffffffffffffffffddfIfff?IIIfIffffff49152c")
StructSizes = {
"x" : 1, # pad byte no value
"c" : 1, # char bytes of length 1
"b" : 1, # signed char integer
"B" : 1, # unsigned char integer
"?" : 1, # _Bool bool
"h" : 2, # short integer
"H" : 2, # unsigned short integer
"i" : 4, # int integer
"I" : 4, # unsigned int integer
"l" : 4, # long integer
"L" : 4, # unsigned long integer
"q" : 8, # long long integer
"Q" : 8, # unsigned long long integer
"f" : 4, # float float
"d" : 8, # double float
"s" : 1, # char[] bytes
"p" : 8, # char[] bytes
"P" : 8, # void * integer
"32c": 32,
"256c": 256,
"120p": 120
}
dictTupleLengths = {
"f": 1,
"I": 1,
"i": 1,
"?": 1,
"d": 1,
"8c": 8,
"4c": 4,
"32c": 32,
"120p": 120,
"256c": 256,
"49152c": 49152,
"24576c": 24576
}
dictByteSizes = {
"f": 4,
"I": 4,
"i": 4,
"?": 1,
"d": 8,
"8c": 8,
"4c": 4,
"32c": 32,
"120p": 120,
"256c": 256,
"49152c": 49152,
"24576c": 24576
}
dictMasterAttributes = {
"Version": "4c",
"FrameTotal": "I",
"FrameRate": "I",
"HighResolution": "?",
"NumRawBeams": "I",
"SampleRate": "f",
"SamplesPerChannel": "I",
"ReceiverGain": "I",
"WindowStart": "I",
"WindowLength": "I",
"Reverse": "?",
"SN": "I",
"Date": "32c",
"HeaderID": "256c",
"UserID1": "i",
"UserID2": "i",
"UserID3": "i",
"UserID4": "i",
"StartFrame":"I",
"EndFrame": "I",
"TimeLapse": "?",
"RecordInterval": "I",
"RadioSeconds": "i",
"FrameInterval": "I",
"Flags": "I",
"AuxFlags": "I",
"Sspd": "I",
"3DFlags": "I"
#"RsvdData": "120p"
}
dictFrameAttributes = {
"FrameNumber": "I",
"FrameTime": "8c",
"Version": "I",
"Status": "I",
"Year": "I",
"Month": "I",
"Day": "I",
"Hour": "I",
"Minute": "I",
"Second": "I",
"H Second": "I",
"Transmit Mode": "I",
"Window Start": "I",
"Window Length": "I",
"Threshhold": "I",
"Intensity": "I",
"Receiver Gain": "I",
"Power Supply Temp C": "I",
"A/D Temp C": "I",
"Humidity": "I",
"Focus": "I",
"Battery": "I",
"User Value 1": "f",
"User Value 2": "f",
"User Value 3": "f",
"User Value 4": "f",
"User Value 5": "f",
"User Value 6": "f",
"User Value 7": "f",
"User Value 8": "f",
"Velocity": "f",
"Depth": "f",
"Altitude": "f",
"Pitch": "f",
"Pitch Rate": "f",
"Roll": "f",
"Roll Rate": "f",
"Heading": "f",
"Heading Rate": "f",
"Compass Heading": "f",
"Compass Pitch": "f",
"Compass Roll": "f",
"Latitude": "d",
"Longitude": "d",
"Sonar Position": "f",
"Config Flags": "I",
"Beam Tilt": "f",
"Target Range": "f",
"Target Bearing": "f",
"Target Present": "?",
"Firmware Revision": "I",
"Flags": "I",
"Source Frame": "I",
"Water Temp": "f",
"Timer Period": "I",
"Sonar X": "f",
"Sonar Y": "f",
"Sonar Z": "f",
"Sonar Pan": "f",
"Sonar Tilt": "f",
"Sonar Roll": "f",
"Data LF": "24576c",
"Data HF": "49152c"
}
# Recursively gets a list of all files, their sub directories, and their
# attributes in the given directory tree
def vRunFullTest():
#allSQLFiles = getListOfFiles('C:\\Users\\Nicholas.Shaffer\\Desktop')
#didsonTestFiles = getListOfFiles('Z:\\Habitat\\Data\\UHSI\\DIDSON\\2014\\UHSI_2014_DIDSON_AFSC')
# Read file tree.
print('Beginning read through directory tree.', datetime.datetime.now())
allDidsonFiles = getListOfFiles('Z:\\Habitat\\Data\\UHSI\\DIDSON')
#allDidsonFiles = getListOfFiles('Z:\\Habitat\\Data\\UHSI\\DIDSON\\2014\\UHSI_2014_DIDSON_AFSC\\UHSI.DIDSON.AFSC.August.07')
print('Fininshed read through directory tree.', datetime.datetime.now())
# Write info to csv file
try:
with open("C:\\Users\\Nicholas.Shaffer\\Desktop\\DIDSON Stuff\\DidsonAttributes.csv",
"w+", newline = '') as csvFile:
csvWriter = csv.writer(csvFile, delimiter=',')
csvWriter.writerow(['File Name', 'File Type', 'DateTime Modified', 'DateTime From Name', 'Location',
'Full Path', 'Version', 'Frame Total', 'Frame Rate', 'High Resolution', 'Num Raw Beams',
'Sample Rate', 'Samples Per Channel', 'Receiver Gain', 'Window Start', 'Window Length',
'Reverse', 'Sonar Serial Number', 'Date', 'HeaderID', 'UserID1', 'UserID2', 'UserID3', 'UserID4',
'Start Frame', 'End Frame', 'Time Lapse', 'Record Interval', 'Frame Interval', 'Flags', 'Aux Flags',
'Sound Velocity in Water', '3D Flags', 'Rsvd Data'])
csvWriter.writerows(allDidsonFiles)
print('Successfully saved as CSV.')
except PermissionError:
print("Permission error.")
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfItems = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for item in listOfItems:
# Create full path
fullPath = os.path.join(dirName, item)
# If item is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
if os.path.getsize(fullPath) > 200000:
try:
lstFileAttributes = lstGetFileAttributes(fullPath)
lstMasterAttributes = lstGetDidsonMasterHeader(fullPath)
lstAllAttributes = lstFileAttributes + lstMasterAttributes
lstAllAttributes = [[lstAllAttributes[j][i] for j in range(len(lstAllAttributes))] for i in range(len(lstAllAttributes[0]))]
allFiles.append(lstAllAttributes[1])
except:
print(fullPath)
return allFiles
def lstTranspose(lstInput):
return [[lstInput[j][i] for j in range(len(lstInput))] for i in range(len(lstInput[0]))]
def lstGetFileAttributes(FullFilePath):
#Get seconds since last updated
SecSinceModified = os.path.getmtime(FullFilePath)
#Convert to time object
TimeModified = time.localtime(SecSinceModified)
#Convert to readable string
DateTimeModified = time.strftime('%Y-%m-%dT%H:%M:%SZ', TimeModified)
#Separate file name and type
FileNameAndType = os.path.basename(FullFilePath)
FileName = os.path.splitext(FileNameAndType)[0]
FileType = os.path.splitext(FileNameAndType)[1]
# Search directory string for facility location
Location = None
if FullFilePath.find('AK') > 0 or FullFilePath.find('AFSC') > 0:
Location = 'AK'
elif FullFilePath.find('NW') > 0 or FullFilePath.find('NWFSC') > 0:
Location = 'NW'
elif FullFilePath.find('SE') > 0 or FullFilePath.find('SEFSC') > 0:
Location = 'SE'
# Get timestamp from the file name, if applicable
TimeFromFileName = None
if len(FileName) == 20 or len(FileName) == 25:
TimeFromFileName = FileName[0:10] + 'T' + FileName[11:13] + ':' + FileName[13:15] + ':' + FileName[15:17] + "Z"
else:
TimeFromFileName = 'N/A'
lstAttributes = list()
lstAttributes.append(["FileName", FileName])
lstAttributes.append(["FileType", FileType])
lstAttributes.append(["DateTimeModified", DateTimeModified])
lstAttributes.append(["TimeFromFileName", TimeFromFileName])
lstAttributes.append(["Location", Location])
lstAttributes.append(["FullFilePath", FullFilePath])
return lstAttributes
def lstGetDidsonMasterHeader(FullFilePath):
#Open file to read header information in the first 512 bytes
lstAttributes = list()
lstNames = list()
strFormat = ''
with open(FullFilePath, mode='rb') as didsonFile:
fileContent = didsonFile.read(512)
for i in dictMasterAttributes:
strFormat += dictMasterAttributes[i]
byBuffer = struct.calcsize(strFormat)
tupAllAttributes = struct.unpack(strFormat, fileContent[0:byBuffer])
iTupleCount = 0
for Attribute in dictMasterAttributes:
strType = dictMasterAttributes[Attribute]
iTupleSize = dictTupleLengths[strType]
Value = None
if iTupleSize == 1:
Value = tupAllAttributes[iTupleCount]
elif Attribute == 'Date' or Attribute == 'HeaderID':
PlaceHolder = 0
else:
Value = b''
for j in range(iTupleCount, iTupleCount + iTupleSize):
Value += tupAllAttributes[j]
Value = Value.decode()
iTupleCount += iTupleSize
lstAttributes.append([Attribute, Value])
return lstAttributes
def lstGetAllDidsonFrames(FullFilePath, iFrameTotal, strDataType, strDictIgnore):
lstFrames = list()
with open(FullFilePath, mode='rb') as didsonFile:
fileContent = didsonFile.read()
strFormat = ''
lstNames = list()
iFrameStartByte = 512
iCurrentByte = 512
for i in dictFrameAttributes:
if i != strDictIgnore:
strFormat += dictFrameAttributes[i]
lstNames.append(i)
lstFrames.append(lstNames)
#iFrameSize = dictFrameAttributes[strDataType]["Size"] + 256
iFrameSize = struct.calcsize(strFormat)
for _ in range(iFrameTotal+1):
tupFrameAttributes = struct.unpack(strFormat, fileContent[iFrameStartByte: iFrameStartByte + iFrameSize])
iTupleCount = 0
lstAttributes = list()
for Attribute in dictFrameAttributes:
if Attribute != strDictIgnore:
strType = dictFrameAttributes[Attribute]
iTupleSize = dictTupleLengths[strType]
iByteSize = dictByteSizes[strType]
Value = None
if iTupleSize == 1:
Value = tupFrameAttributes[iTupleCount]
# else:
# Value = b''
# for j in range(iTupleCount, iTupleCount + iTupleSize):
# Value += tupFrameAttributes[j]
# Value = Value.decode()
bCurrentBytes = fileContent[iCurrentByte: iCurrentByte + iByteSize]
iCurrentByte += iByteSize
iTupleCount += iTupleSize
lstAttributes.append([Attribute, Value])
lstAttributes = lstTranspose(lstAttributes)
lstFrames.append(lstAttributes[1])
iFrameStartByte += iFrameSize
return lstFrames
# Current test files
#strFile = "Z:\\Habitat\\Data\\UHSI\\DIDSON\\2014\\UHSI_DIDSON_NWFSC\\UHSI.DIDSON.NWFSC.August.11.07\\2014-08-09_210855_HF_S001.ddf"
#strFile = "Z:\\Habitat\\Data\\UHSI\\DIDSON\\2015\\UHSI.DIDSON.2015.NW\\UHSI.2015.Station.07\\102\\2015-08-04_#195.ddf"
strFile = "C:\\Users\\Nicholas.Shaffer\\Desktop\\2016-10-29_183900_SyncOut.ddf"
test = lstGetDidsonMasterHeader(strFile)
test = lstGetAllDidsonFrames(strFile, 719, 'Data HF', 'Data LF')
print('DONE.')
# TODO lstGetAllDidsonFrames is unfinished. Still has issues. Needs finalizing/testing. Compare raw bytes to string.
# TODO add better/more comments for documentation