Skip to content

Commit

Permalink
Create skeleton algorithm re #5669
Browse files Browse the repository at this point in the history
This algorithm modifies the header as planned and copies all other lines.

Signed-off-by: Karl Palmen <karl.palmen@stfc.ac.uk>
  • Loading branch information
KarlPalmen committed Aug 23, 2012
1 parent 9af0254 commit 45868de
Show file tree
Hide file tree
Showing 3 changed files with 200 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 @@ -93,6 +93,7 @@ set ( SRC_FILES
src/MaskDetectors.cpp
src/MaskDetectorsInShape.cpp
src/Merge2WorkspaceLogs.cpp
src/ModifyDetectorDotDatFile.cpp
src/MonitorLiveData.cpp
src/MoveInstrumentComponent.cpp
src/NexusTester.cpp
Expand Down Expand Up @@ -223,6 +224,7 @@ set ( INC_FILES
inc/MantidDataHandling/MaskDetectors.h
inc/MantidDataHandling/MaskDetectorsInShape.h
inc/MantidDataHandling/Merge2WorkspaceLogs.h
inc/MantidDataHandling/ModifyDetectorDotDatFile.h
inc/MantidDataHandling/MonitorLiveData.h
inc/MantidDataHandling/MoveInstrumentComponent.h
inc/MantidDataHandling/NexusTester.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef MANTID_DATAHANDLING_MODIFYDETECTORDOTDATFILE_H_
#define MANTID_DATAHANDLING_MODIFYDETECTORDOTDATFILE_H_

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

namespace Mantid
{
namespace DataHandling
{

/** Modifies an ISIS detector dot data file, so that the detector positions are as in the given workspace.
@author Karl Palmen
@date 2012-08-23
Copyright &copy; 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 ModifyDetectorDotDatFile : public API::Algorithm
{
public:
ModifyDetectorDotDatFile();
~ModifyDetectorDotDatFile();

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

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Initialise the properties
void init();
/// Run the algorithm
void exec();


};


} // namespace DataHandling
} // namespace Mantid

#endif /* MANTID_DATAHANDLING_MODIFYDETECTORDOTDATFILE_H_ */
133 changes: 133 additions & 0 deletions Code/Mantid/Framework/DataHandling/src/ModifyDetectorDotDatFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*WIKI*
Modifies an ISIS detector dot data file, so that the detector positions are as in the given workspace.
*WIKI*/
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataHandling/ModifyDetectorDotDatFile.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/System.h"
#include <fstream>
#include "MantidAPI/Workspace.h"
#include "MantidAPI/ExperimentInfo.h"

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

namespace Mantid
{
namespace DataHandling
{

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



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

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


//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void ModifyDetectorDotDatFile::initDocs()
{
this->setWikiSummary("Modifies an ISIS detector dot data file, so that the detector positions are as in the given workspace");
this->setOptionalMessage("Modifies an ISIS detector dot data file, so that the detector positions are as in the given workspace");
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ModifyDetectorDotDatFile::init()
{
declareProperty(new WorkspaceProperty<Workspace>("InputWorkspace","",Direction::Input), "Workspace with detectors in the positions to be put into the detector dot dat file");

std::vector<std::string> exts;
exts.push_back(".dat");
exts.push_back(".txt");

declareProperty(new FileProperty("InputFilename", "", FileProperty::Load, exts),
"Path to a detector dot dat file.");

declareProperty(new FileProperty("OutputFilename", "", FileProperty::Save, exts),
"Path to the modified detector dot dat file.");

}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ModifyDetectorDotDatFile::exec()
{
std::string inputFilename = getPropertyValue("InputFilename");
std::string outputFilename = getPropertyValue("OutputFilename");

Workspace_sptr ws1 = getProperty("InputWorkspace");
ExperimentInfo_sptr ws = boost::dynamic_pointer_cast<ExperimentInfo>(ws1);

// Check instrument
Instrument_const_sptr inst = ws->getInstrument();
if (!inst) throw std::runtime_error("No instrument in the Workspace. Cannot modify detector dot dat file");

// Open files
std::ifstream in;
in.open( inputFilename.c_str());
if(!in) {
throw Exception::FileError("Can't open input file", inputFilename);
}
std::ofstream out;
out.open( outputFilename.c_str());
if(!out) {
in.close();
throw Exception::FileError("Can't open output file", outputFilename);
}

// Read first line, modify it and put into output file
std::string str;
getline( in, str );
out << str << " and modified by MANTID algorithm ModifyDetectorDotDatFile \n";

// Read second line to check number of detectors and columns
int detectorCount, numColumns;
getline( in, str );
std::istringstream header2(str);
header2 >> detectorCount >> numColumns;
// check that we have at least 1 detector and five columns
if( detectorCount < 1 || numColumns < 5) {
throw Exception::FileError("Incompatible file format found when reading line 2 in the input file", inputFilename);
}
out << str << "\n";

// Read input file line by line, modify line as necessary and put line into output file
while( getline( in, str ) ){
// We do not yet modify
out << str << "\n";
}

out.close();
in.close();

}



} // namespace Mantid
} // namespace DataHandling

0 comments on commit 45868de

Please sign in to comment.