23using namespace Kernel;
24using namespace DataObjects;
37 "The workspace name to use as input");
39 "The name to use when writing the file");
41 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
42 mustBePositive->setLower(0.0);
44 "No bin that contains x values lower than this will be saved (default\n"
47 "No bin that contains x values higher than this will saved (default\n"
48 "0 signifies the highest x value)");
57 g_log.
debug() <<
"Parameters: Filename='" << filename <<
"'\n";
62 if (!inputWorkspace) {
63 g_log.
error(
"Failed to retrieve inputWorkspace.");
70 std::ofstream outVTP(filename.c_str());
72 g_log.
error(
"Failed to open file: " + filename);
77 outVTP <<
"<?xml version=\"1.0\"?>\n"
78 "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" "
79 "byte_order=\"LittleEndian\">\n"
80 "<UnstructuredGrid>\n";
82 const std::string workspaceID = inputWorkspace->id();
83 if (workspaceID.find(
"Workspace2D") != std::string::npos) {
84 const Workspace2D_sptr localWorkspace = std::dynamic_pointer_cast<Workspace2D>(inputWorkspace);
90 for (
int hNum = 2; hNum < 100; ++hNum) {
91 writeVTKPiece(outVTP, localWorkspace->x(hNum).rawData(), localWorkspace->y(hNum).rawData(),
92 localWorkspace->e(hNum).rawData(), hNum);
96 for (
int hNum = 2; hNum < 100; ++hNum) {
98 auto &
X = localWorkspace->x(hNum);
99 auto &
Y = localWorkspace->y(hNum);
100 auto &E = localWorkspace->e(hNum);
102 std::vector<double> xValue, yValue, errors;
103 std::vector<double>::size_type nVals(
Y.size());
105 for (
int i = 0; i < static_cast<int>(nVals); ++i) {
111 if (xMax &&
X[i + 1] >
m_Xmax) {
112 xValue.emplace_back(
X[i]);
116 xValue.emplace_back(
X[i]);
117 if (i ==
static_cast<int>(nVals) - 1) {
118 xValue.emplace_back(
X[i + 1]);
121 yValue.emplace_back(
Y[i]);
122 errors.emplace_back(E[i]);
125 assert((
int)xValue.size() == (
int)yValue.size() + 1);
133 Poco::File(filename).remove();
138 outVTP <<
"</UnstructuredGrid>\n</VTKFile>\n";
151 throw std::invalid_argument(
"SaveVTK: Inconsistent range values");
164 const std::vector<double> &errors,
int index)
const {
167 auto nY =
static_cast<int>(yValue.size());
169 outVTP <<
"<Piece NumberOfPoints=\"" << nPoints <<
"\" NumberOfCells=\"" << nY <<
"\">";
170 outVTP <<
"<CellData Scalars=\"counts\">"
171 <<
"<DataArray type=\"Float32\" Name=\"counts\" "
172 "NumberOfComponents=\"1\" format=\"ascii\">\n";
173 for (
int i = 0; i < nY; ++i) {
174 outVTP << yValue[i] <<
"\n";
176 outVTP <<
"</DataArray></CellData>\n";
179 <<
"<DataArray type=\"Float32\" NumberOfComponents=\"3\" "
180 "format=\"ascii\">\n";
183 for (
int i = 0; i < nY; ++i) {
185 double xLow(xValue[i]), xUpp(xValue[i + 1]), ypos(yValue[i]), zpos(-
index * deltaZ);
186 outVTP << xLow <<
" " << 0.0 <<
" " << zpos <<
"\n";
187 outVTP << xUpp <<
" " << 0.0 <<
" " << zpos <<
"\n";
188 outVTP << xLow <<
" " << ypos <<
" " << zpos <<
"\n";
189 outVTP << xUpp <<
" " << ypos <<
" " << zpos <<
"\n";
191 zpos = -(
index + 1) * deltaZ;
192 outVTP << xLow <<
" " << 0.0 <<
" " << zpos <<
"\n";
193 outVTP << xUpp <<
" " << 0.0 <<
" " << zpos <<
"\n";
194 outVTP << xLow <<
" " << ypos <<
" " << zpos <<
"\n";
195 outVTP << xUpp <<
" " << ypos <<
" " << zpos <<
"\n";
197 outVTP <<
"</DataArray></Points>\n";
198 outVTP <<
"<Cells>\n"
199 <<
"<DataArray type=\"Int32\" Name =\"connectivity\" format=\"ascii\">\n";
200 for (
int i = 0; i < nPoints; ++i) {
203 outVTP <<
"</DataArray>\n";
204 outVTP <<
"<DataArray type=\"Int32\" Name =\"offsets\" format=\"ascii\">\n";
205 for (
int i = 8; i <= nPoints; i += 8) {
208 outVTP <<
"</DataArray>\n";
209 outVTP <<
"<DataArray type=\"UInt8\" Name =\"types\" format=\"ascii\">\n";
210 for (
int i = 0; i < nPoints; ++i) {
213 outVTP <<
"</DataArray>\n";
215 outVTP <<
"</Cells>\n";
217 outVTP <<
"</Piece>\n";
#define DECLARE_ALGORITHM(classname)
std::map< DeltaEMode::Type, std::string > index
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
Helper class for reporting progress from algorithms.
A property class for workspaces.
void writeVTKPiece(std::ostream &outVTP, const std::vector< double > &xValue, const std::vector< double > &yValue, const std::vector< double > &errors, int index) const
Write a histogram to the file.
double m_Xmin
The x-axis minimum.
void checkOptionalProperties()
Check the optional properties.
SaveVTK()
Default constructor.
void exec() override
Override virtual exec function.
double m_Xmax
The x-axis minimum.
void init() override
Override virtual init function.
Records the filename and the description of failure.
Marks code as not implemented yet.
Exception thrown when an attempt is made to dereference a null pointer.
void debug(const std::string &msg)
Logs at debug 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< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
@ Input
An input workspace.