20void encode(std::string &data) {
22 buffer.reserve(data.size());
24 for (
auto const &element : data) {
27 buffer.append(
"&");
30 buffer.append(
""");
33 buffer.append(
"'");
36 buffer.append(
"<");
39 buffer.append(
">");
42 buffer.push_back(element);
65 std::make_shared<API::WorkspaceUnitValidator>(
"Wavelength")),
66 "The transmission workspace. Optional. If given, will be saved at "
67 "TransmissionSpectrum");
71 std::make_shared<API::WorkspaceUnitValidator>(
"Wavelength")),
72 "The transmission workspace of the Can. Optional. If given, will be "
73 "saved at TransmissionSpectrum");
75 declareProperty(
"SampleTransmissionRunNumber",
"",
"The run number for the sample transmission workspace. Optional.");
76 declareProperty(
"SampleDirectRunNumber",
"",
"The run number for the sample direct workspace. Optional.");
77 declareProperty(
"CanScatterRunNumber",
"",
"The run number for the can scatter workspace. Optional.");
78 declareProperty(
"CanDirectRunNumber",
"",
"The run number for the can direct workspace. Optional.");
79 declareProperty(
"OneSpectrumPerFile",
false,
"If true, each spectrum will be saved in an invididual file");
82 "BackgroundSubtractionWorkspace",
"",
83 "The name of the workspace used in the scaled background subtraction, to be included in the metadata. Optional.");
85 "BackgroundSubtractionScaleFactor", 0.0,
86 "The scale factor used in the scaled background subtraction, to be included in the metadata. Optional.");
95 throw std::invalid_argument(
"Invalid inputworkspace ,Error in SaveCanSAS1D");
100 throw std::invalid_argument(
"Error in SaveCanSAS1D - more than one "
101 "histogram for the transmission workspaces");
109 while (i < m_workspace->getNumberHistograms()) {
110 std::string fileName;
113 size_t extPosition = fileName.find(
".xml");
114 if (extPosition == std::string::npos)
115 extPosition = fileName.size();
116 std::ostringstream ss;
117 ss << std::string(fileName, 0, extPosition) <<
"_" << i;
119 if (axis->isNumeric()) {
120 auto binEdgeAxis =
dynamic_cast<BinEdgeAxis *
>(axis);
122 ss <<
"_" << binEdgeAxis->
label(i);
124 ss <<
"_" << axis->getValue(i) << axis->unit()->label().ascii();
125 }
else if (axis->isText())
126 ss <<
"_" << axis->label(i);
127 ss << std::string(fileName, extPosition);
136 std::string sasTitle;
153 while (i < m_workspace->getNumberHistograms()) {
161 std::string transData;
166 std::string transData;
171 std::string sasSample;
177 std::string sasInstrument;
182 }
catch (std::runtime_error &) {
187 std::string sasProcess;
191 std::string sasNote =
"\n\t\t<SASnote>";
192 sasNote +=
"\n\t\t</SASnote>";
205 rootElem =
"<SASroot version=\"1.1\"";
206 rootElem +=
"\n\t\t xmlns=\"urn:cansas1d:1.1\"";
207 rootElem +=
"\n\t\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
208 rootElem +=
"\n\t\t xsi:schemaLocation=\"urn:cansas1d:1.1 "
209 "http://www.cansas.org/formats/1.1/cansas1d.xsd\"\n\t\t>";
216 sasProcess =
"\n\t\t<SASprocess>";
219 std::string sasProcname =
"\n\t\t\t<name>";
220 sasProcname +=
"Mantid generated CanSAS1D XML";
221 sasProcname +=
"</name>";
222 sasProcess += sasProcname;
228 strftime(temp, 25,
"%d-%b-%Y %H:%M:%S", localtime(&rawtime));
229 std::string sasDate(temp);
231 std::string sasProcdate =
"\n\t\t\t<date>";
232 sasProcdate += sasDate;
233 sasProcdate +=
"</date>";
234 sasProcess += sasProcdate;
236 std::string sasProcsvn =
"\n\t\t\t<term name=\"svn\">";
238 sasProcsvn +=
"</term>";
239 sasProcess += sasProcsvn;
242 std::string user_file;
249 std::string sasProcuserfile =
"\n\t\t\t<term name=\"user_file\">";
250 sasProcuserfile += user_file;
251 sasProcuserfile +=
"</term>";
253 sasProcess += sasProcuserfile;
255 std::string batch_file;
259 std::string sasProcbatchfile =
"\n\t\t\t<term name=\"batch_file\">";
260 sasProcbatchfile += batch_file;
261 sasProcbatchfile +=
"</term>";
262 sasProcess += sasProcbatchfile;
267 const auto sample_trans_run =
getPropertyValue(
"SampleTransmissionRunNumber");
268 sasProcess +=
"\n\t\t\t<term name=\"sample_trans_run\">";
269 sasProcess += sample_trans_run +
"</term>";
273 sasProcess +=
"\n\t\t\t<term name=\"sample_direct_run\">";
274 sasProcess += sample_direct_run +
"</term>";
282 can_run = logP->
value();
284 g_log.
debug() <<
"Didn't find RunNumber log in workspace. Writing "
285 "<Run></Run> to the CANSAS file\n";
287 std::string sasProcCanRun =
"\n\t\t\t<term name=\"can_trans_run\">";
288 sasProcCanRun += can_run;
289 sasProcCanRun +=
"</term>";
290 sasProcess += sasProcCanRun;
294 sasProcess +=
"\n\t\t\t<term name=\"can_scatter_run\">";
295 sasProcess += can_scatter_run +
"</term>";
299 sasProcess +=
"\n\t\t\t<term name=\"can_direct_run\">";
300 sasProcess += can_direct_run +
"</term>";
304 auto const &bgsubWsName =
getPropertyValue(
"BackgroundSubtractionWorkspace");
305 auto const &bgsubScaleFactor =
getPropertyValue(
"BackgroundSubtractionScaleFactor");
306 if (!bgsubWsName.empty()) {
307 sasProcess +=
"\n\t\t\t<term name=\"scaled_bgsub_workspace\">";
308 sasProcess += bgsubWsName +
"</term>";
309 sasProcess +=
"\n\t\t\t<term name=\"scaled_bgsub_scale_factor\">";
310 sasProcess += bgsubScaleFactor +
"</term>";
315 if (!process_xml.empty()) {
316 std::string processNote =
"\n\t\t\t<SASprocessnote>";
318 processNote += process_xml;
319 processNote +=
"</SASprocessnote>";
320 sasProcess += processNote;
322 sasProcess +=
"\n\t\t\t<SASprocessnote/>";
325 sasProcess +=
"\n\t\t</SASprocess>";
335 if (
name ==
"sample") {
337 }
else if (
name ==
"can") {
343 if (m_ws->getNumberHistograms() != 1)
346 std::stringstream trans;
348 trans <<
"\n\t\t<SAStransmission_spectrum name=\"" <<
name <<
"\">";
349 std::string t_unit = m_ws->YUnit();
350 std::string lambda_unit = m_ws->getAxis(0)->unit()->label();
353 if (lambda_unit.empty() || lambda_unit ==
"Angstrom")
357 const auto lambda = m_ws->points(0);
359 const auto &trans_value = m_ws->y(0);
361 const auto &trans_err = m_ws->e(0);
362 for (
size_t j = 0; j < trans_value.size(); ++j) {
363 trans <<
"\n\t\t\t<Tdata><Lambda unit=\"" << lambda_unit <<
"\">";
364 if (std::isnan(
lambda[j]))
369 <<
"<T unit=\"" << t_unit <<
"\">";
370 if (std::isnan(trans_value[j]))
373 trans << trans_value[j];
374 trans <<
"</T><Tdev unit=\"none\">";
375 if (std::isnan(trans_err[j]))
378 trans << trans_err[j];
379 trans <<
"</Tdev></Tdata>";
381 trans <<
"\n\t\t</SAStransmission_spectrum>";
382 sasTrans += trans.str();
392 m_outFile.open(fileName.c_str(), std::ios::out | std::ios::trunc);
395 <<
"<?xml-stylesheet type=\"text/xsl\" href=\"cansas1d.xsl\" ?>\n";
399 }
catch (std::fstream::failure &) {
#define DECLARE_ALGORITHM(classname)
const std::vector< double > * lambda
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.
Stores numeric values that are assumed to be bin edge values.
std::string label(const std::size_t &index) const override
Returns a text label which shows the value at the given bin index.
bool hasProperty(const std::string &name) const
Does the property exist on the object.
Kernel::Property * getLogData(const std::string &name) const
Access a single log entry.
This class stores information regarding an experimental run as a series of log entries.
A property class for workspaces.
This algorithm saves workspace into CanSAS1d format.
API::MatrixWorkspace_const_sptr m_transcan_ws
API::MatrixWorkspace_const_sptr m_trans_ws
points to the workspace that will be written to file
void createSASRootElement(std::string &rootElem) override
Create the SASRoot element.
void createSASProcessElement(std::string &sasProcess)
this method creates SASProcess element
void createSASTransElement(std::string &sasTrans, const std::string &name)
this method creates SAStransmission_spectrum element
void exec() override
Overwrites Algorithm method.
void writeHeader(const std::string &fileName) override
Overwrites writeHeader method.
std::fstream m_outFile
an fstream object is used to write the xml manually as the user requires a specific format with new l...
const std::string name() const override
function to return a name of the algorithm, must be overridden in all algorithms
void createSASTitleElement(std::string &sasTitle)
this method creates a sasTitle element
void createSASInstrument(std::string &sasInstrument)
this method creates a sasInstrument element
void init() override
Overwrites Algorithm method.
void searchandreplaceSpecialChars(std::string &input)
this method searches for xml special characters and replace with entity references
void createSASDataElement(std::string &sasData, size_t workspaceIndex)
this method creates a sasData element
void createSASSampleElement(std::string &sasSample)
this method creates a sasSample element
void prepareFileToWriteEntry(const std::string &fileName)
Opens the output file and, as necessary blanks it, writes the file header and moves the file pointer.
void createSASRunElement(std::string &sasRun)
this method creates a sasRun Element
API::MatrixWorkspace_const_sptr m_workspace
points to the workspace that will be written to file
Records the filename and the description of failure.
Exception for when an item is not found in a collection.
void debug(const std::string &msg)
Logs at debug level.
void information(const std::string &msg)
Logs at information level.
static const char * version()
The full version number.
Base class for properties.
virtual std::string value() const =0
Returns the value of the property as a string.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
@ Input
An input workspace.