// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int H5PrimaryStatsDataDelegate::readBinNumbers(PrimaryStatsData* data, hid_t groupId)
{
  int err = 0;
  FloatArrayType::Pointer p = FloatArrayType::CreateArray(1, DREAM3D::HDF5::BinNumber);
  err = p->readH5Data(groupId);
  data->setBinNumbers(p);
  return err;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int H5TransformationStatsDataDelegate::readBinNumbers(TransformationStatsData* data, hid_t groupId)
{
  int err = 0;
  FloatArrayType::Pointer p = FloatArrayType::CreateArray(1, DREAM3D::StringConstants::BinNumber);
  err = p->readH5Data(groupId);
  data->setBinNumbers(p);
  return err;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int H5PrimaryStatsDataDelegate::readVectorOfArrays(hid_t pid, VectorOfFloatArray colData)
{
  int err = 0;

  for (VectorOfFloatArray::iterator iter = colData.begin(); iter != colData.end(); ++iter )
  {
    FloatArrayType::Pointer d = *iter;
    err = d->readH5Data(pid);
    if (err < 0) { return err; }
  }

  return err;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int H5PrimaryStatsDataDelegate::readDistributionData(hid_t pid,
                                              const std::string &hdf5GroupName,
                                              VectorOfFloatArray colData)
{
  int err = 0;
  hid_t disId = H5Utilities::openHDF5Object(pid, hdf5GroupName);
  if(disId < 0)
  {
    return -1;
  }

  for (VectorOfFloatArray::iterator iter = colData.begin(); iter != colData.end(); ++iter)
  {
    FloatArrayType::Pointer d = *iter;
    err |= d->readH5Data(disId);
  }

  err |= H5Utilities::closeHDF5Object(disId);
  return err;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int H5PrimaryStatsDataDelegate::readPrimaryStatsData(PrimaryStatsData* data, hid_t groupId)
{
  int err = 0;
  //Read the NumFields
  err = readBoundaryArea(data, groupId);

  //Read the PhaseFraction
  err = readPhaseFraction(data, groupId);

  // Read the Grain Diameter Info
  err = readGrainDiameterInfo(data, groupId);

  // Read the Grain Size Distribution
  uint32_t dType = readDistributionType(groupId, DREAM3D::HDF5::Grain_Size_Distribution);
  data->setGrainSize_DistType(dType);
  data->setGrainSizeDistribution(createDistributionVector(data->getGrainSize_DistType()));
  err = readDistributionData(groupId,
                DREAM3D::HDF5::Grain_Size_Distribution,
                data->getGrainSizeDistribution());

   // Read the Bin Numbers
   err = readBinNumbers(data, groupId);

   // Read the B Over A
   dType = readDistributionType(groupId, DREAM3D::HDF5::Grain_SizeVBoverA_Distributions);
   data->setBOverA_DistType(dType);
   data->setGrainSize_BOverA( createDistributionVector(data->getBOverA_DistType()));
   err = readDistributionData(groupId,
                                DREAM3D::HDF5::Grain_SizeVBoverA_Distributions,
                                data->getGrainSize_BOverA());

   // Read the C Over A
   dType = readDistributionType(groupId, DREAM3D::HDF5::Grain_SizeVCoverA_Distributions);
   data->setCOverA_DistType(dType);
   data->setGrainSize_COverA( createDistributionVector(data->getCOverA_DistType()));
   err = readDistributionData(groupId,
                                DREAM3D::HDF5::Grain_SizeVCoverA_Distributions,
                                data->getGrainSize_COverA());

   // Read the Neighbors
   dType = readDistributionType(groupId, DREAM3D::HDF5::Grain_SizeVNeighbors_Distributions);
   data->setNeighbors_DistType(dType);
   data->setGrainSize_Neighbors( createDistributionVector(data->getNeighbors_DistType()));
   err = readDistributionData(groupId,
                                DREAM3D::HDF5::Grain_SizeVNeighbors_Distributions,
                                data->getGrainSize_Neighbors());

   // Read the Omegas
   dType = readDistributionType(groupId, DREAM3D::HDF5::Grain_SizeVOmega3_Distributions);
   data->setOmegas_DistType(dType);
   data->setGrainSize_Omegas( createDistributionVector(data->getOmegas_DistType()));
   err = readDistributionData(groupId,
                                DREAM3D::HDF5::Grain_SizeVOmega3_Distributions,
                                data->getGrainSize_Omegas());

   // Read the Misorientation Bins
   FloatArrayType::Pointer misoBins = FloatArrayType::CreateArray(0, DREAM3D::HDF5::MisorientationBins);
   err = misoBins->readH5Data(groupId);
   if (err < 0)
   {
     misoBins = FloatArrayType::NullPointer();
   }
   data->setMisorientationBins(misoBins);
   err = readMDFWeights(groupId, data);

   // Read the ODF Data
   FloatArrayType::Pointer odfBins = FloatArrayType::CreateArray(0, DREAM3D::HDF5::ODF);
   err = odfBins->readH5Data(groupId);
   if (err < 0)
   {
     odfBins = FloatArrayType::NullPointer();
   }
   data->setODF(odfBins);
   err = readODFWeights(groupId, data);

   // Read the Axis ODF Data
   FloatArrayType::Pointer axisOdfBins = FloatArrayType::CreateArray(0, DREAM3D::HDF5::AxisOrientation);
   err = axisOdfBins->readH5Data(groupId);
   if (err < 0)
   {
     axisOdfBins = FloatArrayType::NullPointer();
   }
   data->setAxisOrientation(axisOdfBins);
   err = readAxisODFWeights(groupId, data);

  return err;
}