I’m an uber-beginner with Python; I’ve rather been thrown into the deep end. A bit of background: the files we’re reading are from a sonar imaging camera; at the moment I’m trying to read in attributes written into the files such as date, filename, number of frames, number of beams, etc. First, I’d like to read in the FILE header. Then, for each frame, I’d like to read in the FRAME header. I need to read in the frame headers where the file headers have left off… I believe I need seek() to be able to do this. Here’s the code I have currently, to read the file headers (successfully done) and begin where that 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['???']=struct.unpack('26s',didson_data[58:84])
fileheader['idstring']=struct.unpack("33s",didson_data[84:117])
#fileheader['????2']=struct.unpack('235s',didson_data[117:352])
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()
Now if I run “main”, will I be able to read line by line? I’m not sure if it is one value per line… I basically went through and figured out byte by byte to figure out what header values were located where.
Any help would be appreciated!!
You read the entire contents of the file into
didson_data, then seek the file handlerdidson_fileback to zero, and never use it again as you’re splitting all your fields up fromdidson_dataand not stepping through lines/chunks in your file, so of course your second.tell()will still be at position zero as you haven’t moved anywhere since you seeked to position zero.