From f20d2ca8c72e8252d5d44a154d8d9021c56c241d Mon Sep 17 00:00:00 2001
From: "Parker, Peter G" <parkerpg@ornl.gov>
Date: Thu, 18 Oct 2018 14:16:33 -0400
Subject: [PATCH] Refs #22608 - Support HFIR instruments when searching
 archive.

---
 Framework/API/test/FileFinderTest.h           |  2 +-
 Framework/DataHandling/CMakeLists.txt         |  6 +-
 .../{SNSDataArchive.h => ORNLDataArchive.h}   |  8 +--
 ...SNSDataArchive.cpp => ORNLDataArchive.cpp} | 32 +++++++---
 ...ataArchiveTest.h => ORNLDataArchiveTest.h} | 58 ++++++++++++++-----
 instrument/Facilities.xml                     |  8 ++-
 6 files changed, 81 insertions(+), 33 deletions(-)
 rename Framework/DataHandling/inc/MantidDataHandling/{SNSDataArchive.h => ORNLDataArchive.h} (85%)
 rename Framework/DataHandling/src/{SNSDataArchive.cpp => ORNLDataArchive.cpp} (87%)
 rename Framework/DataHandling/test/{SNSDataArchiveTest.h => ORNLDataArchiveTest.h} (66%)

diff --git a/Framework/API/test/FileFinderTest.h b/Framework/API/test/FileFinderTest.h
index 42c4ef8ed82..66b508a8e70 100644
--- a/Framework/API/test/FileFinderTest.h
+++ b/Framework/API/test/FileFinderTest.h
@@ -77,7 +77,7 @@ public:
         "  <facility name=\"SNS\" delimiter=\"_\" "
         "FileExtensions=\"_event.nxs,.nxs,.dat\">"
         "    <archive>"
-        "      <archiveSearch plugin=\"SNSDataSearch\" />"
+        "      <archiveSearch plugin=\"ORNLDataSearch\" />"
         "    </archive>"
         "    <instrument name=\"SEQUOIA\" shortname=\"SEQ\">"
         "      <technique>Inelastic Spectroscopy</technique>"
diff --git a/Framework/DataHandling/CMakeLists.txt b/Framework/DataHandling/CMakeLists.txt
index 655c6239bdd..ceadfb223ff 100644
--- a/Framework/DataHandling/CMakeLists.txt
+++ b/Framework/DataHandling/CMakeLists.txt
@@ -137,7 +137,7 @@
 	src/RenameLog.cpp
 	src/RotateInstrumentComponent.cpp
 	src/RotateSource.cpp
-	src/SNSDataArchive.cpp
+	src/ORNLDataArchive.cpp
     	src/SaveANSTOAscii.cpp
 	src/SaveAscii.cpp
 	src/SaveAscii2.cpp
@@ -326,7 +326,7 @@ set ( INC_FILES
 	inc/MantidDataHandling/RenameLog.h
 	inc/MantidDataHandling/RotateInstrumentComponent.h
 	inc/MantidDataHandling/RotateSource.h
-	inc/MantidDataHandling/SNSDataArchive.h
+	inc/MantidDataHandling/ORNLDataArchive.h
 	inc/MantidDataHandling/SaveANSTOAscii.h
 	inc/MantidDataHandling/SaveAscii.h
 	inc/MantidDataHandling/SaveAscii2.h
@@ -506,7 +506,7 @@ set ( TEST_FILES
 	RenameLogTest.h
 	RotateInstrumentComponentTest.h
 	RotateSourceTest.h
-	SNSDataArchiveTest.h
+	ORNLDataArchiveTest.h
 	SaveANSTOAsciiTest.h
 	SaveAscii2Test.h
 	SaveAsciiTest.h
diff --git a/Framework/DataHandling/inc/MantidDataHandling/SNSDataArchive.h b/Framework/DataHandling/inc/MantidDataHandling/ORNLDataArchive.h
similarity index 85%
rename from Framework/DataHandling/inc/MantidDataHandling/SNSDataArchive.h
rename to Framework/DataHandling/inc/MantidDataHandling/ORNLDataArchive.h
index b1a79d944ec..fa1fa0339f9 100644
--- a/Framework/DataHandling/inc/MantidDataHandling/SNSDataArchive.h
+++ b/Framework/DataHandling/inc/MantidDataHandling/ORNLDataArchive.h
@@ -4,8 +4,8 @@
 //     NScD Oak Ridge National Laboratory, European Spallation Source
 //     & Institut Laue - Langevin
 // SPDX - License - Identifier: GPL - 3.0 +
-#ifndef MANTID_DATAHANDLING_SNSDATAARCHIVE_H_
-#define MANTID_DATAHANDLING_SNSDATAARCHIVE_H_
+#ifndef MANTID_DATAHANDLING_ORNLDATAARCHIVE_H_
+#define MANTID_DATAHANDLING_ORNLDATAARCHIVE_H_
 
 //----------------------------------------------------------------------
 // Includes
@@ -24,7 +24,7 @@ namespace DataHandling {
  * Please see the .cpp file for more information.
  */
 
-class DLLExport SNSDataArchive : public API::IArchiveSearch {
+class DLLExport ORNLDataArchive : public API::IArchiveSearch {
 public:
   std::string
   getArchivePath(const std::set<std::string> &basenames,
@@ -42,4 +42,4 @@ private:
 } // namespace DataHandling
 } // namespace Mantid
 
-#endif /* MANTID_DATAHANDLING_SNSDATAARCHIVE_H_ */
+#endif /* MANTID_DATAHANDLING_ORNLDATAARCHIVE_H_ */
diff --git a/Framework/DataHandling/src/SNSDataArchive.cpp b/Framework/DataHandling/src/ORNLDataArchive.cpp
similarity index 87%
rename from Framework/DataHandling/src/SNSDataArchive.cpp
rename to Framework/DataHandling/src/ORNLDataArchive.cpp
index 3e70314d45a..24497c364c7 100644
--- a/Framework/DataHandling/src/SNSDataArchive.cpp
+++ b/Framework/DataHandling/src/ORNLDataArchive.cpp
@@ -10,8 +10,10 @@
 #include <sstream>
 
 #include "MantidAPI/ArchiveSearchFactory.h"
-#include "MantidDataHandling/SNSDataArchive.h"
+#include "MantidDataHandling/ORNLDataArchive.h"
+#include "MantidKernel/ConfigService.h"
 #include "MantidKernel/Exception.h"
+#include "MantidKernel/FacilityInfo.h"
 #include "MantidKernel/InternetHelper.h"
 #include "MantidKernel/Logger.h"
 
@@ -39,20 +41,20 @@ std::string toUpperCase(const std::string & s) {
 const static boost::regex FILE_REGEX("^(.*?)_(\\d+).*$");
 const static std::string NOT_FOUND("");
 
-Mantid::Kernel::Logger g_log("SNSDataArchive");
+Mantid::Kernel::Logger g_log("ORNLDataArchive");
 }
 
 namespace Mantid {
 namespace DataHandling {
 
-DECLARE_ARCHIVESEARCH(SNSDataArchive, SNSDataSearch)
+DECLARE_ARCHIVESEARCH(ORNLDataArchive, ORNLDataSearch)
 
 /**
  * ****************
  * PLEASE READ THIS
  * ****************
  *
- * This archive searcher now retrieves SNS run locations from ONCat.
+ * This archive searcher now retrieves SNS / HFIR run locations from ONCat.
  *
  * Something to bear in mind here, however, is that the signature of
  * IArchiveSearch's getArchivePath is quite counter-intuitive, and so probably
@@ -73,7 +75,7 @@ DECLARE_ARCHIVESEARCH(SNSDataArchive, SNSDataSearch)
  *    want to search for a range of runs then you will either have to extend
  *    the interface, or make multiple calls to the existing one.)
  *
- * 3) The SNSDataArchive implementation has never (and will never) require all
+ * 3) The implementation for SNS / ORNL has never (and will never) require all
  *    the basenames passed to it -- it just discards all but the first and then
  *    uses that.
  *
@@ -95,7 +97,7 @@ DECLARE_ARCHIVESEARCH(SNSDataArchive, SNSDataSearch)
  *     empty string.
  */
 std::string
-SNSDataArchive::getArchivePath(const std::set<std::string> &basenames,
+ORNLDataArchive::getArchivePath(const std::set<std::string> &basenames,
                                const std::vector<std::string> &suffixes) const {
   if (basenames.size() == 0) {
     return NOT_FOUND;
@@ -116,11 +118,25 @@ SNSDataArchive::getArchivePath(const std::set<std::string> &basenames,
   const std::string instrument = toUpperCase(result[1]);
   const std::string run = result[2];
 
+  const auto &configService = Mantid::Kernel::ConfigService::Instance();
+  std::string facility;
+  try {
+    facility = configService.getInstrument(instrument).facility().name();
+
+    if (facility != "HFIR" && facility != "SNS") {
+      return NOT_FOUND;
+    }
+  } catch (Mantid::Kernel::Exception::NotFoundError &) {
+    g_log.debug() << "\"" << instrument
+                  << "\" is not an instrument known to Mantid." << std::endl;
+    return NOT_FOUND;
+  }
+
   // Note that we will only be asking for raw files with the given instrument
   // and run number, and *not* filtering by suffix at this point.  (ONCat has
   // a strict definition of what a file "extension" is, and has no way of
   // filtering by, for example, "_event.nxs".)
-  const QueryParameters params {QueryParameter("facility", "SNS"),
+  const QueryParameters params {QueryParameter("facility", facility),
                                 QueryParameter("instrument", instrument),
                                 QueryParameter("projection", "location"),
                                 QueryParameter("tags", "type/raw"),
@@ -184,7 +200,7 @@ SNSDataArchive::getArchivePath(const std::set<std::string> &basenames,
   return NOT_FOUND;
 }
 
-void SNSDataArchive::setONCat(ONCat_uptr oncat) {
+void ORNLDataArchive::setONCat(ONCat_uptr oncat) {
   m_oncat = std::move(oncat);
 }
 
diff --git a/Framework/DataHandling/test/SNSDataArchiveTest.h b/Framework/DataHandling/test/ORNLDataArchiveTest.h
similarity index 66%
rename from Framework/DataHandling/test/SNSDataArchiveTest.h
rename to Framework/DataHandling/test/ORNLDataArchiveTest.h
index a3ab3d5c6ce..eca4073f5b3 100644
--- a/Framework/DataHandling/test/SNSDataArchiveTest.h
+++ b/Framework/DataHandling/test/ORNLDataArchiveTest.h
@@ -4,13 +4,13 @@
 //     NScD Oak Ridge National Laboratory, European Spallation Source
 //     & Institut Laue - Langevin
 // SPDX - License - Identifier: GPL - 3.0 +
-#ifndef SNSDATAARCHIVETEST_H_
-#define SNSDATAARCHIVETEST_H_
+#ifndef ORNLDATAARCHIVETEST_H_
+#define ORNLDATAARCHIVETEST_H_
 
 #include <cxxtest/TestSuite.h>
 
 #include "MantidAPI/ArchiveSearchFactory.h"
-#include "MantidDataHandling/SNSDataArchive.h"
+#include "MantidDataHandling/ORNLDataArchive.h"
 #include "MantidCatalog/Exception.h"
 #include "MantidCatalog/ONCat.h"
 #include "MantidKernel/Exception.h"
@@ -34,25 +34,27 @@ using Mantid::Kernel::Exception::InternetError;
 using Mantid::TestHelpers::make_mock_oncat_api;
 using Mantid::TestHelpers::make_oncat_with_mock_api;
 
-class SNSDataArchiveTest : public CxxTest::TestSuite {
+class ORNLDataArchiveTest : public CxxTest::TestSuite {
 public:
   void testSearch() {
-    SNSDataArchive arch;
+    ORNLDataArchive arch;
 
-    const auto generatePowgenRunUrl = [](const std::string &runNumber)  {
+    const auto generateRunUrl = [](const std::string &facility,
+                                   const std::string &instrument,
+                                   const std::string &runNumber)  {
       return std::string(
           "https://oncat.ornl.gov/api/datafiles"
-          "?facility=SNS"
-          "&instrument=PG3"
+          "?facility=") + facility +
+          "&instrument=" + instrument +
           "&projection=location"
           "&tags=type/raw"
           "&sort_by=ingested"
           "&sort_direction=DESCENDING"
-          "&ranges_q=indexed.run_number:") + runNumber;
+          "&ranges_q=indexed.run_number:" + runNumber;
     };
 
     auto mockAPI = make_mock_oncat_api(
-        {{generatePowgenRunUrl("7390"),
+        {{generateRunUrl("SNS", "PG3", "7390"),
           std::make_pair(
               HTTPResponse::HTTP_OK,
               "["
@@ -66,13 +68,27 @@ public:
               "    \"type\": \"datafile\""
               "  }"
               "]")},
-         {generatePowgenRunUrl("9999999"),
+         {generateRunUrl("HFIR", "HB2C", "26506"),
+          std::make_pair(
+              HTTPResponse::HTTP_OK,
+              "["
+              "  {"
+              "    \"location\": "
+              "    \"/HFIR/HB2C/IPTS-7776/nexus/HB2C_26506.nxs.h5\","
+              "    \"id\": \"5ba1c86a4e7bcae781440283\","
+              "    \"indexed\": {"
+              "      \"run_number\": 26506"
+              "    },"
+              "    \"type\": \"datafile\""
+              "  }"
+              "]")},
+         {generateRunUrl("SNS", "PG3", "9999999"),
           std::make_pair(HTTPResponse::HTTP_OK, "[]")},
-         {generatePowgenRunUrl("500"),
+         {generateRunUrl("SNS", "PG3", "500"),
           std::make_pair(
               HTTPResponse::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR,
               "{\"message\" : \"Stack trace or similar...\"}")},
-         {generatePowgenRunUrl("200"),
+         {generateRunUrl("SNS", "PG3", "200"),
           std::make_pair(
               HTTPResponse::HTTPResponse::HTTP_OK,
               "["
@@ -91,11 +107,18 @@ public:
     TS_ASSERT_EQUALS(arch.getArchivePath({"PG3_7390_event.nxs"}, {}),
                      "/SNS/PG3/IPTS-2767/0/7390/NeXus/PG3_7390_event.nxs");
 
+    // Make sure we support HFIR, too!
+    TS_ASSERT_EQUALS(arch.getArchivePath({"HB2C_26506"}, {".nxs.h5"}),
+                     "/HFIR/HB2C/IPTS-7776/nexus/HB2C_26506.nxs.h5");
+    TS_ASSERT_EQUALS(arch.getArchivePath({"HB2C_26506.nxs.h5"}, {}),
+                     "/HFIR/HB2C/IPTS-7776/nexus/HB2C_26506.nxs.h5");
+
     // Return nothing when the run has not been cataloged in ONCat.
     TS_ASSERT_EQUALS(arch.getArchivePath({"PG3_9999999"}, {"_event.nxs"}), "");
 
     // Mimic old behaviour by returning nothing when asking for a run known to
     // ONCat but without providing the "suffix" of the basename.
+    TS_ASSERT_EQUALS(arch.getArchivePath({"PG3_7390"}, {}), "");
     TS_ASSERT_EQUALS(arch.getArchivePath({"PG3_7390"}, {""}), "");
 
     // Ask stupid questions, get stupid answers.
@@ -111,14 +134,19 @@ public:
     // bit of JSON has been returned.
     TS_ASSERT_EQUALS(arch.getArchivePath({"PG3_200"}, {"_event.nxs"}), "");
 
+    // Instruments not known to Mantid, or not compatible with the archive
+    // class should not return anything, either.
+    TS_ASSERT_EQUALS(arch.getArchivePath({"DOESNOTEXIST_200"}, {""}), "");
+    TS_ASSERT_EQUALS(arch.getArchivePath({"MERLIN_200"}, {""}), "");
+
     TS_ASSERT(mockAPI->allResponsesCalled());
   }
 
   void testFactory() {
     boost::shared_ptr<IArchiveSearch> arch =
-        ArchiveSearchFactory::Instance().create("SNSDataSearch");
+        ArchiveSearchFactory::Instance().create("ORNLDataSearch");
     TS_ASSERT(arch);
   }
 };
 
-#endif // SNSDATAARCHIVETEST_H_
+#endif // ORNLDATAARCHIVETEST_H_
diff --git a/instrument/Facilities.xml b/instrument/Facilities.xml
index 5807e78e65c..3ec519e607b 100644
--- a/instrument/Facilities.xml
+++ b/instrument/Facilities.xml
@@ -376,7 +376,11 @@
 </facility>
 
 
-<facility name="HFIR" FileExtensions=".nxs,.dat,.xml">
+<facility name="HFIR" FileExtensions=".nxs,.nxs.h5,.dat,.xml">
+
+   <archive>
+      <archiveSearch plugin="ORNLDataSearch" />
+   </archive>
 
    <timezone>US/Eastern</timezone>
 
@@ -439,7 +443,7 @@
 <facility name="SNS" delimiter="_" FileExtensions=".nxs.h5,_event.nxs,.nxs,.dat,_runinfo.xml,_histo.nxs">
 
    <archive>
-      <archiveSearch plugin="SNSDataSearch" />
+      <archiveSearch plugin="ORNLDataSearch" />
    </archive>
 
    <computeResource name="Fermi">
-- 
GitLab