Mantid
Loading...
Searching...
No Matches
SaveTBL.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
10
14#include "MantidAPI/TableRow.h"
16#include <boost/tokenizer.hpp>
17#include <fstream>
18
19namespace Mantid::DataHandling {
20// Register the algorithm into the algorithm factory
21DECLARE_ALGORITHM(SaveTBL)
22
23using namespace Kernel;
24using namespace API;
25
27SaveTBL::SaveTBL() : m_sep(','), m_stichgroups(), m_nogroup() {}
28
31 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".tbl"),
32 "The filename of the output TBL file.");
33
34 declareProperty(std::make_unique<WorkspaceProperty<ITableWorkspace>>("InputWorkspace", "", Direction::Input),
35 "The name of the workspace containing the data you want to "
36 "save to a TBL file.");
37}
38
44 size_t rowCount = ws->rowCount();
45 for (size_t i = 0; i < rowCount; ++i) {
46 TableRow row = ws->getRow(i);
47 if (row.cell<int>(ws->columnCount() - 2) != 0) {
48 // it was part of a group
49 m_stichgroups[row.cell<int>(ws->columnCount() - 2)].emplace_back(i);
50 } else {
51 // it wasn't part of a group
52 m_nogroup.emplace_back(i);
53 }
54 }
55}
56
61 // Get the workspace
62 ITableWorkspace_sptr ws = getProperty("InputWorkspace");
63 if (!ws)
64 throw std::runtime_error("Please provide an input workspace to be saved.");
65 findGroups(ws);
66 std::string filename = getProperty("Filename");
67 std::ofstream file(filename.c_str());
68
69 if (!file) {
70 throw Exception::FileError("Unable to create file: ", filename);
71 }
72
73 std::vector<std::string> columnHeadings = ws->getColumnNames();
74 writeColumnNames(file, columnHeadings);
75 for (size_t rowIndex = 0; rowIndex < ws->rowCount(); ++rowIndex) {
76 TableRow row = ws->getRow(rowIndex);
77 for (size_t columnIndex = 0; columnIndex < columnHeadings.size(); columnIndex++) {
78 if (ws->getColumn(columnIndex)->type() != "str") {
79 file.close();
80 int error = remove(filename.c_str());
81 if (error == 0)
82 throw std::runtime_error(columnHeadings[columnIndex] + " column must be of type \"str\". The TBL "
83 "file will not be saved");
84 else
85 throw std::runtime_error("Saving TBL unsuccessful, please check the " + columnHeadings[columnIndex] +
86 " column is of type \"str\".");
87 } else {
88 if (columnIndex == columnHeadings.size() - 1)
89 writeVal<std::string>(row.String(columnIndex), file, false, true);
90 else
91 writeVal<std::string>(row.String(columnIndex), file);
92 }
93
94 } // col for-loop
95 } // row for-loop
96 file.close();
97}
98
99void SaveTBL::writeColumnNames(std::ofstream &file, std::vector<std::string> const &columnHeadings) {
100 auto it = columnHeadings.begin();
101 for (; it != columnHeadings.end() - 1; ++it) {
102 auto const &heading = *it;
103 writeVal<std::string>(heading, file);
104 }
105 auto const &lastHeading = *it;
106 writeVal<std::string>(lastHeading, file, false, true);
107}
108
117template <class T> void SaveTBL::writeVal(const T &val, std::ofstream &file, bool endsep, bool endline) {
118 std::string valStr = boost::lexical_cast<std::string>(val);
119 size_t comPos = valStr.find(',');
120 if (comPos != std::string::npos) {
121 file << '"' << val << '"';
122 } else {
123 file << boost::lexical_cast<T>(val);
124 }
125 if (endsep) {
126 file << m_sep;
127 }
128 if (endline) {
129 file << '\n';
130 }
131}
132} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
static char constexpr m_sep
double error
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
@ Save
to specify a file to write to, the file may or may not exist
TableRow represents a row in a TableWorkspace.
Definition TableRow.h:39
std::string & String(size_t col)
Returns a reference to the element in position col if its type is std::string.
Definition TableRow.h:150
T & cell(size_t col)
Templated method to access the element col in the row.
Definition TableRow.h:115
A property class for workspaces.
void writeColumnNames(std::ofstream &file, std::vector< std::string > const &columnHeadings)
Definition SaveTBL.cpp:99
const char m_sep
the separator
Definition SaveTBL.h:50
void writeVal(const T &val, std::ofstream &file, bool endsep=true, bool endline=false)
Writes a value to the file.
Definition SaveTBL.cpp:117
std::map< int, std::vector< size_t > > m_stichgroups
Map the separator options to their string equivalents.
Definition SaveTBL.h:54
std::vector< size_t > m_nogroup
Definition SaveTBL.h:55
void findGroups(const API::ITableWorkspace_sptr &ws)
Finds the stitch groups that need to be on the same line.
Definition SaveTBL.cpp:43
void init() override
Overwrites Algorithm method.
Definition SaveTBL.cpp:30
SaveTBL()
Default constructor.
Definition SaveTBL.cpp:27
void exec() override
Overwrites Algorithm method.
Definition SaveTBL.cpp:60
Records the filename and the description of failure.
Definition Exception.h:98
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
@ Input
An input workspace.
Definition Property.h:53