0

I have data stored in an array of size (4320,2160), reshaped from a list of length 4320*2160. When I save the file in binary format using numpy's tofile method, and then open the file I noticed that the array is double in length. How do I get the original values of the array? I'm assuming it has something to do with endianness, but i'm unfamiliar with dealing with it.

cdom=np.reshape(cdom, (4320,2160), order='F') # array of float values
len(cdom) # 4320*2160
cdom.tofile(filename)

arr = np.fromfile(filename, dtype=np.float32)
len(arr) # double the size of cdom: 2*4320*2160
5
  • What's mycdom? The result of the reshape? Commented May 8, 2015 at 15:26
  • @NathanBartley yes it is the result of the reshape. I should have been specific about that. Commented May 8, 2015 at 15:27
  • 3
    I suspect cdom has type np.float64, and you are reading the binary file as np.float32, so the length is doubled (and the values are effectively garbage). Commented May 8, 2015 at 15:33
  • @WarrenWeckesser You're right! Thank you! changing the dtype to float64 fixed the problem. Commented May 8, 2015 at 15:42
  • OK, I'll make my comment an answer... Commented May 8, 2015 at 15:45

1 Answer 1

2

It looks like cdom has type np.float64, and you are reading the binary file as np.float32, so the length is doubled (and the values are effectively garbage).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.