将原始编码的nrrd数据文件读入double

时间:2014-02-26 21:01:30

标签: c++ file-io data-visualization vtk scivis

有没有人知道如何使用原始编码读取文件?所以难过......我试图读取花车或双打(我想)。我被困在这几个星期了。谢谢!

我想要读取的文件: http://www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.raw

原始编码说明: 您好://teem.sourceforge.net/nrrd/format.html#encoding(将hello更改为http转到页面) - “raw” - 根据字节值和字节顺序,磁盘上显示的数据与内存中的数据完全相同。由write()和fwrite()生成,适用于read()或fread()。

档案信息: http://www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.nhdr - 我认为这里唯一重要的事情是大端(仍然试图理解谷歌的含义)和原始编码。

我目前的做法,不确定是否正确:

 //Function ripped off from example of c++ ifstream::read reference page

void scantensor(string filename){
    ifstream tdata(filename, ifstream::binary); // not sure if I should put ifstream::binary here

    // other things I tried
    // ifstream tdata(filename)  ifstream tdata(filename, ios::in)

    if(tdata){
            tdata.seekg(0, tdata.end);
            int length = tdata.tellg();
            tdata.seekg(0, tdata.beg);

            char* buffer = new char[length];

            tdata.read(buffer, length);

            tdata.close();

            double* d;
            d = (double*) buffer;

    } else cerr << "failed" << endl;
}

/*  P.S. I attempted to print the first 100 elements of the array.

    Then I print 100 other elements at some arbitrary array indices (i.e. 9,900 - 10,000).  I actually kept increasing the number of 0's until I ran out of bound at 100,000,000 (I don't think that's how it works lol but I was just playing around to see what happens)

    Here's the part that makes me suspicious: so the ifstream different has different constructors like the ones I tried above.

    the first 100 values are always the same.

    if I use ifstream::binary, then I get some values for the 100 arbitrary printing
    if I use the other two options, then I get -6.27744e+066 for all 100 of them

    So for now I am going to assume that ifstream::binary is the correct one.  The thing is, I am not sure if the file I provided is how binary files actually look like.  I am also unsure if these are the actual numbers that I am supposed to read in or just casting gone wrong.  I do realize that my casting from char* to double* can be unsafe, and I got that from one of the threads.

*/

我真的很感激!

编辑1:现在使用上述方法读取的数据显然是“不正确的”,因为在paraview中值是:

Dxx,Dxy,Dxz,Dyy,Dyz,Dzz
[0, 1], [-15.4006, 13.2248], [-5.32436, 5.39517], [-5.32915, 5.96026], [-17.87, 19.0954], [-6.02961, 5.24771], [-13.9861, 14.0524]

It's a 3 x 3 symmetric matrix, so 7 distinct values, 7 ranges of values.

我目前正在从文件解析的浮点数非常大(即-4.68855e-229,-1.32351e + 120)。

也许有人知道如何从Paraview中提取浮点数?

1 个答案:

答案 0 :(得分:0)

由于你想使用双打,我建议从文件中读取数据作为双打缓冲区:

const long machineMemory = 0x40000000; // 1 GB

FILE* file = fopen("c:\\data.bin", "rb");

if (file)
{
    int size = machineMemory / sizeof(double);

    if (size > 0)
    {
      double* data = new double[size];

      int read(0);
      while (read = fread(data, sizeof(double), size, file))
      {
         // Process data here (read = number of doubles)
      }

      delete [] data;
   }

   fclose(file);
}