Home:ALL Converter>How to read and extract values from a binary file using python code?

How to read and extract values from a binary file using python code?

Ask Time:2019-05-19T14:38:27         Author:manubjayan

Json Formatter

I am relatively new to python. As part of my astronomy project work, I have to deal with binary files (which of course is again new to me). I was given a binary file and a python code which reads data from the binary file. I was then asked by my professor to understand how the code works on the binary file. I spent couple of days trying to figure out, but nothing helped. Can anyone here help me with the code?

# Read the binary opacity file
f = open(file, "r")

# read file dimension sizes
a = np.fromfile(f, dtype=np.int32, count=16)
NX, NY, NZ = a[1], a[4], a[7]


# read the time and time step
time, time_step = np.fromfile(f, dtype=np.float64, count=2)

# number of iterations
nite = np.fromfile(f, dtype=np.int32, count=1)

# radius array
trash = np.fromfile(f, dtype=np.float64, count=1)
rad = np.fromfile(f, dtype=np.float64, count=a[1])

# phi array
trash = np.fromfile(f, dtype=np.float64, count=1)
phi = np.fromfile(f, dtype=np.float64, count=a[4])

# close the file
f.close()

The binary file as far as I know contains several parameters (eg: radius, phi, sound speed, radiation energy) and its many values. The above code extract the values 2 parameters- radius and phi from the binary file. Both radius and phi have more than 100 values. The program works, but I am not able to understand how it works. Any help would be appreciated.

Author:manubjayan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56205361/how-to-read-and-extract-values-from-a-binary-file-using-python-code
Asmus :

The binary file is essentially just a long list of continuous data; you need to tell np.fromfile() both where to look and what type of data to expect. \nPerhaps it's easiest to understand if you create your own file:\n\nimport numpy as np\n\nwith open('numpy_testfile', 'w+') as f:\n ## we create a \"header\" line, which collects the lengths of all relevant arrays\n ## you can then use this header line to tell np.fromfile() *how long* the arrays are\n dimensions=np.array([0,10,0,0,10,0,3,10],dtype=np.int32)\n dimensions.tofile(f) ## write to file\n\n a=np.arange(0,10,1) ## some fake data, length 10\n a.tofile(f) ## write to file\n print(a.dtype)\n\n b=np.arange(30,40,1) ## more fake data, length 10\n b.tofile(f) ## write to file\n print(b.dtype)\n\n ## more interesting data, this time it's of type float, length 3\n c=np.array([3.14,4.22,55.0],dtype=np.float64) \n c.tofile(f) ## write to file\n print(c.dtype)\n\n a.tofile(f) ## just for fun, let's write \"a\" again\n\nwith open('numpy_testfile', 'r+b') as f:\n ### what's important to know about this step is that \n # numpy is \"seeking\" the file automatically, i.e. it is considering \n # the first count=8, than the next count=10, and so on \n # as \"continuous data\"\n dim=np.fromfile(f,dtype=np.int32,count=8)\n print(dim) ## our header line: [ 0 10 0 0 10 0 3 10]\n a=np.fromfile(f,dtype=np.int64,count=dim[1])## read the dim[1]=10 numbers\n b=np.fromfile(f,dtype=np.int64,count=dim[4])## and the next 10\n ## now it's dim[6]=3, and the dtype is float 10\n c=np.fromfile(f,dtype=np.float64,count=dim[6] )#count=30)\n ## read \"the rest\", unspecified length, let's hope it's all int64 actually!\n d=np.fromfile(f,dtype=np.int64) \n\nprint(a)\nprint(b)\nprint(c)\nprint(d)\n\n\nAddendum: the numpy documentation is quite explicit when it comes to discouraging the use of np.tofile() and np.fromfile():\n\n\n Do not rely on the combination of tofile and fromfile for data storage, as the binary files generated are are not platform independent. In particular, no byte-order or data-type information is saved. Data can be stored in the platform independent .npy format using save and load instead.\n\n\nPersonal side note: if you spent a couple of days to understand this code, don't feel discouraged of learning python; we all start somewhere. I'd suggest to be honest about the obstacles you've hit to your Professor (if this comes up in conversation), as she/he should be able to correctly assert \"where you're at\" when it comes to programming. :-)",
2019-05-19T08:08:26
yy