Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Math Help - multiple binary files to ascii

  1. #1
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10

    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
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10
    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
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Sure, I'll give it a shot. Please upload an example .bin file so I can test the code.

    Elbarto
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10

    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
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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 03:41 AM.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10

    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
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    change the extension to .txt and try again. I can change back to .bin once I download the file.

    Elbarto
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10

    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 Attached Files
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10

    multiple binary files to ascii

    Yes Elbarto,

    I attach the ascii files equivalent here.

    Many thanks for your continuous help
    Zar
    Attached Files Attached Files
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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 04:44 PM. Reason: update code
    Follow Math Help Forum on Facebook and Google+

  14. #14
    Newbie
    Joined
    Nov 2009
    From
    HUntington Beach CA
    Posts
    6
    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.
    Follow Math Help Forum on Facebook and Google+

  15. #15
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    It is a continous list of floating point numbers in binary format. Ie 4 bytes per value as I understand it
    Follow Math Help Forum on Facebook and Google+

Page 1 of 2 12 LastLast

Similar Math Help Forum Discussions

  1. multiple regression, binary
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: March 5th 2011, 12:44 PM
  2. Categorical and binary responses in multiple linear regression
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: February 16th 2011, 02:59 PM
  3. MEX Files
    Posted in the Math Software Forum
    Replies: 5
    Last Post: July 7th 2008, 05:58 AM

Search Tags


/mathhelpforum @mathhelpforum