diff --git a/Framework/DataHandling/inc/MantidDataHandling/LoadSpec.h b/Framework/DataHandling/inc/MantidDataHandling/LoadSpec.h index 12de4462f910..9a182c52143a 100644 --- a/Framework/DataHandling/inc/MantidDataHandling/LoadSpec.h +++ b/Framework/DataHandling/inc/MantidDataHandling/LoadSpec.h @@ -9,9 +9,10 @@ //---------------------------------------------------------------------- // Includes //---------------------------------------------------------------------- -#include "MantidAPI/Algorithm.h" +#include "MantidAPI/IFileLoader.h" #include "MantidDataHandling/DllConfig.h" #include "MantidHistogramData/Histogram.h" +#include "MantidKernel/FileDescriptor.h" namespace Mantid { namespace DataHandling { @@ -31,7 +32,7 @@ data. @author Roman Tolchenov, Tessella plc @date 3/07/09 */ -class MANTID_DATAHANDLING_DLL LoadSpec final : public API::Algorithm { +class MANTID_DATAHANDLING_DLL LoadSpec : public API::IFileLoader { public: LoadSpec(); const std::string name() const override { return "LoadSpec"; } @@ -44,6 +45,9 @@ class MANTID_DATAHANDLING_DLL LoadSpec final : public API::Algorithm { int version() const override { return 1; } const std::string category() const override { return "DataHandling\\Text"; } + /// Returns a confidence value that this algorithm can load a file + int confidence(Kernel::FileDescriptor &descriptor) const override; + private: void init() override; void exec() override; diff --git a/Framework/DataHandling/src/LoadSpec.cpp b/Framework/DataHandling/src/LoadSpec.cpp index 3c9dcb7af98b..8f51b85b7879 100644 --- a/Framework/DataHandling/src/LoadSpec.cpp +++ b/Framework/DataHandling/src/LoadSpec.cpp @@ -8,8 +8,10 @@ // Includes //---------------------------------------------------------------------- #include "MantidDataHandling/LoadSpec.h" +#include "MantidAPI/Algorithm.h" #include "MantidAPI/Axis.h" #include "MantidAPI/FileProperty.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidAPI/WorkspaceFactory.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidKernel/ListValidator.h" @@ -22,7 +24,7 @@ namespace Mantid::DataHandling { // Register the algorithm into the algorithm factory -DECLARE_ALGORITHM(LoadSpec) +DECLARE_FILELOADER_ALGORITHM(LoadSpec) using namespace Kernel; using namespace API; @@ -30,6 +32,58 @@ using namespace API; /// Empty constructor LoadSpec::LoadSpec() = default; +/** + * Return the confidence with with this algorithm can load the file + * @param descriptor A descriptor for the file + * @returns An integer specifying the confidence level. 0 indicates it will not + * be used + */ +int LoadSpec::confidence(Kernel::FileDescriptor &descriptor) const { + if (!descriptor.isAscii()) + return 0; + + auto &file = descriptor.data(); + + int confidence(0); + size_t axiscols(0), datacols(0); + std::string str; + using tokenizer = Mantid::Kernel::StringTokenizer; + const std::string sep = " "; + bool snsspec(false); + + while (std::getline(file, str)) { + // File is opened in binary mode so getline will leave a \r at the end of an + // empty line if it exists + if (str.empty() || str == "\r") + continue; + + try { + // if it's comment line + tokenizer tok(str, sep, Mantid::Kernel::StringTokenizer::TOK_IGNORE_EMPTY); + if (str.at(0) == '#') { + if (str.at(1) == 'L') { + axiscols = tok.count(); + // if the file contains a comment line starting with "#L" followed + // by three columns this could be loadsnsspec file + if (axiscols > 2) { + snsspec = true; + } + } + } else { + // check first data line is a 3 column line + datacols = tok.count(); + break; + } + } catch (std::out_of_range &) { + } + } + if (snsspec && datacols == 3) // three column data + { + confidence = 80; + } + return confidence; +} + /// Initialisation method. void LoadSpec::init() { const std::vector exts{".dat", ".txt"}; diff --git a/Testing/SystemTests/tests/framework/LoadLotsOfFiles.py b/Testing/SystemTests/tests/framework/LoadLotsOfFiles.py index ec5c88625ff0..04cf53816b3f 100644 --- a/Testing/SystemTests/tests/framework/LoadLotsOfFiles.py +++ b/Testing/SystemTests/tests/framework/LoadLotsOfFiles.py @@ -41,7 +41,6 @@ "batch_input.csv", "mar11015.msk", "LET_hard.msk", # It seems loade does not understand it? - "LoadSNSspec.txt", # LoadSpec is not registered as a file loader "MASK.094AA", "MASKSANS2D_094i_RKH.txt", "MASKSANS2D.091A", diff --git a/docs/source/release/v6.13.0/Framework/Data_Handling/New_features/39243.rst b/docs/source/release/v6.13.0/Framework/Data_Handling/New_features/39243.rst new file mode 100644 index 000000000000..4988378e9abe --- /dev/null +++ b/docs/source/release/v6.13.0/Framework/Data_Handling/New_features/39243.rst @@ -0,0 +1 @@ +- :ref:`LoadSpec ` has been updated to be declared as a file loader diff --git a/docs/source/release/v6.13.0/framework.rst b/docs/source/release/v6.13.0/framework.rst index 5ef117a9a79d..0a5259ba5026 100644 --- a/docs/source/release/v6.13.0/framework.rst +++ b/docs/source/release/v6.13.0/framework.rst @@ -44,6 +44,22 @@ Removed .. amalgamate:: Framework/Fit_Functions/Removed +Data Handling +------------- + +New features +############ +.. amalgamate:: Framework/Data_Handling/New_features + +Deprecated +############ +.. amalgamate:: Framework/Data_Handling/Deprecated + +Removed +############ +.. amalgamate:: Framework/Data_Handling/Removed + + Data Objects ------------