Skip to content

Commit

Permalink
Checkpointing work. Refs #6968.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Aug 14, 2013
1 parent 8c581c2 commit 089b049
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class DLLExport SaveGSASInstrumentFile : public API::Algorithm
ChopperConfiguration_sptr setupNOMConstants(int intfrequency);

/// Parse profile table workspace to a map
void parseProfileTableWorkspace(DataObjects::TableWorkspace_sptr ws, std::map<unsigned int, std::map<std::string, double> > profilemap);
void parseProfileTableWorkspace(DataObjects::TableWorkspace_sptr ws,
std::map<unsigned int, std::map<std::string, double> >& profilemap);

/// Convert to GSAS instrument file
void convertToGSAS(std::vector<unsigned int> banks, std::string gsasinstrfilename);
Expand Down Expand Up @@ -135,6 +136,9 @@ class DLLExport SaveGSASInstrumentFile : public API::Algorithm
/// Get parameter value from class storage
double getProfileParameterValue(unsigned int bankid, std::string paramname);

/// Load fullprof resolution file.
double loadFullprofResolutionFile(std::string irffilename);

/// Input workspace
DataObjects::TableWorkspace_sptr m_inpWS;

Expand All @@ -154,7 +158,7 @@ class DLLExport SaveGSASInstrumentFile : public API::Algorithm
std::string m_sample;

/// Banks IDs to process
std::vector<unsigned int> m_bankIDsOutput;
std::vector<unsigned int> m_vecBankID2File;

/// Output file name
std::string m_gsasFileName;
Expand Down
121 changes: 92 additions & 29 deletions Code/Mantid/Framework/Algorithms/src/SaveGSASInstrumentFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ There can be several types of Fullprof files as the input file
#include "MantidKernel/ArrayProperty.h"
#include "MantidAPI/TableRow.h"

#include <stdio.h>

using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
Expand Down Expand Up @@ -153,7 +155,7 @@ namespace Algorithms
else
{
stringstream errss;
errss << "Bank ID : " << bankid << ", Parameter : " << paramname;
errss << "ChopperConfiguration unable to locate: Bank ID = " << bankid << ", Parameter = " << paramname;
throw runtime_error(errss.str());
}

Expand Down Expand Up @@ -272,10 +274,15 @@ namespace Algorithms
*/
void SaveGSASInstrumentFile::init()
{
declareProperty(new WorkspaceProperty<TableWorkspace>("InputWorkspace", "", Direction::Input),
declareProperty(new WorkspaceProperty<TableWorkspace>("InputWorkspace", "", Direction::Input, PropertyMode::Optional),
"Name of the table workspace containing the parameters. Usually it is generated by "
"LoadFullprofResolution.");

vector<string> infileexts;
infileexts.push_back(".irf");
auto infileprop = new FileProperty("InputFileName", "", FileProperty::OptionalLoad, infileexts);
declareProperty(infileprop, "Name of the input Fullprof resolution file (.irf).");

vector<string> exts;
exts.push_back(".iparam");
exts.push_back(".prm");
Expand Down Expand Up @@ -313,36 +320,42 @@ namespace Algorithms
*/
void SaveGSASInstrumentFile::processProperties()
{
// Input workspace
bool loadirf = false;
m_inpWS = getProperty("InputWorkspace");
if (!m_inpWS)
loadirf = true;

if (loadirf)
{
// Load .irf file to m_inpWS
string irffilename = getProperty("InputFileName");
loadFullprofResolutionFile(irffilename);

if (!m_inpWS)
{
stringstream errss;
errss << "Neither input table workspace (" << getPropertyValue("InputWorkspace") << ") nor "
<< "input .irf file " << getPropertyValue("InputFileName") << " is valid. ";
g_log.error(errss.str());
throw runtime_error(errss.str());
}
}

// Instrument information
m_instrument = getPropertyValue("Instrument");
m_id_line = getPropertyValue("IDLine"); // Standard Run LB4844 Vanadium: 4866 J.P. Hodges 2011-09-01
m_sample = getPropertyValue("Sample"); // titleline = "LaB6 NIST RT 4844[V=4866] 60Hz CW=.533"

m_gsasFileName = getPropertyValue("OutputFileName");
m_bankIDsOutput = getProperty("BankIDs");
m_vecBankID2File = getProperty("BankIDs");

m_L1 = getProperty("L1");
m_2theta = getProperty("TwoTheta");
m_L2 = getProperty("L2");
string freqtempstr = getProperty("ChopperFrequency");
m_frequency = atoi(freqtempstr.c_str());

#if 0
// Process input
bool useirf = false;
string irffilename, pcrfilename;
if (endswith(inputfilename, ".irf"))
{
useirf = true;
irffilename = inputfilename;
}
else
{
pcrfilename = inputfilename;
}
#endif

// Set default value for L1
if (m_L1 == EMPTY_DBL())
{
Expand Down Expand Up @@ -410,7 +423,7 @@ namespace Algorithms

makeParameterConsistent();

convertToGSAS(m_bankIDsOutput, m_gsasFileName);
convertToGSAS(m_vecBankID2File, m_gsasFileName);

return;
}
Expand Down Expand Up @@ -450,14 +463,15 @@ namespace Algorithms
parseProfileTableWorkspace(m_inpWS, bankprofileparammap);

// Make input parameters consistent
for (size_t i = 0; i < m_bankIDsOutput.size(); ++i)
for (size_t i = 0; i < m_vecBankID2File.size(); ++i)
{
unsigned int bankid = m_bankIDsOutput[i];
unsigned int bankid = m_vecBankID2File[i];
map<unsigned int, map<string, double> >::iterator biter = bankprofileparammap.find(bankid);
if (biter == bankprofileparammap.end())
{
stringstream errss;
errss << "Bank " << bankid << " does not exist in input workspace. ";
errss << "Bank " << bankid << " does not exist in input workspace. "
<< "Input file/tableworkspace constains " << bankprofileparammap.size() << " banks. ";
g_log.error(errss.str());
throw runtime_error(errss.str());
}
Expand Down Expand Up @@ -494,12 +508,15 @@ namespace Algorithms
//----------------------------------------------------------------------------------------------
/** Parse profile table workspace to a map (the new ...
*/
void SaveGSASInstrumentFile::parseProfileTableWorkspace(TableWorkspace_sptr ws, map<unsigned int, map<string, double> > profilemap)
void SaveGSASInstrumentFile::parseProfileTableWorkspace(TableWorkspace_sptr ws,
map<unsigned int, map<string, double> >& profilemap)
{
size_t numbanks = ws->columnCount();
size_t numparams = ws->rowCount()-1;
g_log.information("[DBx908] Start to parse TableWorkspace.");

size_t numbanks = ws->columnCount()-1;
size_t numparams = ws->rowCount();
vector<map<string, double> > vec_maptemp(numbanks);
vector<unsigned int> vecbankindex;
vector<unsigned int> vecbankindex(numbanks);

// Check
vector<string> colnames = ws->getColumnNames();
Expand All @@ -512,7 +529,7 @@ namespace Algorithms
TableRow tmprow = ws->getRow(irow);
string parname;
tmprow >> parname;
if (parname.compare("Bank"))
if (parname.compare("BANK"))
{
for (size_t icol = 0; icol < numbanks; ++icol)
{
Expand All @@ -525,13 +542,20 @@ namespace Algorithms
{
for (size_t icol = 0; icol < numbanks; ++icol)
{
unsigned int tmpint;
double tmpint;
tmprow >> tmpint;
vecbankindex[icol] = tmpint;
vecbankindex[icol] = static_cast<unsigned int >(tmpint);
}
}
}

// debug output
stringstream db1ss;
db1ss << "[DBx912] Number of banks in profile table = " << vecbankindex.size() << " containing bank ";
for (size_t i = 0; i < vecbankindex.size(); ++i)
db1ss << vecbankindex[i] << ", ";
g_log.information(db1ss.str());

// Construct output
profilemap.clear();

Expand Down Expand Up @@ -681,6 +705,10 @@ namespace Algorithms
*/
void SaveGSASInstrumentFile::buildGSASTabulatedProfile(unsigned int bankid)
{
// FIXME - The profile parameter values should not get from m_configuration.
// but from the profile map!
// THIS IS VERY WRONG!

// Init data structure
vector<double> gdsp(90, 0.); // TOF_thermal(d_k)
vector<double> gtof(90, 0.); // TOF_thermal(d_k) - TOF(d_k)
Expand Down Expand Up @@ -790,6 +818,10 @@ namespace Algorithms
}


// FIXME - Split writePRM to 2 parts. It might be better to split to 2 methods
// (1) Header: write the first 2 lines
// (2) Loop on each bank: write the parameters for each bank

//----------------------------------------------------------------------------------------------
/** Write out .prm/.iparm file
* @bankid : integer, bank ID
Expand All @@ -803,6 +835,7 @@ namespace Algorithms
if (m_mndsp.size() == 0 || m_mxtofs.size() == 0)
throw runtime_error("Implement the setup on m_mndsp and m_mxtof ASAP.");

// FIXME - These parameter should be obtained from profile parameter map from input table workspace
double zero = m_configuration->getParameter(bankid, "Zero");
double dtt1 = m_configuration->getParameter(bankid, "Dtt1");
double alph0 = m_configuration->getParameter(bankid, "Alph0");
Expand Down Expand Up @@ -845,8 +878,25 @@ namespace Algorithms
double gam1 = getProfileParameterValue(bankid, "Gam1");
double gam2 = getProfileParameterValue(bankid, "Gam2");

// Just for test!
// Start (Header)
FILE * pFile;
pFile = fopen (prmfilename.c_str(),"w");
fprintf(pFile, "INS %2d ICONS%10.3f%10.3f%10.3f%10.3f%5d%10.3f\n", bankid, instC*1.00009, 0.0, zero,0.0, 0, 0.0);
fprintf(pFile, "INS %2dBNKPAR%10.3f%10.3f%10.3f%10.3f%10.3f%5d%5d\n", bankid, m_L2, twotheta, 0., 0., 0.2, 1, 1);
fclose (pFile);
// Append
pFile = fopen(prmfilename.c_str(), "a");
fprintf(pFile, "INS %2dBAKGD 1 4 Y 0 Y\n", bankid);
fprintf(pFile, "INS %2dI HEAD %s\n", bankid, titleline.c_str());
fclose(pFile);

// Print the value

printf("INS %2d ICONS%10.3f%10.3f%10.3f%10.3f%5d%10.3f\n", bankid, instC*1.00009, 0.0, zero,0.0, 0, 0.0);
printf("INS %2dBNKPAR%10.3f%10.3f%10.3f%10.3f%10.3f%5d%5d\n", bankid, m_L2, twotheta, 0., 0., 0.2, 1, 1);

// Bank 1...
printf("INS %2dBAKGD 1 4 Y 0 Y\n", bankid);
printf("INS %2dI HEAD %s\n", bankid, titleline.c_str());
printf("INS %2dI ITYP%5d%10.4f%10.4f%10i\n", bankid, 0, m_mndsp[bankid]*0.001*instC, m_mxtofs[bankid], randint);
Expand Down Expand Up @@ -1013,6 +1063,19 @@ namespace Algorithms
return value;
}

//----------------------------------------------------------------------------------------------
/** Load fullprof resolution file.
* - call LoadFullprofResolution
* - set output table workspace to m_inpWS
* @param irffilename
*/
double SaveGSASInstrumentFile::loadFullprofResolutionFile(std::string irffilename)
{
// FIXME - Implement ASAP
throw runtime_error("ASAP");
}


} // namespace Algorithms
} // namespace Mantid

Expand Down
67 changes: 51 additions & 16 deletions Code/Mantid/Framework/Algorithms/test/SaveGSASInstrumentFileTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

#include <cxxtest/TestSuite.h>

//#include "MantidAlgorithms/SaveGSASInstrumentFile.h"
#include "SaveGSASInstrumentFile.h"
#include "MantidAlgorithms/SaveGSASInstrumentFile.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataHandling/LoadNexusProcessed.h"
#include "MantidAPI/TableRow.h"

#include <fstream>

using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::DataHandling;

using namespace std;

Expand Down Expand Up @@ -62,7 +62,7 @@ class SaveGSASInstrumentFileTest : public CxxTest::TestSuite

}

void test_SaveGSSInstrumentFile_MultiBank()
void Xtest_SaveGSSInstrumentFile_MultiBank()
{
// Load a (local) table workspace
loadProfileTable("PG3ProfileTable");
Expand Down Expand Up @@ -102,26 +102,61 @@ class SaveGSASInstrumentFileTest : public CxxTest::TestSuite
// Load table workspace containing instrument parameters
void loadProfileTable(string wsname)
{
string tablewsname("pg3_bank1_params.nxs");

LoadNexusProcessed loader;
loader.initialize();

loader.setProperty("Filename", tablewsname);
loader.setProperty("OutputWorkspace", wsname);
TableWorkspace_sptr tablews(new TableWorkspace);
tablews->addColumn("str", "Name");
tablews->addColumn("double", "Value_4");

vector<string> parnames;
vector<double> parvalues;

parnames.push_back("BANK"); parvalues.push_back(4.);
parnames.push_back("Alph0"); parvalues.push_back(0.5 );
parnames.push_back("Alph0t"); parvalues.push_back(65.14 );
parnames.push_back("Alph1"); parvalues.push_back(8.15 );
parnames.push_back("Alph1t"); parvalues.push_back(0 );
parnames.push_back("Beta0"); parvalues.push_back(3.201 );
parnames.push_back("Beta0t"); parvalues.push_back(78.412 );
parnames.push_back("Beta1"); parvalues.push_back(7.674 );
parnames.push_back("Beta1t"); parvalues.push_back(0 );
parnames.push_back("Dtt1"); parvalues.push_back(22780.6);
parnames.push_back("Dtt1t"); parvalues.push_back(22790.1);
parnames.push_back("Dtt2"); parvalues.push_back(0 );
parnames.push_back("Dtt2t"); parvalues.push_back(0.3 );
parnames.push_back("Gam0"); parvalues.push_back(0 );
parnames.push_back("Gam1"); parvalues.push_back(0 );
parnames.push_back("Gam2"); parvalues.push_back(0 );
parnames.push_back("Sig0"); parvalues.push_back(0 );
parnames.push_back("Sig1"); parvalues.push_back(3.16228);
parnames.push_back("Sig2"); parvalues.push_back(20.0823);
parnames.push_back("Tcross"); parvalues.push_back(0.356 );
parnames.push_back("Width"); parvalues.push_back(1.2141 );
parnames.push_back("Zero"); parvalues.push_back(0 );
parnames.push_back("Zerot"); parvalues.push_back(-70.6 );
parnames.push_back("step"); parvalues.push_back(5 );
parnames.push_back("tof-max"); parvalues.push_back(46760 );
parnames.push_back("tof-min"); parvalues.push_back(2278.06);
parnames.push_back("twotheta"); parvalues.push_back(90.807 );

for (size_t i = 0; i < parnames.size(); ++i)
{
TableRow row = tablews->appendRow();
row << parnames[i] << parvalues[i];
}

loader.execute();
AnalysisDataService::Instance().addOrReplace(wsname, tablews);

return;
}

// Compare 2 files
bool compare2Files(std::string filename1, std::string filename2)
{
ifstream file1, file2;
ifstream file1(filename1.c_str(), std::ifstream::in);
ifstream file2(filename2.c_str(), std::ifstream::in);


file1.open( filename1.c_str(), ios::binary ); //c_str() returns C-style string pointer
file2.open( filename2.c_str(), ios::binary );
// file1.open( filename1.c_str(), ios::binary ); //c_str() returns C-style string pointer
// file2.open( filename2.c_str(), ios::binary );

if (!file1)
{
Expand Down

0 comments on commit 089b049

Please sign in to comment.