diff --git a/Framework/DataHandling/src/LoadGSS.cpp b/Framework/DataHandling/src/LoadGSS.cpp index 44992417e1b088092d05492d03a89171d77e0277..9f26e29dfc93e41129680a9474bd8ae1c53700ea 100644 --- a/Framework/DataHandling/src/LoadGSS.cpp +++ b/Framework/DataHandling/src/LoadGSS.cpp @@ -14,11 +14,11 @@ #include "MantidGeometry/Instrument/Component.h" #include "MantidKernel/UnitFactory.h" -#include <boost/math/special_functions/fpclassify.hpp> -#include <Poco/File.h> - +#include <boost/regex.hpp> #include <fstream> +#include <Poco/File.h> #include <sstream> +#include <string> using namespace Mantid::DataHandling; using namespace Mantid::API; @@ -29,6 +29,13 @@ namespace DataHandling { DECLARE_FILELOADER_ALGORITHM(LoadGSS) +namespace { // anonymous namespace +const boost::regex DET_POS_REG_EXP{"^#.+flight path\\s+([0-9.]+).+" + "tth\\s+([0-9.]+).+" + "DIFC\\s+([0-9.]+)"}; +const boost::regex L1_REG_EXP{"^#.+flight path\\s+([0-9.]+)\\s*m"}; +} // end of anonymous namespace + //---------------------------------------------------------------------------------------------- /** Return the confidence with with this algorithm can load the file * @param descriptor A descriptor for the file @@ -187,33 +194,51 @@ API::MatrixWorkspace_sptr LoadGSS::loadGSASFile(const std::string &filename, } } else if (key1 == "Primary") { // Primary flight path ... - std::string s1, s2; - inputLine >> s1 >> s2; - primaryflightpath = atof(s2.c_str()); // convertToDouble(s2); - g_log.information() << "L1 = " << primaryflightpath << '\n'; + boost::smatch result; + if (boost::regex_search(inputLine.str(), result, L1_REG_EXP) && + result.size() == 2) { + primaryflightpath = std::stod(std::string(result[1])); + + } else { + std::stringstream msg; + msg << "Failed to parse primary flight path from line \"" + << inputLine.str() << "\""; + g_log.warning(msg.str()); + } + + std::stringstream msg; + msg << "L1 = " << primaryflightpath; + g_log.information(msg.str()); } else if (key1 == "Total") { // Total flight path .... .... including total flying path, difc and // 2theta of 1 bank - std::string s1, s2, s3, s4, s5, s6; - inputLine >> s1 >> s2 >> s3 >> s4 >> s5 >> s6; -#if 0 - double totalpath = convertToDouble(s2); - double tth = convertToDouble(s4); - double difc = convertToDouble(s6); -#else - double totalpath = atof(s2.c_str()); - double tth = atof(s4.c_str()); - double difc = atof(s6.c_str()); -#endif + + double totalpath(0.f); + double tth(0.f); + double difc(0.f); + + boost::smatch result; + if (boost::regex_search(inputLine.str(), result, DET_POS_REG_EXP) && + result.size() == 4) { + totalpath = std::stod(std::string(result[1])); + tth = std::stod(std::string(result[2])); + difc = std::stod(std::string(result[3])); + } else { + std::stringstream msg; + msg << "Failed to parse position from line \"" << inputLine.str() + << "\""; + g_log.warning(msg.str()); + } totalflightpaths.push_back(totalpath); twothetas.push_back(tth); difcs.push_back(difc); - g_log.information() << "Bank " << difcs.size() - 1 - << ": Total flight path = " << totalpath - << " 2Theta = " << tth << " DIFC = " << difc - << "\n"; + std::stringstream msg; + msg << "Bank " << difcs.size() - 1 + << ": Total flight path = " << totalpath << " 2Theta = " << tth + << " DIFC = " << difc; + g_log.information(msg.str()); } // if keys.... } // ENDIF for Line with # @@ -443,7 +468,7 @@ double LoadGSS::convertToDouble(std::string inputstring) { } } - double rd = atof(temps.c_str()); + double rd = std::stod(temps); return rd; } diff --git a/Framework/DataHandling/test/LoadGSSTest.h b/Framework/DataHandling/test/LoadGSSTest.h index a20a770b2f84ffa87b6eb38a80457e95b407f1c5..4ff6c579944c343e0662153e48f99ba3596fea70 100644 --- a/Framework/DataHandling/test/LoadGSSTest.h +++ b/Framework/DataHandling/test/LoadGSSTest.h @@ -7,10 +7,12 @@ #include "MantidAPI/Axis.h" #include "MantidAPI/MatrixWorkspace.h" #include "MantidTestHelpers/ScopedFileHelper.h" +#include "MantidGeometry/Instrument.h" using namespace Mantid; using Mantid::DataHandling::LoadGSS; using ScopedFileHelper::ScopedFile; +using Mantid::Kernel::V3D; class LoadGSSTest : public CxxTest::TestSuite { public: @@ -52,7 +54,16 @@ public: " 115206.06310 1234567.00000003 0.00000000\n" " 115209.92877 12345678.00000004 0.00000000\n" " 115213.79731123456789.00000005 0.00000000\n" - " 115217.66873234567890.00000006 0.00000000"; + " 115217.66873234567890.00000006 0.00000000\n" + "\n" + "# Total flight path 42.060 m, tth 154.257 deg, DIFC 10398.8\n" + "# Data for spectrum :1\n" + "BANK 2 4399 4399 RALF 166409 124 166409 0.00074 FXYE\n" + " 115202.20029 1.00000000 0.00000000\n" + " 115206.06310 1.00000000 0.00000000\n" + " 115209.92877 2.00000000 0.00000000\n" + " 115213.79731 3.00000000 0.00000000\n" + " 115217.66873 5.00000000 0.00000000\n"; ScopedFile file(gss, "gss_large_x.txt"); API::IAlgorithm_sptr loader = createAlgorithm(); loader->setPropertyValue("Filename", file.getFileName()); @@ -69,6 +80,21 @@ public: dx = x2 - x1; y = ws->readY(0)[3] * dx; TS_ASSERT_DELTA(y, 123456789.00000005, 1e-10); + + const auto source = ws->getInstrument()->getSource(); + const auto sample = ws->getInstrument()->getSample(); + TS_ASSERT_DELTA(sample->getDistance(*source), 40., 1e-4); + + const auto det0 = ws->getDetector(0); + TS_ASSERT_DELTA(det0->getDistance(*sample), 2.2222, 1e-4); + TS_ASSERT_DELTA(det0->getTwoTheta(V3D(0.0, 0.0, 0.0), V3D(0.0, 0.0, 1.0)) * + 180. / M_PI, + 58.308, 1e-4); + const auto det1 = ws->getDetector(1); + TS_ASSERT_DELTA(det1->getDistance(*sample), 2.060, 1e-4); + TS_ASSERT_DELTA(det1->getTwoTheta(V3D(0.0, 0.0, 0.0), V3D(0.0, 0.0, 1.0)) * + 180. / M_PI, + 154.257, 1e-4); } void test_load_gss_ExtendedHeader_gsa() {