Skip to content
Snippets Groups Projects
Unverified Commit 3220c6a4 authored by Antti Soininen's avatar Antti Soininen Committed by GitHub
Browse files

Merge pull request #23631 from mantidproject/PDLoadCharacterizations_report_errors

PDLoadCharacterizations throw exception when there is unmatch column count
parents 4e929437 941fb01e
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,9 @@ static const std::string ZERO("0."); ...@@ -33,6 +33,9 @@ static const std::string ZERO("0.");
static const std::string EXP_INI_VAN_KEY("Vana"); static const std::string EXP_INI_VAN_KEY("Vana");
static const std::string EXP_INI_EMPTY_KEY("VanaBg"); static const std::string EXP_INI_EMPTY_KEY("VanaBg");
static const std::string EXP_INI_CAN_KEY("MTc"); static const std::string EXP_INI_CAN_KEY("MTc");
/// the offset difference between the information in the table and the the
/// information in version=1 files
static const size_t INFO_OFFSET_V1(6);
// in the filenames vector, each index has a unique location // in the filenames vector, each index has a unique location
static const int F_INDEX_V0 = 0; static const int F_INDEX_V0 = 0;
static const int F_INDEX_V1 = 1; static const int F_INDEX_V1 = 1;
...@@ -70,7 +73,7 @@ extra_columns(const std::vector<std::string> &filenames) { ...@@ -70,7 +73,7 @@ extra_columns(const std::vector<std::string> &filenames) {
if (result.size() == 2) { if (result.size() == 2) {
line = Strings::strip(result[1]); line = Strings::strip(result[1]);
Kernel::StringTokenizer tokenizer( Kernel::StringTokenizer tokenizer(
line, " ", Kernel::StringTokenizer::TOK_IGNORE_EMPTY); line, " \t", Kernel::StringTokenizer::TOK_IGNORE_EMPTY);
for (const auto &token : tokenizer) { for (const auto &token : tokenizer) {
columnSet.insert(token); columnSet.insert(token);
} }
...@@ -344,8 +347,8 @@ int PDLoadCharacterizations::readFocusInfo(std::ifstream &file, ...@@ -344,8 +347,8 @@ int PDLoadCharacterizations::readFocusInfo(std::ifstream &file,
// confirm that everything is the same length // confirm that everything is the same length
if (specIds.size() != l2.size() || specIds.size() != polar.size() || if (specIds.size() != l2.size() || specIds.size() != polar.size() ||
specIds.size() != azi.size()) specIds.size() != azi.size())
throw std::runtime_error( throw Exception::FileError(
"Found different number of spectra, L2 and polar angles"); "Found different number of spectra, L2 and polar angles", filename);
// set the values // set the values
this->setProperty("SpectrumIDs", specIds); this->setProperty("SpectrumIDs", specIds);
...@@ -483,7 +486,6 @@ int findRow(API::ITableWorkspace_sptr &wksp, ...@@ -483,7 +486,6 @@ int findRow(API::ITableWorkspace_sptr &wksp,
// fall through behavior is -1 // fall through behavior is -1
return -1; return -1;
} }
} // namespace
void updateRow(API::ITableWorkspace_sptr &wksp, const size_t rowNum, void updateRow(API::ITableWorkspace_sptr &wksp, const size_t rowNum,
const std::vector<std::string> &names, const std::vector<std::string> &names,
...@@ -494,9 +496,10 @@ void updateRow(API::ITableWorkspace_sptr &wksp, const size_t rowNum, ...@@ -494,9 +496,10 @@ void updateRow(API::ITableWorkspace_sptr &wksp, const size_t rowNum,
wksp->getRef<std::string>("empty_instrument", rowNum) = values[5]; wksp->getRef<std::string>("empty_instrument", rowNum) = values[5];
for (size_t i = 0; i < names.size(); ++i) { for (size_t i = 0; i < names.size(); ++i) {
const auto name = names[i]; const auto name = names[i];
wksp->getRef<std::string>(name, rowNum) = values[i + 6]; wksp->getRef<std::string>(name, rowNum) = values[i + INFO_OFFSET_V1];
} }
} }
} // namespace
void PDLoadCharacterizations::readVersion1(const std::string &filename, void PDLoadCharacterizations::readVersion1(const std::string &filename,
API::ITableWorkspace_sptr &wksp) { API::ITableWorkspace_sptr &wksp) {
...@@ -520,7 +523,8 @@ void PDLoadCharacterizations::readVersion1(const std::string &filename, ...@@ -520,7 +523,8 @@ void PDLoadCharacterizations::readVersion1(const std::string &filename,
g_log.debug() << "Found version " << result[1] << "\n"; g_log.debug() << "Found version " << result[1] << "\n";
} else { } else {
file.close(); file.close();
throw std::runtime_error("file must have \"version=1\" as the first line"); throw Exception::ParseError(
"file must have \"version=1\" as the first line", filename, 0);
} }
// store the names of the columns in order // store the names of the columns in order
...@@ -540,22 +544,29 @@ void PDLoadCharacterizations::readVersion1(const std::string &filename, ...@@ -540,22 +544,29 @@ void PDLoadCharacterizations::readVersion1(const std::string &filename,
if (result.size() == 2) { if (result.size() == 2) {
line = Strings::strip(result[1]); line = Strings::strip(result[1]);
Kernel::StringTokenizer tokenizer( Kernel::StringTokenizer tokenizer(
line, " ", Kernel::StringTokenizer::TOK_IGNORE_EMPTY); line, " \t", Kernel::StringTokenizer::TOK_IGNORE_EMPTY);
for (const auto &token : tokenizer) { for (const auto &token : tokenizer) {
columnNames.push_back(token); columnNames.push_back(token);
} }
} }
} else { } else {
if (columnNames.empty()) // should never happen if (columnNames.empty()) // should never happen
throw std::runtime_error("file missing column names"); throw Exception::FileError("file missing column names", filename);
line = Strings::strip(line); line = Strings::strip(line);
Kernel::StringTokenizer tokenizer( Kernel::StringTokenizer tokenizer(
line, " ", Kernel::StringTokenizer::TOK_IGNORE_EMPTY); line, " \t", Kernel::StringTokenizer::TOK_IGNORE_EMPTY);
std::vector<std::string> valuesAsStr; std::vector<std::string> valuesAsStr;
for (const auto &token : tokenizer) { for (const auto &token : tokenizer) {
valuesAsStr.push_back(token); valuesAsStr.push_back(token);
} }
if (valuesAsStr.size() < columnNames.size() + INFO_OFFSET_V1) {
std::stringstream msg;
msg << "Number of data columns (" << valuesAsStr.size()
<< ") not compatible with number of column labels ("
<< (columnNames.size() + INFO_OFFSET_V1) << ")";
throw Exception::ParseError(msg.str(), filename, linenum);
}
const int row = findRow(wksp, valuesAsStr); const int row = findRow(wksp, valuesAsStr);
...@@ -581,7 +592,7 @@ void PDLoadCharacterizations::readVersion1(const std::string &filename, ...@@ -581,7 +592,7 @@ void PDLoadCharacterizations::readVersion1(const std::string &filename,
row << 0.; // wavelength_min row << 0.; // wavelength_min
row << 0.; // wavelength_max row << 0.; // wavelength_max
// insert all the extras // insert all the extras
for (size_t i = 6; i < valuesAsStr.size(); ++i) { for (size_t i = INFO_OFFSET_V1; i < valuesAsStr.size(); ++i) {
row << valuesAsStr[i]; row << valuesAsStr[i];
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment