From 7b9b7c0dd261f50ccd00d0b0137bae9c9eabab87 Mon Sep 17 00:00:00 2001
From: Joseph Ramsay <joseph.ramsay@stfc.ac.uk>
Date: Tue, 10 Apr 2018 10:28:14 +0100
Subject: [PATCH] Re #22265 Add run label to gpx filename if refining multiple
 runs

---
 .../interfaces/Engineering Diffraction.rst    |  6 ++++
 .../EnggDiffGSASFittingPresenter.cpp          | 34 ++++++++++++++++---
 .../EnggDiffractionQtTabGSAS.ui               |  2 +-
 .../test/EnggDiffGSASFittingPresenterTest.h   | 10 +++---
 4 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/docs/source/interfaces/Engineering Diffraction.rst b/docs/source/interfaces/Engineering Diffraction.rst
index fa846f2dc0c..1c51db031b5 100644
--- a/docs/source/interfaces/Engineering Diffraction.rst	
+++ b/docs/source/interfaces/Engineering Diffraction.rst	
@@ -478,6 +478,9 @@ The following parameters are also required:
 - **New GSAS-II Project** - GSASIIRefineFitPeaks creates a new
   ``.gpx`` project here, which can be opened and inspected from the
   GSAS-II GUI
+
+  - Note, if running **Refine All** on more than one run, the run
+    number and bank ID will be appended to the filename
 - **GSAS-II Installation Directory**
 
   - This is the directory containing the GSAS-II executables and
@@ -515,6 +518,9 @@ To do a refinement, take the following steps:
    coefficients) and lattice parameters should be displayed in the
    **Fit Results** section.
 
+   - You can also click **Refine All** to run refinement on all runs
+     loaded into to GSAS tab
+
 You can toggle the fitted peaks on and off with the **Plot Fitted
 Peaks** checkbox, remove runs from the list with the **Remove Run**
 button, and plot the run and fitted peaks to a larger, separate plot
diff --git a/qt/scientific_interfaces/EnggDiffraction/EnggDiffGSASFittingPresenter.cpp b/qt/scientific_interfaces/EnggDiffraction/EnggDiffGSASFittingPresenter.cpp
index f395c8e2c24..099fbc6e196 100644
--- a/qt/scientific_interfaces/EnggDiffraction/EnggDiffGSASFittingPresenter.cpp
+++ b/qt/scientific_interfaces/EnggDiffraction/EnggDiffGSASFittingPresenter.cpp
@@ -2,6 +2,20 @@
 #include "EnggDiffGSASRefinementMethod.h"
 #include "MantidQtWidgets/LegacyQwt/QwtHelper.h"
 
+namespace {
+
+std::string addRunNumberToGSASIIProjectFile(
+    const std::string &filename,
+    const MantidQt::CustomInterfaces::RunLabel &runLabel) {
+  const auto dotPosition = filename.find_last_of(".");
+  return filename.substr(0, dotPosition) + "_" +
+         std::to_string(runLabel.runNumber) + "_" +
+         std::to_string(runLabel.bank) +
+         filename.substr(dotPosition, filename.length());
+}
+
+} // anonymous namespace
+
 namespace MantidQt {
 namespace CustomInterfaces {
 
@@ -34,7 +48,7 @@ void EnggDiffGSASFittingPresenter::notify(
   case IEnggDiffGSASFittingPresenter::RefineAll:
     processRefineAll();
     break;
-    
+
   case IEnggDiffGSASFittingPresenter::SelectRun:
     processSelectRun();
     break;
@@ -53,13 +67,23 @@ std::vector<GSASIIRefineFitPeaksParameters>
 EnggDiffGSASFittingPresenter::collectAllInputParameters() const {
   const auto runLabels = m_multiRunWidget->getAllRunLabels();
   std::vector<GSASIIRefineFitPeaksParameters> inputParams;
+  std::vector<std::string> GSASIIProjectFiles;
   inputParams.reserve(runLabels.size());
+  GSASIIProjectFiles.reserve(runLabels.size());
 
   const auto refinementMethod = m_view->getRefinementMethod();
   const auto instParamFile = m_view->getInstrumentFileName();
   const auto phaseFiles = m_view->getPhaseFileNames();
   const auto pathToGSASII = m_view->getPathToGSASII();
   const auto GSASIIProjectFile = m_view->getGSASIIProjectPath();
+  if (runLabels.size() == 1) {
+    GSASIIProjectFiles = std::vector<std::string>({GSASIIProjectFile});
+  } else {
+    for (const auto &runLabel : runLabels) {
+      GSASIIProjectFiles.push_back(
+          addRunNumberToGSASIIProjectFile(GSASIIProjectFile, runLabel));
+    }
+  }
 
   const auto dMin = m_view->getPawleyDMin();
   const auto negativeWeight = m_view->getPawleyNegativeWeight();
@@ -68,11 +92,13 @@ EnggDiffGSASFittingPresenter::collectAllInputParameters() const {
   const auto refineSigma = m_view->getRefineSigma();
   const auto refineGamma = m_view->getRefineGamma();
 
-  for (const auto &runLabel : runLabels) {
+  for (size_t i = 0; i < runLabels.size(); i++) {
+    const auto runLabel = runLabels[i];
     const auto inputWS = *(m_multiRunWidget->getFocusedRun(runLabel));
+
     inputParams.push_back(GSASIIRefineFitPeaksParameters(
-        inputWS, runLabel, refinementMethod, instParamFile, phaseFiles,
-        pathToGSASII, GSASIIProjectFile, dMin, negativeWeight, xMin, xMax,
+        inputWS, runLabels[i], refinementMethod, instParamFile, phaseFiles,
+        pathToGSASII, GSASIIProjectFiles[i], dMin, negativeWeight, xMin, xMax,
         refineSigma, refineGamma));
   }
   return inputParams;
diff --git a/qt/scientific_interfaces/EnggDiffraction/EnggDiffractionQtTabGSAS.ui b/qt/scientific_interfaces/EnggDiffraction/EnggDiffractionQtTabGSAS.ui
index 4d59814f469..7bdd172d333 100644
--- a/qt/scientific_interfaces/EnggDiffraction/EnggDiffractionQtTabGSAS.ui
+++ b/qt/scientific_interfaces/EnggDiffraction/EnggDiffractionQtTabGSAS.ui
@@ -117,7 +117,7 @@
       <item row="3" column="0">
        <widget class="QLabel" name="label_gsasProjPath">
         <property name="toolTip">
-         <string>The name of a new *.gpx project to write refinement results out to. This can be opened and used for more complex refinements in GSAS-II</string>
+         <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;The name of a new *.gpx project to write refinement results out to. This can be opened and used for more complex refinements in GSAS-II.&lt;/p&gt;&lt;p&gt;Note, if refining more than one run, the run number and bank ID will be appended to this&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
         </property>
         <property name="text">
          <string>New GSAS-II Project</string>
diff --git a/qt/scientific_interfaces/test/EnggDiffGSASFittingPresenterTest.h b/qt/scientific_interfaces/test/EnggDiffGSASFittingPresenterTest.h
index e6e5f865be8..591b000d5c1 100644
--- a/qt/scientific_interfaces/test/EnggDiffGSASFittingPresenterTest.h
+++ b/qt/scientific_interfaces/test/EnggDiffGSASFittingPresenterTest.h
@@ -232,13 +232,13 @@ public:
     const GSASIIRefineFitPeaksParameters params1(
         WorkspaceCreationHelper::create2DWorkspaceBinned(1, 100),
         RunLabel(123, 1), GSASRefinementMethod::RIETVELD, "Instrument file",
-        {"Phase1", "Phase2"}, "GSASHOME", "GPX.gpx", boost::none, boost::none,
-        10000, 40000, true, false);
+        {"Phase1", "Phase2"}, "GSASHOME", "GPX_123_1.gpx", boost::none,
+        boost::none, 10000, 40000, true, false);
     const GSASIIRefineFitPeaksParameters params2(
         WorkspaceCreationHelper::create2DWorkspaceBinned(2, 200),
         RunLabel(456, 2), GSASRefinementMethod::RIETVELD, "Instrument file",
-        {"Phase1", "Phase2"}, "GSASHOME", "GPX.gpx", boost::none, boost::none,
-        10000, 40000, true, false);
+        {"Phase1", "Phase2"}, "GSASHOME", "GPX_456_2.gpx", boost::none,
+        boost::none, 10000, 40000, true, false);
 
     const std::vector<RunLabel> runLabels({params1.runLabel, params2.runLabel});
     EXPECT_CALL(*m_mockMultiRunWidgetPtr, getAllRunLabels())
@@ -262,7 +262,7 @@ public:
         .WillOnce(Return(params1.gsasHome));
     EXPECT_CALL(*m_mockViewPtr, getGSASIIProjectPath())
         .Times(1)
-        .WillOnce(Return(params1.gsasProjectFile));
+        .WillOnce(Return("GPX.gpx"));
     EXPECT_CALL(*m_mockViewPtr, getPawleyDMin())
         .Times(1)
         .WillOnce(Return(params1.dMin));
-- 
GitLab