16#include <Poco/DateTimeFormatter.h>
17#include <Poco/LocalDateTime.h>
32 "The name of the workspace to save");
33 const std::vector<std::string> fileExts{
".txt",
".Q",
".dat",
".out"};
35 "The name to use when saving the file");
36 declareProperty(
"Append",
true,
"If true and Filename already exists, append, else overwrite");
50 g_log.
error(
"This algorithm expects a 2d workspace to have been converted away from \
51 spectrum numbers on the vertical axis");
52 throw std::invalid_argument(
"Cannot write out this kind of workspace");
57 ios_base::openmode mode = (
getProperty(
"Append") ? (ios_base::out | ios_base::app) : ios_base::out);
60 Poco::Path path(filename);
61 std::string ext = path.getExtension();
66 m_outRKH.open(filename.c_str(), mode);
69 g_log.
error() <<
"An error occurred while attempting to open the file " << filename <<
"\n";
70 throw std::runtime_error(
"An error occurred while trying to open the output file for writing");
90 Poco::LocalDateTime timestamp;
93 m_outRKH << Poco::DateTimeFormatter::format(timestamp, std::string(
"%w")) <<
" "
94 << Poco::DateTimeFormatter::format(timestamp, std::string(
"%d")) <<
"-";
95 std::string month = Poco::DateTimeFormatter::format(timestamp, std::string(
"%b"));
96 std::transform(month.begin(), month.end(), month.begin(), toupper);
97 m_outRKH << month <<
"-" << Poco::DateTimeFormatter::format(timestamp, std::string(
"%Y %H:%M"))
104 const int unitCode1 = unit1->caption() ==
"q" ?
Q_CODE : 0;
105 const int unitCode2 = unit2->caption() ==
"q" ?
Q_CODE : 0;
106 m_outRKH <<
" " << unitCode1 <<
" " << unit1->caption() <<
" (" << unit1->label().ascii() <<
")\n"
107 <<
" " << unitCode2 <<
" " << unit2->caption() <<
" (" << unit2->label().ascii() <<
")\n"
116 m_outRKH << std::setw(5) << noDataPoints <<
" 0 0 0 1" << std::setw(5) << noDataPoints <<
" 0\n"
118 <<
" 3 (F12.5,2E16.6)\n";
125 const size_t nhist =
m_workspace->getNumberHistograms();
126 const bool horizontal = nhist == 1;
128 g_log.
notice() <<
"Values in first column are the X values\n";
132 g_log.
notice(
"Values in first column are spectrum numbers");
133 const bool histogram =
m_workspace->isHistogramData();
134 Progress prg(
this, 0.0, 1.0, noDataPoints);
137 for (
size_t i = 0; i < nhist; ++i) {
144 specid =
m_workspace->getSpectrum(i).getSpectrumNo();
150 auto dXvals =
m_workspace->pointStandardDeviations(i);
152 for (
size_t j = 0; j < nbins; ++j) {
156 xval = histogram ? 0.5 * (xdata[j] + xdata[j + 1]) : xdata[j];
158 xval =
static_cast<double>(specid);
161 m_outRKH << std::fixed << std::setw(12) << std::setprecision(5) << xval << std::scientific << std::setw(16)
162 << std::setprecision(6) << ydata[j] << std::setw(16) << edata[j];
165 m_outRKH << std::setw(16) << dXvals[j];
178 const size_t Xbins =
X->
length();
180 for (
size_t i = 0; i < Xbins; ++i) {
181 m_outRKH << std::setw(14) << std::scientific << std::setprecision(6) << (*X)(i);
186 const size_t Ybins =
Y->
length();
188 for (
size_t i = 0; i < Ybins; ++i) {
189 m_outRKH << std::setw(14) << std::scientific << std::setprecision(6) << (*Y)(i);
195 const size_t num_hist =
m_workspace->getNumberHistograms();
197 << std::setprecision(12) << 1.0 <<
"\n";
199 m_outRKH <<
" " << iflag <<
"(8E12.4)\n";
201 bool requireNewLine =
false;
203 for (
size_t i = 0; i < num_hist; ++i) {
205 m_outRKH << std::setw(12) << std::scientific << std::setprecision(4) << yVal;
206 requireNewLine =
true;
209 requireNewLine =
false;
222 for (
size_t i = 0; i < num_hist; ++i) {
224 m_outRKH << std::setw(12) << std::scientific << std::setprecision(4) << eVal;
#define DECLARE_ALGORITHM(classname)
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Class to represent the axis of a workspace.
virtual std::size_t length() const =0
Get the length of the axis.
@ Save
to specify a file to write to, the file may or may not exist
Helper class for reporting progress from algorithms.
A property class for workspaces.
@ LINE_LENGTH
with the unit Q
@ Q_CODE
this is the integer code the RKH file format associates
std::ofstream m_outRKH
The output filehandle.
void init() override
Initialisation code.
API::MatrixWorkspace_const_sptr m_workspace
The input workspace.
void write2D()
Writes out the 2D data.
void exec() override
Execution code.
void write1D()
Writes out the 1D data.
void writeHeader()
Writes out the header of the output file.
bool m_2d
Whether this is a 2D dataset.
void notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
std::shared_ptr< const Unit > Unit_const_sptr
Shared pointer to the Unit base class (const version)
int32_t specnum_t
Typedef for a spectrum Number.
@ Input
An input workspace.