Writing formatted data (numeric) to a binary file

T

Trevor.Dhu

I am just starting to use python, and as such my question may well be a
bit simplistic. I am currently trying to write what should be a very
basic set of routines for reading and writing to a binary grid format
(ERMapper). However:

I am struggling to find the correct tools to write a row of a
2-dimensional array to a binary file. In matlab the command looks
like:

fid=fopen(filename,'wb');
for y=1:NrOfLines
fwrite(fid,in(y,:)','real*4');
end
fclose(fid);

I'm conscious that the "write" command would allow me to write text to
a binary file, but I'm not clear how to:
1) format my numeric data such that it matches the real*4 format
2) parse an entire row of a 2D array without using a for loop.

If anyone has any suggestions on how to do this (or even hints as to
what modules I should be looking at) they would be greatly appreciated.
Alternativly, if anyone has written routines for dealing with basic
ERMapper grids (as opposed to the wavelet compressed data) I'd love to
save myself from re-inventing the wheel.

Thanks
Trevor Dhu
 
R

Robert Kern

I'm conscious that the "write" command would allow me to write text to
a binary file, but I'm not clear how to:
1) format my numeric data such that it matches the real*4 format
2) parse an entire row of a 2D array without using a for loop.

For homogeneous blocks of numbers like this, you'll probably want to use
Numeric of numarray.

http://numeric.scipy.org

import Numeric
data = Numeric.array(all_of_the_numbers, Numeric.Float32)
s = data.tostring()
f = open(filename, 'wb')
f.write(s)
f.close()

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
D

Dennis Lee Bieber

On 1 Sep 2005 20:39:20 -0700, (e-mail address removed) declaimed the
following in comp.lang.python:

said:
I'm conscious that the "write" command would allow me to write text to
a binary file, but I'm not clear how to:
1) format my numeric data such that it matches the real*4 format

Look at the struct module
2) parse an entire row of a 2D array without using a for loop.
Suspect you won't be able to avoid it, unless it is a predetermined
length (so you can supply enough struct formats AND destination
variables in one call)
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top