21void encode(std::string &data) {
23 buffer.reserve(data.size());
25 for (
auto const &element : data) {
28 buffer.append(
"&");
31 buffer.append(
""");
34 buffer.append(
"'");
37 buffer.append(
"<");
40 buffer.append(
">");
43 buffer.push_back(element);
66 std::make_shared<API::WorkspaceUnitValidator>(
"Wavelength")),
67 "The transmission workspace. Optional. If given, will be saved at "
68 "TransmissionSpectrum");
72 std::make_shared<API::WorkspaceUnitValidator>(
"Wavelength")),
73 "The transmission workspace of the Can. Optional. If given, will be "
74 "saved at TransmissionSpectrum");
76 declareProperty(
"SampleTransmissionRunNumber",
"",
"The run number for the sample transmission workspace. Optional.");
77 declareProperty(
"SampleDirectRunNumber",
"",
"The run number for the sample direct workspace. Optional.");
78 declareProperty(
"CanScatterRunNumber",
"",
"The run number for the can scatter workspace. Optional.");
79 declareProperty(
"CanDirectRunNumber",
"",
"The run number for the can direct workspace. Optional.");
80 declareProperty(
"OneSpectrumPerFile",
false,
"If true, each spectrum will be saved in an invididual file");
83 "BackgroundSubtractionWorkspace",
"",
84 "The name of the workspace used in the scaled background subtraction, to be included in the metadata. Optional.");
86 "BackgroundSubtractionScaleFactor", 0.0,
87 "The scale factor used in the scaled background subtraction, to be included in the metadata. Optional.");
96 throw std::invalid_argument(
"Invalid inputworkspace ,Error in SaveCanSAS1D");
101 throw std::invalid_argument(
"Error in SaveCanSAS1D - more than one "
102 "histogram for the transmission workspaces");
110 while (i < m_workspace->getNumberHistograms()) {
111 std::string fileName;
114 size_t extPosition = fileName.find(
".xml");
115 if (extPosition == std::string::npos)
116 extPosition = fileName.size();
117 std::ostringstream ss;
118 ss << std::string(fileName, 0, extPosition) <<
"_" << i;
120 if (axis->isNumeric()) {
121 auto binEdgeAxis =
dynamic_cast<BinEdgeAxis *
>(axis);
123 ss <<
"_" << binEdgeAxis->
label(i);
125 ss <<
"_" << axis->getValue(i) << axis->unit()->label().ascii();
126 }
else if (axis->isText())
127 ss <<
"_" << axis->label(i);
128 ss << std::string(fileName, extPosition);
137 std::string sasTitle;
154 while (i < m_workspace->getNumberHistograms()) {
162 std::string transData;
167 std::string transData;
172 std::string sasSample;
178 std::string sasInstrument;
183 }
catch (std::runtime_error &) {
188 std::string sasProcess;
192 std::string sasNote =
"\n\t\t<SASnote>";
193 sasNote +=
"\n\t\t</SASnote>";
206 rootElem =
"<SASroot version=\"1.1\"";
207 rootElem +=
"\n\t\t xmlns=\"urn:cansas1d:1.1\"";
208 rootElem +=
"\n\t\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
209 rootElem +=
"\n\t\t xsi:schemaLocation=\"urn:cansas1d:1.1 "
210 "http://www.cansas.org/formats/1.1/cansas1d.xsd\"\n\t\t>";
217 sasProcess =
"\n\t\t<SASprocess>";
220 std::string sasProcname =
"\n\t\t\t<name>";
221 sasProcname +=
"Mantid generated CanSAS1D XML";
222 sasProcname +=
"</name>";
223 sasProcess += sasProcname;
229 strftime(temp, 25,
"%d-%b-%Y %H:%M:%S", localtime(&rawtime));
230 std::string sasDate(temp);
232 std::string sasProcdate =
"\n\t\t\t<date>";
233 sasProcdate += sasDate;
234 sasProcdate +=
"</date>";
235 sasProcess += sasProcdate;
237 std::string sasProcsvn =
"\n\t\t\t<term name=\"svn\">";
239 sasProcsvn +=
"</term>";
240 sasProcess += sasProcsvn;
243 std::string user_file;
250 std::string sasProcuserfile =
"\n\t\t\t<term name=\"user_file\">";
251 sasProcuserfile += user_file;
252 sasProcuserfile +=
"</term>";
254 sasProcess += sasProcuserfile;
256 std::string batch_file;
260 std::string sasProcbatchfile =
"\n\t\t\t<term name=\"batch_file\">";
261 sasProcbatchfile += batch_file;
262 sasProcbatchfile +=
"</term>";
263 sasProcess += sasProcbatchfile;
268 const auto sample_trans_run =
getPropertyValue(
"SampleTransmissionRunNumber");
269 sasProcess +=
"\n\t\t\t<term name=\"sample_trans_run\">";
270 sasProcess += sample_trans_run +
"</term>";
274 sasProcess +=
"\n\t\t\t<term name=\"sample_direct_run\">";
275 sasProcess += sample_direct_run +
"</term>";
283 can_run = logP->
value();
285 g_log.
debug() <<
"Didn't find RunNumber log in workspace. Writing "
286 "<Run></Run> to the CANSAS file\n";
288 std::string sasProcCanRun =
"\n\t\t\t<term name=\"can_trans_run\">";
289 sasProcCanRun += can_run;
290 sasProcCanRun +=
"</term>";
291 sasProcess += sasProcCanRun;
295 sasProcess +=
"\n\t\t\t<term name=\"can_scatter_run\">";
296 sasProcess += can_scatter_run +
"</term>";
300 sasProcess +=
"\n\t\t\t<term name=\"can_direct_run\">";
301 sasProcess += can_direct_run +
"</term>";
305 auto const &bgsubWsName =
getPropertyValue(
"BackgroundSubtractionWorkspace");
306 auto const &bgsubScaleFactor =
getPropertyValue(
"BackgroundSubtractionScaleFactor");
307 if (!bgsubWsName.empty()) {
308 sasProcess +=
"\n\t\t\t<term name=\"scaled_bgsub_workspace\">";
309 sasProcess += bgsubWsName +
"</term>";
310 sasProcess +=
"\n\t\t\t<term name=\"scaled_bgsub_scale_factor\">";
311 sasProcess += bgsubScaleFactor +
"</term>";
316 if (!process_xml.empty()) {
317 std::string processNote =
"\n\t\t\t<SASprocessnote>";
319 processNote += process_xml;
320 processNote +=
"</SASprocessnote>";
321 sasProcess += processNote;
323 sasProcess +=
"\n\t\t\t<SASprocessnote/>";
326 sasProcess +=
"\n\t\t</SASprocess>";
336 if (
name ==
"sample") {
338 }
else if (
name ==
"can") {
344 if (m_ws->getNumberHistograms() != 1)
347 std::stringstream trans;
349 trans <<
"\n\t\t<SAStransmission_spectrum name=\"" <<
name <<
"\">";
350 std::string t_unit = m_ws->YUnit();
351 std::string lambda_unit = m_ws->getAxis(0)->unit()->label();
354 if (lambda_unit.empty() || lambda_unit ==
"Angstrom")
358 const auto lambda = m_ws->points(0);
360 const auto &trans_value = m_ws->y(0);
362 const auto &trans_err = m_ws->e(0);
363 for (
size_t j = 0; j < trans_value.size(); ++j) {
364 trans <<
"\n\t\t\t<Tdata><Lambda unit=\"" << lambda_unit <<
"\">";
365 if (std::isnan(
lambda[j]))
370 <<
"<T unit=\"" << t_unit <<
"\">";
371 if (std::isnan(trans_value[j]))
375 trans <<
"</T><Tdev unit=\"none\">";
376 if (std::isnan(trans_err[j]))
380 trans <<
"</Tdev></Tdata>";
382 trans <<
"\n\t\t</SAStransmission_spectrum>";
383 sasTrans += trans.str();
393 m_outFile.open(fileName.c_str(), std::ios::out | std::ios::trunc);
396 <<
"<?xml-stylesheet type=\"text/xsl\" href=\"cansas1d.xsl\" ?>\n";
400 }
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 std::string & 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)
MANTID_DATAHANDLING_DLL std::string formatDouble(double const value)
@ Input
An input workspace.