473,659 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.r aw. 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.dat a ... projection500.d ata 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 3705
On 10 Mar 2005 09:41:05 -0800, rumours say that "Albert Tu"
<sj*******@gmai l.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.ra w ... up to projection500.r aw. 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.dat a ... projection500.d ata so that our
visualizatio n 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*******@gma il.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.r aw ... up to projection500.r aw. 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.dat a ... projection500.d ata so that our
visualizati on 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_ENDIA N = 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_ENDIA N:
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*******@gmai l.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.ra w ... up to projection500.r aw. 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.dat a ... projection500.d ata so that our
visualizatio n 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
3052
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 single file, but then be able to take this one single file and recreate the files I put into it. Im really at a loss as to how I go about recovering the files?
3
4602
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 options. I can get this to worksuccessfully, but only when I'm NOT dealing with arrays in thestructure definition. For example: In C, you are able to simply read a binary file intoa structure as follows: ...
1
4297
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 time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
35
11418
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", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
3
18958
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 original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
7
3094
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 assumptions are, well, least unportable. In particular, do anyone know if there are real-life systems where the text file assumptions below don't hold? For text mode FILE*s,
3
18440
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 functions of the language. But wondering the advantages of searching and reading from a binary file. If the files get too large, I guess the advantage of reading from a binary file is that it is much faster. Regards,
12
5460
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 author of one of the python database clients mentioned that using one thread to retrieve the data from the oracle database and another to insert the data into postgresql with something like a pipe between the two threads might make sense, keeping...
3
2937
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 datatype for the file instead of using IMAGE datatype. I can not use SqlDataReader to read the data. I need your help, Thanks. -David (1) I have a table TestFile for testing: ID int FileName navrchar(255)
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.