# Thread: multiple binary files to ascii

1. ## multiple binary files to ascii

Dear all,
I'm sorry for this primary issue. I'm using a Fortran program to read a single binary file and writes a single ascii file. As I have many files I would like the program to read and write to ascii those multiple binary files once.
Example files: c_002.bip_c1.bin, c_005.bip_c1.bin, c_006.bip_c1.bin,c_008.bip_c1.bin,c_010.bip_c1.bin ,c_013.bip_c1.bin etc. Only the numbers after the "c_" change from one file to the other.Below is the program:
implicit none
integer lat, lon, irec,x,y
real*4 bmap(180,90)
parameter(irec=180*90*4)
open(21,file=
& 'c_015.bip_c1.bin',
& access='direct',
& form='unformatted',
& recl=irec,status='old',err=999)
& ((bmap(lon,lat),lon=1,180),lat=1,90)
close(21)
open(31,file=
& 'c_015.bip_c1.txt',
& form='formatted')
do 123 y=1,90
write(31,30) (bmap(x,y),x=1,180)
30 Format(180F12.6)
123 continue
999 continue

stop
End

Hope somebody would help
Thanks
Zar

2. So do you want a solution in FORTRAN aswell? Is it possible to use another language? If so I would consider using Python for such tasks if it is an option. You could do something like this very neatly in Python I believe. You might be lucky enough to find someone on here with some FORTRAN experience tho.

Regards Elbarto

3. Originally Posted by elbarto
So do you want a solution in FORTRAN aswell? Is it possible to use another language? If so I would consider using Python for such tasks if it is an option. You could do something like this very neatly in Python I believe. You might be lucky enough to find someone on here with some FORTRAN experience tho.

Regards Elbarto
Thanks Elbarto,

Fortran was just the code I had in hand. I don't know much about Python. But if you may have easy to follow instructions in Python solving the issue of reading multiple binary files and writing in the corresponding ascii, I will greatly appreciate that.

Zar

4. Sure, I'll give it a shot. Please upload an example .bin file so I can test the code.

Elbarto

5. ## multiple binary files to ascii

Elbarto,

It's nice from you to give me a hand for this issue. I attach to this mail 2 of the binary files.

Thanks again

6. zar, I cannot see the files in your last post. If they are too large, maybe just post up some of the file just so I can see the format.

You will need a copy python. Use this version Python 2.5.4 Release (it is the best supported version at the moment).

Here is the code so far:
Code:
import os
num_converted = 0
for f in os.listdir(os.getcwd()):
if os.path.isdir(f):
continue
else:
try:
fname,ext = f.split('.')
if ext.lower() == 'bin':
#code here to convert to ascii
print(f + " converted to " + fname + ".txt")
num_converted += 1
except ValueError:
pass
print(str(num_converted) + " file(s) converted...")
raw_input("Press Enter to Exit...")
Just needs a few more lines to convert the file over (need to see the format to do this as I haven't worked with binary files before).

Elbarto

7. ## multiple binary files to ascii

Sorry Elbarto,

I realized later that the files could not be uploaded as the extesion is not among the list of extensions allowed to be send. I don't know what to do about this.

Zar

8. change the extension to .txt and try again. I can change back to .bin once I download the file.

Elbarto

9. ## multiple binary files to ascii

I changed the extension to .txt but I don't know if it keeps the files readable.

Thanks
Zar

10. Thanks Zar,

Just having a quick look now. Might have to take another look tomorrow. I am looking into the binascii python module to convert the file.

Out of curiosity what does the output file look like? Could you post a short example of that also?

I can read it to this format currently (sample shown only):
Code:
'00b8d5450018d6450030d4450060d6450070d2450080d34500f8d1450088d54500a0d6450080d34500c0d34500a0d4450050d5450000d24500b8d24500e8d5450000d8450070d64500b0d54500b0d4450040d44500a8d1450070d4450038d54500e8d7450058d5450060d74500b0d44500d8d1450080d3450010d3450050d4450008d54500e0d2450060d74500c8d44500b0d4450030d6450018d24500e8d34500b0d5450000d3450020d1450028d3450088d54500a8d64500f0d6450040d34500c8d0450098d54500f8d44500a8d64500c0d6450068d24500f8d2450068d24500c8d4450098d44500a8d34500a8d24500f0d34500d0d3450068d3450060d4450040d74500d0d3450038d14500d0d5450010d44500c8d54
Elbarto

11. ## multiple binary files to ascii

Yes Elbarto,

I attach the ascii files equivalent here.

Many thanks for your continuous help
Zar

12. ok. Thanks zar, that helps. I will take another look tomorrow night when I get home from uni (got to love final exams). I haven't worked much with binary before so this is an interesting little problem. There must be an easy way to convert this in python, I might make a post on the python forum about this if I cant find an elegant solution.

Regards Elbarto

13. Hi Zar,

Once you have installed python, create a new file and save this code as mhfBin2Ascii.py (or any other name that you prefer).

Make sure this code is in the same folder as all your .bin files your want converted, then simply double click the file and it will do the rest.

Code:
import os
import struct
def convertFile(fname_in, fname_out):
fin = open(fname_in,'rb')
fout = open(fname_out,'w')
while 1:
try:
fout.write('%0.0f\t' % val)
except:
break
fin.close()
fout.close()
num_converted = 0
for fname in os.listdir(os.getcwd()):
if os.path.isdir(fname):
continue
else:
try:
f,ext = fname.rsplit('.',1)
if ext.lower() == 'bin':
convertFile(fname,f + '.txt')
print(fname + " converted to " + f + ".txt")
num_converted += 1
except ValueError:
pass
print(str(num_converted) + " file(s) converted...")
raw_input("Press Enter to Exit...")
Just give it shot and check the formatting of the output files. My python skills are a little rough and I assume that there is no problem with the data file (ie no missing values etc). Let me know how you get on.

Regards Elbarto

14. What's in the Binary files, what made them?
I took a peak inside one with a binary editor, it's not a exe, Looks like pure data or a list of some sort.

15. It is a continous list of floating point numbers in binary format. Ie 4 bytes per value as I understand it

Page 1 of 2 12 Last