Search code examples
pythonbinarynastran

Read and decoding binary file to get results


I would like to read and decodinga binary file in order to get values.

Below the code I use to read the file :

with open("modele_petite_zone.op2", "rb") as op2File :
for lines in op2File :
    byte = op2File.read()
    print(byte)

and here a part of what I have in output :

b'\xaf\xc9Dq\xdd\xa8\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x9e\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97EHq\xccD{\xdc\xc3\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x9f\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97E3S\xc1D{4\xbf\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa8\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97E\xe1\xc2\xc2D\xc3\xed\xd7\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97Ef\xb6\xb7D\xa4\xd8\xd1\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb2\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97ER\x10\xb7DR\xc8\xea\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb5\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97E\xa40\xacD\xec1\xe3\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbc\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97E\xcd|\xa9D\x00@\xfc\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbd\xdd\x1f\x004\x08\x00\x00\x00\xb0\x97E\x1f\xed\x9eD\x1f\x15\xf3\xc44\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xec\x01\x00\x00\x04\x00\x00\x00\xfb\xff\xff\xff\x04\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x0c\x00\x00\x00\xff\xff\x00\x00\xff\xff\x00\x00\xff\xff\x00\x00\x0c\x00\x00\x00\x04\x00\x00\x00\xfa\xff\xff\xff\x04\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x04\x00\x00\x00\x08\x00\x00\x00GEOM3

What can I do to decoding the ouptut ?

Binary file can be downloaded here : https://www.dropbox.com/s/uimba2xojc55uii/modele_petite_zone.op2?dl=0


Solution

  • Binary format means that the saved file is not readable as text. However binary is not a standard that can be parsed easily, it only means that the data is saved as "data" (bits) instead of text. The actual format (or structure) can be open source or proprietary, meaning it might be parsable (if you know how to read the data) or near impossible to understand.

    The op2 format seems to be known and have modules implemented that can parse the files (look here for the format description https://docs.plm.automation.siemens.com/tdoc/nxnastran/11/help/#uid:index_dmap:xid666580:id496821).

    Checkout this module for parsing op2 files: https://pynastran-git.readthedocs.io/en/latest/index.html

    Example:

    from pyNastran.op2.op2 import OP2
    model = OP2()
    model.read_op2("modele_petite_zone.op2")
    print(model.get_op2_stats())