/** Create the table workspace containing experimental data
Each row is a data point measured in experiment
 * @brief LoadSpiceAscii::createDataWS
 * @param datalist
 * @param titles
 * @return
 */
API::ITableWorkspace_sptr LoadSpiceAscii::createDataWS(
    const std::vector<std::vector<std::string>> &datalist,
    const std::vector<std::string> &titles) {
  // Create a table workspace with columns defined
  DataObjects::TableWorkspace_sptr outws =
      boost::make_shared<DataObjects::TableWorkspace>();
  size_t ipt = -1;
  for (size_t i = 0; i < titles.size(); ++i) {
    if (titles[i].compare("Pt.") == 0) {
      outws->addColumn("int", titles[i]);
      ipt = i;
    } else {
      outws->addColumn("double", titles[i]);
    }
  }

  // Add rows
  size_t numrows = datalist.size();
  size_t numcols = outws->columnCount();
  for (size_t irow = 0; irow < numrows; ++irow) {
    TableRow newrow = outws->appendRow();
    for (size_t icol = 0; icol < numcols; ++icol) {
      std::string item = datalist[irow][icol];
      if (icol == ipt)
        newrow << atoi(item.c_str());
      else
        newrow << atof(item.c_str());
    }
  }

  ITableWorkspace_sptr tablews =
      boost::dynamic_pointer_cast<ITableWorkspace>(outws);
  return tablews;
}
/** Parse input TableWorkspace to get a list of detectors IDs of which detector
 * are already masked
  * @param masktablews :: TableWorkspace containing masking information
  * @param maskeddetectorids :: (output) vector of detector IDs that are masked
  */
void ExtractMaskToTable::parseMaskTable(
    DataObjects::TableWorkspace_sptr masktablews,
    std::vector<detid_t> &maskeddetectorids) {
  // Clear input
  maskeddetectorids.clear();

  // Check format of mask table workspace
  if (masktablews->columnCount() != 3) {
    g_log.error("Mask table workspace must have more than 3 columns.  First 3 "
                "must be Xmin, Xmax and Spectrum List.");
    return;
  } else {
    vector<string> colnames = masktablews->getColumnNames();
    vector<string> chkcolumans(3);
    chkcolumans[0] = "XMin";
    chkcolumans[1] = "XMax";
    chkcolumans[2] = "DetectorIDsList";
    for (int i = 0; i < 3; ++i) {
      if (colnames[i] != chkcolumans[i]) {
        g_log.error() << "Mask table workspace " << masktablews->name() << "'s "
                      << i << "-th column name is " << colnames[i]
                      << ", while it should be " << chkcolumans[i]
                      << ". MaskWorkspace is invalid"
                      << " and thus not used.\n";
        return;
      }
    }
  }

  // Parse each row
  size_t numrows = masktablews->rowCount();
  double xmin, xmax;
  string specliststr;
  for (size_t i = 0; i < numrows; ++i) {
    TableRow tmprow = masktablews->getRow(i);
    tmprow >> xmin >> xmax >> specliststr;

    vector<detid_t> tmpdetidvec;
    parseStringToVector(specliststr, tmpdetidvec);
    maskeddetectorids.insert(maskeddetectorids.end(), tmpdetidvec.begin(),
                             tmpdetidvec.end());
  }

  return;
}