473,396 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

writing arrays to binary file

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

Jan 25 '06 #1
5 4785
Sheldon wrote:
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>

Jan 25 '06 #2
Sheldon wrote:
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

Jan 25 '06 #3
Thanks Travis,

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

/Sheldon

Jan 25 '06 #4
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.

Jan 25 '06 #5
sven wrote:
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

Jan 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
3
by: Romain | last post by:
Hello, I am writing out a binary file. I figured that the number "10" is automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are line feed and carriage return. I know it does...
2
by: phyzics | last post by:
I am porting an application from C++ to C#, and am having trouble finding a way to quickly and efficiently write structures to a binary file. In C++ this is trivial because all that is necessary is...
2
by: DBC User | last post by:
Hi Sharpies, I have a C program I am converting it into C#. Everything is fine except this process creates a 6K byte binary file. This file initially filled with 6K null and then start...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
5
by: montyphyton | last post by:
as i understand there are two ways to write data to a file: using f.write("foo") and print >>f, "foo". what i want to know is which one is faster (if there is any difference in speed) since i'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.