20void encode(std::string &data) {
22 buffer.reserve(data.size());
24 for (
auto &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");
88 throw std::invalid_argument(
"Invalid inputworkspace ,Error in SaveCanSAS1D");
93 throw std::invalid_argument(
"Error in SaveCanSAS1D - more than one "
94 "histogram for the transmission workspaces");
102 while (i < m_workspace->getNumberHistograms()) {
103 std::string fileName;
106 size_t extPosition = fileName.find(
".xml");
107 if (extPosition == std::string::npos)
108 extPosition = fileName.size();
109 std::ostringstream ss;
110 ss << std::string(fileName, 0, extPosition) <<
"_" << i;
112 if (axis->isNumeric()) {
113 auto binEdgeAxis =
dynamic_cast<BinEdgeAxis *
>(axis);
115 ss <<
"_" << binEdgeAxis->
label(i);
117 ss <<
"_" << axis->getValue(i) << axis->unit()->label().ascii();
118 }
else if (axis->isText())
119 ss <<
"_" << axis->label(i);
120 ss << std::string(fileName, extPosition);
129 std::string sasTitle;
146 while (i < m_workspace->getNumberHistograms()) {
154 std::string transData;
159 std::string transData;
164 std::string sasSample;
170 std::string sasInstrument;
175 }
catch (std::runtime_error &) {
180 std::string sasProcess;
184 std::string sasNote =
"\n\t\t<SASnote>";
185 sasNote +=
"\n\t\t</SASnote>";
198 rootElem =
"<SASroot version=\"1.1\"";
199 rootElem +=
"\n\t\t xmlns=\"urn:cansas1d:1.1\"";
200 rootElem +=
"\n\t\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
201 rootElem +=
"\n\t\t xsi:schemaLocation=\"urn:cansas1d:1.1 "
202 "http://www.cansas.org/formats/1.1/cansas1d.xsd\"\n\t\t>";
209 sasProcess =
"\n\t\t<SASprocess>";
212 std::string sasProcname =
"\n\t\t\t<name>";
213 sasProcname +=
"Mantid generated CanSAS1D XML";
214 sasProcname +=
"</name>";
215 sasProcess += sasProcname;
221 strftime(temp, 25,
"%d-%b-%Y %H:%M:%S", localtime(&rawtime));
222 std::string sasDate(temp);
224 std::string sasProcdate =
"\n\t\t\t<date>";
225 sasProcdate += sasDate;
226 sasProcdate +=
"</date>";
227 sasProcess += sasProcdate;
229 std::string sasProcsvn =
"\n\t\t\t<term name=\"svn\">";
231 sasProcsvn +=
"</term>";
232 sasProcess += sasProcsvn;
235 std::string user_file;
242 std::string sasProcuserfile =
"\n\t\t\t<term name=\"user_file\">";
243 sasProcuserfile += user_file;
244 sasProcuserfile +=
"</term>";
246 sasProcess += sasProcuserfile;
248 std::string batch_file;
252 std::string sasProcbatchfile =
"\n\t\t\t<term name=\"batch_file\">";
253 sasProcbatchfile += batch_file;
254 sasProcbatchfile +=
"</term>";
255 sasProcess += sasProcbatchfile;
260 const auto sample_trans_run =
getPropertyValue(
"SampleTransmissionRunNumber");
261 sasProcess +=
"\n\t\t\t<term name=\"sample_trans_run\">";
262 sasProcess += sample_trans_run +
"</term>";
266 sasProcess +=
"\n\t\t\t<term name=\"sample_direct_run\">";
267 sasProcess += sample_direct_run +
"</term>";
275 can_run = logP->
value();
277 g_log.
debug() <<
"Didn't find RunNumber log in workspace. Writing "
278 "<Run></Run> to the CANSAS file\n";
280 std::string sasProcCanRun =
"\n\t\t\t<term name=\"can_trans_run\">";
281 sasProcCanRun += can_run;
282 sasProcCanRun +=
"</term>";
283 sasProcess += sasProcCanRun;
287 sasProcess +=
"\n\t\t\t<term name=\"can_scatter_run\">";
288 sasProcess += can_scatter_run +
"</term>";
292 sasProcess +=
"\n\t\t\t<term name=\"can_direct_run\">";
293 sasProcess += can_direct_run +
"</term>";
298 if (!process_xml.empty()) {
299 std::string processNote =
"\n\t\t\t<SASprocessnote>";
301 processNote += process_xml;
302 processNote +=
"</SASprocessnote>";
303 sasProcess += processNote;
305 sasProcess +=
"\n\t\t\t<SASprocessnote/>";
308 sasProcess +=
"\n\t\t</SASprocess>";
318 if (
name ==
"sample") {
320 }
else if (
name ==
"can") {
326 if (m_ws->getNumberHistograms() != 1)
329 std::stringstream trans;
331 trans <<
"\n\t\t<SAStransmission_spectrum name=\"" <<
name <<
"\">";
332 std::string t_unit = m_ws->YUnit();
333 std::string lambda_unit = m_ws->getAxis(0)->unit()->label();
336 if (lambda_unit.empty() || lambda_unit ==
"Angstrom")
340 const auto lambda = m_ws->points(0);
342 const auto &trans_value = m_ws->y(0);
344 const auto &trans_err = m_ws->e(0);
345 for (
size_t j = 0; j < trans_value.size(); ++j) {
346 trans <<
"\n\t\t\t<Tdata><Lambda unit=\"" << lambda_unit <<
"\">";
347 if (std::isnan(
lambda[j]))
352 <<
"<T unit=\"" << t_unit <<
"\">";
353 if (std::isnan(trans_value[j]))
356 trans << trans_value[j];
357 trans <<
"</T><Tdev unit=\"none\">";
358 if (std::isnan(trans_err[j]))
361 trans << trans_err[j];
362 trans <<
"</Tdev></Tdata>";
364 trans <<
"\n\t\t</SAStransmission_spectrum>";
365 sasTrans += trans.str();
375 m_outFile.open(fileName.c_str(), std::ios::out | std::ios::trunc);
378 <<
"<?xml-stylesheet type=\"text/xsl\" href=\"cansas1d.xsl\" ?>\n";
382 }
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.