Field3D
MACFieldIO Class Reference

#include <MACFieldIO.h>

Inheritance diagram for MACFieldIO:
FieldIO RefBase

List of all members.

Public Types

typedef MACFieldIO class_type
typedef boost::intrusive_ptr
< MACFieldIO
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.

Static Public Member Functions

static const char * classType ()
static FieldIO::Ptr create ()

Public Attributes

 DEFINE_FIELD_RTTI_CONCRETE_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

Reimplemented from FieldIO.

Definition at line 85 of file MACFieldIO.h.

Reimplemented from FieldIO.

Definition at line 89 of file MACFieldIO.h.

typedef FieldIO MACFieldIO::base [private]

Convenience typedef for referring to base class.

Reimplemented from FieldIO.

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 ( ) [inline, virtual]

Dtor.

Definition at line 105 of file MACFieldIO.h.

  { /* Empty */ }

Member Function Documentation

static const char* MACFieldIO::classType ( ) [inline, static]

Reimplemented from FieldIO.

Definition at line 92 of file MACFieldIO.h.

  {
    return "MACFieldIO";
  }
static FieldIO::Ptr MACFieldIO::create ( ) [inline, static]

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 " + 
                                    k_versionAttrName);

  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 " + 
                                    k_extentsStr);

  if (!readAttribute(layerGroup, k_dataWindowStr, 6, dataW.min.x)) 
    throw MissingAttributeException("Couldn't find attribute " + 
                                    k_dataWindowStr);
  
  if (!readAttribute(layerGroup, k_componentsStr, 1, components)) 
    throw MissingAttributeException("Couldn't find attribute " + 
                                    k_componentsStr);
  // Check the data type ---
  int bits;
  if (!readAttribute(layerGroup, k_bitsPerComponentStr, 1, bits)) 
    throw MissingAttributeException("Couldn't find attribute: " +
                                    k_bitsPerComponentStr);  

  // Build a MACField to store everything in
  FieldBase::Ptr result;
  switch (bits) {
  case 16:
    {
      if (typeEnum != DataTypeVecHalf) break;
      MACField<V3h>::Ptr field(new MACField<V3h>);
      field->setSize(extents, dataW);
      readData<V3h>(layerGroup, field);

      result = field;      
    }
    break;
  case 64:
    {
      if (typeEnum != DataTypeVecDouble) break;
      MACField<V3d>::Ptr field(new MACField<V3d>);
      field->setSize(extents, dataW);
      readData<V3d>(layerGroup, field);

      result = field;      
    }
    break;
  case 32:
  default:
    {
      if (typeEnum != DataTypeVecFloat) break;
      MACField<V3f>::Ptr field(new MACField<V3f>);
      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, 
                      1, k_versionNumber)) {
    throw WriteAttributeException("Couldn't write attribute " + 
                                  k_versionAttrName);
  }

  MACField<V3h>::Ptr vecHalfField = 
    field_dynamic_cast<MACField<V3h> >(field);
  MACField<V3f>::Ptr vecFloatField = 
    field_dynamic_cast<MACField<V3f> >(field);
  MACField<V3d>::Ptr vecDoubleField = 
    field_dynamic_cast<MACField<V3d> >(field);

  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 [inline, virtual]

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 FieldRes::dataWindow(), FieldRes::extents(), MACField< Data_T >::getComponentSize(), 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 ---

  int bits = DataTypeTraits<Data_T>::h5bits();
  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, 
                          DataTypeTraits<Data_T>::h5type(), 
                          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, 
                       DataTypeTraits<Data_T>::h5type(), 
                       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, and MACCompW.

{
  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<" + 
          DataTypeTraits<Data_T>::name() + ">";
        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<" + 
          DataTypeTraits<Data_T>::name() + ">";
        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<" + 
          DataTypeTraits<Data_T>::name() + ">";
        throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
      } 

  }

  return true;
}

Member Data Documentation

const int MACFieldIO::k_versionNumber [static, private]

Definition at line 147 of file MACFieldIO.h.

const std::string MACFieldIO::k_versionAttrName [static, private]

Definition at line 148 of file MACFieldIO.h.

const std::string MACFieldIO::k_extentsStr [static, private]

Definition at line 149 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_dataWindowStr [static, private]

Definition at line 150 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_componentsStr [static, private]

Definition at line 151 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_bitsPerComponentStr [static, private]

Definition at line 152 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_uDataStr [static, private]

Definition at line 153 of file MACFieldIO.h.

Referenced by readData(), and writeData().

const std::string MACFieldIO::k_vDataStr [static, private]

Definition at line 154 of file MACFieldIO.h.

Referenced by readData(), and writeData().

const std::string MACFieldIO::k_wDataStr [static, private]

Definition at line 155 of file MACFieldIO.h.

Referenced by readData(), and writeData().


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