Can not get struct to read the entire binary file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Cousins

    Can not get struct to read the entire binary file.

    I succeed in extracting all of the header information, and then only a portion of the data. The data section is 123,410 doubles, but after retrieving 62 of the numbers, I can not go further, struct.unpack indicates that the string argument is the wrong length. I set up a loop to get the numbers one at a time, and it stops at the same place. The file is not corrupt, and will open in the original software. Does anyone have suggestions for figuring out the problem?
    Code:
    import struct, string, os
    
    bf = "F:/Junker/TestT/TU_TSA.grd"
    wbf = open(bf).read()  # "rb")
    start, stop = 0, struct.calcsize('<7l8d2l')
    
    #Retrieve the header information
    try:
        TheadID,TheadSize,HSver,TgridID,TgridSize,nRow,nCol,xLL,yLL,xSize,ySize,zMin,zMax,\
        Rotation,BlankVal,TdataID,TdataSize = struct.unpack('<7l8d2l', wbf[start:stop])
    except:
        print "Problem reading Header"
    print "TheadID ", TheadID, "\n ", "TheadSize ",TheadSize,"\n ","HSver ", HSver,"\n ","TheadID ",TgridID,"\n ","TgridSize ",TgridSize,"\n ","nRow ",nRow,"\n ","nCol ",nCol,"\n ","xLL ",xLL,"\n ","yLL ",yLL,"\n ","xSize ",xSize,"\n ","ySize ",ySize,"\n ","zMin ",zMin,"\n ","zMax ",zMax,"\n ","Rotation ",Rotation,"\n ","BlankVal ",BlankVal,"\n ","TdataID ",TdataID,"\n ","TdataSize",TdataSize
    
    #Retrieve the data
    try:
        x = nCol * nRow
        while x:
            start, stop = stop, stop + struct.calcsize('<d')
            DataBlock = struct.unpack('<d', wbf[start:stop])
            print DataBlock, " start ",start, " stop ",stop
    
    except:
        print "Failed at: start ",start, " stop ",stop
        
    del TheadID,TheadSize,HSver,TgridID,TgridSize,nRow,nCol,xLL,yLL,xSize,ySize,zMin,zMax,\
        Rotation,BlankVal,TdataID,TdataSize,start,stop
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Test the length of "wbf" and see if it is approximately the same size as the file. Also it is not being read in binary mode:
    Code:
    ##  should be
    wbf = open(bf, "rb").read()  # "rb")

    Comment

    • Jim Cousins
      New Member
      • Nov 2010
      • 1

      #3
      That is just embarrassing, I changed this so many times and tried so many things, that at some point I commented out the read binary mode, and never put it back. Grrrrrr.
      Thank you for pointing out what I was too close to see.

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        The answer is usually embarrassingly simple _after_ you know it.

        Comment

        Working...