6

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?

2
  • Instead of just doing your own file-parsing, can't you use a file format that's supported by NumPy and Matlab? Commented Jun 2, 2017 at 19:11
  • The file format is ".bin" and unfortunately this is the required format for input files for the model I'm using. Not sure if that answers your question. Commented Jun 2, 2017 at 19:15

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! As noted by @Xiangrui Li, the 'b' argument in the matlab function specifies big-endian.
Ah, good to know. I thought it was equivalent to the 'b' in pythons open (there it stands for "binary"). :)
That was my thought as well!
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.