From 21463dba42d5d7033dbbd7ea879bb8d9b09c3f2e Mon Sep 17 00:00:00 2001
From: Pete Peterson <petersonpf@ornl.gov>
Date: Wed, 24 May 2017 15:19:22 -0400
Subject: [PATCH] Only load calibration file once per invocation

---
 .../AlignAndFocusPowderFromFiles.py           | 37 ++++++++++++++++---
 1 file changed, 32 insertions(+), 5 deletions(-)

diff --git a/Framework/PythonInterface/plugins/algorithms/AlignAndFocusPowderFromFiles.py b/Framework/PythonInterface/plugins/algorithms/AlignAndFocusPowderFromFiles.py
index 03e53bc5674..8e3d1903a56 100644
--- a/Framework/PythonInterface/plugins/algorithms/AlignAndFocusPowderFromFiles.py
+++ b/Framework/PythonInterface/plugins/algorithms/AlignAndFocusPowderFromFiles.py
@@ -2,7 +2,7 @@ from __future__ import (absolute_import, division, print_function)
 
 from mantid.api import mtd, AlgorithmFactory, DataProcessorAlgorithm, ITableWorkspaceProperty, \
     MatrixWorkspaceProperty, MultipleFileProperty, PropertyMode
-from mantid.kernel import Direction
+from mantid.kernel import ConfigService, Direction
 from mantid.simpleapi import AlignAndFocusPowder, CompressEvents, ConvertUnits, CreateCacheFilename, \
     DeleteWorkspace, DetermineChunking, Divide, EditInstrumentGeometry, FilterBadPulses, Load, \
     LoadNexusProcessed, PDDetermineCharacterizations, Plus, RenameWorkspace, SaveNexusProcessed
@@ -10,9 +10,11 @@ import os
 
 EXTENSIONS_NXS = ["_event.nxs", ".nxs.h5"]
 PROPS_FOR_INSTR = ["PrimaryFlightPath", "SpectrumIDs", "L2", "Polar", "Azimuthal"]
-PROPS_FOR_ALIGN = ["CalFileName", "GroupFilename", "GroupingWorkspace",
-                   "CalibrationWorkspace", "OffsetsWorkspace",
-                   "MaskWorkspace", "MaskBinTable",
+CAL_FILE, GROUP_FILE = "CalFileName", "GroupFilename"
+CAL_WKSP, GRP_WKSP, MASK_WKSP = "CalibrationWorkspace", "GroupingWorkspace", "MaskWorkspace"
+PROPS_FOR_ALIGN = [CAL_FILE, GROUP_FILE,
+                   GRP_WKSP,CAL_WKSP, "OffsetsWorkspace",
+                   MASK_WKSP, "MaskBinTable",
                    "Params", "ResampleX", "Dspacing", "DMin", "DMax",
                    "TMin", "TMax", "PreserveEvents",
                    "RemovePromptPulseWidth", "CompressTolerance",
@@ -99,6 +101,29 @@ class AlignAndFocusPowderFromFiles(DataProcessorAlgorithm):
                 args[name] = prop.value
         return args
 
+    def __updateAlignAndFocusArgs(self, wkspname):
+        self.log().warning('__updateAlignAndFocusArgs(%s)!!!!!!!' % wkspname)
+        # if the files are missing, there is nothing to do
+        if (not CAL_FILE in self.kwargs) and (not GROUP_FILE in self.kwargs):
+            self.log().warning('--> Nothing to do')
+        self.log().warning('--> Updating')
+
+        # delete the files from the list of kwargs
+        if CAL_FILE in self.kwargs:
+            del self.kwargs[CAL_FILE]
+        if CAL_FILE in self.kwargs:
+            del self.kwargs[GROUP_FILE]
+
+        # get the instrument name
+        instr = mtd[wkspname].getInstrument().getName()
+        instr = ConfigService.getInstrument(instr).shortName()
+
+        # use the canonical names if they weren't specifed
+        for key, ext in zip((CAL_WKSP, GRP_WKSP, MASK_WKSP),
+                            ('_cal', '_group', '_mask')):
+            if not key in self.kwargs:
+                self.kwargs[key] = instr + ext
+
     def __determineCharacterizations(self, filename, wkspname):
         tempname = '__%s_temp' % wkspname
         Load(Filename=filename, OutputWorkspace=tempname,
@@ -172,6 +197,7 @@ class AlignAndFocusPowderFromFiles(DataProcessorAlgorithm):
             prog_start += 2.*prog_per_chunk_step # AlignAndFocusPowder counts for two steps
 
             if j == 0:
+                self.__updateAlignAndFocusArgs(chunkname)
                 RenameWorkspace(InputWorkspace=chunkname, OutputWorkspace=wkspname)
             else:
                 Plus(LHSWorkspace=wkspname, RHSWorkspace=chunkname, OutputWorkspace=wkspname,
@@ -199,7 +225,8 @@ class AlignAndFocusPowderFromFiles(DataProcessorAlgorithm):
         for (i, filename) in enumerate(filenames):
             # default name is based off of filename
             wkspname = os.path.split(filename)[-1].split('.')[0]
-            self.__determineCharacterizations(filename, wkspname)
+            self.__determineCharacterizations(filename,
+                                              wkspname) # updates instance variable
             cachefile = self.__getCacheName(wkspname)
             wkspname += '_f%d' % i # add file number to be unique
 
-- 
GitLab