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 "Poco/File.h"
17#include <boost/tokenizer.hpp>
18#include <fstream>
19
20namespace Mantid::DataHandling {
21// Register the algorithm into the algorithm factory
22DECLARE_ALGORITHM(SaveTBL)
23
24using namespace Kernel;
25using namespace API;
26
28SaveTBL::SaveTBL() : m_sep(','), m_stichgroups(), m_nogroup() {}
29
32 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".tbl"),
33 "The filename of the output TBL file.");
34
35 declareProperty(std::make_unique<WorkspaceProperty<ITableWorkspace>>("InputWorkspace", "", Direction::Input),
36 "The name of the workspace containing the data you want to "
37 "save to a TBL file.");
38}
39
45 size_t rowCount = ws->rowCount();
46 for (size_t i = 0; i < rowCount; ++i) {
47 TableRow row = ws->getRow(i);
48 if (row.cell<int>(ws->columnCount() - 2) != 0) {
49 // it was part of a group
50 m_stichgroups[row.cell<int>(ws->columnCount() - 2)].emplace_back(i);
51 } else {
52 // it wasn't part of a group
53 m_nogroup.emplace_back(i);
54 }
55 }
56}
57
62 // Get the workspace
63 ITableWorkspace_sptr ws = getProperty("InputWorkspace");
64 if (!ws)
65 throw std::runtime_error("Please provide an input workspace to be saved.");
66 findGroups(ws);
67 std::string filename = getProperty("Filename");
68 std::ofstream file(filename.c_str());
69
70 if (!file) {
71 throw Exception::FileError("Unable to create file: ", filename);
72 }
73
74 std::vector<std::string> columnHeadings = ws->getColumnNames();
75 writeColumnNames(file, columnHeadings);
76 for (size_t rowIndex = 0; rowIndex < ws->rowCount(); ++rowIndex) {
77 TableRow row = ws->getRow(rowIndex);
78 for (size_t columnIndex = 0; columnIndex < columnHeadings.size(); columnIndex++) {
79 if (ws->getColumn(columnIndex)->type() != "str") {
80 file.close();
81 int error = remove(filename.c_str());
82 if (error == 0)
83 throw std::runtime_error(columnHeadings[columnIndex] + " column must be of type \"str\". The TBL "
84 "file will not be saved");
85 else
86 throw std::runtime_error("Saving TBL unsuccessful, please check the " + columnHeadings[columnIndex] +
87 " column is of type \"str\".");
88 } else {
89 if (columnIndex == columnHeadings.size() - 1)
90 writeVal<std::string>(row.String(columnIndex), file, false, true);
91 else
92 writeVal<std::string>(row.String(columnIndex), file);
93 }
94
95 } // col for-loop
96 } // row for-loop
97 file.close();
98}
99
100void SaveTBL::writeColumnNames(std::ofstream &file, std::vector<std::string> const &columnHeadings) {
101 auto it = columnHeadings.begin();
102 for (; it != columnHeadings.end() - 1; ++it) {
103 auto const &heading = *it;
104 writeVal<std::string>(heading, file);
105 }
106 auto const &lastHeading = *it;
107 writeVal<std::string>(lastHeading, file, false, true);
108}
109
118template <class T> void SaveTBL::writeVal(const T &val, std::ofstream &file, bool endsep, bool endline) {
119 std::string valStr = boost::lexical_cast<std::string>(val);
120 size_t comPos = valStr.find(',');
121 if (comPos != std::string::npos) {
122 file << '"' << val << '"';
123 } else {
124 file << boost::lexical_cast<T>(val);
125 }
126 if (endsep) {
127 file << m_sep;
128 }
129 if (endline) {
130 file << '\n';
131 }
132}
133} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double error
Definition: IndexPeaks.cpp:133
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
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:100
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:118
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:44
void init() override
Overwrites Algorithm method.
Definition: SaveTBL.cpp:31
SaveTBL()
Default constructor.
Definition: SaveTBL.cpp:28
void exec() override
Overwrites Algorithm method.
Definition: SaveTBL.cpp:61
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