From 10bc44f4ab6358b441f30d5653af71adce8badd8 Mon Sep 17 00:00:00 2001
From: Joe Ramsay <joseph.ramsay@stfc.ac.uk>
Date: Tue, 22 May 2018 14:01:23 +0100
Subject: [PATCH] Re #22456 Enable saving .gem output when texture focusing

---
 docs/source/techniques/ISISPowder-GEM-v1.rst         | 12 ++++++++++++
 scripts/Diffraction/isis_powder/gem.py               | 11 +++++++++++
 .../isis_powder/gem_routines/gem_advanced_config.py  |  3 ++-
 .../isis_powder/gem_routines/gem_param_mapping.py    |  1 +
 .../isis_powder/routines/common_output.py            |  9 +++++++++
 5 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/docs/source/techniques/ISISPowder-GEM-v1.rst b/docs/source/techniques/ISISPowder-GEM-v1.rst
index 1c37ba20031..81ac56ae254 100644
--- a/docs/source/techniques/ISISPowder-GEM-v1.rst
+++ b/docs/source/techniques/ISISPowder-GEM-v1.rst
@@ -523,6 +523,8 @@ Example Input:
 
 .. _texture_mode_isis-powder-diffraction-ref:
 
+texture_mode
+^^^^^^^^^^^^
 If set to **True**, then this specifies that the reduction is to be
 carried out using Gem's 160-bank texture mode, as opposed to the
 standard 6 banks. This means using altered cropping values for the
@@ -700,6 +702,16 @@ On GEM this is set to the following:
 
   raw_tof_cropping_values: (500, 20000)
 
+.. _save_maud_gem_isis-powder-diffraction-ref:
+
+save_maud
+^^^^^^^^^
+
+If set to **True**, this saves the focus output to the Maud
+three-column format (``.gem`` file extension). If
+:ref:`texture_mode_gem_isis-powder-diffraction-ref` is set to **True**
+this is enabled, and disabled if it is set to **False**.
+
 .. _spline_coefficient_gem_isis-powder-diffraction-ref:
 
 spline_coefficient
diff --git a/scripts/Diffraction/isis_powder/gem.py b/scripts/Diffraction/isis_powder/gem.py
index 7324bd2b21f..d8f3664d87e 100644
--- a/scripts/Diffraction/isis_powder/gem.py
+++ b/scripts/Diffraction/isis_powder/gem.py
@@ -57,6 +57,17 @@ class Gem(AbstractInst):
     def _generate_output_file_name(self, run_number_string):
         return self._generate_input_file_name(run_number_string)
 
+    def _generate_out_file_paths(self, run_details):
+        out_file_names = super(Gem, self)._generate_out_file_paths(run_details)
+
+        if self._inst_settings.save_maud:
+            nxs_filename = out_file_names["nxs_filename"]
+            filename_stub = nxs_filename.split(".")[:-1]
+            maud_filename = ".".join(filename_stub) + "_MAUD.gem"
+            out_file_names["maud_filename"] = maud_filename
+
+        return out_file_names
+
     @staticmethod
     def _generate_input_file_name(run_number):
         return _gem_generate_inst_name(run_number=run_number)
diff --git a/scripts/Diffraction/isis_powder/gem_routines/gem_advanced_config.py b/scripts/Diffraction/isis_powder/gem_routines/gem_advanced_config.py
index 554620c2ff2..cd65ad8242d 100644
--- a/scripts/Diffraction/isis_powder/gem_routines/gem_advanced_config.py
+++ b/scripts/Diffraction/isis_powder/gem_routines/gem_advanced_config.py
@@ -97,7 +97,8 @@ def get_mode_specific_variables(is_texture_mode):
     return {"focused_cropping_values": texture_focused_cropping_values if is_texture_mode else focused_cropping_values,
             "vanadium_cropping_values": texture_vanadium_cropping_values if is_texture_mode
             else vanadium_cropping_values,
-            "grouping_file_name": "offsets_xie_test_2.cal" if is_texture_mode else "GEM_Instrument_grouping.cal"}
+            "grouping_file_name": "offsets_xie_test_2.cal" if is_texture_mode else "GEM_Instrument_grouping.cal",
+            "save_maud": is_texture_mode}
 
 
 def get_all_adv_variables():
diff --git a/scripts/Diffraction/isis_powder/gem_routines/gem_param_mapping.py b/scripts/Diffraction/isis_powder/gem_routines/gem_param_mapping.py
index 8a757af4be9..1cdf674a3e2 100644
--- a/scripts/Diffraction/isis_powder/gem_routines/gem_param_mapping.py
+++ b/scripts/Diffraction/isis_powder/gem_routines/gem_param_mapping.py
@@ -22,6 +22,7 @@ attr_mapping = \
      ParamMapEntry(ext_name="run_number",                int_name="run_number"),
      ParamMapEntry(ext_name="sample_empty",              int_name="sample_empty",   optional=True),
      ParamMapEntry(ext_name="sample_empty_scale",        int_name="sample_empty_scale"),
+     ParamMapEntry(ext_name="save_maud",                 int_name="save_maud"),
      ParamMapEntry(ext_name="spline_coefficient",        int_name="spline_coeff"),
      ParamMapEntry(ext_name="suffix",                    int_name="suffix",         optional=True),
      ParamMapEntry(ext_name="texture_mode",              int_name="texture_mode"),
diff --git a/scripts/Diffraction/isis_powder/routines/common_output.py b/scripts/Diffraction/isis_powder/routines/common_output.py
index 5eeb0450a08..289815280cc 100644
--- a/scripts/Diffraction/isis_powder/routines/common_output.py
+++ b/scripts/Diffraction/isis_powder/routines/common_output.py
@@ -61,6 +61,15 @@ def save_focused_data(d_spacing_group, tof_group, output_paths, run_number_strin
     _save_xye(ws_group=tof_group, ws_units="TOF", run_number=run_number_string,
               output_folder=dat_file_destination, inst_prefix=inst_prefix, file_ext=file_ext)
 
+    if "maud_filename" in output_paths:
+        _save_maud(d_spacing_group, output_paths["maud_filename"])
+
+
+def _save_maud(d_spacing_group, output_path):
+    for i, ws in enumerate(d_spacing_group):
+        mantid.SaveFocusedXYE(InputWorkspace=ws, Filename=output_path, SplitFiles=False, StartAtBankNumber=i,
+                              Append=i > 0, IncludeHeader=True, Format="MAUD")
+
 
 def _save_xye(ws_group, ws_units, run_number, output_folder, inst_prefix, file_ext):
     """
-- 
GitLab