Results 1 to 11 of 11

Thread: reading a binary file full of floats and/or doubles

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reading a binary file full of floats and/or doubles

    Thanks for the replies guys/gals.

    I will try and avoid the C/C++ route if I can as I want to keep everything Qt if I can. I will investigate the QFile option more thoroughly. As long as I can set my read position aka seek then I should be alright (QIODevice). I did go over my code last night and cleaned it up somewhat. Being the noob that I am I still need to get used to not having a bunch of really convenient data reading/writing tools as I had in IDL (Look for RSI ENVI if you are curious).

    Cheers
    Oz

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: reading a binary file full of floats and/or doubles

    If your reading machine is little-endian (most are) then the problem should be trivial:
    Qt Code:
    1. const int vectSize = 531;
    2. const int blockSize = vectSize * sizeof(float);
    3. QVector<float> vect(vectSize, 0.0f);
    4.  
    5. int desiredBlock = 0; // zero based, e.g 0..49999
    6. QFile file("data");
    7. if (file.open(QIODevice::ReadOnly)) {
    8. if (file.seek(desiredBlock * blockSize)) {
    9. qint64 bytes = file.read(reinterpret_cast<char*>(vect.data()), blockSize);
    10. if (bytes != blockSize)
    11. qFatal("Oops!");
    12.  
    13. qDebug() << vect;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Effectively just dropping the raw bytes into the internal buffer of the QVector.

    You can use:
    Qt Code:
    1. #if Q_BYTE_ORDER == Q_BIG_ENDIAN
    2. // byte swapping code
    3. #endif
    To copy to clipboard, switch view to plain text mode 
    at line 12 if you need portability.

  3. The following user says thank you to ChrisW67 for this useful post:

    OzQTNoob (9th February 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: reading a binary file full of floats and/or doubles

    Qt is C++, so everywhere you use Qt, C++ mechanisms will work as well.

    As for convenient tools -- I don't know what conveniences you need to read four bytes from a file. Your current code is not very optimal, for instance performing seek in every iteration is a waste of time since the file pointer is already positioned in the right place.

    The fastest way to read, say... 1000 floats is the following:

    Qt Code:
    1. QFile file(...);
    2. file.open(...);
    3. file.seek(...);
    4. float array[1000];
    5. QByteArray ba = file.read(1000*sizeof(float)); // reading 4000 bytes is faster than reading 4 bytes 1000 times (even though the disk caches the data)
    6. memcpy(&array, ba.constData(), ba.size());
    To copy to clipboard, switch view to plain text mode 

    Of course all that provided the interpretation of float on your machine is the same as the one in the file. Otherwise you'll need to iterate over the byte array and unmangle bytes properly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    OzQTNoob (9th February 2012)

  6. #4
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reading a binary file full of floats and/or doubles

    Hi Chris and Wysota,

    both methods worked a treat and taught me a little bit more about C++ along the way so thanks for that. All of my work and code is on a PC and will more than likely stay that way. Occasionally though I may get an image file (talking remote sensing image here e.g. spectral imagery) that has come from a BigEndian machine. In this case I can always identify it from its header file (An ascii file). If I am going to swap the endian of the data (assuming that happens) then I will have a look at methods for that (I am sure there are plenty of existing examples around).

    In relation to the 2 code snippets you guys posted is their any reason to adopt one over the other? And as an aside, if I don't know the actual array sizes prior to runtime (they can actually vary a lot depending on the instrument used to collect the spectra) I assume that I can replace the expressions that have used the const terms etc with Qt Dynamic equivalents? e.g. use resize with the QVector and/or QByteArray. I will try this with the code snippets I got from you guys and see what happens

    Cheers
    Oz

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: reading a binary file full of floats and/or doubles

    What is producing your spectra? ... or would you have to kill me I you told me

  8. #6
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reading a binary file full of floats and/or doubles

    Hi Chris,

    no killing needed. The spectra can come from a number of sources such as an ASD field spectrometer (400-2500nm spectral region) or from airborne hyperspectral imagers such as HyMap (an Australian sensor) to name but 2. The datasets that we work with tend to come with an ENVI header file and almost always in the form of a spectral library or a multiband image file (has been adopted by a very large portion of the remote sensing community). Most of my work, although not all of it, is about getting at what minerals are present in a spectral dataset via the spectral absorption features in a given spectrum, and/or correcting the airborne imagery for atmospheric effects etc.

    I have written lots of code in IDL to process the various datasets (I work for CSIRO) but the problem that I have with IDL is that it requires licensing and therefore we have to keep coming up money to fund our licences, and ultimately IDL is an interpreted language that defers back to C anyway, the for loops in IDL are painfully slow if you have to use them. Plus, with Qt it still lets me use UI's and is a slightly easier way for me to ease into C++ programming as well

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: reading a binary file full of floats and/or doubles

    Cool. Only ask because there is much spectra processing in astronomy (one of my quals).

  10. #8
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reading a binary file full of floats and/or doubles

    Indeed, in fact there are whole IDL libraries dedicated to astronomy software and routines, idlastro.gsfc.nasa.gov to name but one. Some of my best routines have been lifted from astro code
    "To steal the work of one is plagiarism, to steal the work of many is research"

Similar Threads

  1. Replies: 4
    Last Post: 4th November 2014, 22:53
  2. Replies: 2
    Last Post: 17th May 2011, 10:47
  3. Binary file reading using Structure
    By umulingu in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2009, 11:35
  4. Reading characters in Binary file.....
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 04:51
  5. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.