henrifdrake
henrifdrake

Reputation: 63

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?

Upvotes: 6

Views: 6606

Answers (2)

MSeifert
MSeifert

Reputation: 152795

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.

Upvotes: 7

Xiangrui Li
Xiangrui Li

Reputation: 2436

Base on your matlab fopen, the file is in big endian ('b'). But your python code does not take care of the endianness.

Upvotes: 0

Related Questions