jbssm
jbssm

Reputation: 7151

How to read a binary file with a specific bytesize (32bit) values in Julia?

I'm trying to get started with Julia and decided to re-write a small program I made in Python that was taking too much time to run.

My 1st bump was on how to read some binary data I had written.

I have a file (gziped, although I think it's the same) written with tabbed binary data in "rows" of 6 Float32 numbers.

I found a way to read the 6 numbers row by row, but I don't know how to end reading (how to use EOF with read? ) and I also think that it would be better just to read the file right away instead of in 6 values at each time.

using GZip
dataFile = GZip.open("ACE_ions_fieldrotation.bin.gz", "r")
read(dataFile, Float32, 6)
# tried also this but it doesn't work, besides not allowing me to specify that I'm reading Float32 numbers:
# readuntil(dataFile, eof)
# it fails with the error: `read` has no method matching read(::GZipStream, ::Type{Function})
close(dataFile)

So, how do I go about doing this task? Any ideas?

EDIT:

These are the 1st 128 bytes of the GZip file using hexdump in case it helps.

0000000 1f 8b 08 08 0b 87 a1 54 02 ff 41 43 45 5f 69 6f
0000010 6e 73 5f 66 69 65 6c 64 72 6f 74 61 74 69 6f 6e
0000020 2e 62 69 6e 00 7c bd 75 58 97 dd d2 3d ae 22 25
0000030 dd dd dd dd 21 31 84 48 29 88 d8 18 28 16 22 62
0000040 8b 81 88 1d 80 85 8a 2d 06 28 26 d8 8d 22 26 60
0000050 2b d8 20 76 b7 f2 f5 3c bf df 79 df eb bc b3 f6
0000060 79 fe 7b c6 7d cd bd ef bd 67 af 59 b3 f6 dc 1f
0000070 da c4 87 46 ac bc 61 4a 23 e6 6a 53 9b bf ff c9
0000080

Upvotes: 2

Views: 1521

Answers (2)

jbssm
jbssm

Reputation: 7151

I found out the answer so I'll post it here for future reference.

We should use the readbytes function and it becomes pretty simple to read the bytes. After using the reshape and transpose (any way to get reshape to put this correctly right away without having to do the transpose?) we get the array with the correct dimensions.

using GZip
simDataFile = GZip.open("ACE_ions_fieldrotation.bin.gz", "r")
simData = reinterpret(Float32,readbytes(simDataFile))
close(simDataFile)
simArray = transpose(reshape(simData, 6, div(length(simData),6)))

Upvotes: 1

tholy
tholy

Reputation: 12179

Does

x = readuntil(dataFile, '\n')
y = reinterpret(Float32, x.data[1:end-1])

work for you?

Upvotes: 1

Related Questions