/** Parse profile table workspace to a map (the new ...
  */
void SaveGSASInstrumentFile::parseProfileTableWorkspace(
    ITableWorkspace_sptr ws,
    map<unsigned int, map<string, double>> &profilemap) {
  size_t numbanks = ws->columnCount() - 1;
  size_t numparams = ws->rowCount();
  vector<map<string, double>> vec_maptemp(numbanks);
  vector<unsigned int> vecbankindex(numbanks);

  // Check
  vector<string> colnames = ws->getColumnNames();
  if (colnames[0].compare("Name"))
    throw runtime_error("The first column must be Name");

  // Parse
  for (size_t irow = 0; irow < numparams; ++irow) {
    TableRow tmprow = ws->getRow(irow);
    string parname;
    tmprow >> parname;
    if (parname.compare("BANK")) {
      for (size_t icol = 0; icol < numbanks; ++icol) {
        double tmpdbl;
        tmprow >> tmpdbl;
        vec_maptemp[icol].insert(make_pair(parname, tmpdbl));
      }
    } else {
      for (size_t icol = 0; icol < numbanks; ++icol) {
        double tmpint;
        tmprow >> tmpint;
        vecbankindex[icol] = static_cast<unsigned int>(tmpint);
      }
    }
  }
/**
 * Removes error columns from the table if all errors are zero,
 * as these columns correspond to fixed parameters.
 * @param table :: [input, output] Pointer to TableWorkspace to edit
 */
void MuonAnalysisResultTableCreator::removeFixedParameterErrors(
    const ITableWorkspace_sptr table) const {
  assert(table);
  const size_t nRows = table->rowCount();
  const auto colNames = table->getColumnNames();
  std::vector<std::string> zeroErrorColumns;

  for (const auto &name : colNames) {
    // if name does not end with "Error", continue
    const size_t nameLength = name.length();
    if (nameLength < ERROR_LENGTH ||
        name.compare(nameLength - ERROR_LENGTH, ERROR_LENGTH, ERROR_STRING)) {
      continue;
    }

    auto col = table->getColumn(name);
    bool allZeros = true;
    // Check if all values in the column are zero
    for (size_t iRow = 0; iRow < nRows; ++iRow) {
      const double val = col->toDouble(iRow);
      if (std::abs(val) > std::numeric_limits<double>::epsilon()) {
        allZeros = false;
        break;
      }
    }
    if (allZeros) {
      zeroErrorColumns.push_back(name);
    }
  }

  for (const auto &name : zeroErrorColumns) {
    table->removeColumn(name);
  }
}