// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void RegularizeZSpacing::dataCheck()
{
    setErrorCondition(0);

    if (getNewZRes() <= 0)
    {
        QString ss = QObject::tr("The new Z resolution Y (%1) must be positive").arg(getNewZRes());
        setErrorCondition(-5555);
        notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
    }

    std::ifstream inFile;
    inFile.open(m_InputFile.toLatin1().data());

    if (!inFile.good())
    {
        QString ss = QObject::tr("Unable to open input file with name '%1'").arg(getInputFile());
        setErrorCondition(-5556);
        notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
        return;
    }

    ImageGeom::Pointer image = getDataContainerArray()->getPrereqGeometryFromDataContainer<ImageGeom, AbstractFilter>(this, getCellAttributeMatrixPath().getDataContainerName());
    AttributeMatrix::Pointer cellAttrMat = getDataContainerArray()->getPrereqAttributeMatrixFromPath<AbstractFilter>(this, getCellAttributeMatrixPath(), -301);
    if(getErrorCondition() < 0) {
        return;
    }

    float zval = 0.0f;
    for (size_t iter = 0; iter < image->getZPoints() + 1; iter++)
    {
        inFile >> zval;
    }
    size_t zP = static_cast<size_t>(zval / getNewZRes());
    if(zP == 0) {
        zP = 1;
    }

    if (getInPreflight())
    {
        image->setDimensions(image->getXPoints(), image->getYPoints(), zP);
        QVector<size_t> tDims(3, 0);
        tDims[0] = image->getXPoints();
        tDims[1] = image->getYPoints();
        tDims[2] = zP;
        cellAttrMat->resizeAttributeArrays(tDims);
    }

    inFile.close();
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int VTKFileReader::readHeader()
{

  int err = 0;
  if (getInputFile().isEmpty() == true)
  {
    setErrorCondition(-1);
    notifyErrorMessage(getHumanLabel(), "FileName was not set and must be valid", -1);
    return -1;
  }

  if (NULL == getDataContainerArray()->getDataContainer(getDataContainerName()).get())
  {
    setErrorCondition(-1);
    notifyErrorMessage(getHumanLabel(), "DataContainer Pointer was NULL and must be valid", -1);
    return -1;
  }

  QFile in(getInputFile());
  if (!in.open(QIODevice::ReadOnly | QIODevice::Text))
  {
    QString msg = QObject::tr("VTF file could not be opened: %1").arg(getInputFile());
    setErrorCondition(-100);
    notifyErrorMessage(getHumanLabel(), msg, getErrorCondition());
    return -100;
  }

  QByteArray buf;
  buf = in.readLine(); // Read Line 1 - VTK Version Info

  buf = in.readLine(); // Read Line 2 - User Comment
  setComment(QString(buf.trimmed()));

  buf = in.readLine(); // Read Line 3 - BINARY or ASCII
  QString fileType(buf);
  if (fileType.startsWith("BINARY") == true)
  {
    setFileIsBinary(true);
  }
  else if (fileType.startsWith("ASCII") == true)
  {
    setFileIsBinary(false);
  }
  else
  {
    err = -1;
    qDebug()
        << "The file type of the VTK legacy file could not be determined. It should be ASCII' or 'BINARY' and should appear on line 3 of the file."
        ;
    return err;
  }


  QList<QByteArray> tokens;
  buf = in.readLine(); // Read Line 4 - Type of Dataset
  {
    tokens = buf.split(' ');
    setDatasetType(QString(tokens[1]));
  }


  buf = in.readLine(); // Read Line 5 which is the Dimension values
  bool ok = false;
  int64_t dims[3];
  tokens = buf.split(' ');
  dims[0] = tokens[1].toLongLong(&ok, 10);
  dims[1] = tokens[2].toLongLong(&ok, 10);
  dims[2] = tokens[3].toLongLong(&ok, 10);
#if   (CMP_SIZEOF_SSIZE_T==4)
  int64_t max = std::numeric_limits<size_t>::max();
#else
  int64_t max = std::numeric_limits<int64_t>::max();
#endif
  if (dims[0] * dims[1] * dims[2] > max )
  {
    err = -1;
    QString ss = QObject::tr("The total number of elements '%1' is greater than this program can hold. Try the 64 bit version.").arg(dims[0] * dims[1] * dims[2]);
    setErrorCondition(err);
    notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
    return err;
  }

  if (dims[0] > max || dims[1] > max || dims[2] > max)
  {
    err = -1;
    QString ss = QObject::tr("One of the dimensions is greater than the max index for this sysem. Try the 64 bit version. dim[0]=%1  dim[1]=%2im[2]=%3")\
                 .arg(dims[0]).arg(dims[1]).arg(dims[2]);
    setErrorCondition(err);
    notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
    return err;
  }

  size_t dcDims[3] = { static_cast<size_t>(dims[0]), static_cast<size_t>(dims[1]), static_cast<size_t>(dims[2]) };
  DataContainer::Pointer dc = getDataContainerArray()->getDataContainer(getDataContainerName());
  if (dc.get() == NULL)
  {
    return -1;
  }
  ImageGeom::Pointer image = dc->getGeometryAs<ImageGeom>();
  if (image.get() == NULL)
  {
    return -1;
  }
  image->setDimensions(dcDims);

  buf = in.readLine(); // Read Line 6 which is the Origin values
  float origin[3];
  tokens = buf.split(' ');
  origin[0] = tokens[1].toFloat(&ok);
  origin[1] = tokens[2].toFloat(&ok);
  origin[2] = tokens[3].toFloat(&ok);
  image->setOrigin(origin);

  buf = in.readLine(); // Read Line 7 which is the Scaling values
  float resolution[3];
  tokens = buf.split(' ');
  resolution[0] = tokens[1].toFloat(&ok);
  resolution[1] = tokens[2].toFloat(&ok);
  resolution[2] = tokens[3].toFloat(&ok);
  image->setResolution(resolution);

  return err;

}