XdmfHDF5Controller.cpp 1.84 KB
Newer Older
1 2 3
// Kenneth Leiter
// Xdmf Smart Pointer Test

4 5 6
#include <hdf5.h>
#include <sstream>
#include "XdmfArray.hpp"
7
#include "XdmfArrayType.hpp"
8 9
#include "XdmfHDF5Controller.hpp"

10
XdmfHDF5Controller::XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type) :
11 12 13
		mSize(size),
		mType(type)
{
14
	size_t colonLocation = dataSetPath.find(":");
15 16
	if(colonLocation != std::string::npos)
	{
17
		mHDF5FilePath = dataSetPath.substr(0, colonLocation);
18 19
		if(colonLocation + 1  != mHDF5FilePath.size())
		{
20
			mDataSetName = dataSetPath.substr(colonLocation + 1, dataSetPath.size());
21 22 23 24 25 26 27 28 29 30
		}
		else
		{
			assert(false);
		}
	}
	else
	{
		assert(false);
	}
31 32 33 34 35 36
}

XdmfHDF5Controller::~XdmfHDF5Controller()
{
}

37 38 39 40 41
std::string XdmfHDF5Controller::getDataSetName() const
{
	return mDataSetName;
}

42 43
std::string XdmfHDF5Controller::getDataSetPath() const
{
44 45 46
	std::stringstream toReturn;
	toReturn << mHDF5FilePath << ":" << mDataSetName;
	return toReturn.str();
47 48
}

49 50 51 52 53
std::string XdmfHDF5Controller::getHDF5FilePath() const
{
	return mHDF5FilePath;
}

54
unsigned int XdmfHDF5Controller::getSize() const
55 56 57 58
{
	return mSize;
}

59
boost::shared_ptr<const XdmfArrayType> XdmfHDF5Controller::getType() const
60 61 62
{
	return mType;
}
63 64 65

void XdmfHDF5Controller::read(XdmfArray * const array)
{
66
	hid_t hdf5Handle = H5Fopen(mHDF5FilePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
67 68 69 70 71
	hid_t dataset = H5Dopen(hdf5Handle, mDataSetName.c_str(), H5P_DEFAULT);
	hid_t dataspace = H5Dget_space(dataset);
	hssize_t numVals = H5Sget_simple_extent_npoints(dataspace);
	hid_t datatype = H5Dget_type(dataset);

72 73 74
	array->initialize(mType);
	array->resize(numVals, 0);
	H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array->getValuesPointer());
75 76 77 78 79 80 81

	herr_t status;
	status = H5Tclose(datatype);
	status = H5Sclose(dataspace);
	status = H5Dclose(dataset);
	status = H5Fclose(hdf5Handle);
}