27#include <boost/algorithm/string/classification.hpp>
28#include <boost/algorithm/string/split.hpp>
29#include <boost/filesystem.hpp>
30#include <boost/filesystem/operations.hpp>
36using namespace Kernel;
38using namespace Geometry;
39using namespace DataObjects;
40using Types::Core::DateAndTime;
42namespace CorelliCalibration {
69 if (wsname.size() > 0) {
77 tablews->addColumn(
"str",
"YYYMMDD");
79 tablews->addColumn(
"str",
"ComponentName");
84 tablews->addColumn(type, colname);
102 std::string &errormsg) {
104 std::vector<std::string> colNames = calibws->getColumnNames();
110 std::stringstream errorss;
112 <<
" columns. Input workspace " << calibws->getName() <<
" get " << calibws->getColumnNames().size()
114 errormsg = errorss.str();
119 for (
size_t i = 0; i < colNames.size(); ++i) {
121 std::stringstream errorss;
123 <<
", but instead in TableWorkspace " << calibws->getName() <<
" it is " << colNames[i];
124 errormsg = errorss.str();
146 throw std::runtime_error(
"Single component calibration table workspace is not correct.");
162 std::string errmsg{
""};
165 throw std::runtime_error(errmsg);
184 throw std::runtime_error(
"TableWorkspace contains a single component's "
185 "calibration in various dates");
187 std::vector<std::string> names =
mCalibWS->getColVector<std::string>(0);
201 throw std::runtime_error(
"Calibration workspace has not been set up yet.");
204 size_t row_number =
mCalibWS->rowCount();
205 for (
size_t i = 0; i < row_number; ++i) {
206 if (
mCalibWS->cell<std::string>(i, 0) == component) {
212 if (row_number ==
mCalibWS->rowCount())
213 throw std::runtime_error(
"Specified component does not exist");
217 pos.
x =
mCalibWS->cell<
double>(row_number, 1);
218 pos.
y =
mCalibWS->cell<
double>(row_number, 2);
219 pos.
z =
mCalibWS->cell<
double>(row_number, 3);
245 throw std::runtime_error(
"Failed to load ASCII as OutputWorkspace name is empty string.");
247 loadAsciiAlg->initialize();
248 loadAsciiAlg->setPropertyValue(
"Filename", filename);
249 loadAsciiAlg->setPropertyValue(
"OutputWorkspace",
tablewsname);
250 loadAsciiAlg->setPropertyValue(
"Separator",
"CSV");
251 loadAsciiAlg->setPropertyValue(
"CommentIndicator",
"#");
252 loadAsciiAlg->execute();
275 const std::string &component,
276 const std::string &filename) {
278 std::string
tablewsname = component +
"_" + datestamp;
283 if (boost::filesystem::exists(filename)) {
298 saveAsciiAlg->initialize();
299 saveAsciiAlg->setProperty(
"InputWorkspace", compcaltable);
300 saveAsciiAlg->setProperty(
"Filename", filename);
301 saveAsciiAlg->setPropertyValue(
"CommentIndicator",
"#");
302 saveAsciiAlg->setPropertyValue(
"Separator",
"CSV");
303 saveAsciiAlg->setProperty(
"ColumnHeader",
true);
304 saveAsciiAlg->setProperty(
"AppendToFile",
307 saveAsciiAlg->execute();
320 saveAsciiAlg->initialize();
321 saveAsciiAlg->setProperty(
"InputWorkspace",
mCalibWS);
322 saveAsciiAlg->setProperty(
"Filename", filename);
323 saveAsciiAlg->setPropertyValue(
"CommentIndicator",
"#");
324 saveAsciiAlg->setPropertyValue(
"Separator",
"CSV");
325 saveAsciiAlg->setProperty(
"ColumnHeader",
true);
327 saveAsciiAlg->execute();
334 size_t num_rows = componentcaltable->rowCount();
347 pos.
x = componentcaltable->cell<
double>(rownumber, 1);
348 pos.
y = componentcaltable->cell<
double>(rownumber, 2);
349 pos.
z = componentcaltable->cell<
double>(rownumber, 3);
350 pos.
xCosine = componentcaltable->cell<
double>(rownumber, 4);
351 pos.
yCosine = componentcaltable->cell<
double>(rownumber, 5);
352 pos.
zCosine = componentcaltable->cell<
double>(rownumber, 6);
353 pos.
rotAngle = componentcaltable->cell<
double>(rownumber, 7);
366 auto wsValidator = std::make_shared<CompositeValidator>();
372 "Workspace containing the day-stamp of the calibration");
377 "Table workspace containing calibrated positions and "
378 "orientations for a subset of the banks");
382 "The directory that the database (csv) files are saved to");
386 "Table workspace containing calibrated positions and "
387 "orientations for all banks");
392 std::map<std::string, std::string> errors;
402 if (
mInputWS->getInstrument()->getName() !=
"CORELLI")
403 errors[
"InputWorkspace"] =
"This Algorithm will only work for Corelli.";
405 else if (!
mInputWS->run().hasProperty(
"start_time") && !
mInputWS->run().hasProperty(
"run_start"))
406 errors[
"InputWorkspace"] =
"Workspace is missing property start_time.";
411 errors[
"InputCalibrationPatchWorkspace"] =
"Input calibration patch workspace is not specified";
416 std::string error_msg{
""};
421 errors[
"InputCalibrationPatchWorkspace"] = error_msg;
434 throw std::runtime_error(
"input workspace not specified");
436 throw std::runtime_error(
"input calibration workspace not specified");
438 std::string calibDatabaseDir =
getProperty(
"DatabaseDirectory");
443 std::map<std::string, TableWorkspace_sptr> component_caibws_map;
460 for (
auto &[compname, calibws] : component_caibws_map) {
462 g_log.
debug() <<
"Removing " << compname <<
"calibration table from ADS\n";
477 std::map<std::string, TableWorkspace_sptr> &calibwsmap) {
479 std::string timestampstr{
""};
480 if (
mInputWS->run().hasProperty(
"start_time")) {
482 timestampstr =
mInputWS->run().getProperty(
"start_time")->value();
487 timestampstr =
mInputWS->run().getProperty(
"run_start")->value();
499 for (
size_t i = 0; i < num_rows; ++i) {
507 calibwsmap[compname] = comptablews;
508 g_log.
debug() <<
"Component " << compname <<
" is updated to " << compdbname <<
" and saved to "
509 << comptablews->getName() <<
"\n";
519 const std::string &calibdbdir, std::map<std::string, TableWorkspace_sptr> &calibwsmap) {
521 for (
auto &[componentname, componentcaltable] : calibwsmap) {
523 if (componentcaltable)
531 g_log.
debug() <<
"Component " << componentname <<
": No database file is found at " << compdbname <<
"\n";
537 compdbname, componentname +
"_" +
mDateStamp);
538 calibwsmap[componentname] = loaded_compcalibws;
540 g_log.
debug() <<
"Component " << componentname <<
" is loaded from " << compdbname <<
" and saved to "
541 << loaded_compcalibws->getName() <<
"\n";
548 const std::vector<std::string> &orderedcomponents) {
554 for (
auto componentname : orderedcomponents) {
555 auto componentcaltable = calibwsmap[componentname];
556 if (componentcaltable) {
568 std::string filename =
"corelli_instrument_" +
mDateStamp +
".csv";
569 filename =
joinPath(calibdbdir, filename);
585 std::string date_str = run_start_time.substr(0, run_start_time.find(
"T"));
588 std::string year = date_str.substr(0, date_str.find(
"-"));
589 std::string monthday = date_str.substr(date_str.find(
"-") + 1, date_str.size());
590 std::string month = monthday.substr(0, monthday.find(
"-"));
591 std::string day = monthday.substr(monthday.find(
"-") + 1,
593 std::string datestamp = year + month + day;
610 const std::string &directory) {
613 std::string shortName = componentname;
614 std::string suffix{
"/sixteenpack"};
615 size_t pos = componentname.find(suffix);
616 if (pos != std::string::npos)
617 shortName.erase(pos, suffix.length());
619 std::string basename = shortName +
".csv";
620 std::string filename =
joinPath(directory, basename);
633 const std::string &directory) {
634 std::string basename = datestamp +
".csv";
635 std::string filename =
joinPath(directory, basename);
649 return boost::filesystem::exists(filepath);
660 boost::filesystem::path dir(directory);
661 boost::filesystem::path file(basename);
662 boost::filesystem::path fullpath = dir / file;
664 return fullpath.string();
676 std::map<std::string, DataObjects::TableWorkspace_sptr> &compmap) {
678 for (
auto compname : componentnames)
679 compmap[compname] =
nullptr;
694 const auto &component_info = ws->componentInfo();
697 std::vector<std::string> componentnames = {
"moderator",
"sample-position"};
700 const size_t num_components = component_info.size();
701 for (
size_t i = 0; i < num_components; ++i) {
702 std::string compname = component_info.name(i);
704 if (compname.compare(0, 4,
"bank") == 0) {
705 componentnames.push_back(compname +
"/sixteenpack");
709 return componentnames;
#define DECLARE_ALGORITHM(classname)
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
@ Directory
to specify a directory that must exist
A validator which checks that a workspace has a valid instrument.
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
CorelliCalibrationDatabase: blablabla TODO.
static bool isFileExist(const std::string &filepath)
Check whether a given file does exist.
void saveCalibrationTable(const std::string &calibdbdir)
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
std::string mDateStamp
Date stamp: YYYYMMDD.
void exec() override
Execute the algorithm.
void loadNonCalibratedComponentDatabase(const std::string &calibddir, std::map< std::string, DataObjects::TableWorkspace_sptr > &calibwsmap)
Load data file if necessary and possible: component_caibws_map.
void setComponentMap(const std::vector< std::string > &componentnames, std::map< std::string, DataObjects::TableWorkspace_sptr > &compmap)
Set up a component name - TableWorkspace map for single component calibration.
DataObjects::TableWorkspace_sptr mInputCalibrationTableWS
Input calibration worksapce.
static std::string joinPath(const std::string &directory, const std::string &basename)
Join two string for a new path.
static std::string corelliComponentDatabaseName(const std::string &componentname, const std::string &directory)
get standard component calibration database (CSV) file name
DataObjects::TableWorkspace_sptr mOutputWS
Output calibration worksapce (merged with previous calibrated data)
API::MatrixWorkspace_sptr mInputWS
Input workspace where the calibration is from.
void createOutputCalibrationTable(std::map< std::string, DataObjects::TableWorkspace_sptr > &calibwsmap, const std::vector< std::string > &orderedcomponents)
Create output full set calibration workspace.
void updateComponentDatabaseFiles(const std::string &calibdbdir, std::map< std::string, DataObjects::TableWorkspace_sptr > &calibwsmap)
append the newly calibration to each component csv file
static std::string convertTimeStamp(const std::string &run_start_time)
A static method to convert Mantid datetime string to YYYYMMDD format.
static std::vector< std::string > retrieveInstrumentComponents(const API::MatrixWorkspace_sptr &ws)
Retrieve the bank level components names in order.
static std::string corelliCalibrationDatabaseName(const std::string &datestamp, const std::string &directory)
get standard date-base calibration database (CSV) file name
Class containing static and member methods to work with calibration table workspaces.
DataObjects::TableWorkspace_sptr mCalibWS
ComponentPosition getComponentCalibratedPosition(const std::string &component)
Get the calibration of a component.
static bool isValidCalibrationTableWorkspace(const DataObjects::TableWorkspace_sptr &calibws, std::string &errormsg)
Check whether a TableWorkspace is a valid Corelli geometry calibration table for all components.
static ComponentPosition getLatestCalibratedPosition(const DataObjects::TableWorkspace_sptr &componentcaltable)
Get the last entry (latest update) of a compoent calibrated position.
void setCalibrationTable(const DataObjects::TableWorkspace_sptr &calibws)
Set calibration table file.
static ComponentPosition getCalibratedPosition(const DataObjects::TableWorkspace_sptr &componentcaltable, size_t rownumber)
Get the calibration position in the table (component table or full calibration table)
static DataObjects::TableWorkspace_sptr loadComponentCalibrationTable(const std::string &filename, const std::string &tablewsname)
Load a single-component calibration table.
CalibrationTableHandler()
Constructor.
static DataObjects::TableWorkspace_sptr createCalibrationTableWorkspace(const std::string &wsname, bool iscomponent)
Create a calibration TableWorkspace from scratch for either single component or full set of component...
bool isSingleComponentTable
DataObjects::TableWorkspace_sptr saveCompomentDatabase(const std::string &datestamp, const std::string &component, const std::string &filename)
Save a single component in the calibration workspace.
void saveCalibrationTable(const std::string &filename)
Save the calibration table (of a single date)
static void appendCalibration(const DataObjects::TableWorkspace_sptr &tablews, const std::string &datestamp, const ComponentPosition &pos)
Append a new row to single component calibration table.
std::vector< std::string > getComponentNames()
Get component name from the table.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
static const std::vector< std::string > calibrationTableColumnTypes
static const std::vector< std::string > calibrationTableColumnNames
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
Load a single-component database file to a table workspace of history of positions for the component.
Structure to handle all the calibration component positions.
@ Input
An input workspace.
@ Output
An output workspace.