Skip to content

Commit

Permalink
Refs #5319: fix WorkspaceHistory loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed May 16, 2012
1 parent 7eb9215 commit 30a23a8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 103 deletions.
10 changes: 6 additions & 4 deletions Code/Mantid/Framework/API/src/WorkspaceHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void WorkspaceHistory::saveNexus(::NeXus::File * file) const
time_t now;
time(&now);
strftime (buffer,25,"%Y-%b-%d %H:%M:%S",localtime(&now));
file->makeGroup("MantidEnvironment", "NXNote", true);
file->makeGroup("MantidEnvironment", "NXnote", true);
file->writeData("author", "mantid");
file->openData("author");
file->putAttr("date", std::string(buffer));
Expand Down Expand Up @@ -204,7 +204,7 @@ void WorkspaceHistory::saveNexus(::NeXus::File * file) const
std::stringstream algNumber;
algNumber << "MantidAlgorithm_" << num;

file->makeGroup(algNumber.str(), "NXNote", true);
file->makeGroup(algNumber.str(), "NXnote", true);
file->writeData("author", std::string("mantid"));
file->writeData("description", std::string("Mantid Algorithm data"));
file->writeData("data", m_Iter->second);
Expand Down Expand Up @@ -285,10 +285,9 @@ void WorkspaceHistory::loadNexus(::NeXus::File * file)
std::string entryName = it->first;
if( entryName.find("MantidAlgorithm") != std::string::npos )
{
file->openGroup(entryName, "NXprocess");
file->openGroup(entryName, "NXnote");
std::string rawData;
file->readData("data", rawData);
file->closeData();
file->closeGroup();

// Split into separate lines
Expand Down Expand Up @@ -321,6 +320,7 @@ void WorkspaceHistory::loadNexus(::NeXus::File * file)
if( !Poco::DateTimeParser::tryParse("%Y-%b-%d %H:%M:%S", date + " " + time, start_timedate, tzdiff))
{
g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
file->closeGroup();
return;
}
//Get the duration
Expand All @@ -329,6 +329,7 @@ void WorkspaceHistory::loadNexus(::NeXus::File * file)
if ( dur < 0.0 )
{
g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
file->closeGroup();
return;
}
//Convert the timestamp to time_t to DateAndTime
Expand Down Expand Up @@ -362,6 +363,7 @@ void WorkspaceHistory::loadNexus(::NeXus::File * file)
this->addHistory(alg_hist);
}
}
file->closeGroup();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ namespace Mantid
bool addSampleProperty(Mantid::NeXus::NXMainClass & sample_entry, const std::string & entryName, API::Sample& sampleDetails);
/// Read the spectra
void readInstrumentGroup(Mantid::NeXus::NXEntry & mtd_entry, API::MatrixWorkspace_sptr local_workspace);
/// Read the algorithm history
void readAlgorithmHistory(Mantid::NeXus::NXEntry & mtd_entry, API::MatrixWorkspace_sptr local_workspace);
/// Splits a string of exactly three words into the separate words
void getWordsInString(const std::string & words3, std::string & w1, std::string & w2, std::string & w3);
/// Splits a string of exactly four words into the separate words
Expand Down
195 changes: 98 additions & 97 deletions Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,9 +950,10 @@ API::Workspace_sptr LoadNexusProcessed::loadEntry(NXRoot & root, const std::stri
}

progress(progressStart+0.15*progressRange,"Reading the workspace history...");
m_cppFile->openPath(mtd_entry.path());
try
{
readAlgorithmHistory(mtd_entry, local_workspace);
local_workspace->history().loadNexus(m_cppFile);
}
catch (std::out_of_range&)
{
Expand Down Expand Up @@ -1143,102 +1144,102 @@ bool UDlesserExecCount(NXClassInfo elem1,NXClassInfo elem2)
return false;
}


//-------------------------------------------------------------------------------------------------
/**
* Read the algorithm history from the "mantid_workspace_i/process" group
* @param mtd_entry :: The node for the current workspace
* @param local_workspace :: The workspace to attach the history to
* @throw out_of_range an algorithm history entry doesn't have the excepted number of entries
*/
void LoadNexusProcessed::readAlgorithmHistory(NXEntry & mtd_entry, API::MatrixWorkspace_sptr local_workspace)
{

NXMainClass history = mtd_entry.openNXClass<NXMainClass>("process");
//Group will contain a class for each algorithm, called MantidAlgorithm_i and then an
//environment class
//const std::vector<NXClassInfo> & classes = history.groups();
std::vector<NXClassInfo>& classes = history.groups();
//sort by execution order - to execute the script generated by algorithmhistory in proper order
sort(classes.begin(),classes.end(),UDlesserExecCount);
std::vector<NXClassInfo>::const_iterator iend = classes.end();
for( std::vector<NXClassInfo>::const_iterator itr = classes.begin(); itr != iend; ++itr )
{
if( itr->nxname.find("MantidAlgorithm") != std::string::npos )
{
NXNote entry(history,itr->nxname);
entry.openLocal();
const std::vector<std::string> & info = entry.data();
const size_t nlines = info.size();
if( nlines < 4 )
{// ignore badly formed history entries
continue;
}

std::string algName, dummy, temp;
// get the name and version of the algorithm
getWordsInString(info[NAME], dummy, algName, temp);

//Chop of the v from the version string
size_t numStart = temp.find('v');
// this doesn't abort if the version string doesn't contain a v
numStart = numStart != 1 ? 1 : 0;
temp = std::string(temp.begin() + numStart, temp.end());
const int version = boost::lexical_cast<int>(temp);

//Get the execution date/time
std::string date, time;
getWordsInString(info[EXEC_TIME], dummy, dummy, date, time);
Poco::DateTime start_timedate;
//This is needed by the Poco parsing function
int tzdiff(-1);
if( !Poco::DateTimeParser::tryParse(Mantid::NeXus::g_processed_datetime, date + " " + time, start_timedate, tzdiff))
{
g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
return;
}
//Get the duration
getWordsInString(info[EXEC_DUR], dummy, dummy, temp, dummy);
double dur = boost::lexical_cast<double>(temp);
if ( dur < 0.0 )
{
g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
return;
}
//Convert the timestamp to time_t to DateAndTime
Mantid::Kernel::DateAndTime utc_start;
utc_start.set_from_time_t( start_timedate.timestamp().epochTime() );
//Create the algorithm history
API::AlgorithmHistory alg_hist(algName, version, utc_start, dur,Algorithm::g_execCount);
// Simulate running an algorithm
++Algorithm::g_execCount;

//Add property information
for( size_t index = static_cast<size_t>(PARAMS)+1;index < nlines;++index )
{
const std::string line = info[index];
std::string::size_type colon = line.find(":");
std::string::size_type comma = line.find(",");
//Each colon has a space after it
std::string prop_name = line.substr(colon + 2, comma - colon - 2);
colon = line.find(":", comma);
comma = line.find(", Default?", colon);
std::string prop_value = line.substr(colon + 2, comma - colon - 2);
colon = line.find(":", comma);
comma = line.find(", Direction", colon);
std::string is_def = line.substr(colon + 2, comma - colon - 2);
colon = line.find(":", comma);
comma = line.find(",", colon);
std::string direction = line.substr(colon + 2, comma - colon - 2);
unsigned int direc(Mantid::Kernel::Direction::asEnum(direction));
alg_hist.addProperty(prop_name, prop_value, (is_def[0] == 'Y'), direc);
}
local_workspace->history().addHistory(alg_hist);
entry.close();
}
}

}
//
////-------------------------------------------------------------------------------------------------
///**
// * Read the algorithm history from the "mantid_workspace_i/process" group
// * @param mtd_entry :: The node for the current workspace
// * @param local_workspace :: The workspace to attach the history to
// * @throw out_of_range an algorithm history entry doesn't have the excepted number of entries
// */
//void LoadNexusProcessed::readAlgorithmHistory(NXEntry & mtd_entry, API::MatrixWorkspace_sptr local_workspace)
//{
//
// NXMainClass history = mtd_entry.openNXClass<NXMainClass>("process");
// //Group will contain a class for each algorithm, called MantidAlgorithm_i and then an
// //environment class
// //const std::vector<NXClassInfo> & classes = history.groups();
// std::vector<NXClassInfo>& classes = history.groups();
// //sort by execution order - to execute the script generated by algorithmhistory in proper order
// sort(classes.begin(),classes.end(),UDlesserExecCount);
// std::vector<NXClassInfo>::const_iterator iend = classes.end();
// for( std::vector<NXClassInfo>::const_iterator itr = classes.begin(); itr != iend; ++itr )
// {
// if( itr->nxname.find("MantidAlgorithm") != std::string::npos )
// {
// NXNote entry(history,itr->nxname);
// entry.openLocal();
// const std::vector<std::string> & info = entry.data();
// const size_t nlines = info.size();
// if( nlines < 4 )
// {// ignore badly formed history entries
// continue;
// }
//
// std::string algName, dummy, temp;
// // get the name and version of the algorithm
// getWordsInString(info[NAME], dummy, algName, temp);
//
// //Chop of the v from the version string
// size_t numStart = temp.find('v');
// // this doesn't abort if the version string doesn't contain a v
// numStart = numStart != 1 ? 1 : 0;
// temp = std::string(temp.begin() + numStart, temp.end());
// const int version = boost::lexical_cast<int>(temp);
//
// //Get the execution date/time
// std::string date, time;
// getWordsInString(info[EXEC_TIME], dummy, dummy, date, time);
// Poco::DateTime start_timedate;
// //This is needed by the Poco parsing function
// int tzdiff(-1);
// if( !Poco::DateTimeParser::tryParse(Mantid::NeXus::g_processed_datetime, date + " " + time, start_timedate, tzdiff))
// {
// g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
// return;
// }
// //Get the duration
// getWordsInString(info[EXEC_DUR], dummy, dummy, temp, dummy);
// double dur = boost::lexical_cast<double>(temp);
// if ( dur < 0.0 )
// {
// g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
// return;
// }
// //Convert the timestamp to time_t to DateAndTime
// Mantid::Kernel::DateAndTime utc_start;
// utc_start.set_from_time_t( start_timedate.timestamp().epochTime() );
// //Create the algorithm history
// API::AlgorithmHistory alg_hist(algName, version, utc_start, dur,Algorithm::g_execCount);
// // Simulate running an algorithm
// ++Algorithm::g_execCount;
//
// //Add property information
// for( size_t index = static_cast<size_t>(PARAMS)+1;index < nlines;++index )
// {
// const std::string line = info[index];
// std::string::size_type colon = line.find(":");
// std::string::size_type comma = line.find(",");
// //Each colon has a space after it
// std::string prop_name = line.substr(colon + 2, comma - colon - 2);
// colon = line.find(":", comma);
// comma = line.find(", Default?", colon);
// std::string prop_value = line.substr(colon + 2, comma - colon - 2);
// colon = line.find(":", comma);
// comma = line.find(", Direction", colon);
// std::string is_def = line.substr(colon + 2, comma - colon - 2);
// colon = line.find(":", comma);
// comma = line.find(",", colon);
// std::string direction = line.substr(colon + 2, comma - colon - 2);
// unsigned int direc(Mantid::Kernel::Direction::asEnum(direction));
// alg_hist.addProperty(prop_name, prop_value, (is_def[0] == 'Y'), direc);
// }
// local_workspace->history().addHistory(alg_hist);
// entry.close();
// }
// }
//
//}


//-------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 30a23a8

Please sign in to comment.