Skip to content

Commit

Permalink
Implement SaveFullprofResolution. Refs #5836.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Sep 12, 2012
1 parent 56edbf9 commit 7efbc22
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Code/Mantid/Framework/DataHandling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ set ( SRC_FILES
src/SaveDetectorMasks.cpp
src/SaveDetectorsGrouping.cpp
src/SaveDspacemap.cpp
src/SaveFullprofResolution.cpp
src/SaveFocusedXYE.cpp
src/SaveGSS.cpp
src/SaveISISNexus.cpp
Expand Down Expand Up @@ -245,6 +246,7 @@ set ( INC_FILES
inc/MantidDataHandling/SaveDetectorMasks.h
inc/MantidDataHandling/SaveDetectorsGrouping.h
inc/MantidDataHandling/SaveDspacemap.h
inc/MantidDataHandling/SaveFullprofResolution.h
inc/MantidDataHandling/SaveFocusedXYE.h
inc/MantidDataHandling/SaveGSS.h
inc/MantidDataHandling/SaveISISNexus.h
Expand All @@ -271,11 +273,11 @@ set ( INC_FILES
)

set ( TEST_FILES
#Needs fixing to not rely on network. SNSLiveEventDataListenerTest.h
#LoadDAETest.h
#LoadILLTest.h
#LoadSNSNexusTest.h # TODO has no active tests in it
#LoadSassenaTest.h
#Needs fixing to not rely on network. SNSLiveEventDataListenerTest.h
AppendGeometryToSNSNexusTest.h
CompressEventsTest.h
CreateChopperModelTest.h
Expand Down Expand Up @@ -363,6 +365,7 @@ set ( TEST_FILES
SaveDaveGrpTest.h
SaveDetectorsGroupingTest.h
SaveDspacemapTest.h
SaveFullprofResolutionTest.h
SaveFocussedXYETest.h
SaveIsawDetCalTest.h
SaveMaskTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef MANTID_DATAHANDLING_SAVEFullprofRESOLUTION_H_
#define MANTID_DATAHANDLING_SAVEFullprofRESOLUTION_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidDataObjects/TableWorkspace.h"

namespace Mantid
{
namespace DataHandling
{

/** SaveFullprofResolution : TODO: DESCRIPTION
Copyright © 2012 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport SaveFullprofResolution : public API::Algorithm
{
public:
SaveFullprofResolution();
virtual ~SaveFullprofResolution();

/// Algorithm's name
virtual const std::string name() const { return "SaveFullprofResolution"; }
/// Algorithm's version
virtual int version() const { return (1); }
/// Algorithm's category for identification
virtual const std::string category() const { return "Diffraction;DataHandling\\Text"; }

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Initialisation code
void init();
///Execution code
void exec();
///Write the header information

std::string toIRFString(int bankid);

/// Parse input workspace to map of parameters
void parseTableWorkspace();

/// Map containing the name of value of each parameter required by .irf file
std::map<std::string, double> mParameters;

/// Input table workspace
DataObjects::TableWorkspace_sptr inpWS;

};


} // namespace DataHandling
} // namespace Mantid

#endif /* MANTID_DATAHANDLING_SAVEFullprofRESOLUTION_H_ */
176 changes: 176 additions & 0 deletions Code/Mantid/Framework/DataHandling/src/SaveFullprofResolution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include "MantidDataHandling/SaveFullprofResolution.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/TableRow.h"
#include <iomanip>
#include <fstream>

using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::DataObjects;
using namespace std;

namespace Mantid
{
namespace DataHandling
{


//----------------------------------------------------------------------------------------------
/** Constructor
*/
SaveFullprofResolution::SaveFullprofResolution()
{
}


//----------------------------------------------------------------------------------------------
/** Destructor
*/
SaveFullprofResolution::~SaveFullprofResolution()
{
}

/** Wiki docs
*/
void SaveFullprofResolution::initDocs()
{
this->setWikiSummary("Generate Fullprof's resolution file (.irf) from table workspace.");
this->setOptionalMessage("Genearte Fullprof's .irf file from TableWorkspace. ");
}

/** Init to define parameters
*/
void SaveFullprofResolution::init()
{
this->declareProperty(new API::WorkspaceProperty<DataObjects::TableWorkspace>("InputWorkspace", "Anonymous", Direction::InOut),
"Input TableWorkspace containing the parameters for .irf file.");

std::vector<std::string> exts;
exts.push_back(".irf");
this->declareProperty(new API::FileProperty("OutputFile", "fp.irf", API::FileProperty::Save, exts),
"Name of the output .irf file.");

this->declareProperty("Bank", 1, "Bank number of the parameters belonged to. ");
}

/** Main execution body
*/
void SaveFullprofResolution::exec()
{
// 1. Get input parameters
inpWS = getProperty("InputWorkspace");
std::string irffilename = getProperty("OutputFile");
int bankid = getProperty("Bank");

// 2. Generate the string for the file to write
std::string filestr = toIRFString(bankid);

// 3. Write to file
std::ofstream ofile;
ofile.open(irffilename);
ofile << filestr;
ofile.close();

return;
}

/** Parse the table workspace to a map of parameters (name and value)
* to look up
*/
void SaveFullprofResolution::parseTableWorkspace()
{
// 1. Clear the parameter
mParameters.clear();

// 2. Check the table workspace
std::vector<std::string> colnames = inpWS->getColumnNames();
// FIXME - The order of the column name can be flexible in future
if (colnames.size() < 2 || colnames[0].compare("Name") || colnames[1].compare("Value"))
{
std::stringstream errmsg;
errmsg << "Input parameter workspace is not supported or recoganizable. Possible reason is " << std::endl;
errmsg << "(1) too few columns. (2) first and second column are not Name and Value.";
g_log.error() << errmsg.str() << std::endl;
throw std::invalid_argument(errmsg.str());
}

// 3. Parse
size_t numrows = inpWS->rowCount();
for (size_t ir = 0; ir < numrows; ++ir)
{
const API::TableRow& row = inpWS->getRow(ir);
std::string parname;
double parvalue;
row >> parname >> parvalue;
mParameters.insert(std::make_pair(parname, parvalue));
}

return;
}


/** Convert the parameters to Fullprof resolution file string
*/
std::string SaveFullprofResolution::toIRFString(int bankid)
{
double tofmin = mParameters["tof-min"];
double tofmax = mParameters["tof-max"];
double zero = mParameters["Zero"];
double zerot = mParameters["Zerot"];
double tofstep = mParameters["step"];
double dtt1 = mParameters["Dtt1"];
double dtt1t = mParameters["Dtt1t"];
double dtt2t = mParameters["Dtt2t"];
double xcross = mParameters["Tcross"];
double width = mParameters["Width"];
double sig2 = mParameters["Sig2"];
double sig1 = mParameters["Sig1"];
double sig0 = mParameters["Sig0"];
double gam2 = mParameters["Gam2"];
double gam1 = mParameters["Gam1"];
double gam0 = mParameters["Gam0"];
double alph0 = mParameters["Alph0"];
double alph1 = mParameters["Alph1"];
double alph0t = mParameters["Alph0t"];
double alph1t = mParameters["Alph1t"];
double beta0 = mParameters["Beta0"];
double beta1 = mParameters["Beta1"];
double beta0t = mParameters["Beta0t"];
double beta1t = mParameters["Beta0"];
double profindex = mParameters["Profile"];
double twotheta = mParameters["twotheta"];

std::stringstream content;

content << " Instrumental resolution function for POWGEN/SNS J.P. Hodges 2011-09-02 ireso: 6" << std::endl;
content << "! To be used with function NPROF=" << profindex << " in FullProf (Res=6)" << std::endl;
content << "! ---------------------------------------------- Bank " << bankid << " CWL = 0.5330A" << std::endl;
content << "! Type of profile function: back-to-back exponentials * pseudo-Voigt" << std::endl;
content << "NPROF " << profindex << std::endl;
content << "! Tof-min(us) step Tof-max(us)" << std::endl;
content << "TOFRG " << setw(10) << tofmin << setw(10) << tofstep << setw(10) << tofmax << std::endl;
content << "! Zero Dtt1" << std::endl;
content << "ZD2TOF " << setw(10) << zero << setw(10) << dtt1 << std::endl;
content << "! Zerot Dtt1t Dtt2t x-cross Width" << std::endl;
content << "ZD2TOT " << setw(10) << zerot << setw(10) << dtt1t << setw(10) << dtt2t << setw(10) << xcross << setw(10) << width << std::endl;
content << "! TOF-TWOTH of the bank" << std::endl;
content << "TWOTH " << twotheta << std::endl;
content << "! Sig-2 Sig-1 Sig-0" << std::endl;
content << "SIGMA " << setw(10) << sig2 << setw(10) << sig1 << setw(10) << sig0 << std::endl;
content << "! Gam-2 Gam-1 Gam-0" << std::endl;
content << "GAMMA " << setw(10) << gam2 << setw(10) << gam1 << setw(10) << gam0 << std::endl;
content << "! alph0 beta0 alph1 beta1" << std::endl;
content << "ALFBE " << setw(10) << alph0 << setw(10) << beta0 << setw(10) << alph1 << setw(10) << beta1 << std::endl;
content << "! alph0t beta0t alph1t beta1t" << std::endl;
content << "ALFBT " << setw(10) << alph0t << setw(10) << beta0t << setw(10) << alph1t << setw(10) << beta1t << std::endl;
content << "END" << std::endl;

return content.str();

}



} // namespace DataHandling
} // namespace Mantid
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef MANTID_DATAHANDLING_SAVEFullprofRESOLUTIONTEST_H_
#define MANTID_DATAHANDLING_SAVEFullprofRESOLUTIONTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidDataHandling/SaveFullprofResolution.h"

using Mantid::DataHandling::SaveFullprofResolution;

class SaveFullprofResolutionTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static SaveFullprofResolutionTest *createSuite() { return new SaveFullprofResolutionTest(); }
static void destroySuite( SaveFullprofResolutionTest *suite ) { delete suite; }


void test_Init()
{
Mantid::DataHandling::SaveFullprofResolution alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
}


};


#endif /* MANTID_DATAHANDLING_SAVEFullprofRESOLUTIONTEST_H_ */

0 comments on commit 7efbc22

Please sign in to comment.