HDF5 C++ API's -Finding datatype of a DataSet ?

Hi HDF5-users,
   
  I have a specific question on - how to identify a specific datatype of a dataset, based on which you could allocate memory and store the data read from HDF5 file.
  Supposedly, i store few datasets as given below -
  const int dims0 = 1;
  const int RANK0 = 1;
  const int dims1 = 3;
  const int RANK1 = 1;
  const int dims21 = 2;
  const int dims22 = 3;
  const int RANK2 = 2;
  
  //WRITING DATA on DATASET
  hsize_t dimsf0[]={dims0};
  hsize_t dimsf1[]={dims1};
  hsize_t dimsf2[]={dims21,dims22};
  H5File file( FILE_NAME, H5F_ACC_TRUNC );
  DataSpace sid0(RANK0, dimsf0);
  DataSpace sid1(RANK1, dimsf1);
  DataSpace sid2(RANK2, dimsf2);
  StrType strdatatype(PredType::C_S1, 100);
  Attribute a_s=dataset.createAttribute("at_string1",strdatatype,sid0);
  a_s.write(strdatatype,"This is the value for string attribute");
  // Create integer attribute and add to dataset
  int d0[dims0]={10};
  float d2[dims21][dims22]={{11.1,21.1,32.2},{31.1,41.1,72.2}};
  double d4[dims21][dims22]={{11.1,21.1,32.2},{31.1,41.1,72.2}};
  char d3[dims21][dims22]={{'3','4','5'},{'6','7','8'}};
  
  Group G2 = G1.createGroup("G2-GROUP");
  dataset = G2.createDataSet("ds_int2_group", PredType::NATIVE_INT, sid0);
  dataset.write(d0, PredType::NATIVE_INT);
  
  dataset = G2.createDataSet("ds_float2_group", PredType::NATIVE_FLOAT, sid2);
  dataset.write((void*)d2, PredType::NATIVE_FLOAT);
  dataset = G2.createDataSet("ds_float2_group", PredType::NATIVE_DOUBLE, sid2);
  dataset.write((void*)d4, PredType::NATIVE_DOUBLE);
  dataset = G2.createDataSet("ds_char_group", PredType::NATIVE_CHAR, sid2);
  dataset.write((void*)d3, PredType::NATIVE_CHAR);
  //READING DATA from DATASET
  StrType tid1(0, H5T_STRING);
  dataset=nfile.openDataSet(tag);
  DataSpace sid1 = dataset.getSpace();
  hssize_t n = sid1.getSimpleExtentNpoints();
  int ndims1 = sid1.getSimpleExtentNdims();
  ndims1 = sid1.getSimpleExtentDims(tdims1);
  datatype = dataset.getDataType();
  dtype = datatype.getClass();
  cout << ", StorageSize1=" << (size_t)dataset.getStorageSize();
  cout << ", StorageSize2=" << (size_t)datatype.getSize();
  switch(dtype)
  {
  case H5T_INTEGER:
  a = new int[n];
  dataset.read(a,datatype);
  break;
  case H5T_FLOAT:
  b = new float[n];
  dataset.read(b,datatype);
  break;
  case H5T_STRING:
  dataset.read((void*)c, tid1);
  break;
  default:
  cout <<", Type = something"<<endl;
  break;
  }
  Question:
  Since Character datasets are also stored as Equivalent ASCII int values, how would you differentiate between what exact type of the dataset, when dataset would return
  datatype as H5T_INTEGER for both native_char and native_int datasets ? (one approach would be to get storage size and elements and analyse what exact datatype
  to which data belongs to.)
  Similarly, how would i know whether data is native_float or native_double ?
  It would be great if somebody can tell me/provide some suggestion on this.
   
  Thanks
  Anish Anto

···

---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.

A Wednesday 30 January 2008, Anish Anto escrigué:

  Hi HDF5-users,

  I have a specific question on - how to identify a specific datatype
of a dataset, based on which you could allocate memory and store the
data read from HDF5 file. Supposedly, i store few datasets as given
below -
  const int dims0 = 1;
  const int RANK0 = 1;
  const int dims1 = 3;
  const int RANK1 = 1;
  const int dims21 = 2;
  const int dims22 = 3;
  const int RANK2 = 2;

  //WRITING DATA on DATASET
  hsize_t dimsf0[]={dims0};
  hsize_t dimsf1[]={dims1};
  hsize_t dimsf2[]={dims21,dims22};
  H5File file( FILE_NAME, H5F_ACC_TRUNC );
  DataSpace sid0(RANK0, dimsf0);
  DataSpace sid1(RANK1, dimsf1);
  DataSpace sid2(RANK2, dimsf2);
  StrType strdatatype(PredType::C_S1, 100);
  Attribute
a_s=dataset.createAttribute("at_string1",strdatatype,sid0);
a_s.write(strdatatype,"This is the value for string attribute"); //
Create integer attribute and add to dataset
  int d0[dims0]={10};
  float d2[dims21][dims22]={{11.1,21.1,32.2},{31.1,41.1,72.2}};
  double d4[dims21][dims22]={{11.1,21.1,32.2},{31.1,41.1,72.2}};
  char d3[dims21][dims22]={{'3','4','5'},{'6','7','8'}};

  Group G2 = G1.createGroup("G2-GROUP");
  dataset = G2.createDataSet("ds_int2_group", PredType::NATIVE_INT,
sid0); dataset.write(d0, PredType::NATIVE_INT);

  dataset = G2.createDataSet("ds_float2_group",
PredType::NATIVE_FLOAT, sid2); dataset.write((void*)d2,
PredType::NATIVE_FLOAT);
  dataset = G2.createDataSet("ds_float2_group",
PredType::NATIVE_DOUBLE, sid2); dataset.write((void*)d4,
PredType::NATIVE_DOUBLE);
  dataset = G2.createDataSet("ds_char_group", PredType::NATIVE_CHAR,
sid2); dataset.write((void*)d3, PredType::NATIVE_CHAR);
  //READING DATA from DATASET
  StrType tid1(0, H5T_STRING);
  dataset=nfile.openDataSet(tag);
  DataSpace sid1 = dataset.getSpace();
  hssize_t n = sid1.getSimpleExtentNpoints();
  int ndims1 = sid1.getSimpleExtentNdims();
  ndims1 = sid1.getSimpleExtentDims(tdims1);
  datatype = dataset.getDataType();
  dtype = datatype.getClass();
  cout << ", StorageSize1=" << (size_t)dataset.getStorageSize();
  cout << ", StorageSize2=" << (size_t)datatype.getSize();
  switch(dtype)
  {
  case H5T_INTEGER:
  a = new int[n];
  dataset.read(a,datatype);
  break;
  case H5T_FLOAT:
  b = new float[n];
  dataset.read(b,datatype);
  break;
  case H5T_STRING:
  dataset.read((void*)c, tid1);
  break;
  default:
  cout <<", Type = something"<<endl;
  break;
  }
  Question:
  Since Character datasets are also stored as Equivalent ASCII int
values, how would you differentiate between what exact type of the
dataset, when dataset would return datatype as H5T_INTEGER for both
native_char and native_int datasets ? (one approach would be to get
storage size and elements and analyse what exact datatype to which
data belongs to.)

Perhaps your are getting mislead because NATIVE_CHAR looks like a
string, but actually it belongs to the H5T_INTEGER class. And yes, you
can distinguish between both by using the storage size
(dtype.getSize()).

  Similarly, how would i know whether data is native_float or
native_double ?

Same way, use dtype.getSize(). 4 is a float and 8 is a double (at least,
in most platforms :wink:

HTH,

···

--

0,0< Francesc Altet http://www.carabos.com/

V V Cárabos Coop. V. Enjoy Data
"-"

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.