Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old November 8th, 2009, 01:21 AM
Zar Zar is offline
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Zar is on a distinguished road
Default 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)
read(21,rec=1,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
Reply With Quote
Advertisement
 
  #2  
Old November 8th, 2009, 02:10 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
The following users thank elbarto for this useful post:
Donate to MHF
  #3  
Old November 8th, 2009, 02:29 AM
Zar Zar is offline
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Zar is on a distinguished road
Default

Quote:
Originally Posted by elbarto View Post
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.

Thanks in advance
Zar
Reply With Quote
  #4  
Old November 8th, 2009, 02:46 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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

Elbarto
Reply With Quote
  #5  
Old November 8th, 2009, 03:29 AM
Zar Zar is offline
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Zar is on a distinguished road
Default 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
Reply With Quote
  #6  
Old November 8th, 2009, 03:36 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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

Last edited by elbarto; November 8th, 2009 at 04:41 AM.
Reply With Quote
  #7  
Old November 8th, 2009, 03:47 AM
Zar Zar is offline
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Zar is on a distinguished road
Default 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
Reply With Quote
  #8  
Old November 8th, 2009, 03:54 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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

Elbarto
Reply With Quote
  #9  
Old November 8th, 2009, 04:10 AM
Zar Zar is offline
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Zar is on a distinguished road
Default multiple binary files to ascii

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

Thanks
Zar
Attached Files
File Type: txt c_002.bip_c1.txt (63.3 KB, 5 views)
File Type: txt c_005.bip_c1.txt (63.3 KB, 4 views)
Reply With Quote
  #10  
Old November 8th, 2009, 04:41 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
  #11  
Old November 8th, 2009, 05:02 AM
Zar Zar is offline
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Zar is on a distinguished road
Default multiple binary files to ascii

Yes Elbarto,

I attach the ascii files equivalent here.

Many thanks for your continuous help
Zar
Attached Files
File Type: txt c_002.txt (79.2 KB, 9 views)
File Type: txt c_005.txt (94.6 KB, 3 views)
Reply With Quote
  #12  
Old November 8th, 2009, 05:30 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
  #13  
Old November 9th, 2009, 06:22 AM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

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:
            val = struct.unpack('f',fin.read(4))[0]
            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

Last edited by elbarto; November 9th, 2009 at 05:44 PM. Reason: update code
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 01:55 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.