def _define_corrections(self):
        """
        Defines all the corrections that are required
        """
        self._correction_workspaces = list()

        if self._container_ws != "":
            container_name = str(self._correction_wsg) + "_Container"
            self._container_ws = ms.ExtractSingleSpectrum(
                InputWorkspace=self._container_ws,
                OutputWorkspace=container_name,
                WorkspaceIndex=self._spec_idx)
            self._correction_workspaces.append(self._container_ws.name())

        # Do gamma correction
        if self.getProperty(
                "GammaBackground").value and not self._back_scattering:
            self._correction_workspaces.append(self._gamma_correction())

        # Do multiple scattering correction
        if self.getProperty("MultipleScattering").value:
            self._correction_workspaces.extend(self._ms_correction())

        # Output correction workspaces as a WorkspaceGroup
        if self._correction_wsg != "":
            ms.GroupWorkspaces(InputWorkspaces=self._correction_workspaces,
                               OutputWorkspace=self._correction_wsg)
            self.setProperty("CorrectionWorkspaces", self._correction_wsg)
Exemple #2
0
def split(focus):
    for i in range(Wish.NUM_PANELS):
        out = "{0}-{1}foc".format(focus[0:len(focus) - 3], i + 1)
        simple.ExtractSingleSpectrum(InputWorkspace=focus,
                                     OutputWorkspace=out,
                                     WorkspaceIndex=i)
        simple.DeleteWorkspace(focus)
Exemple #3
0
    def PyExec(self):
        # get parameter values
        wsString = self.getPropertyValue("InputWorkspace").strip()
        #internal values
        wsOutput = "__OutputWorkspace"
        wsTemp = "__Sort_temp"
        #get the workspace list
        wsNames = []
        for wsName in wsString.split(","):
            ws = mtd[wsName.strip()]
            if isinstance(ws, WorkspaceGroup):
                wsNames.extend(ws.getNames())
            else:
                wsNames.append(wsName)

        if wsOutput in mtd:
            ms.DeleteWorkspace(Workspace=wsOutput)
        sortStat = []
        for wsName in wsNames:
            if "qvectors" in wsName:
                #extract the spectrum
                ws = mtd[wsName.strip()]
                for s in range(0, ws.getNumberHistograms()):
                    y_s = ws.readY(s)
                    stuple = (self.GetXValue(y_s), s)
                    sortStat.append(stuple)
                sortStat.sort()
        if len(sortStat) == 0:
            raise RuntimeError("Cannot find file with qvectors, aborting")
        #sort spectra using norm of q
        for wsName in wsNames:
            ws = mtd[wsName.strip()]
            yUnit = ws.getAxis(1).getUnit().unitID()
            transposed = False
            if ws.getNumberHistograms() < len(sortStat):
                ms.Transpose(InputWorkspace=wsName, OutputWorkspace=wsName)
                transposed = True
            for norm, spec in sortStat:
                ms.ExtractSingleSpectrum(InputWorkspace=wsName, OutputWorkspace=wsTemp, WorkspaceIndex=spec)
                if wsOutput in mtd:
                    ms.ConjoinWorkspaces(InputWorkspace1=wsOutput,InputWorkspace2=wsTemp,CheckOverlapping=False)
                    if wsTemp in mtd:
                        ms.DeleteWorkspace(Workspace=wsTemp)
                else:
                    ms.RenameWorkspace(InputWorkspace=wsTemp, OutputWorkspace=wsOutput)

            #put norm as y value and copy units from input
            loopIndex = 0
            wsOut = mtd[wsOutput]
            for norm, spec in sortStat:
                wsOut.getSpectrum(loopIndex).setSpectrumNo(int(norm*1000))
                loopIndex = loopIndex + 1
            if len(yUnit) > 0:
                wsOut.getAxis(1).setUnit(yUnit)
            if transposed:
                ms.Transpose(InputWorkspace=wsOutput, OutputWorkspace=wsOutput)
            ms.RenameWorkspace(InputWorkspace=wsOutput, OutputWorkspace=wsName)
Exemple #4
0
def calc_calibration_without_vanadium(focused_ws, index, instrument):
    focus_spectrum = mantid.ExtractSingleSpectrum(InputWorkspace=focused_ws,
                                                  WorkspaceIndex=index)
    focus_spectrum = mantid.ConvertUnits(InputWorkspace=focus_spectrum,
                                         Target="TOF")
    focus_spectrum = mantid.Rebin(InputWorkspace=focus_spectrum,
                                  Params=instrument.tof_binning)
    focus_calibrated = mantid.CropWorkspace(InputWorkspace=focus_spectrum,
                                            XMin=0.1)
    return focus_calibrated
Exemple #5
0
def extract_single_spectrum(ws_to_process, spectrum_number_to_extract):
    """
    Extracts the monitor spectrum into its own individual workspaces from the input workspace
    based on the number of the spectrum given
    :param ws_to_process: The workspace to extract the monitor from
    :param spectrum_number_to_extract: The spectrum of the workspace to extract
    :return: The extracted monitor as a workspace
    """
    load_monitor_ws = mantid.ExtractSingleSpectrum(InputWorkspace=ws_to_process,
                                                   WorkspaceIndex=spectrum_number_to_extract)
    return load_monitor_ws
Exemple #6
0
def calc_calibration_with_vanadium(focused_ws, index, vanadium_ws, instrument):
    data_ws = mantid.ExtractSingleSpectrum(InputWorkspace=focused_ws,
                                           WorkspaceIndex=index)
    data_ws = mantid.ConvertUnits(InputWorkspace=data_ws, Target="TOF")
    data_ws = mantid.Rebin(InputWorkspace=data_ws,
                           Params=instrument._get_focus_tof_binning())

    data_processed = "van_processed" + str(
        index)  # Workaround for Mantid overwriting the WS in a loop

    mantid.Divide(LHSWorkspace=data_ws,
                  RHSWorkspace=vanadium_ws,
                  OutputWorkspace=data_processed)
    mantid.CropWorkspace(InputWorkspace=data_processed,
                         XMin=0.1,
                         OutputWorkspace=data_processed)
    mantid.Scale(InputWorkspace=data_processed,
                 Factor=10,
                 OutputWorkspace=data_processed)

    remove_intermediate_workspace(data_ws)

    return data_processed
    def PyExec(self):
        ms.ExtractSingleSpectrum(InputWorkspace=self._input_ws,
                                 OutputWorkspace=self._output_ws,
                                 WorkspaceIndex=self._spec_idx)

        # Performs corrections
        self._define_corrections()

        # The workspaces to fit for correction scale factors
        fit_corrections = [
            wks for wks in self._correction_workspaces
            if 'MultipleScattering' not in wks
        ]

        # Perform fitting of corrections
        fixed_params = {}

        fixed_gamma_factor = self.getProperty("GammaBackgroundScale").value
        if fixed_gamma_factor != 0.0 and not self._back_scattering:
            fixed_params['GammaBackground'] = fixed_gamma_factor

        fixed_container_scale = self.getProperty("ContainerScale").value
        if fixed_container_scale != 0.0:
            fixed_params['Container'] = fixed_container_scale

        params_ws = self._fit_corrections(fit_corrections,
                                          self._linear_fit_table,
                                          **fixed_params)
        self.setProperty("LinearFitResult", params_ws)

        # Scale gamma background
        if self.getProperty(
                "GammaBackground").value and not self._back_scattering:
            gamma_correct_ws = self._get_correction_workspace(
                'GammaBackground')[1]
            gamma_factor = self._get_correction_scale_factor(
                'GammaBackground', fit_corrections, params_ws)
            ms.Scale(InputWorkspace=gamma_correct_ws,
                     OutputWorkspace=gamma_correct_ws,
                     Factor=gamma_factor)

        # Scale multiple scattering
        if self.getProperty("MultipleScattering").value:
            # Use factor of total scattering as this includes single and multiple scattering
            multi_scatter_correct_ws = self._get_correction_workspace(
                'MultipleScattering')[1]
            total_scatter_correct_ws = self._get_correction_workspace(
                'TotalScattering')[1]
            total_scatter_factor = self._get_correction_scale_factor(
                'TotalScattering', fit_corrections, params_ws)
            ms.Scale(InputWorkspace=multi_scatter_correct_ws,
                     OutputWorkspace=multi_scatter_correct_ws,
                     Factor=total_scatter_factor)
            ms.Scale(InputWorkspace=total_scatter_correct_ws,
                     OutputWorkspace=total_scatter_correct_ws,
                     Factor=total_scatter_factor)

        # Scale by container
        if self._container_ws != "":
            container_correct_ws = self._get_correction_workspace(
                'Container')[1]
            container_factor = self._get_correction_scale_factor(
                'Container', fit_corrections, params_ws)
            ms.Scale(InputWorkspace=container_correct_ws,
                     OutputWorkspace=container_correct_ws,
                     Factor=container_factor)

        # Calculate and output corrected workspaces as a WorkspaceGroup
        if self._corrected_wsg != "":
            corrected_workspaces = [
                ws_name.replace(self._correction_wsg, self._corrected_wsg)
                for ws_name in self._correction_workspaces
            ]
            for corrected, correction in zip(corrected_workspaces,
                                             self._correction_workspaces):
                ms.Minus(LHSWorkspace=self._output_ws,
                         RHSWorkspace=correction,
                         OutputWorkspace=corrected)
            ms.GroupWorkspaces(InputWorkspaces=corrected_workspaces,
                               OutputWorkspace=self._corrected_wsg)
            self.setProperty("CorrectedWorkspaces", self._corrected_wsg)

        # Apply corrections
        for correction in self._correction_workspaces:
            if 'TotalScattering' not in correction:
                ms.Minus(LHSWorkspace=self._output_ws,
                         RHSWorkspace=correction,
                         OutputWorkspace=self._output_ws)

        self.setProperty("OutputWorkspace", self._output_ws)

        # Remove correction workspaces if they are no longer required
        if self._correction_wsg == "":
            for wksp in self._correction_workspaces:
                ms.DeleteWorkspace(wksp)
Exemple #8
0
    def process_incidentmon(self, number, extension, spline_terms=20):
        if type(number) is int:
            filename = self.get_file_name(number, extension)
            works = "monitor{}".format(number)
            shared_load_files(extension, filename, works, 4, 4, True)
            if extension[:9] == "nxs_event":
                temp = "w{}_monitors".format(number)
                works = "w{}_monitor4".format(number)
                simple.Rebin(InputWorkspace=temp,
                             OutputWorkspace=temp,
                             Params='6000,-0.00063,110000',
                             PreserveEvents=False)
                simple.ExtractSingleSpectrum(InputWorkspace=temp,
                                             OutputWorkspace=works,
                                             WorkspaceIndex=3)
        else:
            num_1, num_2 = split_run_string(number)
            works = "monitor{0}_{1}".format(num_1, num_2)
            filename = self.get_file_name(num_1, extension)
            works1 = "monitor{}".format(num_1)
            simple.LoadRaw(Filename=filename,
                           OutputWorkspace=works1,
                           SpectrumMin=4,
                           SpectrumMax=4,
                           LoadLogFiles="0")
            filename = self.get_file_name(num_2, extension)
            works2 = "monitor{}".format(num_2)
            simple.LoadRaw(Filename=filename,
                           OutputWorkspace=works2,
                           SpectrumMin=4,
                           SpectrumMax=4,
                           LoadLogFiles="0")
            simple.MergeRuns(InputWorkspaces=works1 + "," + works2,
                             OutputWorkspace=works)
            simple.DeleteWorkspace(works1)
            simple.DeleteWorkspace(works2)
        simple.ConvertUnits(InputWorkspace=works,
                            OutputWorkspace=works,
                            Target="Wavelength",
                            Emode="Elastic")
        lambda_min, lambda_max = Wish.LAMBDA_RANGE
        simple.CropWorkspace(InputWorkspace=works,
                             OutputWorkspace=works,
                             XMin=lambda_min,
                             XMax=lambda_max)
        ex_regions = np.array([[4.57, 4.76], [3.87, 4.12], [2.75, 2.91],
                               [2.24, 2.50]])
        simple.ConvertToDistribution(works)

        for reg in range(0, 4):
            simple.MaskBins(InputWorkspace=works,
                            OutputWorkspace=works,
                            XMin=ex_regions[reg, 0],
                            XMax=ex_regions[reg, 1])

        simple.SplineBackground(InputWorkspace=works,
                                OutputWorkspace=works,
                                WorkspaceIndex=0,
                                NCoeff=spline_terms)

        simple.SmoothData(InputWorkspace=works,
                          OutputWorkspace=works,
                          NPoints=40)
        simple.ConvertFromDistribution(works)
        return works