Skip to content

Commit

Permalink
Minor refactor to improve readability.
Browse files Browse the repository at this point in the history
Refs #9227
  • Loading branch information
martyngigg committed Apr 1, 2014
1 parent d2eabc9 commit a057122
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Expand Up @@ -40,6 +40,11 @@ namespace Mantid
virtual void initDocs();
void init();
void exec();

/// Remove an existing log of the given name
void removeExisting(API::MatrixWorkspace_sptr &logWS, const std::string & name);
/// Create or update the named log entry
void createOrUpdate(API::Run &run, const std::string & name);
};

} // namespace Algorithms
Expand Down
43 changes: 30 additions & 13 deletions Code/Mantid/Framework/Algorithms/src/AddTimeSeriesLog.cpp
Expand Up @@ -43,8 +43,8 @@ namespace Mantid
* @param value The value at the given time
*/
template<typename T>
void createOrUpdate(API::Run& run, const std::string & name,
const std::string & time, const T value)
void createOrUpdateValue(API::Run& run, const std::string & name,
const std::string & time, const T value)
{
TimeSeriesProperty<T> *timeSeries(NULL);
if(run.hasProperty(name))
Expand All @@ -62,7 +62,6 @@ namespace Mantid

}


// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddTimeSeriesLog)

Expand Down Expand Up @@ -122,31 +121,49 @@ namespace Mantid
void AddTimeSeriesLog::exec()
{
MatrixWorkspace_sptr logWS = getProperty("Workspace");
auto &run = logWS->mutableRun();
std::string name = getProperty("Name");

const bool deleteExisting = getProperty("DeleteExisting");
if(deleteExisting && run.hasProperty(name))
{
auto deleter = createChildAlgorithm("DeleteLog",-1,-1,false);
deleter->setProperty("Workspace", logWS);
deleter->setProperty("Name", name);
deleter->executeAsChildAlg();
}
auto &run = logWS->mutableRun();
if(deleteExisting && run.hasProperty(name)) removeExisting(logWS, name);

createOrUpdate(run, name);
}

/**
* @param logWS The workspace containing the log
* @param name The name of the log to delete
*/
void AddTimeSeriesLog::removeExisting(API::MatrixWorkspace_sptr &logWS, const std::string & name)
{
auto deleter = createChildAlgorithm("DeleteLog",-1,-1,false);
deleter->setProperty("Workspace", logWS);
deleter->setProperty("Name", name);
deleter->executeAsChildAlg();
}

/**
* @param run The run object that either has the log or will store it
* @param name The name of the log to create/update
*/
void AddTimeSeriesLog::createOrUpdate(API::Run &run, const std::string & name)
{
std::string time = getProperty("Time");
double valueAsDouble = getProperty("Value");
std::string type = getProperty("Type");
bool asInt = (type == "int");

if(asInt)
{
createOrUpdate<int>(run, name, time, static_cast<int>(valueAsDouble));
createOrUpdateValue<int>(run, name, time, static_cast<int>(valueAsDouble));
}
else
{
createOrUpdate<double>(run, name, time, valueAsDouble);
createOrUpdateValue<double>(run, name, time, valueAsDouble);
}

}


} // namespace Algorithms
} // namespace Mantid

0 comments on commit a057122

Please sign in to comment.