From 605124237304f87b661d72c70cc53b384aaada74 Mon Sep 17 00:00:00 2001
From: David Fairbrother <DavidFair@users.noreply.github.com>
Date: Mon, 6 Mar 2017 18:04:38 +0000
Subject: [PATCH] Re #18643 Add optional chemical properties for abs.
 corrections

---
 .../gem_routines/gem_advanced_config.py       |  1 -
 .../isis_powder/gem_routines/gem_algs.py      |  3 +-
 .../isis_powder/routines/RunDetails.py        |  1 +
 .../routines/absorb_corrections.py            | 60 +++++++++++++++++--
 .../isis_powder/routines/calibrate.py         |  2 -
 5 files changed, 57 insertions(+), 10 deletions(-)

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 a37298079e2..0f1d6e7f40e 100644
--- a/scripts/Diffraction/isis_powder/gem_routines/gem_advanced_config.py
+++ b/scripts/Diffraction/isis_powder/gem_routines/gem_advanced_config.py
@@ -14,7 +14,6 @@ absorption_correction_params = {
 gem_adv_config_params = {
     "raw_tof_cropping_values": (500, 20000),
     "spline_coefficient": 30
-
 }
 
 focused_cropping_values = [(550, 19900),  # Bank 1
diff --git a/scripts/Diffraction/isis_powder/gem_routines/gem_algs.py b/scripts/Diffraction/isis_powder/gem_routines/gem_algs.py
index 3e4bba06b54..0cca76da2c2 100644
--- a/scripts/Diffraction/isis_powder/gem_routines/gem_algs.py
+++ b/scripts/Diffraction/isis_powder/gem_routines/gem_algs.py
@@ -27,6 +27,7 @@ def get_run_details(run_number_string, inst_settings):
     label = common.cal_map_dictionary_key_helper(cycle_map, "label")
     offset_file_name = common.cal_map_dictionary_key_helper(cycle_map, "offset_file_name")
     empty_runs = common.cal_map_dictionary_key_helper(cycle_map, "empty_run_numbers")
+    empty_containers_runs = common.cal_map_dictionary_key_helper(cycle_map, "empty_container_run_numbers")
     vanadium_runs = common.cal_map_dictionary_key_helper(cycle_map, "vanadium_run_numbers")
 
     # For GEM the grouping and offset file are identical
@@ -38,8 +39,6 @@ def get_run_details(run_number_string, inst_settings):
     offset_file_path = os.path.join(label_calibration_folder, offset_file_name)
     splined_file_path = os.path.join(label_calibration_folder, splined_vanadium_name)
 
-    # TODO generate splined vanadium name from common
-
     run_details = RunDetails.RunDetails(run_number=run_number)
     run_details.empty_runs = empty_runs
     run_details.user_input_run_number = run_number_string
diff --git a/scripts/Diffraction/isis_powder/routines/RunDetails.py b/scripts/Diffraction/isis_powder/routines/RunDetails.py
index b03e87ba945..3b761192da5 100644
--- a/scripts/Diffraction/isis_powder/routines/RunDetails.py
+++ b/scripts/Diffraction/isis_powder/routines/RunDetails.py
@@ -11,6 +11,7 @@ class RunDetails(object):
         self.run_number = run_number
         self.user_input_run_number = None
 
+        self.empty_container_runs = None
         self.empty_runs = None
         self.label = None
 
diff --git a/scripts/Diffraction/isis_powder/routines/absorb_corrections.py b/scripts/Diffraction/isis_powder/routines/absorb_corrections.py
index 4a9933e0482..7f4dfbd8d4a 100644
--- a/scripts/Diffraction/isis_powder/routines/absorb_corrections.py
+++ b/scripts/Diffraction/isis_powder/routines/absorb_corrections.py
@@ -23,6 +23,7 @@ def run_cylinder_absorb_corrections(ws_to_correct, multiple_scattering, config_d
     radius_key = "cylinder_sample_radius"
     pos_key = "cylinder_position"
     formula_key = "chemical_formula"
+    chemical_key = "chemical_properties"
 
     e_msg = "The following key was not found in the advanced configuration for sample correction:\n"
 
@@ -31,16 +32,23 @@ def run_cylinder_absorb_corrections(ws_to_correct, multiple_scattering, config_d
     pos = common.dictionary_key_helper(dictionary=config_dict, key=pos_key, exception_msg=e_msg + pos_key)
 
     formula = common.dictionary_key_helper(dictionary=config_dict, key=formula_key, exception_msg=e_msg + formula_key)
+    chemical_properties = common.dictionary_key_helper(dictionary=config_dict, key=chemical_key, throws=False)
+
+    position_dict = {
+        "cylinder_height": height,
+        "cylinder_radius": radius,
+        "cylinder_pos": pos
+    }
 
     ws_to_correct = _calculate__cylinder_absorb_corrections(
         ws_to_correct=ws_to_correct, multiple_scattering=multiple_scattering,
-        c_height=height, c_radius=radius, c_pos=pos, chemical_formula=formula)
+        position_dict=position_dict, chemical_formula=formula, material_properties=chemical_properties)
 
     return ws_to_correct
 
 
 def _calculate__cylinder_absorb_corrections(ws_to_correct, multiple_scattering,
-                                            c_height, c_radius, c_pos, chemical_formula):
+                                            position_dict, chemical_formula, material_properties):
     """
     Calculates vanadium absorption corrections for the specified workspace. The workspace
     should have any monitor spectra masked before being passed into this method. Additionally
@@ -54,9 +62,9 @@ def _calculate__cylinder_absorb_corrections(ws_to_correct, multiple_scattering,
     :param chemical_formula: The chemical formula of the container - usually set to 'V' for Vanadium
     :return: The workspace with corrections applied
     """
-    geometry_json = {'Shape': 'Cylinder', 'Height': c_height,
-                     'Radius': c_radius, 'Center': c_pos}
-    material_json = {'ChemicalFormula': chemical_formula}
+    geometry_json = {'Shape': 'Cylinder', 'Height': position_dict["cylinder_height"],
+                     'Radius': position_dict["cylinder_radius"], 'Center': position_dict["cylinder_pos"]}
+    material_json = _get_material_json(chemical_formula=chemical_formula, material_properties=material_properties)
 
     mantid.SetSample(InputWorkspace=ws_to_correct, Geometry=geometry_json, Material=material_json)
 
@@ -66,3 +74,45 @@ def _calculate__cylinder_absorb_corrections(ws_to_correct, multiple_scattering,
     ws_to_correct = mantid.ConvertUnits(InputWorkspace=ws_to_correct, OutputWorkspace=ws_to_correct, Target="dSpacing")
 
     return ws_to_correct
+
+
+def _get_material_json(chemical_formula, material_properties):
+    """
+    Returns a material JSON with either a chemical formula when using elemental chemicals
+    or uses user set chemical properties when required or the user has set them to generate
+    the required input for SetMaterial
+    :param chemical_formula: The formula of the chemical to use
+    :param material_properties: The materials properties dictionary as set by the user
+    :return: The material JSON for setSample with appropriate settings.
+    """
+    material_json = {'ChemicalFormula': chemical_formula}
+
+    is_elemental_vanadium = True if chemical_formula.lower() == 'v' else False
+
+    # Test if we can just use built in Mantid properties
+    if is_elemental_vanadium and not material_properties:
+        return material_json
+
+    # Else we have to parse the chemical formula specified below
+    err_message = "Custom properties was detected for the material but the required key was not set:\n"
+
+    attenuation_key = "attenuation_cross_section"
+    scattering_key = "scattering_cross_section"
+    sample_density_key = "sample_number_density"
+
+    attenuation_x_section = common.dictionary_key_helper(material_properties, attenuation_key,
+                                                         exception_msg=err_message + attenuation_key)
+    scattering_x_section = common.dictionary_key_helper(material_properties, scattering_key,
+                                                        exception_msg=err_message + scattering_key)
+    sample_density = common.dictionary_key_helper(material_properties, sample_density_key,
+                                                  exception_msg=err_message + sample_density_key)
+
+    material_json = {"SampleNumberDensity": sample_density,
+                     "AttenuationXSection": attenuation_x_section,
+                     "ScatteringXSection": scattering_x_section}
+
+    print ("Using custom chemical properties:")
+    for k, v in material_json.viewitems():
+        print (k, v)
+
+    return material_json
diff --git a/scripts/Diffraction/isis_powder/routines/calibrate.py b/scripts/Diffraction/isis_powder/routines/calibrate.py
index 77077021941..eafce174eee 100644
--- a/scripts/Diffraction/isis_powder/routines/calibrate.py
+++ b/scripts/Diffraction/isis_powder/routines/calibrate.py
@@ -21,8 +21,6 @@ def create_van(instrument, run_details, absorb):
 
     aligned_ws = mantid.AlignDetectors(InputWorkspace=corrected_van_ws,
                                        CalibrationFile=run_details.offset_file_path)
-    import pydevd
-    pydevd.settrace('localhost', port=51205, stdoutToServer=True, stderrToServer=True)
     if absorb:
         aligned_ws = _apply_absorb_corrections(instrument=instrument, run_details=run_details,
                                                van_ws=aligned_ws)
-- 
GitLab