From 4ada00eddc24c691b43f2cccfad0b4a64b49c90b Mon Sep 17 00:00:00 2001
From: Sam Jenkins <s.jenkins@stfc.ac.uk>
Date: Fri, 22 Feb 2019 11:30:55 +0000
Subject: [PATCH] Re #24775 general tidying work

---
 scripts/Engineering/EnginX.py | 175 ++++++++++++++++++++--------------
 1 file changed, 103 insertions(+), 72 deletions(-)

diff --git a/scripts/Engineering/EnginX.py b/scripts/Engineering/EnginX.py
index 3737f96e328..ecb13d80acc 100644
--- a/scripts/Engineering/EnginX.py
+++ b/scripts/Engineering/EnginX.py
@@ -13,7 +13,7 @@ import os
 from platform import system
 from shutil import copy2
 
-import mantid.plots
+import mantid.plots # noqa
 import Engineering.EnggUtils as Utils
 import mantid.simpleapi as simple
 
@@ -160,70 +160,6 @@ def create_vanadium(van_run, calibration_directory):
     simple.DeleteWorkspace(van_name)
 
 
-def calibration_ws(difc, tzero, specNo, name):
-    plot_spec_num = False
-    if specNo.lower() == "north":
-        banks = [1]
-    elif specNo.lower() == "south":
-        banks = [2]
-    elif not specNo == "":
-        banks = [1]
-        plot_spec_num = True
-    else:
-        banks = [1, 2]
-    for i in banks:
-        if not plot_spec_num:
-            bank_ws = simple.AnalysisDataService.retrieve("engg_calibration_bank_{}".format(i))
-        else:
-            bank_ws = simple.AnalysisDataService.retrieve(name)
-        x_val = []
-        y_val = []
-        y2_val = []
-        for irow in range(0, bank_ws.rowCount()):
-            x_val.append(bank_ws.cell(irow, 0))
-            y_val.append(bank_ws.cell(irow, 5))
-
-            y2_val.append(x_val[irow] * difc[i-1] + tzero[i-1])
-        simple.CreateWorkspace(OutputWorkspace="ws1", DataX=x_val, DataY=y_val,
-                               UnitX="Expected Peaks Centre(dSpacing, A)", YUnitLabel="Fitted Peaks Centre(TOF, us)")
-        simple.CreateWorkspace(OutputWorkspace="ws2", DataX=x_val, DataY=y2_val)
-
-        if not plot_spec_num:
-            name = i
-        output_name = "Engg difc Zero Peaks Bank {}".format(name)
-        output = simple.AppendSpectra(InputWorkspace1="ws1", InputWorkspace2="ws2",
-                                      OutputWorkspace=output_name)
-        plot_calibration(output, output_name)
-    simple.DeleteWorkspace("ws1")
-    simple.DeleteWorkspace("ws2")
-
-
-def plot_calibration(workspace, name):
-    fig, ax = plt.subplots(subplot_kw={"projection": "mantid"})
-    fig.canvas.set_window_title(name)
-
-    # plot lines based off of old gui plot
-    ax.plot(workspace, wkspIndex=0, label="Peaks Fitted", linestyle=":", color="black", marker='o', markersize=2,
-            linewidth=1.5)
-    ax.plot(workspace, wkspIndex=1, label="Expected Peaks Centre(dspacing, A)", color="orange", marker='o',
-            markersize=2)
-
-    # set the plot and axes titles
-    ax.set_title(name, fontweight="bold")
-    ax.set_xlabel("Expected Peaks Centre(dspacing, A)", fontweight="bold")
-    ax.set_ylabel("Fitted Peaks Centre(TOF, us)", fontweight="bold")
-
-    # set the ticks on the axes
-    ax.set_xticks(np.arange(0.5, 3.6, step=0.1), True)
-    ax.set_xticks(np.arange(0.5, 4, step=0.5))
-
-    ax.set_yticks(np.arange(1e4, 7e4, step=1e4))
-    ax.set_yticks(np.arange(1e4, 6.2e4, step=0.2e4), True)
-
-    ax.legend()
-    fig.show()
-
-
 def create_calibration(ceria_run, van_run, calibration_directory, calibration_general, cropped, crop_name, crop_on):
     """
     create the calibration files
@@ -286,7 +222,7 @@ def create_calibration_cropped_file(ceria_run, van_run, curve_van, int_van, cali
         save_calibration(ceria_run, van_run, calibration_directory, calibration_general, "all_banks", [param_tbl_name],
                          tzero, difc)
         save_calibration(ceria_run, van_run, calibration_directory, calibration_general,
-                         "bank_{}".format(param_tbl_name), [param_tbl_name],  tzero, difc)
+                         "bank_{}".format(param_tbl_name), [param_tbl_name], tzero, difc)
     else:
         # work out which bank number to crop on, then calibrate
         if spec_nos.lower() == "north":
@@ -308,7 +244,7 @@ def create_calibration_cropped_file(ceria_run, van_run, curve_van, int_van, cali
                          [spec_nos], tzero, difc)
     # create the table workspace containing the parameters
     create_params_table(difc, tzero, difa)
-    calibration_ws(difc, tzero, spec_nos, param_tbl_name)
+    create_difc_zero_workspace(difc, tzero, spec_nos, param_tbl_name)
 
 
 def create_calibration_files(ceria_run, van_run, curve_van, int_van, calibration_directory, calibration_general):
@@ -348,7 +284,7 @@ def create_calibration_files(ceria_run, van_run, curve_van, int_van, calibration
     save_calibration(ceria_run, van_run, calibration_directory, calibration_general, "all_banks", bank_names, tzeros,
                      difcs)
     create_params_table(difcs, tzeros, difa)
-    calibration_ws(difcs, tzeros, "", None)
+    create_difc_zero_workspace(difcs, tzeros, "", None)
 
 
 def load_van_files(curves_van, ints_van):
@@ -379,7 +315,7 @@ def save_calibration(ceria_run, van_run, calibration_directory, calibration_gene
 
     """
 
-    gsas_iparm_fname = os.path.join(calibration_directory, "ENGINX_"+van_run+"_"+ceria_run+"_"+name+".prm")
+    gsas_iparm_fname = os.path.join(calibration_directory, "ENGINX_" + van_run + "_" + ceria_run + "_" + name + ".prm")
     # work out what template to use
     if name == "all_banks":
         template_file = None
@@ -421,6 +357,99 @@ def create_params_table(difc, tzero, difa):
         param_table.addRow(next_row)
 
 
+def create_difc_zero_workspace(difc, tzero, crop_on, name):
+    """
+    create a workspace that can be used to plot the expected peaks against the fitted ones
+
+    @param difc :: the list of difc values to add to the table
+    @param tzero :: the list of tzero values to add to the table
+    @param crop_on :: where the cropping occured, either a bank, a spectra, or empty
+    @param name :: the name of a cropped workspace to use, if it is a non default name
+
+    """
+    plot_spec_num = False
+    # check what banks to use
+    if crop_on.lower() == "north":
+        banks = [1]
+    elif crop_on.lower() == "south":
+        banks = [2]
+    elif not crop_on == "":
+        banks = [1]
+        plot_spec_num = True
+    else:
+        banks = [1, 2]
+
+    # loop through used banks
+    for i in banks:
+        # retrieve required workspace
+        if not plot_spec_num:
+            bank_ws = simple.AnalysisDataService.retrieve("engg_calibration_bank_{}".format(i))
+        else:
+            bank_ws = simple.AnalysisDataService.retrieve(name)
+
+        # get the data to be used
+        x_val = []
+        y_val = []
+        y2_val = []
+        for irow in range(0, bank_ws.rowCount()):
+            x_val.append(bank_ws.cell(irow, 0))
+            y_val.append(bank_ws.cell(irow, 5))
+
+            y2_val.append(x_val[irow] * difc[i - 1] + tzero[i - 1])
+
+        # create workspaces to temporary hold the data
+        simple.CreateWorkspace(OutputWorkspace="ws1", DataX=x_val, DataY=y_val,
+                               UnitX="Expected Peaks Centre(dSpacing, A)",
+                               YUnitLabel="Fitted Peaks Centre(TOF, us)")
+        simple.CreateWorkspace(OutputWorkspace="ws2", DataX=x_val, DataY=y2_val)
+
+        # get correct name for output
+        if not plot_spec_num:
+            name = i
+        output_name = "Engg difc Zero Peaks Bank {}".format(name)
+
+        # use the two workspaces to creat the output
+        output = simple.AppendSpectra(InputWorkspace1="ws1", InputWorkspace2="ws2",
+                                      OutputWorkspace=output_name)
+        plot_calibration(output, output_name)
+
+    # remove the left-over workspaces
+    simple.DeleteWorkspace("ws1")
+    simple.DeleteWorkspace("ws2")
+
+
+def plot_calibration(workspace, name):
+    """
+    Plot the fitted peaks and expected peaks of a bank
+    @param workspace :: The workspace object to be plotted
+    @param name :: The name to be plotted, should be a variation of "Engg difc Zero Peaks Bank..."
+
+    """
+    fig, ax = plt.subplots(subplot_kw={"projection": "mantid"})
+    fig.canvas.set_window_title(name)
+
+    # plot lines based off of old gui plot
+    ax.plot(workspace, wkspIndex=0, label="Peaks Fitted", linestyle=":", color="black", marker='o', markersize=2,
+            linewidth=1.5)
+    ax.plot(workspace, wkspIndex=1, label="Expected Peaks Centre(dspacing, A)", color="orange", marker='o',
+            markersize=2)
+
+    # set the plot and axes titles
+    ax.set_title(name, fontweight="bold")
+    ax.set_xlabel("Expected Peaks Centre(dspacing, A)", fontweight="bold")
+    ax.set_ylabel("Fitted Peaks Centre(TOF, us)", fontweight="bold")
+
+    # set the ticks on the axes
+    ax.set_xticks(np.arange(0.5, 3.6, step=0.1), True)
+    ax.set_xticks(np.arange(0.5, 4, step=0.5))
+
+    ax.set_yticks(np.arange(1e4, 7e4, step=1e4))
+    ax.set_yticks(np.arange(1e4, 6.2e4, step=0.2e4), True)
+
+    ax.legend()
+    fig.show()
+
+
 def focus(run_no, van_run, calibration_directory, focus_directory, focus_general, do_pre_process, params, time_period,
           grouping_file, cropped, crop_on):
     """
@@ -484,7 +513,8 @@ def focus_whole(run_number, van_curves, van_int, focus_directory, focus_general,
         _save_out(run_number, focus_directory, focus_general, output_ws, "ENGINX_{}_{}", str(i))
 
 
-def focus_cropped(run_number, van_curves, van_int, focus_directory, focus_general, do_pre_process, params, time_period, crop_on,
+def focus_cropped(run_number, van_curves, van_int, focus_directory, focus_general, do_pre_process, params, time_period,
+                  crop_on,
                   use_spectra):
     """
     focus a partial run, cropping either on banks or on specific spectra
@@ -523,7 +553,8 @@ def focus_cropped(run_number, van_curves, van_int, focus_directory, focus_genera
         _save_out(run_number, focus_directory, focus_general, output_ws, "ENGINX_{}_bank_{}", "cropped")
 
 
-def focus_texture_mode(run_number, van_curves, van_int, focus_directory, focus_general, do_pre_process, params, time_period,
+def focus_texture_mode(run_number, van_curves, van_int, focus_directory, focus_general, do_pre_process, params,
+                       time_period,
                        dg_file):
     """
     perform a texture mode focusing using the grouping csv file
@@ -607,7 +638,7 @@ def _save_out(run_number, focus_directory, focus_general, output, join_string, b
         if not os.path.exists(focus_general):
             os.makedirs(focus_general)
         # copy the files to the general directory
-        copy2(filename+".dat", focus_general)
+        copy2(filename + ".dat", focus_general)
         copy2(filename + ".gss", focus_general)
         copy2(filename + ".his", focus_general)
         copy2(filename + ".nxs", focus_general)
-- 
GitLab