Commit 436acbe1 authored by Ken Leiter (Civ's avatar Ken Leiter (Civ

ENH: Allow for storage, retrieval, and output of datatypes other than int. ...

ENH: Allow for storage, retrieval, and output of datatypes other than int.  Incorporate into HDF5 output.
parent 41c12c95
......@@ -9,7 +9,7 @@
#include "XdmfVisitor.hpp"
XdmfDataItem::XdmfDataItem() :
mDataType(XdmfDataItemType::Unknown()),
mDataItemType(XdmfDataItemType::Unknown()),
mNumberValues(0)
{
std::cout << "Created DataItem " << this << std::endl;
......@@ -29,82 +29,80 @@ template <>
void XdmfDataItem::setValues<char>(char* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int8();
mDataItemType = XdmfDataItemType::Int8();
}
template <>
void XdmfDataItem::setValues<unsigned char>(unsigned char* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::UInt8();
mDataItemType = XdmfDataItemType::UInt8();
}
template <>
void XdmfDataItem::setValues<short>(short* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int16();
mDataItemType = XdmfDataItemType::Int16();
}
template <>
void XdmfDataItem::setValues<unsigned short>(unsigned short* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::UInt16();
mDataItemType = XdmfDataItemType::UInt16();
}
template <>
void XdmfDataItem::setValues<int>(int* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int32();
mDataItemType = XdmfDataItemType::Int32();
}
template <>
void XdmfDataItem::setValues<unsigned int>(unsigned int* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::UInt32();
mDataItemType = XdmfDataItemType::UInt32();
}
template <>
void XdmfDataItem::setValues<long>(long* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int64();
mDataItemType = XdmfDataItemType::Int64();
}
template <>
void XdmfDataItem::setValues<float>(float* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Float32();
mDataItemType = XdmfDataItemType::Float32();
}
template <>
void XdmfDataItem::setValues<double>(double* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Float64();
mDataItemType = XdmfDataItemType::Float64();
}
template <typename T>
void XdmfDataItem::setValues(T* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Unknown();
mDataItemType = XdmfDataItemType::Unknown();
}
template <>
int* XdmfDataItem::getValues<int>()
void* XdmfDataItem::getValues()
{
return (int*)mDataPointer;
return mDataPointer;
}
template <>
const int* const XdmfDataItem::getValues<int>() const
const void* const XdmfDataItem::getValues() const
{
return (int*)mDataPointer;
return mDataPointer;
}
int XdmfDataItem::getNumberValues() const
......@@ -112,6 +110,35 @@ int XdmfDataItem::getNumberValues() const
return mNumberValues;
}
hid_t XdmfDataItem::getHDF5DataType() const
{
return mDataItemType.getHDF5DataType();
}
std::string XdmfDataItem::getName() const
{
return mDataItemType.getName();
}
int XdmfDataItem::getPrecision() const
{
return mDataItemType.getPrecision();
}
/*
template <typename T>
T* XdmfDataItem::getValues()
{
return (T*)mDataPointer;
}
template <typename T>
const T* const XdmfDataItem::getValues() const
{
return (T*)mDataPointer;
}
*/
//void XdmfDataItem::write(boost::shared_ptr<XdmfVisitor> visitor) const
//{
// visitor->visit(this);
......
......@@ -20,9 +20,17 @@ public:
template <typename T> void setValues(T * dataPointer);
template <typename T> T* getValues();
//template <typename T> T* getValues();
void* getValues();
template <typename T> const T* const getValues() const;
//template <typename T> const T* const getValues() const;
const void* const getValues() const;
hid_t getHDF5DataType() const;
std::string getName() const;
int getPrecision() const;
int getNumberValues() const;
......@@ -43,7 +51,7 @@ private:
void operator=(const XdmfDataItem&); // Not implemented.
void* mDataPointer;
XdmfDataItemType mDataType;
XdmfDataItemType mDataItemType;
};
#endif /* XDMFDATAITEM_HPP_ */
......@@ -10,60 +10,64 @@
// Supported XdmfDataItemTypes
XdmfDataItemType XdmfDataItemType::Unknown()
{
return XdmfDataItemType("Unknown");
return XdmfDataItemType("Unknown", 0, 0);
}
XdmfDataItemType XdmfDataItemType::Int8()
{
return XdmfDataItemType("Int8");
return XdmfDataItemType("Char", H5T_NATIVE_CHAR, 1);
}
XdmfDataItemType XdmfDataItemType::Int16()
{
return XdmfDataItemType("Int16");
return XdmfDataItemType("Short", H5T_NATIVE_SHORT, 2);
}
XdmfDataItemType XdmfDataItemType::Int32()
{
return XdmfDataItemType("Int32");
return XdmfDataItemType("Int", H5T_NATIVE_INT, 4);
}
XdmfDataItemType XdmfDataItemType::Int64()
{
return XdmfDataItemType("Int64");
return XdmfDataItemType("Int", H5T_NATIVE_LONG, 8);
}
XdmfDataItemType XdmfDataItemType::Float32()
{
return XdmfDataItemType("Float32");
return XdmfDataItemType("Float", H5T_NATIVE_FLOAT, 4);
}
XdmfDataItemType XdmfDataItemType::Float64()
{
return XdmfDataItemType("Float64");
return XdmfDataItemType("Float", H5T_NATIVE_DOUBLE, 8);
}
XdmfDataItemType XdmfDataItemType::UInt8()
{
return XdmfDataItemType("UInt8");
return XdmfDataItemType("UChar", H5T_NATIVE_UCHAR, 1);
}
XdmfDataItemType XdmfDataItemType::UInt16()
{
return XdmfDataItemType("UInt16");
return XdmfDataItemType("UShort", H5T_NATIVE_USHORT, 2);
}
XdmfDataItemType XdmfDataItemType::UInt32()
{
return XdmfDataItemType("UInt32");
return XdmfDataItemType("UInt", H5T_NATIVE_UINT, 4);
}
XdmfDataItemType::XdmfDataItemType(const std::string& name) :
mName(name)
XdmfDataItemType::XdmfDataItemType(const std::string& name, hid_t hdfDataType, int precision) :
mName(name),
mHDF5DataType(hdfDataType),
mPrecision(precision)
{};
XdmfDataItemType::XdmfDataItemType(const XdmfDataItemType& dataItemType):
mName(dataItemType.mName)
mName(dataItemType.mName),
mHDF5DataType(dataItemType.mHDF5DataType),
mPrecision(dataItemType.mPrecision)
{
}
......@@ -72,13 +76,15 @@ XdmfDataItemType& XdmfDataItemType::operator=(const XdmfDataItemType& dataItemTy
if(this != &dataItemType)
{
mName = dataItemType.mName;
mHDF5DataType = dataItemType.mHDF5DataType;
mPrecision = dataItemType.mPrecision;
}
return *this;
}
bool XdmfDataItemType::operator==(const XdmfDataItemType& dataItemType) const
{
return mName.compare(dataItemType.mName) == 0;
return mName.compare(dataItemType.mName) == 0 && mHDF5DataType == dataItemType.mHDF5DataType && mPrecision == dataItemType.mPrecision;
}
bool XdmfDataItemType::operator!=(const XdmfDataItemType& dataItemType) const
......@@ -90,3 +96,13 @@ std::string XdmfDataItemType::getName() const
{
return mName;
}
hid_t XdmfDataItemType::getHDF5DataType() const
{
return mHDF5DataType;
}
int XdmfDataItemType::getPrecision() const
{
return mPrecision;
}
......@@ -9,6 +9,7 @@
#define XDMFDATAITEMTYPE_HPP_
#include <string>
#include <hdf5.h>
class XdmfDataItemType {
......@@ -27,12 +28,26 @@ public:
static XdmfDataItemType UInt32();
/**
* Get the name of this data item type
* Get the name of this data item type.
*
* @return a string containing the name.
*/
std::string getName() const;
/**
* Return the equivalent HDF5 data type for this data item type.
*
* @return a hid_t giving the equivalent HDF5 data type.
*/
hid_t getHDF5DataType() const;
/**
* Return the precision(number of bytes) of this data item type.
*
* @return an int containing the precision.
*/
int getPrecision() const;
/*
* Compare two XdmfDataItemTypes for equality.
*
......@@ -49,8 +64,8 @@ public:
*/
bool operator!=(const XdmfDataItemType& top) const;
XdmfDataItemType(const XdmfDataItemType& attributeType);
XdmfDataItemType& operator=(const XdmfDataItemType& attributeType);
XdmfDataItemType(const XdmfDataItemType& dataItemType);
XdmfDataItemType& operator=(const XdmfDataItemType& dataItemType);
protected:
......@@ -58,13 +73,14 @@ protected:
* Protected constructor for XdmfDataItemType. The constructor is protected because all data item types supported
* by Xdmf should be accessed through more specific static methods that construct XdmfDataItemTypes - i.e. XdmfDataItemType::Int8().
*/
XdmfDataItemType(const std::string& name);
XdmfDataItemType(const std::string& name, hid_t HDFDataType, int precision);
private:
static XdmfDataItemType* UnknownPtr;
std::string mName;
hid_t mHDF5DataType;
int mPrecision;
};
#endif /* XDMFDATAITEMTYPE_HPP_ */
......@@ -45,9 +45,9 @@ void XdmfVisitor::visit(const XdmfDataItem * const dataItem)
{
format = "HDF";
}
xmlData << std::setw(mTabIndex) << "" << "<DataItem Format=\"" << format << "\" DataType=\"Int\" Precision=\"4\" Dimensions=\"" << dataItem->getNumberValues() << "\">";
xmlData << std::setw(mTabIndex) << "" << "<DataItem Format=\"" << format << "\" DataType=\"" << dataItem->getName() << "\" Precision=\"" << dataItem->getPrecision() << "\" Dimensions=\"" << dataItem->getNumberValues() << "\">";
const int* const intPointer = dataItem->getValues<int>();
const void* const pointer = dataItem->getValues();
mTabIndex++;
if(dataItem->getNumberValues() > mLightDataLimit)
{
......@@ -78,25 +78,25 @@ void XdmfVisitor::visit(const XdmfDataItem * const dataItem)
handle = H5Gcreate(hdf5Handle, groupName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
}
}
hid_t dataset = H5Dcreate(handle, dataHierarchy.back().c_str(), H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t dataset = H5Dcreate(handle, dataHierarchy.back().c_str(), dataItem->getHDF5DataType(), dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
xmlData << "\n" << std::setw(mTabIndex) << "" << mHeavyFileName << ":" << groupName << "/" << dataHierarchy.back();
status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dataItem->getValues<int>());
status = H5Dwrite(dataset, dataItem->getHDF5DataType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dataItem->getValues());
status = H5Dclose(dataset);
status = H5Sclose(dataspace);
}
else
{
for (unsigned int i=0; i<dataItem->getNumberValues(); ++i)
{
if (i % 10 == 0)
{
xmlData << "\n" << std::setw(mTabIndex) << "" << intPointer[i] << " ";
}
else
{
xmlData << intPointer[i] << " ";
}
}
//for (unsigned int i=0; i<dataItem->getNumberValues(); ++i)
//{
// if (i % 10 == 0)
// {
// xmlData << "\n" << std::setw(mTabIndex) << "" << pointer[i] << " ";
// }
// else
// {
// xmlData << pointer[i] << " ";
// }
//}
}
mTabIndex--;
xmlData << "\n" << std::setw(mTabIndex) << "" << "</DataItem>\n";
......
......@@ -15,8 +15,8 @@ int main(int argc, char* argv[])
grid->setName("test");
// Set Geometry
int points[] = {0, 0, 1, 1, 0, 1, 3, 0, 2, 0, 1, 1, 1, 1, 1, 3, 2, 2,
0, 0, -1, 1, 0, -1, 3, 0, -2, 0, 1, -1, 1, 1, -1, 3, 2, -2};
double points[] = {0.1, 0.1, 1.1, 1.1, 0.1, 1.1, 3.1, 0.1, 2.1, 0.1, 1.1, 1.1, 1.1, 1.1, 1.1, 3.1, 2.1, 2.1,
0.1, 0.1, -1.1, 1.1, 0.1, -1.1, 3.1, 0.1, -2.1, 0.1, 1.1, -1.1, 1.1, 1.1, -1.1, 3.1, 2.1, -2.1};
grid->getGeometry()->setNumberPoints(12);
grid->getGeometry()->setGeometryType(XdmfGeometryType::XYZ());
grid->getGeometry()->setValues(points);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment