writing arrays to binary file

S

Sheldon

Hi everyone,

I have a short program the writes the output data to an acsii file:

import sys
import os
import string
import gisdata
from Numeric import *

def latlon(area_id, i, j):
lon_file = "lon_%s.dat" % area_id
flon = open(lon_file, 'wa')
lat_file = "lat_%s.dat" % area_id
flat = open(lat_file, 'wa')
for x in range(i):
for y in range(j):
flat.write("%f\n" % gisdata.xy2lonlat(area_id,x,y)[1])
flon.write("%f\n" % gisdata.xy2lonlat(area_id,x,y)[0])
flat.close()
flon.close()
#------------------------------------------------------------
if __name__ == '__main__':
tile_info ={"ibla_35n13e":[1215,1215],
"ibla_46n16e":[1215,1215],
"ibla_57n40w":[1215,1215],
}
for t in tile_info.keys():
xsize = tile_info[t][0]
ysize = tile_info[t][1]
result = latlon_(t, xsize, ysize)
Now this works but the output is in ascii form. Whenever I try to write
it binary form by creating the 2D array and then opening the file with
open("filename", 'wb'), I lose the decimals that are vital. The input
data is float with about 4 decimal places.
Does anyone have any ideas of how to improve this by writing the 2D
array in binary form?

Thanks,
Sheldon
 
F

Fredrik Lundh

Sheldon said:
I have a short program the writes the output data to an acsii file:

import sys
import os
import string
import gisdata
from Numeric import *

def latlon(area_id, i, j):
lon_file = "lon_%s.dat" % area_id
flon = open(lon_file, 'wa')
lat_file = "lat_%s.dat" % area_id
flat = open(lat_file, 'wa')
for x in range(i):
for y in range(j):
flat.write("%f\n" % gisdata.xy2lonlat(area_id,x,y)[1])
flon.write("%f\n" % gisdata.xy2lonlat(area_id,x,y)[0])
flat.close()
flon.close()
#------------------------------------------------------------
if __name__ == '__main__':
tile_info ={"ibla_35n13e":[1215,1215],
"ibla_46n16e":[1215,1215],
"ibla_57n40w":[1215,1215],
}
for t in tile_info.keys():
xsize = tile_info[t][0]
ysize = tile_info[t][1]
result = latlon_(t, xsize, ysize)

Now this works but the output is in ascii form. Whenever I try to write
it binary form by creating the 2D array and then opening the file with
open("filename", 'wb'), I lose the decimals that are vital. The input
data is float with about 4 decimal places.

define "binary form".

printing text to a file opened with the "b" flag doesn't make the contents
binary, in any normal sense of that word.

</F>
 
T

Travis E. Oliphant

Sheldon said:
Hi everyone,

I have a short program the writes the output data to an acsii file:

import sys
import os
import string
import gisdata
from Numeric import *

def latlon(area_id, i, j):
lon_file = "lon_%s.dat" % area_id
flon = open(lon_file, 'wa')
lat_file = "lat_%s.dat" % area_id
flat = open(lat_file, 'wa')
for x in range(i):
for y in range(j):
flat.write("%f\n" % gisdata.xy2lonlat(area_id,x,y)[1])
flon.write("%f\n" % gisdata.xy2lonlat(area_id,x,y)[0])
flat.close()
flon.close()
#------------------------------------------------------------
if __name__ == '__main__':
tile_info ={"ibla_35n13e":[1215,1215],
"ibla_46n16e":[1215,1215],
"ibla_57n40w":[1215,1215],
}
for t in tile_info.keys():
xsize = tile_info[t][0]
ysize = tile_info[t][1]
result = latlon_(t, xsize, ysize)
Now this works but the output is in ascii form. Whenever I try to write
it binary form by creating the 2D array and then opening the file with
open("filename", 'wb'), I lose the decimals that are vital. The input
data is float with about 4 decimal places.

You need to use the tostring() method and write that out in binary form.

flon = open(lon_file,'wb')
flon.write(your2darray.tostring())

of course you will need to manage byte-order issues if you will be
transferring this file to a different kind of processor.


Alternatively, with new NumPy there is a tofile method that you can use
to write in either ascii or binary form.


-Travis Oliphant
 
S

Sheldon

Thanks Travis,

I don't have the new NumPy yet but this tofile() method should work
fine.

/Sheldon
 
S

sven

hi list,
i'd like to send keystrokes to a (terminal) window.
the windowmanager is gnome (ubuntu).
what i want to do is to control dvgrab which can be
started in interactive mode.
thx in advance,

sven.
 
J

Johan Dahlin

sven said:
hi list,
i'd like to send keystrokes to a (terminal) window.
the windowmanager is gnome (ubuntu).
what i want to do is to control dvgrab which can be
started in interactive mode.
thx in advance,

This is not entirely trivial to do. The best way to do that would
be to use the wnck bindings which can be found in the python-gnome2-extras,
which unfortunately is broken on ubuntu/breezy.

There is a separate list for the gtk python bindings:

http://www.daa.com.au/mailman/listinfo/pygtk

Johan
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top