By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

Writing formatted data (numeric) to a binary file

P: n/a
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

Sep 2 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2005-09-02, Tr********@hotmail.com <Tr********@hotmail.com> wrote:
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.


http://docs.python.org/lib/module-struct.html

--
Grant Edwards grante Yow! I'm in DISGUISE as a
at BAGGAGE CHECKER....I can
visi.com watch the house, if it's
ORANGE...
Sep 2 '05 #2

P: n/a
Tr********@hotmail.com wrote:
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
rk***@ucsd.edu

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

Sep 2 '05 #3

P: n/a
On 1 Sep 2005 20:39:20 -0700, Tr********@hotmail.com declaimed the
following in comp.lang.python:

<snip>

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)
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Sep 2 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.