I am starting uber with Python; I was rather thrown into the deep end. A bit of background: the files we read are an image of a sonar camera; I'm currently trying to read attributes written to files, such as date, file name, number of frames, number of rays, etc. First, I would like to read in the FILE header. Then for each frame I would like to read in the FRAME header. I need to read in the frame headers where the file headers stopped ... I believe I need seek () to do this. Here is the code that I currently have for reading the file headers (successfully executed) and starting from where this information ends for the frame headers:
EDITED CODE:
import math, struct
def __init__(didson):
print "this better work"
def get_file_header(data,offset=0):
fileheader={}
winlengths=[1.125,2.25,4.5,9,18,36]
fileheader['filetype']=struct.unpack("3s",didson_data[0:3])
fileheader['fileversion']=struct.unpack('B',didson_data[3:4])[0]
fileheader['numframes']=struct.unpack('l',didson_data[4:8])
fileheader['framerate']=struct.unpack('l',didson_data[8:12])
fileheader['resolution']=struct.unpack('i',didson_data[12:16])
fileheader['numbeams']=struct.unpack('i',didson_data[16:20])
fileheader['samplerate']=struct.unpack('f',didson_data[20:24])
fileheader['samplesperchannel']=struct.unpack('l',didson_data[24:28])
fileheader['receivergain']=struct.unpack('l',didson_data[28:32])
fileheader['windowstart']=struct.unpack('i',didson_data[32:36])
fileheader['winlengthsindex']=struct.unpack('i',didson_data[36:40])
fileheader['reverse']=struct.unpack('l',didson_data[40:44])
fileheader['serialnumber']=struct.unpack('l',didson_data[44:48])
fileheader['date']=struct.unpack("10s",didson_data[48:58])
fileheader['idstring']=struct.unpack("33s",didson_data[84:117])
fileheader['framestart']=struct.unpack('i',didson_data[352:356])
fileheader['frameend']=struct.unpack('i',didson_data[356:360])
fileheader['timelapse']=struct.unpack('i',didson_data[360:364])
fileheader['recordInterval']=struct.unpack('i',didson_data[364:368])
fileheader['radioseconds']=struct.unpack('i',didson_data[368:372])
fileheader['frameinterval']=struct.unpack('i',didson_data[372:376])
return fileheader
def num_datagrams(didson_data):
assert(len(didson_data) % datagram_size==0)
return len(didson_data)/datagram_size
def get_offset(datagram_number):
return datagram_number * datagram_size
def didson_print(fileheader):
print fileheader
for key in fileheader:
print ' ',key, fileheader[key]
def main():
didson_file=open('C:/vprice/DIDSON/DIDSON Data/test.ddf', 'rb')
didson_data=didson_file.read()
print 'Number of datagrams:', num_datagrams(didson_data)
didson_print(datagram)
if __name__=='main':
main()
, "main", ? , ... , , .
!