1

I found a similar question at HDF5 Example code
but I'm having trouble viewing the hdf5 dataset contents correctly.

The dataset I'm looking at contains string headers with strings in the first column and doubles in the others.

Here's what my code looks like:

public static void readh5(string path, string filename)
{
    H5.Open();
    var fileID = H5F.open(path + filename, H5F.OpenMode.ACC_RDONLY);

    var groupID = H5G.open(fileID, "/Example Group/");
    var datasetID = H5D.open(groupID, "Events");
    var dataSpace = H5D.getSpace(datasetID);
    var size = H5S.getSimpleExtentDims(dataSpace);
    var dataType = H5D.getType(datasetID);

    double[,] dataArray = new double[size[0],11];
    var wrapArray = new H5Array<double>(dataArray);
    H5D.read(datasetID, dataType, wrapArray);
    Console.WriteLine(wrapArray);
}

When I debug and look into wrapArray each element is an incredibly large or small doubles from 10^300 to 10^-300 in value and I don't know why. I don't think those are ID numbers of the elements. I've tried changing the datatype of wrapArray and dataArray to object but that still doesn't give me the exact contents of the dataset.

The output I'm getting for wrapArray looks like:

[0,0] 4.0633928641260729E+87  
[0,1] 9.77854726248995E-320  
[0,2] 1.52021104712121E-312  

etc.

But what I want is:

[0,0] Event1  
[0,1] 2  
[0,2] 56  

etc.

After reading in the dataset I want to loop through the first column to find specific strings, and get the corresponding elements in the other columns. But I have to figure out this out.

Community
  • 1
  • 1
Freddy
  • 57
  • 1
  • 5

2 Answers2

0

For me it worked by simply checking the actual datatype of the DataSet (using HDFView) and then make arrays containing that datatype instead of doubles.

Magnus
  • 1
0

John, if a dataset has one column filled with string values and the seccond column with double values, than the dataset is made of "COMPOUND" type. Things are a little bit complicated then and (from what I know today.. I am a newbie to HDF5) it is not possible to simply load values to 2D array. Instead, you have to:

//1) Define byte array in memory. We know that it is one string and two doubles. 
//Check that string in dataset is really 256 chars long.
 int rows = size[0]; //this should be number of rows in dataset. 
 int oneRowDataSize = 256+8+8; //string+double+double 
 byte[] data_to_read = new byte[oneRowDataSize * rows];

// 2) Read data to our byte array
 H5D.read(datasetID, dataType, new H5Array<byte>(data_to_read));

 // 3) Decompose our byte array to rows and individual values
 for (int m = 0; m < rows; m++)
  {

 //4) offset of the row in the byte array
      int pos = m*oneRowDataSize;

 //5) compute individual offsets
      int posString = pos;
      int posDouble1 = pos + 256; //change the 256 to the correct size of string in dataset
      int posDouble2 = pos + 256 + 8;

 //6) convert bytes to values
     string valString = Encoding.UTF8.GetString(data_to_read, posString, 256);
     double valDouble1 = BitConverter.ToDouble(data_to_read, posDouble1);
     double valDouble2 = BitConverter.ToDouble(data_to_read, posDouble2);

//7 And use these values for your csharp lists/arrays...

  }

I did not test this code. It was just rewriten from mine for your case. Hope this will help.

Filip

  • John didn't ask the question. And in general, you don't need to format answers like a letter. Welcome to stack overflow! You can click on edit below your answer to change it. – Mark Bailey Apr 22 '15 at 20:59