473,386 Members | 1,798 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,386 software developers and data experts.

How can I Read/Write multiple sequential Binary/Text data files

Dear there,

We have an x-ray CT system. The acquisition computer acquires x-ray
projections and outputs multiple data files in binary format (2-byte
unsigned integer) such as projection0.raw, projection1.raw,
projection2.raw ... up to projection500.raw. Each file is
2*1024*768-byte big.

I would like to read those files and convert to ascii files in %5.0f/n
format as projection0.data ... projection500.data so that our
visualization software can undersatnd the projection images. I was
trying to do this conversion using Python. However, I had troubles
declaring the file names using the do-loop index. Anyone had previous
experience?

Thanks,
Albert

Jul 18 '05 #1
3 3691
On 10 Mar 2005 09:41:05 -0800, rumours say that "Albert Tu"
<sj*******@gmail.com> might have written:
Dear there,

We have an x-ray CT system. The acquisition computer acquires x-ray
projections and outputs multiple data files in binary format (2-byte
unsigned integer) such as projection0.raw, projection1.raw,
projection2.raw ... up to projection500.raw. Each file is
2*1024*768-byte big.

I would like to read those files and convert to ascii files in %5.0f/n
format as projection0.data ... projection500.data so that our
visualization software can undersatnd the projection images. I was
trying to do this conversion using Python. However, I had troubles
declaring the file names using the do-loop index. Anyone had previous
experience?


Regular expressions could help, but if you *know* that these are the filenames,
you can (untested code):

PREFIX= "projection"
SUFFIX_I= ".raw"
SUFFIX_O= ".data"

import glob, struct

for filename in glob.glob("%s*%s" % (PREFIX, SUFFIX_I)):
number= filename[len(PREFIX):-len(SUFFIX_I)]
fpi= open(filename, "rb")
fpo= open("%s%s%s" % (PREFIX, number, SUFFIX_O), "w")
while 1:
datum= fpi.read(2)
if not datum: break
fpo.write("%5d\n" % struct.unpack("H", datum)) # check endianness!!!
fpi.close()
fpo.close()
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #2
On Thu, 10 Mar 2005 20:06:29 +0200, Christos "TZOTZIOY" Georgiou
<tz**@sil-tec.gr> wrote:
On 10 Mar 2005 09:41:05 -0800, rumours say that "Albert Tu"
<sj*******@gmail.com> might have written:
Dear there,

We have an x-ray CT system. The acquisition computer acquires x-ray
projections and outputs multiple data files in binary format (2-byte
unsigned integer) such as projection0.raw, projection1.raw,
projection2.raw ... up to projection500.raw. Each file is
2*1024*768-byte big.

I would like to read those files and convert to ascii files in %5.0f/n
format as projection0.data ... projection500.data so that our
visualization software can undersatnd the projection images. I was
trying to do this conversion using Python. However, I had troubles
declaring the file names using the do-loop index. Anyone had previous
experience?
Regular expressions could help, but if you *know* that these are the filenames,
you can (untested code):

PREFIX= "projection"
SUFFIX_I= ".raw"
SUFFIX_O= ".data"

import glob, struct


import array

DIFFERENT_ENDIAN = True/False

for filename in glob.glob("%s*%s" % (PREFIX, SUFFIX_I)):
number= filename[len(PREFIX):-len(SUFFIX_I)]
fpi= open(filename, "rb")
fpo= open("%s%s%s" % (PREFIX, number, SUFFIX_O), "w")
while 1:
datum= fpi.read(2)
if not datum: break
fpo.write("%5d\n" % struct.unpack("H", datum)) # check endianness!!!
If the OP knows that each input file is small enough (1.5Mb each as
stated), then the agony of file.read(2) can be avoided by reading the
whole file in one hit. The agony of struct.unpack() on each datum can
be avoided by using the array module. E.g. replace the whole 'while'
loop by this:

ary = array.array('H', fpi.read())
if DIFFERENT_ENDIAN:
ary.byteswap()
for datum in ary:
fpo.write("%5d\n" % datum)

Even if the input files were too large to fit in memory, they could
still be processed fast enough by reading a big chunk at a time.

fpi.close()
fpo.close()


Jul 18 '05 #3
On 10 Mar 2005 09:41:05 -0800, "Albert Tu" <sj*******@gmail.com> wrote:
Dear there,

We have an x-ray CT system. The acquisition computer acquires x-ray
projections and outputs multiple data files in binary format (2-byte
unsigned integer) such as projection0.raw, projection1.raw,
projection2.raw ... up to projection500.raw. Each file is
2*1024*768-byte big.

I would like to read those files and convert to ascii files in %5.0f/n
format as projection0.data ... projection500.data so that our
visualization software can undersatnd the projection images. I was Is there no chance of fixing the visualization software instead? The format seems
easy and efficient, and it seems a shame to make a redundant bloated copy
of the same info. What next? XML tags surrounding your ascii floats?

What platform are you on? What is the visualization software's method of
accessing data? Only files as you describe? Are you visualizing interactively,
or setting up batch processing?
trying to do this conversion using Python. However, I had troubles
declaring the file names using the do-loop index. Anyone had previous
experience?

Are you the same person who posted re this format some time ago?

Regards,
Bengt Richter
Jul 18 '05 #4

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

Similar topics

4
by: googlinggoogler | last post by:
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one...
3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
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...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
3
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
12
by: Sean Davis | last post by:
I am working on a simple script to read from one database (oracle) and write to another (postgresql). I retrieve the data from oracle in chunks and drop the data to postgresql continuously. The...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.