Seek () when reading in binary files

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['???']=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()

, "main", ? , ... , , .

!

+3
3

didson_data, didson_file , didson_data / , , , .tell() - , , .

+2

, . . , read :

didson_data=didson_file.read()

pos=didson_file.seek(0,0)

:

didson_data=didson_file.read(377)

, 377 frameinterval.

, , .

, , . , , , , ( ) + ( ), .

:

,

def get_chunk(fileobject):
    result = fileobject.read(1024)
    if len(result) == 0: # End of file
        return Null
    ## Determine what this is = thing 
    fileobject.seek(fileobject.tell()-1024+len(thing)
    return thing

Null

 while True:
        the_thing = get_chunk(didson_file)
        if not the_thing: # It a Null--it the end of the file
            return
        # process the_thing
# End the program

, - , . get_chunk Python. the_think, * # the_thing * .


readlines . , , . , , , , . , -, . , , ( ).

0

, , . , didson_file.read().

, , , :

with open("my_file_name") as f:
    for line in f:
        do_something_with_line(line)

In fact, since you have these structures that you need to parse, it is pretty clear that you are reading a binary. In this case, you should either discard all this (if memory usage is not a problem) or read it in chunks (more complicated, but does not allow memory usage).

0
source

All Articles