XdmfHDF5Writer.cpp 3.25 KB
Newer Older
1
2
3
// Kenneth Leiter
// Xdmf Smart Pointer Test

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

10
11
12
13
14
15
16
/**
 * PIMPL
 */
class XdmfHDF5Writer::XdmfHDF5WriterImpl {

public:

17
	XdmfHDF5WriterImpl(std::string & hdf5FilePath) :
18
19
20
		mHDF5Handle(H5Fcreate(hdf5FilePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)),
		mHeavyFileName(hdf5FilePath),
		mLastWrittenDataSet("")
21
22
23
24
25
26
27
28
	{
	};
	~XdmfHDF5WriterImpl()
	{
		herr_t status = H5Fclose(mHDF5Handle);
	};
	hid_t mHDF5Handle;
	std::string mHeavyFileName;
29
	std::string mLastWrittenDataSet;
30
	static int mDataSetId;
31
32
};

33
34
int XdmfHDF5Writer::XdmfHDF5WriterImpl::mDataSetId = 0;

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class XdmfHDF5Writer::GetHDF5Type : public boost::static_visitor <hid_t> {
public:

	GetHDF5Type()
	{
	}

	hid_t getHDF5Type(const char * const) const
	{
		return H5T_NATIVE_CHAR;
	}

	hid_t getHDF5Type(const short * const) const
	{
		return H5T_NATIVE_SHORT;
	}

	hid_t getHDF5Type(const int * const) const
	{
		return H5T_NATIVE_INT;
	}

	hid_t getHDF5Type(const long * const) const
	{
		return H5T_NATIVE_LONG;
	}

	hid_t getHDF5Type(const float * const) const
	{
		return H5T_NATIVE_FLOAT;
	}

	hid_t getHDF5Type(const double * const) const
	{
		return H5T_NATIVE_DOUBLE;
	}

	hid_t getHDF5Type(const unsigned char * const) const
	{
		return H5T_NATIVE_UCHAR;
	}

	hid_t getHDF5Type(const unsigned short * const) const
	{
		return H5T_NATIVE_USHORT;
	}

	hid_t getHDF5Type(const unsigned int * const) const
	{
		return H5T_NATIVE_UINT;
	}

	template<typename T>
	hid_t operator()(const boost::shared_ptr<std::vector<T> > & array) const
	{
		return this->getHDF5Type(&(array.get()->operator[](0)));
	}

	template<typename T>
	hid_t operator()(const boost::shared_array<const T> & array) const
	{
		return this->getHDF5Type(array.get());
	}
};

XdmfHDF5Writer::XdmfHDF5Writer(std::string & hdf5FilePath) :
	mImpl(new XdmfHDF5WriterImpl(hdf5FilePath))
102
103
104
105
106
107
{
	std::cout << "Created XdmfHDF5Writer " << this << std::endl;
}

XdmfHDF5Writer::~XdmfHDF5Writer()
{
108
	delete mImpl;
109
110
111
	std::cout << "Deleted XdmfHDF5Writer " << this << std::endl;
}

112
113
114
115
116
117
std::string XdmfHDF5Writer::getLastWrittenDataSet() const
{
	return mImpl->mLastWrittenDataSet;
}

void XdmfHDF5Writer::visit(XdmfArray & array, boost::shared_ptr<Loki::BaseVisitor> visitor)
118
{
119
120
	hid_t datatype = -1;
	if(array.mHaveArray)
121
	{
122
		datatype = boost::apply_visitor(GetHDF5Type(), array.mArray);
123
	}
124
	else if(array.mHaveArrayPointer)
125
	{
126
		datatype = boost::apply_visitor(GetHDF5Type(), array.mArrayPointer);
127
128
	}

129
130
	if(datatype != -1)
	{
131
132
133
134
		std::stringstream dataSetName;
		dataSetName << "Data" << mImpl->mDataSetId;

		// Open a hdf5 dataset and write to it on disk.
135
136
137
		herr_t status;
		hsize_t size = array.getSize();
		hid_t dataspace = H5Screate_simple(1, &size, NULL);
138
		hid_t dataset = H5Dcreate(mImpl->mHDF5Handle, dataSetName.str().c_str(), datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
139
140
141
142
		status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.getValuesPointer());
		status = H5Dclose(dataset);
		status = H5Sclose(dataspace);

143
144
145
146
147
148
149
150
		std::stringstream writtenDataSetName;
		writtenDataSetName << mImpl->mHeavyFileName << ":" << dataSetName.str();
		mImpl->mLastWrittenDataSet = writtenDataSetName.str();
		mImpl->mDataSetId++;
	}
	else
	{
		mImpl->mLastWrittenDataSet = "";
151
	}
152
}