Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: multiple binary files to ascii

  1. #16
    Newbie
    Joined
    Nov 2009
    From
    HUntington Beach CA
    Posts
    6
    The data did look like that, it was the same range of numbers over and over and over. Was wondering what it was. Did you ever get your program?

    You pretty good with math, I have a post I need help with. I may be able to write you a program pretty quick in visual basic, but I need to know the format you want. Comma Delimited text file so you can parse it, or an XML file
    http://www.mathhelpforum.com/math-he...t-pallets.html
    Follow Math Help Forum on Facebook and Google+

  2. #17
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    I would not worry about spending too much time on this problem mate - I dont think the OP has even visited the forum since I posted my solution but hey I learned a little about binary. The output format was tab delimited in case you are keen tho.

    I will have another look at your problem. I am in the process of moving so do not have easy access to the net at the moment. Maybe if you would like to find something similar to what you are trying to achieve (is there similar algorithms on the net?) then someone might be able to offer you a faster and more robust solution.


    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  3. #18
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10
    DearElbarto,

    I lost the contact with the forum for quite some time. Thanks for your kind reply and the Python decoder. It seems to work but the format is a problem. When I open the output text file in Excel I can't get the numbers in Real (not integers) aligned as a table of 180 columns and 90 rows. The output only gives a long row of integer values.
    As I'm not familiar with Python and if you are still around,I would appreciate your help or anybody else.
    Thanks
    Zar
    Follow Math Help Forum on Facebook and Google+

  4. #19
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10
    Dear Jkirkerx,

    The values are a series of pressure data(180 columns * 90 rows), some may be repetitive. I use Fortran to decode these. My problem is that I wanted to decode many files automatically rather than running the program for every file

    Thanks
    Zar
    Follow Math Help Forum on Facebook and Google+

  5. #20
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Zar - you are correct my code will only return a single row as is but if you know how many columns you are expecting this is pretty trivial as printing a \n character every 180 iterations will produce the format desired. I will have a go tonight or tommorow to recitfy this issue.

    As for not reading the numbers as floats - I will have to look into this as I am not familiar with binary at all. I suspect the problem might be the "'%0.0f" statment in my code that should maybe just be "%f". In other words it is being read correctly just not printed to any decimal places in my code.

    When I get 15 mins I will post a new solution up.

    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  6. #21
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10
    Dear Elbarto,

    It's really impressive to have you so promptly. Thank you very much for that. I'm almost there with your hint. I can read the data normally as "real" and now in columns using:
    fout.write('%6.5f\t\n' % val)

    But still have to figure out how to have the 180 columns.

    Thanks
    Zar
    Follow Math Help Forum on Facebook and Google+

  7. #22
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    This should be work I think. Are you expecting numbers after the decimal point? I only see trailing zeros.

    Code:
    import os
    import struct
    def convertFile(fname_in, fname_out): 
        NUM_COLS = 180 
        fin = open(fname_in,'rb')
        fout = open(fname_out,'w')
        isEOF = False
        while not isEOF:
            for i in range(NUM_COLS):
                try:
                    val = struct.unpack('f',fin.read(4))[0]
                    fout.write('%f\t' % val)
                except:
                    isEOF = True
                    break
            fout.write('\n')
        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...")
    Its a bit of an ugly code that im sure could be writen much simpler but its just the first thing I could bash out.
    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  8. #23
    Zar
    Zar is offline
    Newbie
    Joined
    Nov 2009
    Posts
    10
    Dear Elbarto,

    Many thanks for all. You've been really fast. The program works well. The zeros are normal. The result is absolutely what I expected. Great.

    You've been very helpful indeed and I appreciate that very much
    Thanks a lot
    Zar
    Follow Math Help Forum on Facebook and Google+

  9. #24
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    No problem Zar - python is a nice language for those sort of problems (and many more). There was probably a easy way to modify your existing code but you dont see too much FORTRAN on the forums.

    By the way that code is released as beerware
    Beerware - Wikipedia, the free encyclopedia

    Good luck with your project.

    Elbarto
    Follow Math Help Forum on Facebook and Google+

Page 2 of 2 FirstFirst 12

Similar Math Help Forum Discussions

  1. multiple regression, binary
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: Mar 5th 2011, 11:44 AM
  2. Categorical and binary responses in multiple linear regression
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: Feb 16th 2011, 01:59 PM
  3. MEX Files
    Posted in the Math Software Forum
    Replies: 5
    Last Post: Jul 7th 2008, 04:58 AM

Search Tags


/mathhelpforum @mathhelpforum