Esempio n. 1
0
    def test_generate_summed_runs_scale_factor(self):
        sample_empty_number = "100"
        ws_file_name = "POL" + sample_empty_number
        original_ws = mantid.Load(ws_file_name)
        original_y = original_ws.readY(0)
        scale_factor = 0.75

        scaled_ws = common.generate_summed_runs(
            empty_sample_ws_string=sample_empty_number,
            instrument=ISISPowderMockInst(),
            scale_factor=scale_factor)
        scaled_y_values = scaled_ws.readY(0)
        self.assertAlmostEqual(scaled_y_values[2],
                               original_y[2] * scale_factor)
        self.assertAlmostEqual(scaled_y_values[4],
                               original_y[4] * scale_factor)
        self.assertAlmostEqual(scaled_y_values[7],
                               original_y[7] * scale_factor)

        mantid.DeleteWorkspace(original_ws)
        mantid.DeleteWorkspace(scaled_ws)
Esempio n. 2
0
def _focus_one_ws(input_workspace,
                  run_number,
                  instrument,
                  perform_vanadium_norm,
                  absorb,
                  sample_details,
                  vanadium_path,
                  empty_can_subtraction_method,
                  paalman_pings_events_per_point=None):
    run_details = instrument._get_run_details(run_number_string=run_number)
    if perform_vanadium_norm:
        _test_splined_vanadium_exists(instrument, run_details)

    # Subtract empty instrument runs, as long as this run isn't an empty, user hasn't turned empty subtraction off, or
    # The user has not supplied a sample empty
    is_run_empty = common.runs_overlap(run_number, run_details.empty_inst_runs)
    summed_empty = None
    if not is_run_empty and instrument.should_subtract_empty_inst(
    ) and not run_details.sample_empty:
        if os.path.isfile(run_details.summed_empty_inst_file_path):
            logger.warning('Pre-summed empty instrument workspace found at ' +
                           run_details.summed_empty_inst_file_path)
            summed_empty = mantid.LoadNexus(
                Filename=run_details.summed_empty_inst_file_path)
        else:
            summed_empty = common.generate_summed_runs(
                empty_sample_ws_string=run_details.empty_inst_runs,
                instrument=instrument)
    elif run_details.sample_empty:
        scale_factor = 1.0
        if empty_can_subtraction_method != 'PaalmanPings':
            scale_factor = instrument._inst_settings.sample_empty_scale
        # Subtract a sample empty if specified ie empty can
        summed_empty = common.generate_summed_runs(
            empty_sample_ws_string=run_details.sample_empty,
            instrument=instrument,
            scale_factor=scale_factor)

    if absorb and empty_can_subtraction_method == 'PaalmanPings':
        if run_details.sample_empty:  # need summed_empty including container
            input_workspace = instrument._apply_paalmanpings_absorb_and_subtract_empty(
                workspace=input_workspace,
                summed_empty=summed_empty,
                sample_details=sample_details,
                paalman_pings_events_per_point=paalman_pings_events_per_point)
            # Crop to largest acceptable TOF range
            input_workspace = instrument._crop_raw_to_expected_tof_range(
                ws_to_crop=input_workspace)
        else:
            raise TypeError(
                "The PaalmanPings absorption method requires 'sample_empty' to be supplied."
            )
    else:
        if summed_empty:
            input_workspace = common.subtract_summed_runs(
                ws_to_correct=input_workspace, empty_sample=summed_empty)
        # Crop to largest acceptable TOF range
        input_workspace = instrument._crop_raw_to_expected_tof_range(
            ws_to_crop=input_workspace)

        if absorb:
            input_workspace = instrument._apply_absorb_corrections(
                run_details=run_details, ws_to_correct=input_workspace)
        else:
            # Set sample material if specified by the user
            if sample_details is not None:
                mantid.SetSample(
                    InputWorkspace=input_workspace,
                    Geometry=sample_details.generate_sample_geometry(),
                    Material=sample_details.generate_sample_material())

    # Align
    mantid.ApplyDiffCal(InstrumentWorkspace=input_workspace,
                        CalibrationFile=run_details.offset_file_path)
    aligned_ws = mantid.ConvertUnits(InputWorkspace=input_workspace,
                                     Target="dSpacing")

    solid_angle = instrument.get_solid_angle_corrections(
        run_details.vanadium_run_numbers, run_details)
    if solid_angle:
        aligned_ws = mantid.Divide(LHSWorkspace=aligned_ws,
                                   RHSWorkspace=solid_angle)
        mantid.DeleteWorkspace(solid_angle)

    # Focus the spectra into banks
    focused_ws = mantid.DiffractionFocussing(
        InputWorkspace=aligned_ws,
        GroupingFileName=run_details.grouping_file_path)

    instrument.apply_calibration_to_focused_data(focused_ws)

    calibrated_spectra = _apply_vanadium_corrections(
        instrument=instrument,
        input_workspace=focused_ws,
        perform_vanadium_norm=perform_vanadium_norm,
        vanadium_splines=vanadium_path)

    output_spectra = instrument._crop_banks_to_user_tof(calibrated_spectra)

    bin_widths = instrument._get_instrument_bin_widths()
    if bin_widths:
        # Reduce the bin width if required on this instrument
        output_spectra = common.rebin_workspace_list(
            workspace_list=output_spectra, bin_width_list=bin_widths)

    # Output
    d_spacing_group, tof_group = instrument._output_focused_ws(
        output_spectra, run_details=run_details)

    common.keep_single_ws_unit(d_spacing_group=d_spacing_group,
                               tof_group=tof_group,
                               unit_to_keep=instrument._get_unit_to_keep())

    # Tidy workspaces from Mantid
    common.remove_intermediate_workspace(input_workspace)
    common.remove_intermediate_workspace(aligned_ws)
    common.remove_intermediate_workspace(focused_ws)
    common.remove_intermediate_workspace(output_spectra)

    return d_spacing_group
Esempio n. 3
0
def create_van(instrument, run_details, absorb):
    """
    Creates a splined vanadium run for the following instrument. Requires the run_details for the
    vanadium workspace we will process and whether to apply absorption corrections.
    :param instrument: The instrument object that will be used to supply various instrument specific methods
    :param run_details: The run details associated with this vanadium run
    :param absorb: Boolean flag whether to apply absorption corrections
    :return: Processed workspace group in dSpacing (but not splined)
    """
    van = run_details.vanadium_run_numbers
    # Always sum a range of inputs as its a vanadium run over multiple captures
    input_van_ws_list = common.load_current_normalised_ws_list(
        run_number_string=van,
        instrument=instrument,
        input_batching=INPUT_BATCHING.Summed)
    input_van_ws = input_van_ws_list[
        0]  # As we asked for a summed ws there should only be one returned

    instrument.create_solid_angle_corrections(input_van_ws, run_details)

    if not (run_details.empty_runs is None):
        summed_empty = common.generate_summed_runs(
            empty_sample_ws_string=run_details.empty_runs,
            instrument=instrument)
        mantid.SaveNexus(Filename=run_details.summed_empty_file_path,
                         InputWorkspace=summed_empty)
        corrected_van_ws = common.subtract_summed_runs(
            ws_to_correct=input_van_ws, empty_sample=summed_empty)

    # Crop the tail end of the data on PEARL if they are not capturing slow neutrons
    corrected_van_ws = instrument._crop_raw_to_expected_tof_range(
        ws_to_crop=corrected_van_ws)

    if absorb:
        corrected_van_ws = instrument._apply_absorb_corrections(
            run_details=run_details, ws_to_correct=corrected_van_ws)
    else:
        # Assume that create_van only uses Vanadium runs
        mantid.SetSampleMaterial(InputWorkspace=corrected_van_ws,
                                 ChemicalFormula='V')

    mantid.ApplyDiffCal(InstrumentWorkspace=corrected_van_ws,
                        CalibrationFile=run_details.offset_file_path)
    aligned_ws = mantid.ConvertUnits(InputWorkspace=corrected_van_ws,
                                     Target="dSpacing")
    solid_angle = instrument.get_solid_angle_corrections(
        run_details.run_number, run_details)
    if solid_angle:
        aligned_ws = mantid.Divide(LHSWorkspace=aligned_ws,
                                   RHSWorkspace=solid_angle)
        mantid.DeleteWorkspace(solid_angle)
    focused_vanadium = mantid.DiffractionFocussing(
        InputWorkspace=aligned_ws,
        GroupingFileName=run_details.grouping_file_path)
    # convert back to TOF based on engineered detector positions
    mantid.ApplyDiffCal(InstrumentWorkspace=focused_vanadium,
                        ClearCalibration=True)
    focused_spectra = common.extract_ws_spectra(focused_vanadium)
    focused_spectra = instrument._crop_van_to_expected_tof_range(
        focused_spectra)

    d_spacing_group, tof_group = instrument._output_focused_ws(
        processed_spectra=focused_spectra, run_details=run_details)
    _create_vanadium_splines(focused_spectra, instrument, run_details)

    common.keep_single_ws_unit(d_spacing_group=d_spacing_group,
                               tof_group=tof_group,
                               unit_to_keep=instrument._get_unit_to_keep())

    common.remove_intermediate_workspace(corrected_van_ws)
    common.remove_intermediate_workspace(aligned_ws)
    common.remove_intermediate_workspace(focused_vanadium)
    common.remove_intermediate_workspace(focused_spectra)

    return d_spacing_group