Hi,
I am relatively new to using the hdf5 code, and am having trouble
reading in a .h5 file. I have followed to examples on the hdfgroup
website, but I when read in the file, I am getting a segmentation fault.
I was hoping someone might be able to point out where I am making a
memory mistake. I have attached a copy of my code. Also, the data set I
am attempting to read is a 1051200 by 2 matrix with a complex compound
datatype in each of the cells. Thanks in advance for any help.
Scott
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <hdf5.h>
#include <hdf5_hl.h>
#include <H5GNSSPVTType.h>
#include <H5GNSScommontypes.h>
#include <H5GNSSTimeType.h>
#include <H5Cpp.h>
#define CPTR(VAR,CONST) ((VAR)=(CONST),&(VAR))
using std::endl;
using std::cout;
using std::string;
using namespace H5;
const size_t LENGTH = 21;
int main()
{
hid_t fileID, datasetID, dataspaceID,
ramDataspaceID, stringTypeID,
arrayTypeID, enumTypeID;
hid_t native_type,dtype,memb_id,
selectTypeID,datatimeTypeID, memtype;
H5T_class_t clss, memb_class;
HGSelectionEnum val;
int nmembs;
herr_t hErrVal;
/*hsize_t fileSlabCount[3] = {100,1,1};
hsize_t fileSlabOffset[3] = {0,0,0};
hsize_t ramSlabCount[2] = {100,1};
hsize_t ramSlabOffset[2] = {1,0};
hsize_t ramDims[2] = {101,2};
size_t offset;*/
char* name;
int i,j,ndims;
hErrVal = H5open();
HGPVTType *temp;
HGTimeType dataTime;
HGSelectionEnum selected;
const char* FILE_NAME = "hg85401-2010.h5";
const char* DATASET_NAME = "/Data/85401/PVT";
//open file and data set and store access to repective IDs
fileID = H5Fopen(FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT);
datasetID = H5Dopen(fileID,DATASET_NAME);
//create a handle for the compound data type and the include compound types from the struct
hid_t HGPVT_id = H5Tcreate(H5T_COMPOUND, sizeof(HGPVTType));
//create datatype id for the string values
stringTypeID = H5Tcopy(H5T_C_S1);
hErrVal = H5Tset_size(stringTypeID, LENGTH);
const hsize_t arraylen = 3;
//pulls type and class from the desired dataset
dtype = H5Dget_type(datasetID);
clss = H5Tget_class(dtype);
//Creates a compound datatype id.
datatimeTypeID = H5Tcreate(H5T_COMPOUND, sizeof(HGTimeType));
hErrVal = H5Tinsert(datatimeTypeID, "mjd", 0, H5T_NATIVE_UINT);
hErrVal = H5Tinsert(datatimeTypeID, "sod e+4", 4, H5T_NATIVE_UINT);
native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT);
//Creates an arraytype hid
arrayTypeID = H5Tarray_create2(H5T_NATIVE_DOUBLE,1,&arraylen);
//Creates an enumtype hid
selectTypeID = H5Tcreate(H5T_ENUM, sizeof(HGSelectionEnum));
hErrVal = H5Tenum_insert(selectTypeID, "Unknown", CPTR(selected, hgSelUnknown));
hErrVal = H5Tenum_insert(selectTypeID, "Selected", CPTR(selected, hgSelSelected));
hErrVal = H5Tenum_insert(selectTypeID, "Unselected", CPTR(selected, hgSelUnselected));
//------------------------------------------------------------------------------------------
// The following block was used to check and make sure the datatypes were the same as used
// in the .h5 file
if(clss == H5T_COMPOUND)
{
nmembs = H5Tget_nmembers(native_type);
cout << nmembs << endl;
cout << datatimeTypeID << endl;
for (i=0; i < nmembs ; i++)
{
memb_id = H5Tget_member_type (native_type, i);
memb_class = H5Tget_member_class (native_type, i);
cout << i << " is class " << memb_class;
offset = H5Tget_member_offset(native_type,i);
cout << " has offset " << offset;
name = H5Tget_member_name(native_type,i);
cout << " has name " << name;
memtype = H5Tget_member_type(native_type,i);
cout << " has type " << memtype <<endl;
if(H5Tequal(memtype, H5T_NATIVE_DOUBLE))
{ cout << i << " is the same datatype as native double" << endl; }
if(H5Tequal(memtype, H5T_NATIVE_FLOAT))
{ cout << i << " is the same datatype as native float" << endl; }
if(H5Tequal(memtype,arrayTypeID))
{ cout << i << " is the same datatype as arraytype" <<endl;}
if(H5Tequal(memtype,stringTypeID))
{ cout << i << " is the same datatype as stringTypeID" <<endl;}
if(H5Tequal(memtype, selectTypeID))
{ cout << i << " is the same datatype as Enum" <<endl;}
if(H5Tequal(memtype,datatimeTypeID))
{ cout << i << " is the same datatype as datatime" << endl;}
}
}
//------------------------------------------------------------------------------------------//
//HGPVTType struct build
H5Tinsert(HGPVT_id, "dataTime", 0, datatimeTypeID);
H5Tinsert(HGPVT_id, "latency", 8, H5T_NATIVE_DOUBLE);
H5Tinsert(HGPVT_id, "stationID", 16, stringTypeID);
H5Tinsert(HGPVT_id, "receiverID", 37, stringTypeID);
H5Tinsert(HGPVT_id, "antennaID", 58, stringTypeID);
H5Tinsert(HGPVT_id, "selected", 80, selectTypeID);
H5Tinsert(HGPVT_id, "position", 88, arrayTypeID);
H5Tinsert(HGPVT_id, "clockOffset", 112, H5T_NATIVE_DOUBLE);
H5Tinsert(HGPVT_id, "velocity", 120, arrayTypeID);
H5Tinsert(HGPVT_id, "clockDrift", 144, H5T_NATIVE_DOUBLE);
H5Tinsert(HGPVT_id, "pdop", 152, H5T_NATIVE_FLOAT);
dataspaceID = H5Dget_space(datasetID);
temp = (HGPVTType *)malloc(11*sizeof(HGPVTType));
hErrVal = H5Dread(datasetID, HGPVT_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, temp);
H5Dclose(datasetID);
H5Fclose(fileID);
hErrVal = H5close();
return 0;
}
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org