Skip to content

Commit

Permalink
UpdateInstrumentFromFile supports .dat files natively.
Browse files Browse the repository at this point in the history
The header can still be overridden using the AsciiHeader property.
Refs #8057
  • Loading branch information
martyngigg committed Oct 4, 2013
1 parent 061b18f commit 1101288
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace Mantid
exts.push_back(".s*");
declareProperty(new FileProperty("Filename", "", FileProperty::Load, exts),
"The filename of the input file.\n"
"Currently supports RAW, ISIS NeXus & multi-column (at least 2) ascii file"
"Currently supports RAW, ISIS NeXus, DAT & multi-column (at least 2) ascii files"
);
declareProperty("MoveMonitors", (!m_ignoreMonitors),
"If true the positions of any detectors marked as monitors "
Expand All @@ -109,8 +109,7 @@ namespace Mantid
"Keywords=spectrum,ID,R,theta,phi. A dash means skip column. Keywords are recognised"
"as identifying components to move to new positions. Any other names in the list"
"are added as instrument parameters.");


declareProperty("SkipFirstNLines", 0, "If the file is ASCII, then skip this number of lines at the start of the file");
}

/** Executes the algorithm. Reading in the file and creating and populating
Expand Down Expand Up @@ -148,6 +147,14 @@ namespace Mantid

if(FileDescriptor::isAscii(filename))
{
// If no header specified & the extension is .dat or .sca, then assume ISIS
// DAT file structure
if(getPropertyValue("AsciiHeader").empty() &&
(boost::iends_with(filename,".dat") || boost::iends_with(filename,".sca")))
{
this->setPropertyValue("AsciiHeader", "ID,-,R,-,theta,phi,-,-,-,-,-,-,-,-,-,-,-,-,-");
this->setProperty("SkipFirstNLines",2);
}
updateFromAscii(filename);
return;
}
Expand Down Expand Up @@ -263,11 +270,19 @@ namespace Mantid
const spec2index_map specToIndex(m_workspace->getSpectrumToWorkspaceIndexMap());

std::ifstream datfile(filename.c_str(), std::ios_base::in);

const int skipNLines = getProperty("SkipFirstNLines");
std::string line;
int lineCount(0);
while(lineCount < skipNLines)
{
std::getline(datfile,line);
++lineCount;
}

std::vector<double> colValues(header.colCount - 1, 0.0);
while(std::getline(datfile,line))
{
boost::trim(line);
std::istringstream is(line);
// Column 0 should be ID/spectrum number
int32_t detOrSpec(-1000);
Expand Down Expand Up @@ -418,10 +433,6 @@ namespace Mantid
try
{
Geometry::IDetector_const_sptr det = inst->getDetector(detID[i]);
if( m_ignoreMonitors && det->isMonitor() )
{
continue;
}
setDetectorPosition(det, l2[i], theta[i], phi[i]);
}
catch (Kernel::Exception::NotFoundError&)
Expand All @@ -442,6 +453,8 @@ namespace Mantid
void UpdateInstrumentFromFile::setDetectorPosition(const Geometry::IDetector_const_sptr & det, const float l2,
const float theta, const float phi)
{
if( m_ignoreMonitors && det->isMonitor() ) return;

Geometry::ParameterMap & pmap = m_workspace->instrumentParameters();
Kernel::V3D pos;
if (!m_ignorePhi)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class UpdateInstrumentFromFileTest : public CxxTest::TestSuite
" 3 130.4653 -0.4157 11.0050 0.6708\n"
" 4 131.9319 -0.5338 11.0050 0.6545";

const std::string filename = "__detpars.dat";
const std::string filename = "__detpars.par";
ScopedFileHelper::ScopedFile datfile(contents, filename);

doTestWithAsciiFile(datfile.getFileName(), header);
Expand All @@ -79,11 +79,59 @@ class UpdateInstrumentFromFileTest : public CxxTest::TestSuite
" 3 130.4653 -0.4157 11.0050 0.6708\n"
" 4 131.9319 -0.5338 11.0050 0.6545";

const std::string filename = "__detpars_with_header.par";
ScopedFileHelper::ScopedFile parfile(contents, filename);

doTestWithAsciiFile(parfile.getFileName(), colNames);

}

void test_DAT_Extension_Without_AsciiHeader_Assumes_Detector_Calibration_File()
{
const std::string contents =
"DETECTOR.DAT generated by CREATE_DETECTOR_FILE\n"
"163848 14\n"
"det no. offset l2 code theta phi w_x w_y w_z f_x f_y f_z a_x a_y a_z det_1 det_2 det_3 det4\n"
"3 0.0 0.6708 5 130.4653 179.0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"
"4 5.3 0.6545 5 131.9319 179.0 0 0 0 0 0 0 0 0 0 0 0 0 0";

const std::string filename = "__detpars_with_header.dat";
ScopedFileHelper::ScopedFile datfile(contents, filename);

doTestWithAsciiFile(datfile.getFileName(), colNames);
loadTestInstrument();
// No header
TS_ASSERT_THROWS_NOTHING(runUpdateInstrument(datfile.getFileName()));

using namespace Mantid::API;
// Instrument check
MatrixWorkspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(wsName));

auto det4 = output->getDetector(3); // spectrum 4 = ws index 3
TS_ASSERT(det4);
double r(-1.0), theta(-1.0), phi(-1.0);
det4->getPos().getSpherical(r, theta, phi);
TS_ASSERT_DELTA(0.6545, r, 1e-4);
TS_ASSERT_DELTA(131.9319, theta, 1e-4);
TS_ASSERT_DELTA(179.0,phi,1e-4);

AnalysisDataService::Instance().remove(wsName);
}

void test_DAT_Extension_Without_Header_Assumes_Detector_Calibration_File_And_Fails_If_Number_Of_Columns_Incorrect()
{
const std::string colNames = "spectrum,theta,t0,-,R";
const std::string contents =
"plik det t0 l0 l1\n"
" 3 130.4653 -0.4157 11.0050 0.6708\n"
" 4 131.9319 -0.5338 11.0050 0.6545";

const std::string filename = "__detpars_with_header.dat";
ScopedFileHelper::ScopedFile datfile(contents, filename);

loadTestInstrument();
// No header
TS_ASSERT_THROWS(runUpdateInstrument(datfile.getFileName()), std::runtime_error);
}

private:
Expand Down Expand Up @@ -154,7 +202,7 @@ class UpdateInstrumentFromFileTest : public CxxTest::TestSuite
spec->addDetectorID(static_cast<Mantid::detid_t>(i+1));
}

TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().add(wsName, ws));
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().addOrReplace(wsName, ws));

}

Expand Down

0 comments on commit 1101288

Please sign in to comment.