Skip to content

Commit

Permalink
Introduction of SaveCanSAS1D2.
Browse files Browse the repository at this point in the history
This algorithm implements the CanSAS version 1.1

re #6839
  • Loading branch information
gesnerpassos committed Apr 12, 2013
1 parent 447cc30 commit 2441d60
Show file tree
Hide file tree
Showing 3 changed files with 372 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/DataHandling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ set ( SRC_FILES
src/SaveCSV.cpp
src/SaveCalFile.cpp
src/SaveCanSAS1D.cpp
src/SaveCanSAS1D2.cpp
src/SaveDASC.cpp
src/SaveDaveGrp.cpp
src/SaveDetectorMasks.cpp
Expand Down Expand Up @@ -248,6 +249,7 @@ set ( INC_FILES
inc/MantidDataHandling/SaveCSV.h
inc/MantidDataHandling/SaveCalFile.h
inc/MantidDataHandling/SaveCanSAS1D.h
inc/MantidDataHandling/SaveCanSAS1D2.h
inc/MantidDataHandling/SaveDASC.h
inc/MantidDataHandling/SaveDaveGrp.h
inc/MantidDataHandling/SaveDetectorMasks.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifndef MANTID_DATAHANDLING_SAVECANSAS1D2_H
#define MANTID_DATAHANDLING_SAVECANSAS1D2_H

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "SaveCanSAS1D.h"
#include <fstream>

namespace Poco{
namespace XML{
class Document;
class Element;
class Text;
}
}

namespace Mantid
{
namespace DataHandling
{
/** @class SaveCanSAS1D2 DataHandling/SaveCanSAS1D2.h
@verbatim
This algorithm saves workspace into CanSAS1d format. This is an xml format except
the <Idata>, </Idata> tags and all data in between must be one line, which necesitates
the files be written iostream functions outside xml libraries.
The second version of CanSAS1D implements the version 1.1, whose schema is found at
http://www.cansas.org/formats/1.1/cansas1d.xsd. See the tutorial for more infomation
about: http://www.cansas.org/svn/1dwg/trunk/doc/cansas-1d-1_1-manual.pdf.
The first version of SaveCanSAS1D implemented the version 1.0 of CanSAS.
The main difference among them is the definition of the SASRoot and the
introduction of a new element called SAStransmission_spectrum, which allows
to record the Spectrum of the transmission for the sample and can.
So, the SaveCanSAS1D2 will extend SaveCanSAS1D in the following:
- Introduction of 2 new (optional) workspace properties:
- Transmission - The workspace for of the transmission
- TransmissionCan - The workspace for the transmission can
- Extension of the ::init method in order to introduce these workspaces properties.
- Overide the ::createSASRootElement to conform the new header.
- Introduction of the method to deal with the new element: ::createSASTransElement.
- Override the ::exec method to introduce this new element when apropriated.
@author Gesner Passos, Rutherford Appleton Laboratory
@date 11/04/2013
Copyright &copy; 2007-10 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport SaveCanSAS1D2 : public SaveCanSAS1D
{
public:
/// default constructor
SaveCanSAS1D2();
virtual ~SaveCanSAS1D2();

/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return 2; }
protected:
/// Extends the SaveCanSAS1D init method
virtual void init();
/// Overwrites Algorithm method
virtual void exec();

/// Create the SASRoot element
virtual void createSASRootElement(std::string& rootElem);

/// this method creates SAStransmission_spectrum element
void createSASTransElement(std::string& sasTrans, const std::string & name );

///points to the workspace that will be written to file
API::MatrixWorkspace_const_sptr m_trans_ws, m_transcan_ws;
};

}
}

#endif
269 changes: 269 additions & 0 deletions Code/Mantid/Framework/DataHandling/src/SaveCanSAS1D2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/*WIKI*
Saves the given workspace to a file which will be in canSAS 1-D format specified by canSAS 1-D Data Formats Working Group schema http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd. CANSAS has a Wiki page at http://www.smallangles.net/wgwiki/index.php/canSAS_Working_Groups.
The second version is compatible with canSAS - 1D - Version 1.1. If you need to save to the version 1.0 please
use the first version of this algorithm. You can see it at: [SaveCanSAS1D_1.0].
Workspace group members and appended workspaces are stored in separate SASentry [http://en.wikipedia.org/wiki/Xml xml] elements.
This algorithm saves workspace into CanSAS1d format. This is an xml format except
the <Idata>, </Idata> tags and all data in between must be one line, which necesitates
the files be written iostream functions outside xml libraries.
The second version of CanSAS1D implements the version 1.1, whose schema is found at
http://www.cansas.org/formats/1.1/cansas1d.xsd. See the tutorial for more infomation
about: http://www.cansas.org/svn/1dwg/trunk/doc/cansas-1d-1_1-manual.pdf.
The structure of CanSAS1d xml is defined at the links above.
*WIKI*/
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/SaveCanSAS1D2.h"
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/Exception.h"
#include "MantidGeometry/IComponent.h"
#include "MantidAPI/WorkspaceValidators.h"
#include "MantidKernel/MantidVersion.h"
#include "MantidKernel/ListValidator.h"
#include "MantidAPI/Run.h"
#include <boost/shared_ptr.hpp>

//-----------------------------------------------------------------------------
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
using namespace Mantid::API;

namespace Mantid
{
namespace DataHandling
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SaveCanSAS1D2)

/// constructor
SaveCanSAS1D2::SaveCanSAS1D2()
{}

/// destructor
SaveCanSAS1D2::~SaveCanSAS1D2()
{}

/// Overwrites Algorithm method.
void SaveCanSAS1D2::init()
{
SaveCanSAS1D::init();

declareProperty(new API::WorkspaceProperty<>("Transmission","",Kernel::Direction::Input,PropertyMode::Optional,
boost::make_shared<API::WorkspaceUnitValidator>("Wavelength")),"The transmission workspace. Optional. If given, will be saved at TransmissionSpectrum");

declareProperty(new API::WorkspaceProperty<>("TransmissionCan","",Kernel::Direction::Input,PropertyMode::Optional,
boost::make_shared<API::WorkspaceUnitValidator>("Wavelength")),"The transmission workspace of the Can. Optional. If given, will be saved at TransmissionSpectrum");

}

/// Overwrites Algorithm method
void SaveCanSAS1D2::exec()
{
m_workspace = getProperty("InputWorkspace");
m_trans_ws = getProperty("Transmission");
m_transcan_ws = getProperty("TransmissionCan");
if( ! m_workspace )
{
throw std::invalid_argument("Invalid inputworkspace ,Error in SaveCanSAS1D");
}

if (m_workspace->getNumberHistograms() > 1)
{
throw std::invalid_argument("Error in SaveCanSAS1D - more than one histogram.");
}

if ((m_trans_ws && m_trans_ws->getNumberHistograms() > 1)
||
(m_transcan_ws && m_transcan_ws->getNumberHistograms() > 1)){
throw std::invalid_argument("Error in SaveCanSAS1D - more than one histogram for the transmission workspaces");
}

// write xml manually as the user requires a specific format were the placement of new line characters is controled
//and this can't be done in using the stylesheet part in Poco or libXML
prepareFileToWriteEntry();

m_outFile << "\n\t<SASentry name=\"" << m_workspace->getName() << "\">";

std::string sasTitle;
createSASTitleElement(sasTitle);
m_outFile<<sasTitle;

std::string sasRun;
createSASRunElement(sasRun);
m_outFile<<sasRun;

std::string dataUnit = m_workspace->YUnitLabel();
//look for xml special characters and replace with entity refrence
searchandreplaceSpecialChars(dataUnit);


std::string sasData;
createSASDataElement(sasData);
m_outFile<<sasData;

if (m_trans_ws){
std::string transData;
createSASTransElement(transData, "sample");
m_outFile << transData;
}
if (m_transcan_ws){
std::string transData;
createSASTransElement(transData, "can");
m_outFile << transData;
}

std::string sasSample;
createSASSampleElement(sasSample);
m_outFile<<sasSample;

std::string sasInstr="\n\t\t<SASinstrument>";
m_outFile<<sasInstr;
std::string sasInstrName="\n\t\t\t<name>";
std::string instrname=m_workspace->getInstrument()->getName();
//look for xml special characters and replace with entity refrence
searchandreplaceSpecialChars(instrname);
sasInstrName+=instrname;
sasInstrName+="</name>";
m_outFile<<sasInstrName;

std::string sasSource;
createSASSourceElement(sasSource);
m_outFile<<sasSource;

std::string sasCollimation="\n\t\t\t<SAScollimation/>";
m_outFile<<sasCollimation;


try
{
std::string sasDet;
createSASDetectorElement(sasDet);
m_outFile<<sasDet;
}
catch(Kernel::Exception::NotFoundError&)
{
m_outFile.close();
throw;
}
catch(std::runtime_error& )
{
m_outFile.close();
throw ;
}

sasInstr="\n\t\t</SASinstrument>";
m_outFile<<sasInstr;

std::string sasProcess;
createSASProcessElement(sasProcess);
m_outFile<<sasProcess;

// Reduction process, if available
const std::string process_xml = getProperty("Process");
if (process_xml.size()>0)
{
m_outFile << "\n\t\t<SASProcess>\n";
m_outFile << process_xml;
m_outFile << "\n\t\t</SASProcess>\n";
}

std::string sasNote="\n\t\t<SASnote>";
sasNote+="\n\t\t</SASnote>";
m_outFile<<sasNote;

m_outFile << "\n\t</SASentry>";
m_outFile << "\n</SASroot>";
m_outFile.close();
}


/** This method creates an XML element named "SASroot"
* @param rootElem :: xml root element string
*/
void SaveCanSAS1D2::createSASRootElement(std::string& rootElem)
{
rootElem="<SASroot version=\"1.1\"";
rootElem +="\n\t\t xmlns=\"urn:cansas1d:1.1\"";
rootElem+="\n\t\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
rootElem+="\n\t\t xsi:schemaLocation=\"urn:cansas1d:1.1 http://www.cansas.org/formats/1.1/cansas1d.xsd\"\n\t\t>";
}

/** This method creates an XML element named "SAStransmission_spectrum"
* @param sasData :: string for sasdata element in the xml
* @param name :: name of the type of spectrum. Two values are acceptable: sample, can
*/
void SaveCanSAS1D2::createSASTransElement(std::string& sasTrans, const std::string & name)
{
API::MatrixWorkspace_const_sptr m_ws;
if (name == "sample"){
m_ws = m_trans_ws;
}else if (name == "can"){
m_ws = m_transcan_ws;
}else{
return ; // does not change sasTrans... it will add nothing
}

if (m_ws->getNumberHistograms() != 1)
return; // does not change sasTrans

std::stringstream trans;

trans << "\n\t\t<SAStransmission_spectrum name=\""
<< name
<< "\">";
std::string t_unit = m_ws->YUnitLabel();
std::string lambda_unit;// = m_ws->getUnits(); FIXME: how to get the unit
if (t_unit.empty())
t_unit = "none";
if (lambda_unit.empty())
lambda_unit = "A";

const MantidVec& xdata = m_ws->readX(0);
const MantidVec& ydata = m_ws->readY(0);
const MantidVec& edata = m_ws->readE(0);
const bool isHistogram = m_ws->isHistogramData();
double lambda, trans_value, trans_err;
for (size_t j = 0; j < m_workspace->blocksize(); ++j)
{
// x data is the Lambda in xml. If histogramdata take the mean
lambda = isHistogram ? (xdata[j] + xdata[j+1])/2: xdata[j];
// y data is the T in xml.
trans_value = ydata[j];
// e data is the Tdev in xml.
trans_err = edata[j];

trans << "\n\t\t\t<Tdata><Lambda unit=\""<<lambda_unit
<<"\">";
if (lambda == lambda) // check for NAN
trans << lambda ;
else
trans << "NaN";
trans << "</Lambda>"
<<"<T unit=\""<< t_unit <<"\">";
if (trans_value == trans_value)
trans << trans_value;
else
trans << "NaN";
trans << "</T><Tdev unit=\"none\">";
if (trans_err == trans_err)
trans << trans_err;
else
trans << "NaN";
trans << "</Tdev></Tdata>";
}
trans << "\n\t\t</SAStransmission_spectrum>";
sasTrans += trans.str();
}

}

}

0 comments on commit 2441d60

Please sign in to comment.