Field3D
MACFieldIO Class Reference

#include <MACFieldIO.h>

Inheritance diagram for MACFieldIO:
FieldIO RefBase

Public Types

typedef MACFieldIO class_type
 
typedef boost::intrusive_ptr
< MACFieldIO
Ptr
 
- Public Types inherited from FieldIO
typedef FieldIO class_type
 
typedef boost::intrusive_ptr
< FieldIO
Ptr
 
- Public Types inherited from RefBase
typedef boost::intrusive_ptr
< RefBase
Ptr
 

Public Member Functions

virtual std::string className () const
 Returns the class name.
 
 MACFieldIO ()
 Ctor.
 
virtual FieldBase::Ptr read (hid_t layerGroup, const std::string &filename, const std::string &layerPath, DataTypeEnum typeEnum)
 Reads the field at the given location and tries to create a MACField object from it.
 
virtual bool write (hid_t layerGroup, FieldBase::Ptr field)
 Writes the given field to disk.
 
virtual ~MACFieldIO ()
 Dtor.
 
- Public Member Functions inherited from FieldIO
 FieldIO ()
 Ctor.
 
virtual ~FieldIO ()
 Dtor.
 
- Public Member Functions inherited from RefBase
void ref () const
 Used by boost::intrusive_pointer.
 
size_t refcnt ()
 Used by boost::intrusive_pointer.
 
void unref () const
 Used by boost::intrusive_pointer.
 
 RefBase ()
 
 RefBase (const RefBase &)
 Copy constructor.
 
RefBaseoperator= (const RefBase &)
 Assignment operator.
 
virtual ~RefBase ()
 Destructor.
 
virtual bool checkRTTI (const char *typenameStr)=0
 This function is only implemented by concrete classes and triggers the actual RTTI check through matchRTTI();.
 
bool matchRTTI (const char *typenameStr)
 Performs a check to see if the given typename string matches this class' This needs to be implemented in -all- subclasses, even abstract ones.
 

Static Public Member Functions

static const char * classType ()
 
static FieldIO::Ptr create ()
 
- Static Public Member Functions inherited from FieldIO
static const char * classType ()
 
- Static Public Member Functions inherited from RefBase
static const char * classType ()
 

Public Attributes

 DEFINE_FIELD_RTTI_CONCRETE_CLASS
 
- Public Attributes inherited from FieldIO
 DEFINE_FIELD_RTTI_ABSTRACT_CLASS
 

Private Types

typedef FieldIO base
 Convenience typedef for referring to base class.
 

Private Member Functions

template<class Data_T >
bool readData (hid_t location, typename MACField< Data_T >::Ptr result)
 Reads the data that is dependent on the data type on disk.
 
template<class Data_T >
bool writeData (hid_t layerGroup, typename MACField< Data_T >::Ptr field, MACComponent comp)
 This call writes out the u,v,w data.
 
template<class Data_T >
bool writeInternal (hid_t layerGroup, typename MACField< Data_T >::Ptr field)
 This call writes all the attributes and sets up the data space.
 

Static Private Attributes

static const std::string k_bitsPerComponentStr
 
static const std::string k_componentsStr
 
static const std::string k_dataWindowStr
 
static const std::string k_extentsStr
 
static const std::string k_uDataStr
 
static const std::string k_vDataStr
 
static const std::string k_versionAttrName
 
static const int k_versionNumber
 
static const std::string k_wDataStr
 

Detailed Description

Defines the IO for a MACField object

Definition at line 78 of file MACFieldIO.h.

Member Typedef Documentation

typedef boost::intrusive_ptr<MACFieldIO> MACFieldIO::Ptr

Definition at line 85 of file MACFieldIO.h.

Definition at line 89 of file MACFieldIO.h.

typedef FieldIO MACFieldIO::base
private

Convenience typedef for referring to base class.

Definition at line 160 of file MACFieldIO.h.

Constructor & Destructor Documentation

MACFieldIO::MACFieldIO ( )
inline

Ctor.

Definition at line 100 of file MACFieldIO.h.

: FieldIO()
{ }
virtual MACFieldIO::~MACFieldIO ( )
inlinevirtual

Dtor.

Definition at line 105 of file MACFieldIO.h.

{ /* Empty */ }

Member Function Documentation

static const char* MACFieldIO::classType ( )
inlinestatic

Definition at line 92 of file MACFieldIO.h.

{
return "MACFieldIO";
}
static FieldIO::Ptr MACFieldIO::create ( )
inlinestatic

Definition at line 108 of file MACFieldIO.h.

Referenced by initIO().

{ return Ptr(new MACFieldIO); }
FieldBase::Ptr MACFieldIO::read ( hid_t  layerGroup,
const std::string &  filename,
const std::string &  layerPath,
DataTypeEnum  typeEnum 
)
virtual

Reads the field at the given location and tries to create a MACField object from it.

Returns
Null if no object was read

Implements FieldIO.

Definition at line 79 of file MACFieldIO.cpp.

References DataTypeVecDouble, DataTypeVecFloat, DataTypeVecHalf, Hdf5Util::readAttribute(), and ResizableField< Data_T >::setSize().

{
Box3i extents, dataW;
int components;
//hsize_t dims[1];
if (layerGroup == -1)
throw BadHdf5IdException("Bad layer group in MACFieldIO::read");
int version;
if (!readAttribute(layerGroup, k_versionAttrName, 1, version))
throw MissingAttributeException("Couldn't find attribute " +
if (version != k_versionNumber)
throw UnsupportedVersionException("MACField version not supported: " +
lexical_cast<std::string>(version));
if (!readAttribute(layerGroup, k_extentsStr, 6, extents.min.x))
throw MissingAttributeException("Couldn't find attribute " +
if (!readAttribute(layerGroup, k_dataWindowStr, 6, dataW.min.x))
throw MissingAttributeException("Couldn't find attribute " +
if (!readAttribute(layerGroup, k_componentsStr, 1, components))
throw MissingAttributeException("Couldn't find attribute " +
// Check the data type ---
int bits;
if (!readAttribute(layerGroup, k_bitsPerComponentStr, 1, bits))
throw MissingAttributeException("Couldn't find attribute: " +
// Build a MACField to store everything in
switch (bits) {
case 16:
{
if (typeEnum != DataTypeVecHalf) break;
field->setSize(extents, dataW);
readData<V3h>(layerGroup, field);
result = field;
}
break;
case 64:
{
if (typeEnum != DataTypeVecDouble) break;
field->setSize(extents, dataW);
readData<V3d>(layerGroup, field);
result = field;
}
break;
case 32:
default:
{
if (typeEnum != DataTypeVecFloat) break;
field->setSize(extents, dataW);
readData<V3f>(layerGroup, field);
result = field;
}
}
return result;
}
bool MACFieldIO::write ( hid_t  layerGroup,
FieldBase::Ptr  field 
)
virtual

Writes the given field to disk.

Returns
true if successful, otherwise false

Implements FieldIO.

Definition at line 158 of file MACFieldIO.cpp.

References field_dynamic_cast(), and Hdf5Util::writeAttribute().

{
if (layerGroup == -1) {
throw BadHdf5IdException("Bad layer group in MACFieldIO::write");
}
// Add version attribute
if (!writeAttribute(layerGroup, k_versionAttrName,
throw WriteAttributeException("Couldn't write attribute " +
}
MACField<V3h>::Ptr vecHalfField =
MACField<V3f>::Ptr vecFloatField =
MACField<V3d>::Ptr vecDoubleField =
bool success = true;
if (vecFloatField) {
success = writeInternal<V3f>(layerGroup, vecFloatField);
} else if (vecHalfField) {
success = writeInternal<V3h>(layerGroup, vecHalfField);
} else if (vecDoubleField) {
success = writeInternal<V3d>(layerGroup, vecDoubleField);
} else {
throw WriteLayerException("MACFieldIO does not support the given "
"MACField template parameter");
}
return success;
}
virtual std::string MACFieldIO::className ( ) const
inlinevirtual

Returns the class name.

Implements FieldIO.

Definition at line 125 of file MACFieldIO.h.

{ return "MACField"; }
template<class Data_T >
bool MACFieldIO::writeInternal ( hid_t  layerGroup,
typename MACField< Data_T >::Ptr  field 
)
private

This call writes all the attributes and sets up the data space.

Todo:
Tune the chunk size of the gzip call

Definition at line 169 of file MACFieldIO.h.

References FieldTraits< Data_T >::dataDims(), FieldRes::dataWindow(), FieldRes::extents(), MACField< Data_T >::getComponentSize(), DataTypeTraits< T >::h5bits(), k_bitsPerComponentStr, k_componentsStr, k_dataWindowStr, k_extentsStr, MACCompU, MACCompV, MACCompW, and Hdf5Util::writeAttribute().

{
using namespace Exc;
using namespace Hdf5Util;
int components = FieldTraits<Data_T>::dataDims();
V3i compSize = field->getComponentSize();
int size[3];
size[0] = compSize.x;
size[1] = compSize.y;
size[2] = compSize.z;
Box3i ext(field->extents()), dw(field->dataWindow());
// Add extents attribute ---
int extents[6] =
{ ext.min.x, ext.min.y, ext.min.z, ext.max.x, ext.max.y, ext.max.z };
if (!writeAttribute(layerGroup, k_extentsStr, 6, extents[0]))
throw WriteAttributeException("Couldn't write attribute " + k_extentsStr);
// Add data window attribute ---
int dataWindow[6] =
{ dw.min.x, dw.min.y, dw.min.z, dw.max.x, dw.max.y, dw.max.z };
if (!writeAttribute(layerGroup, k_dataWindowStr, 6, dataWindow[0]))
throw WriteAttributeException("Couldn't write attribute " + k_dataWindowStr);
// Add components attribute ---
if (!writeAttribute(layerGroup, k_componentsStr, 1, components))
throw WriteAttributeException("Couldn't write attribute " + k_componentsStr);
// Add the bits per component attribute ---
if (!writeAttribute(layerGroup, k_bitsPerComponentStr, 1, bits)) {
throw WriteAttributeException("Couldn't write attribute " + k_bitsPerComponentStr);
return false;
}
// Add data to file ---
if (!writeData<Data_T>(layerGroup, field, MACCompU)) {
throw WriteMACFieldDataException("Error writing u_data");
return false;
}
if (!writeData<Data_T>(layerGroup, field, MACCompV)) {
throw WriteMACFieldDataException("Error writing v_data");
return false;
}
if (!writeData<Data_T>(layerGroup, field, MACCompW)) {
throw WriteMACFieldDataException("Error writing w_data");
return false;
}
return true;
}
template<class Data_T >
bool MACFieldIO::writeData ( hid_t  layerGroup,
typename MACField< Data_T >::Ptr  field,
MACComponent  comp 
)
private

This call writes out the u,v,w data.

Definition at line 233 of file MACFieldIO.h.

References MACField< Data_T >::cbegin_comp(), Hdf5Util::checkHdf5Gzip(), MACField< Data_T >::getComponentSize(), Hdf5Util::H5Base::id(), k_uDataStr, k_vDataStr, k_wDataStr, MACCompU, MACCompV, and MACCompW.

{
using namespace Exc;
using namespace Hdf5Util;
const V3i &compSize = field->getComponentSize();
hsize_t totalSize[1];
std::string compStr;
switch (comp) {
case MACCompU:
totalSize[0] = compSize.x;
compStr = k_uDataStr;
break;
case MACCompV:
totalSize[0] = compSize.y;
compStr = k_vDataStr;
break;
case MACCompW:
totalSize[0] = compSize.z;
compStr = k_wDataStr;
break;
default:
break;
}
// Make sure chunk size isn't too big.
hsize_t preferredChunkSize = 4096 * 16;
const hsize_t chunkSize = std::min(preferredChunkSize, totalSize[0] / 2);
H5ScopedScreate dataSpace(H5S_SIMPLE);
if (dataSpace.id() < 0)
throw CreateDataSpaceException("Couldn't create data space in "
"MACFieldIO::writeData");
// Create a "simple" data structure ---
H5Sset_extent_simple(dataSpace.id(), 1, totalSize, NULL);
// Set up gzip property list
bool gzipAvailable = checkHdf5Gzip();
hid_t dcpl = H5Pcreate(H5P_DATASET_CREATE);
if (gzipAvailable) {
herr_t status = H5Pset_deflate(dcpl, 9);
if (status < 0) {
return false;
}
status = H5Pset_chunk(dcpl, 1, &chunkSize);
if (status < 0) {
return false;
}
}
H5ScopedDcreate dataSet(layerGroup, compStr,
dataSpace.id(),
H5P_DEFAULT, dcpl, H5P_DEFAULT);
if (dataSet.id() < 0)
throw CreateDataSetException("Couldn't create data set in "
"MACFieldIO::writeData");
hid_t err = H5Dwrite(dataSet,
H5S_ALL, H5S_ALL,
H5P_DEFAULT, &(*field->cbegin_comp(comp)));
if (err < 0)
throw Exc::WriteLayerException("Error writing layer in "
"MACFieldIO::writeData");
return true;
}
template<class Data_T >
bool MACFieldIO::readData ( hid_t  location,
typename MACField< Data_T >::Ptr  result 
)
private

Reads the data that is dependent on the data type on disk.

Definition at line 314 of file MACFieldIO.h.

References MACField< Data_T >::begin_comp(), Hdf5Util::H5Base::id(), k_uDataStr, k_vDataStr, k_wDataStr, MACCompU, MACCompV, MACCompW, and DataTypeTraits< T >::name().

{
using namespace std;
using namespace Exc;
using namespace Hdf5Util;
hsize_t dims[1];
// read u_data
{
H5ScopedDopen dataSet(layerGroup, k_uDataStr, H5P_DEFAULT);
if (dataSet.id() < 0)
throw OpenDataSetException("Couldn't open data set: " + k_uDataStr);
H5ScopedDget_space dataSpace(dataSet.id());
H5ScopedDget_type dataType(dataSet.id());
H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
if (dataSpace.id() < 0)
throw GetDataSpaceException("Couldn't get data space");
if (dataType.id() < 0)
throw GetDataTypeException("Couldn't get data type");
if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin_comp(MACCompU))) < 0)
{
std::string typeName = "MACField<" +
throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
}
}
// read v_data
{
H5ScopedDopen dataSet(layerGroup, k_vDataStr, H5P_DEFAULT);
if (dataSet.id() < 0)
throw OpenDataSetException("Couldn't open data set: " + k_vDataStr);
H5ScopedDget_space dataSpace(dataSet.id());
H5ScopedDget_type dataType(dataSet.id());
H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
if (dataSpace.id() < 0)
throw GetDataSpaceException("Couldn't get data space");
if (dataType.id() < 0)
throw GetDataTypeException("Couldn't get data type");
if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin_comp(MACCompV))) < 0)
{
std::string typeName = "MACField<" +
throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
}
}
// read w_data
{
H5ScopedDopen dataSet(layerGroup, k_wDataStr, H5P_DEFAULT);
if (dataSet.id() < 0)
throw OpenDataSetException("Couldn't open data set: " + k_wDataStr);
H5ScopedDget_space dataSpace(dataSet.id());
H5ScopedDget_type dataType(dataSet.id());
H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
if (dataSpace.id() < 0)
throw GetDataSpaceException("Couldn't get data space");
if (dataType.id() < 0)
throw GetDataTypeException("Couldn't get data type");
if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin_comp(MACCompW))) < 0)
{
std::string typeName = "MACField<" +
throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
}
}
return true;
}

Member Data Documentation

MACFieldIO::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 90 of file MACFieldIO.h.

const int MACFieldIO::k_versionNumber
staticprivate

Definition at line 147 of file MACFieldIO.h.

const std::string MACFieldIO::k_versionAttrName
staticprivate

Definition at line 148 of file MACFieldIO.h.

const std::string MACFieldIO::k_extentsStr
staticprivate

Definition at line 149 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_dataWindowStr
staticprivate

Definition at line 150 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_componentsStr
staticprivate

Definition at line 151 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_bitsPerComponentStr
staticprivate

Definition at line 152 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_uDataStr
staticprivate

Definition at line 153 of file MACFieldIO.h.

Referenced by readData(), and writeData().

const std::string MACFieldIO::k_vDataStr
staticprivate

Definition at line 154 of file MACFieldIO.h.

Referenced by readData(), and writeData().

const std::string MACFieldIO::k_wDataStr
staticprivate

Definition at line 155 of file MACFieldIO.h.

Referenced by readData(), and writeData().


The documentation for this class was generated from the following files: