Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a float binary file into 2D arrays in python and matlab

I have some binary input files (extension ".bin") that describe a 2D field of ocean depth and which are all negative float numbers. I have been able to load them in matlab as follows:

f = fopen(filename,'r','b');
data = reshape(fread(f,'float32'),[128 64]);

This matlab code gives me double values between 0 and -5200. However, when I try to the same in Python, I strangely get values between 0 and 1e-37. The Python code is:

f = open(filename, 'rb')
data = np.fromfile(f, np.float32)
data.shape = (64,128)

The strange thing is that there is a mask value of 0 for land which shows up in the right places in the (64,128) array in both cases. It seems to just be the magnitude and sign of the numpy.float32 values that are off.

What am I doing wrong in the Python code?

like image 851
henrifdrake Avatar asked Jun 02 '17 18:06

henrifdrake


1 Answers

numpy.fromfile isn't platform independant, especially the "byte-order" is mentioned in the documentation:

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.

You could try:

data = np.fromfile(f, '>f4')  # big-endian float32

and:

data = np.fromfile(f, '<f4')  # little-endian float32

and check which one (big endian or little endian) gives the correct values.

like image 188
MSeifert Avatar answered Nov 15 '22 00:11

MSeifert