def _get_temperature(self, ws_name):
        """
        Gets the sample temperature for a given workspace.

        @param ws_name Name of workspace
        @returns Temperature in Kelvin or None if not found
        """
        from IndirectCommon import getInstrRun

        instr, run_number = getInstrRun(ws_name)

        facility = config.getFacility()
        pad_num = facility.instrument(instr).zeroPadding(int(run_number))
        zero_padding = '0' * (pad_num - len(run_number))

        run_name = instr + zero_padding + run_number
        log_filename = run_name.upper() + '.log'

        run = mtd[ws_name].getRun()

        if self._sample_log_name in run:
            # Look for temperature in logs in workspace
            tmp = run[self._sample_log_name].value
            value_action = {
                'last_value': lambda x: x[len(x) - 1],
                'average': lambda x: x.mean()
            }
            temp = value_action[self._sample_log_value](tmp)
            logger.debug('Temperature %d K found for run: %s' %
                         (temp, run_name))
            return temp

        else:
            # Logs not in workspace, try loading from file
            logger.information(
                'Log parameter not found in workspace. Searching for log file.'
            )
            log_path = FileFinder.getFullPath(log_filename)

            if log_path != '':
                # Get temperature from log file
                LoadLog(Workspace=ws_name, Filename=log_path)
                run_logs = mtd[ws_name].getRun()
                if self._sample_log_name in run_logs:
                    tmp = run_logs[self._sample_log_name].value
                    temp = tmp[len(tmp) - 1]
                    logger.debug('Temperature %d K found for run: %s' %
                                 (temp, run_name))
                    return temp
                else:
                    logger.warning('Log entry %s for run %s not found' %
                                   (self._sample_log_name, run_name))
            else:
                logger.warning('Log file for run %s not found' % run_name)

        # Can't find log file
        logger.warning('No temperature found for run: %s' % run_name)
        return None
Example #2
0
    def _get_temperature(self, ws_name):
        """
        Gets the sample temperature for a given workspace.

        @param ws_name Name of workspace
        @returns Temperature in Kelvin or None if not found
        """
        from IndirectCommon import getInstrRun

        instr, run_number = getInstrRun(ws_name)

        facility = config.getFacility()
        pad_num = facility.instrument(instr).zeroPadding(int(run_number))
        zero_padding = '0' * (pad_num - len(run_number))

        run_name = instr + zero_padding + run_number
        log_filename = run_name.upper() + '.log'

        run = mtd[ws_name].getRun()

        if self._sample_log_name in run:
            # Look for temperature in logs in workspace
            tmp = run[self._sample_log_name].value
            value_action = {'last_value': lambda x: x[len(x)-1],
                            'average': lambda x: x.mean()
                           }
            temp = value_action[self._sample_log_value](tmp)
            logger.debug('Temperature %d K found for run: %s' % (temp, run_name))
            return temp

        else:
            # Logs not in workspace, try loading from file
            logger.information('Log parameter not found in workspace. Searching for log file.')
            log_path = FileFinder.getFullPath(log_filename)

            if log_path != '':
                # Get temperature from log file
                LoadLog(Workspace=ws_name, Filename=log_path)
                run_logs = mtd[ws_name].getRun()
                if self._sample_log_name in run_logs:
                    tmp = run_logs[self._sample_log_name].value
                    temp = tmp[len(tmp) - 1]
                    logger.debug('Temperature %d K found for run: %s' % (temp, run_name))
                    return temp
                else:
                    logger.warning('Log entry %s for run %s not found' % (self._sample_log_name, run_name))
            else:
                logger.warning('Log file for run %s not found' % run_name)

        # Can't find log file
        logger.warning('No temperature found for run: %s' % run_name)
        return None
    def _get_sample_units(self, workspace):
        """
        Gets the sample environment units for a given workspace.

        @param workspace The workspace
        @returns sample in given units or None if not found
        """
        from IndirectCommon import getInstrRun

        instr, run_number = getInstrRun(workspace.name())

        pad_num = config.getInstrument(instr).zeroPadding(int(run_number))
        zero_padding = '0' * (pad_num - len(run_number))

        run_name = instr + zero_padding + run_number
        log_filename = run_name.upper() + '.log'

        run = workspace.getRun()

        position_logs = ['position', 'samp_posn']
        if self._sample_log_name.lower() in position_logs:
            self._sample_log_name = _extract_sensor_name(
                self._sample_log_name, run, workspace.getInstrument())

        if self._sample_log_name in run:
            # Look for sample unit in logs in workspace
            if self._sample_log_value == 'last_value':
                sample = run[self._sample_log_name].value[-1]
            else:
                sample = run[self._sample_log_name].value.mean()

            unit = run[self._sample_log_name].units
        else:
            # Logs not in workspace, try loading from file
            logger.information(
                'Log parameter not found in workspace. Searching for log file.'
            )
            sample, unit = _extract_temperature_from_log(
                workspace, self._sample_log_name, log_filename, run_name)

        if sample is not None and unit is not None:
            logger.debug('{0} {1} found for run: {2}'.format(
                sample, unit, run_name))
        else:
            logger.warning(
                'No sample units found for run: {}'.format(run_name))

        if unit is not None and unit.isspace():
            unit = ""

        return sample, unit
    def _get_sample_units(self, workspace):
        """
        Gets the sample environment units for a given workspace.

        @param workspace The workspace
        @returns sample in given units or None if not found
        """
        from IndirectCommon import getInstrRun

        instr, run_number = getInstrRun(workspace.getName())

        instrument = config.getFacility().instrument(instr)
        pad_num = instrument.zeroPadding(int(run_number))
        zero_padding = '0' * (pad_num - len(run_number))

        run_name = instr + zero_padding + run_number
        log_filename = run_name.upper() + '.log'

        run = workspace.getRun()

        if self._sample_log_name.lower() == 'position':
            self._sample_log_name = _extract_sensor_name(self._sample_log_name, run, workspace.getInstrument())

        if self._sample_log_name in run:
            # Look for sample unit in logs in workspace
            if self._sample_log_value == 'last_value':
                sample = run[self._sample_log_name].value[-1]
            else:
                sample = run[self._sample_log_name].value.mean()

            unit = run[self._sample_log_name].units
        else:
            # Logs not in workspace, try loading from file
            logger.information('Log parameter not found in workspace. Searching for log file.')
            sample, unit = _extract_temperature_from_log(workspace, self._sample_log_name, log_filename, run_name)

        if sample is not None and unit is not None:
            logger.debug('{0} {1} found for run: {2}'.format(sample, unit, run_name))
        else:
            logger.warning('No sample units found for run: {}'.format(run_name))
        return sample, unit
    def PyExec(self):
        from IndirectCommon import getInstrRun

        # Do setup
        self._setup()

        logger.debug('in_ws:' + str(type(self._input_workspaces)))

        # Get input workspaces
        input_workspace_names = self._input_workspaces.getNames()

        # Lists of input and output workspaces
        q_workspaces = list()
        q2_workspaces = list()
        run_numbers = list()
        temperatures = list()

        # Perform the ElasticWindow algorithms
        for input_ws in input_workspace_names:
            logger.information('Running ElasticWindow for workspace: %s' %
                               input_ws)

            q_ws = '__' + input_ws + '_q'
            q2_ws = '__' + input_ws + '_q2'

            if self._background_range_start != Property.EMPTY_DBL and self._background_range_end != Property.EMPTY_DBL:
                ElasticWindow(
                    InputWorkspace=input_ws,
                    OutputInQ=q_ws,
                    OutputInQSquared=q2_ws,
                    IntegrationRangeStart=self._integration_range_start,
                    IntegrationRangeEnd=self._integration_range_end,
                    BackgroundRangeStart=self._background_range_start,
                    BackgroundRangeEnd=self._background_range_end)
            else:
                ElasticWindow(
                    InputWorkspace=input_ws,
                    OutputInQ=q_ws,
                    OutputInQSquared=q2_ws,
                    IntegrationRangeStart=self._integration_range_start,
                    IntegrationRangeEnd=self._integration_range_end)

            Logarithm(InputWorkspace=q2_ws, OutputWorkspace=q2_ws)

            q_workspaces.append(q_ws)
            q2_workspaces.append(q2_ws)

            # Get the run number
            run_no = getInstrRun(input_ws)[1]
            run_numbers.append(run_no)

            # Get the sample temperature
            temp = self._get_temperature(input_ws)
            if temp is not None:
                temperatures.append(temp)
            else:
                # No need to output a tmperature workspace if there are no temperatures
                self._elt_workspace = ''

        logger.information('Creating Q and Q^2 workspaces')

        if len(input_workspace_names) == 1:
            # Just rename single workspaces
            RenameWorkspace(InputWorkspace=q_workspaces[0],
                            OutputWorkspace=self._q_workspace)
            RenameWorkspace(InputWorkspace=q2_workspaces[0],
                            OutputWorkspace=self._q2_workspace)
        else:
            # Append the spectra of the first two workspaces
            AppendSpectra(InputWorkspace1=q_workspaces[0],
                          InputWorkspace2=q_workspaces[1],
                          OutputWorkspace=self._q_workspace)
            AppendSpectra(InputWorkspace1=q2_workspaces[0],
                          InputWorkspace2=q2_workspaces[1],
                          OutputWorkspace=self._q2_workspace)

            # Append to the spectra of each remaining workspace
            for idx in range(2, len(input_workspace_names)):
                AppendSpectra(InputWorkspace1=self._q_workspace,
                              InputWorkspace2=q_workspaces[idx],
                              OutputWorkspace=self._q_workspace)
                AppendSpectra(InputWorkspace1=self._q2_workspace,
                              InputWorkspace2=q2_workspaces[idx],
                              OutputWorkspace=self._q2_workspace)

            # Delete the output workspaces from the ElasticWindow algorithms
            for q_ws in q_workspaces:
                DeleteWorkspace(q_ws)
            for q2_ws in q2_workspaces:
                DeleteWorkspace(q2_ws)

        logger.information('Setting vertical axis units and values')

        # Set the verical axis units
        v_axis_is_temp = len(input_workspace_names) == len(temperatures)

        if v_axis_is_temp:
            logger.notice('Vertical axis is in temperature')
            unit = ('Temperature', 'K')
        else:
            logger.notice('Vertical axis is in run number')
            unit = ('Run No', 'last 3 digits')

        # Create a new vertical axis for the Q and Q**2 workspaces
        q_ws_axis = NumericAxis.create(len(input_workspace_names))
        q_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        q2_ws_axis = NumericAxis.create(len(input_workspace_names))
        q2_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        # Set the vertical axis values
        for idx in range(0, len(input_workspace_names)):
            if v_axis_is_temp:
                q_ws_axis.setValue(idx, float(temperatures[idx]))
                q2_ws_axis.setValue(idx, float(temperatures[idx]))
            else:
                q_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))
                q2_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))

        # Add the new vertical axis to each workspace
        mtd[self._q_workspace].replaceAxis(1, q_ws_axis)
        mtd[self._q2_workspace].replaceAxis(1, q2_ws_axis)

        # Process the ELF workspace
        if self._elf_workspace != '':
            logger.information('Creating ELF workspace')

            Transpose(InputWorkspace=self._q_workspace,
                      OutputWorkspace=self._elf_workspace)
            SortXAxis(InputWorkspace=self._elf_workspace,
                      OutputWorkspace=self._elf_workspace)

            self.setProperty('OutputELF', self._elf_workspace)

        # Do temperature normalisation
        if self._elt_workspace != '':
            logger.information('Creating ELT workspace')

            # If the ELT workspace was not already created then create it here,
            # otherwise just clone it
            if self._elf_workspace == '':
                Transpose(InputWorkspace=self._q_workspace,
                          OutputWorkspace=self._elt_workspace)
                SortXAxis(InputWorkspace=self._elt_workspace,
                          OutputWorkspace=self._elt_workspace)
            else:
                CloneWorkspace(InputWorkspace=self._elf_workspace,
                               OutputWorkspace=self._elt_workspace)

            _normalize_to_lowest_temp(self._elt_workspace)

            self.setProperty('OutputELT', self._elt_workspace)

        # Set the output workspace
        self.setProperty('OutputInQ', self._q_workspace)
        self.setProperty('OutputInQSquared', self._q2_workspace)
    def PyExec(self):
        from IndirectCommon import getInstrRun

        # Do setup
        self._setup()

        # Lists of input and output workspaces
        q_workspaces = list()
        q2_workspaces = list()
        run_numbers = list()
        sample_param = list()

        progress = Progress(self, 0.0, 0.05, 3)

        # Perform the ElasticWindow algorithms
        for input_ws in self._input_workspaces:
            logger.information('Running ElasticWindow for workspace: {}'.format(input_ws.getName()))
            progress.report('ElasticWindow for workspace: {}'.format(input_ws.getName()))

            q_workspace, q2_workspace = ElasticWindow(InputWorkspace=input_ws,
                                                      IntegrationRangeStart=self._integration_range_start,
                                                      IntegrationRangeEnd=self._integration_range_end,
                                                      BackgroundRangeStart=self._background_range_start,
                                                      BackgroundRangeEnd=self._background_range_end,
                                                      OutputInQ="__q", OutputInQSquared="__q2",
                                                      StoreInADS=False, EnableLogging=False)

            q2_workspace = Logarithm(InputWorkspace=q2_workspace, OutputWorkspace="__q2",
                                     StoreInADS=False, EnableLogging=False)

            q_workspaces.append(q_workspace)
            q2_workspaces.append(q2_workspace)

            # Get the run number
            run_no = getInstrRun(input_ws.getName())[1]
            run_numbers.append(run_no)

            # Get the sample environment unit
            sample, unit = self._get_sample_units(input_ws)
            if sample is not None:
                sample_param.append(sample)
            else:
                # No need to output a temperature workspace if there are no temperatures
                self._elt_ws_name = ''

        logger.information('Creating Q and Q^2 workspaces')
        progress.report('Creating Q workspaces')

        if self._input_size == 1:
            q_workspace = q_workspaces[0]
            q2_workspace = q2_workspaces[0]
        else:
            q_workspace = _append_all(q_workspaces)
            q2_workspace = _append_all(q2_workspaces)

        # Set the vertical axis units
        v_axis_is_sample = self._input_size == len(sample_param)

        if v_axis_is_sample:
            logger.notice('Vertical axis is in units of {}'.format(unit))
            unit = (self._sample_log_name, unit)

            def axis_value(index):
                return float(sample_param[index])
        else:
            logger.notice('Vertical axis is in run number')
            unit = ('Run No', 'last 3 digits')

            def axis_value(index):
                return float(run_numbers[index][-3:])

        # Create and set new vertical axis for the Q and Q**2 workspaces
        _set_numeric_y_axis(q_workspace, self._input_size, unit, axis_value)
        _set_numeric_y_axis(q2_workspace, self._input_size, unit, axis_value)

        progress.report('Creating ELF workspaces')

        # Process the ELF workspace
        if self._elf_ws_name != '':
            logger.information('Creating ELF workspace')
            elf_workspace = _sort_x_axis(_transpose(q_workspace))
            self.setProperty('OutputELF', elf_workspace)

        # Do temperature normalisation
        if self._elt_ws_name != '':
            logger.information('Creating ELT workspace')

            # If the ELF workspace was not created, create the ELT workspace
            # from the Q workspace. Else, clone the ELF workspace.
            if self._elf_ws_name == '':
                elt_workspace = _sort_x_axis(_transpose(q_workspace))
            else:
                elt_workspace = CloneWorkspace(InputWorkspace=elf_workspace, OutputWorkspace="__cloned",
                                               StoreInADS=False, EnableLogging=False)

            _normalize_by_index(elt_workspace, np.argmin(sample_param))

            self.setProperty('OutputELT', elt_workspace)

        # Set the output workspace
        self.setProperty('OutputInQ', q_workspace)
        self.setProperty('OutputInQSquared', q2_workspace)
    def PyExec(self):
        from IndirectCommon import getInstrRun

        # Do setup
        self._setup()

        # Lists of input and output workspaces
        q_workspaces = list()
        q2_workspaces = list()
        run_numbers = list()
        sample_param = list()

        progress = Progress(self, 0.0, 0.05, 3)

        # Perform the ElasticWindow algorithms
        for input_ws in self._input_workspaces:
            logger.information(
                'Running ElasticWindow for workspace: {}'.format(
                    input_ws.name()))
            progress.report('ElasticWindow for workspace: {}'.format(
                input_ws.name()))

            q_workspace, q2_workspace = ElasticWindow(
                InputWorkspace=input_ws,
                IntegrationRangeStart=self._integration_range_start,
                IntegrationRangeEnd=self._integration_range_end,
                BackgroundRangeStart=self._background_range_start,
                BackgroundRangeEnd=self._background_range_end,
                OutputInQ="__q",
                OutputInQSquared="__q2",
                StoreInADS=False,
                EnableLogging=False)

            q2_workspace = Logarithm(InputWorkspace=q2_workspace,
                                     OutputWorkspace="__q2",
                                     StoreInADS=False,
                                     EnableLogging=False)

            q_workspaces.append(q_workspace)
            q2_workspaces.append(q2_workspace)

            # Get the run number
            run_no = getInstrRun(input_ws.name())[1]
            run_numbers.append(run_no)

            # Get the sample environment unit
            sample, unit = self._get_sample_units(input_ws)
            if sample is not None:
                sample_param.append(sample)
            else:
                # No need to output a temperature workspace if there are no temperatures
                self._elt_ws_name = ''

        logger.information('Creating Q and Q^2 workspaces')
        progress.report('Creating Q workspaces')

        if self._input_size == 1:
            q_workspace = q_workspaces[0]
            q2_workspace = q2_workspaces[0]
        else:
            if not workspaces_have_same_size(
                    q_workspaces) or not workspaces_have_same_size(
                        q2_workspaces):
                raise RuntimeError(
                    'The ElasticWindow algorithm produced differently sized workspaces. Please check '
                    'the input files are compatible.')
            q_workspace = _append_all(q_workspaces)
            q2_workspace = _append_all(q2_workspaces)

        # Set the vertical axis units
        v_axis_is_sample = self._input_size == len(sample_param)

        if v_axis_is_sample:
            logger.notice('Vertical axis is in units of {}'.format(unit))
            unit = (self._sample_log_name, unit)

            def axis_value(index):
                return float(sample_param[index])
        else:
            logger.notice('Vertical axis is in run number')
            unit = ('Run No', 'last 3 digits')

            def axis_value(index):
                return float(run_numbers[index][-3:])

        # Create and set new vertical axis for the Q and Q**2 workspaces
        _set_numeric_y_axis(q_workspace, self._input_size, unit, axis_value)
        _set_numeric_y_axis(q2_workspace, self._input_size, unit, axis_value)

        progress.report('Creating ELF workspaces')

        # Process the ELF workspace
        if self._elf_ws_name != '':
            logger.information('Creating ELF workspace')
            elf_workspace = _sort_x_axis(_transpose(q_workspace))
            self.setProperty('OutputELF', elf_workspace)

        # Do temperature normalisation
        if self._elt_ws_name != '':
            logger.information('Creating ELT workspace')

            # If the ELF workspace was not created, create the ELT workspace
            # from the Q workspace. Else, clone the ELF workspace.
            if self._elf_ws_name == '':
                elt_workspace = _sort_x_axis(_transpose(q_workspace))
            else:
                elt_workspace = CloneWorkspace(InputWorkspace=elf_workspace,
                                               OutputWorkspace="__cloned",
                                               StoreInADS=False,
                                               EnableLogging=False)

            _normalize_by_index(elt_workspace, 0)

            self.setProperty('OutputELT', elt_workspace)

        # Set the output workspace
        self.setProperty('OutputInQ', q_workspace)
        self.setProperty('OutputInQSquared', q2_workspace)
Example #8
0
    def PyExec(self):
        from IndirectImport import import_mantidplot
        from IndirectCommon import getInstrRun

        # Do setup
        self._setup()

        logger.debug('in_ws:'+str(type(self._input_workspaces)))

        # Get input workspaces
        input_workspace_names = self._input_workspaces.getNames()

        # Lists of input and output workspaces
        q_workspaces = list()
        q2_workspaces = list()
        run_numbers = list()
        temperatures = list()

        # Perform the ElasticWindow algorithms
        for input_ws in input_workspace_names:
            logger.information('Running ElasticWindow for workspace: %s' % input_ws)

            q_ws = '__' + input_ws + '_q'
            q2_ws = '__' + input_ws + '_q2'

            if self._background_range_start != Property.EMPTY_DBL and self._background_range_end != Property.EMPTY_DBL:
                ElasticWindow(InputWorkspace=input_ws,
                              OutputInQ=q_ws,
                              OutputInQSquared=q2_ws,
                              IntegrationRangeStart=self._integration_range_start,
                              IntegrationRangeEnd=self._integration_range_end,
                              BackgroundRangeStart=self._background_range_start,
                              BackgroundRangeEnd=self._background_range_end)
            else:
                ElasticWindow(InputWorkspace=input_ws,
                              OutputInQ=q_ws,
                              OutputInQSquared=q2_ws,
                              IntegrationRangeStart=self._integration_range_start,
                              IntegrationRangeEnd=self._integration_range_end)

            Logarithm(InputWorkspace=q2_ws,
                      OutputWorkspace=q2_ws)

            q_workspaces.append(q_ws)
            q2_workspaces.append(q2_ws)

            # Get the run number
            run_no = getInstrRun(input_ws)[1]
            run_numbers.append(run_no)

            # Get the sample temperature
            temp = self._get_temperature(input_ws)
            if temp is not None:
                temperatures.append(temp)
            else:
                # No need to output a tmperature workspace if there are no temperatures
                self._elt_workspace = ''

        logger.information('Creating Q and Q^2 workspaces')

        if len(input_workspace_names) == 1:
            # Just rename single workspaces
            RenameWorkspace(InputWorkspace=q_workspaces[0],
                            OutputWorkspace=self._q_workspace)
            RenameWorkspace(InputWorkspace=q2_workspaces[0],
                            OutputWorkspace=self._q2_workspace)
        else:
            # Append the spectra of the first two workspaces
            AppendSpectra(InputWorkspace1=q_workspaces[0],
                          InputWorkspace2=q_workspaces[1],
                          OutputWorkspace=self._q_workspace)
            AppendSpectra(InputWorkspace1=q2_workspaces[0],
                          InputWorkspace2=q2_workspaces[1],
                          OutputWorkspace=self._q2_workspace)

            # Append to the spectra of each remaining workspace
            for idx in range(2, len(input_workspace_names)):
                AppendSpectra(InputWorkspace1=self._q_workspace,
                              InputWorkspace2=q_workspaces[idx],
                              OutputWorkspace=self._q_workspace)
                AppendSpectra(InputWorkspace1=self._q2_workspace,
                              InputWorkspace2=q2_workspaces[idx],
                              OutputWorkspace=self._q2_workspace)

            # Delete the output workspaces from the ElasticWindow algorithms
            for q_ws in q_workspaces:
                DeleteWorkspace(q_ws)
            for q2_ws in q2_workspaces:
                DeleteWorkspace(q2_ws)

        logger.information('Setting vertical axis units and values')

        # Set the verical axis units
        v_axis_is_temp = len(input_workspace_names) == len(temperatures)

        if v_axis_is_temp:
            logger.notice('Vertical axis is in temperature')
            unit = ('Temperature', 'K')
        else:
            logger.notice('Vertical axis is in run number')
            unit = ('Run No', 'last 3 digits')

        # Create a new vertical axis for the Q and Q**2 workspaces
        q_ws_axis = NumericAxis.create(len(input_workspace_names))
        q_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        q2_ws_axis = NumericAxis.create(len(input_workspace_names))
        q2_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        # Set the vertical axis values
        for idx in range(0, len(input_workspace_names)):
            if v_axis_is_temp:
                q_ws_axis.setValue(idx, float(temperatures[idx]))
                q2_ws_axis.setValue(idx, float(temperatures[idx]))
            else:
                q_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))
                q2_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))

        # Add the new vertical axis to each workspace
        mtd[self._q_workspace].replaceAxis(1, q_ws_axis)
        mtd[self._q2_workspace].replaceAxis(1, q2_ws_axis)

        # Process the ELF workspace
        if self._elf_workspace != '':
            logger.information('Creating ELF workspace')

            Transpose(InputWorkspace=self._q_workspace,
                      OutputWorkspace=self._elf_workspace)
            SortXAxis(InputWorkspace=self._elf_workspace,
                      OutputWorkspace=self._elf_workspace)

            self.setProperty('OutputELF', self._elf_workspace)

        # Do temperature normalisation
        if self._elt_workspace != '':
            logger.information('Creating ELT workspace')

            # If the ELT workspace was not already created then create it here,
            # otherwise just clone it
            if self._elf_workspace == '':
                Transpose(InputWorkspace=self._q_workspace,
                          OutputWorkspace=self._elt_workspace)
                SortXAxis(InputWorkspace=self._elt_workspace,
                          OutputWorkspace=self._elt_workspace)
            else:
                CloneWorkspace(InputWorkspace=self._elf_workspace,
                               OutputWorkspace=self._elt_workspace)

            _normalize_to_lowest_temp(self._elt_workspace)

            self.setProperty('OutputELT', self._elt_workspace)

        # Set the output workspace
        self.setProperty('OutputInQ', self._q_workspace)
        self.setProperty('OutputInQSquared', self._q2_workspace)

        # Plot spectra plots
        if self._plot:
            self._mtd_plot = import_mantidplot()

            self._plot_spectra(self._q_workspace)
            self._plot_spectra(self._q2_workspace)

            if self._elf_workspace != '':
                self._plot_spectra(self._elf_workspace)

            if self._elt_workspace != '':
                self._plot_spectra(self._elt_workspace)
Example #9
0
    def PyExec(self):
        from IndirectCommon import getInstrRun

        # Do setup
        self._setup()

        # Get input workspaces
        input_workspace_names = self._input_workspaces.getNames()

        # Lists of input and output workspaces
        q_workspaces = list()
        q2_workspaces = list()
        run_numbers = list()
        sample_param = list()

        progress = Progress(self, 0.0, 0.05, 3)

        # Perform the ElasticWindow algorithms
        for input_ws in input_workspace_names:

            logger.information('Running ElasticWindow for workspace: %s' % input_ws)
            progress.report('ElasticWindow for workspace: %s' % input_ws)

            q_ws = '__' + input_ws + '_q'
            q2_ws = '__' + input_ws + '_q2'

            elwin_alg = self.createChildAlgorithm("ElasticWindow", enableLogging=False)
            elwin_alg.setProperty("InputWorkspace", input_ws)
            elwin_alg.setProperty("IntegrationRangeStart", self._integration_range_start)
            elwin_alg.setProperty("IntegrationRangeEnd", self._integration_range_end)
            elwin_alg.setProperty("OutputInQ", q_ws)
            elwin_alg.setProperty("OutputInQSquared", q2_ws)

            if self._background_range_start != Property.EMPTY_DBL and self._background_range_end != Property.EMPTY_DBL:
                elwin_alg.setProperty("BackgroundRangeStart", self._background_range_start)
                elwin_alg.setProperty("BackgroundRangeEnd", self._background_range_end)

            elwin_alg.execute()

            log_alg = self.createChildAlgorithm("Logarithm", enableLogging=False)
            log_alg.setProperty("InputWorkspace", elwin_alg.getProperty("OutputInQSquared").value)
            log_alg.setProperty("OutputWorkspace", q2_ws)
            log_alg.execute()

            q_workspaces.append(elwin_alg.getProperty("OutputInQ").value)
            q2_workspaces.append(log_alg.getProperty("OutputWorkspace").value)

            # Get the run number
            run_no = getInstrRun(input_ws)[1]
            run_numbers.append(run_no)

            # Get the sample environment unit
            sample, unit = self._get_sample_units(input_ws)
            if sample is not None:
                sample_param.append(sample)
            else:
                # No need to output a temperature workspace if there are no temperatures
                self._elt_ws_name = ''

        logger.information('Creating Q and Q^2 workspaces')
        progress.report('Creating Q workspaces')

        if len(input_workspace_names) == 1:
            # Just rename single workspaces
            self._q_workspace = elwin_alg.getProperty("OutputInQ").value
            self._q2_workspace = log_alg.getProperty("OutputWorkspace").value
        else:
            # Append the spectra of the first two workspaces
            append_alg = self.createChildAlgorithm("AppendSpectra", enableLogging=False)
            append_alg.setProperty("InputWorkspace1", q_workspaces[0])
            append_alg.setProperty("InputWorkspace2", q_workspaces[1])
            append_alg.setProperty("OutputWorkspace", self._q_workspace)
            append_alg.execute()
            self._q_workspace = append_alg.getProperty("OutputWorkspace").value
            append_alg.setProperty("InputWorkspace1", q2_workspaces[0])
            append_alg.setProperty("InputWorkspace2", q2_workspaces[1])
            append_alg.setProperty("OutputWorkspace", self._q2_workspace)
            append_alg.execute()
            self._q2_workspace = append_alg.getProperty("OutputWorkspace").value

            # Append to the spectra of each remaining workspace
            for idx in range(2, len(input_workspace_names)):
                append_alg.setProperty("InputWorkspace1", self._q_workspace)
                append_alg.setProperty("InputWorkspace2", q_workspaces[idx])
                append_alg.setProperty("OutputWorkspace", self._q_workspace)
                append_alg.execute()
                self._q_workspace = append_alg.getProperty("OutputWorkspace").value
                append_alg.setProperty("InputWorkspace1", self._q2_workspace)
                append_alg.setProperty("InputWorkspace2", q2_workspaces[idx])
                append_alg.setProperty("OutputWorkspace", self._q2_workspace)
                append_alg.execute()
                self._q2_workspace = append_alg.getProperty("OutputWorkspace").value

        # Set the vertical axis units
        v_axis_is_sample = len(input_workspace_names) == len(sample_param)

        if v_axis_is_sample:
            logger.notice('Vertical axis is in units of %s' % unit)
            unit = (self._sample_log_name, unit)
        else:
            logger.notice('Vertical axis is in run number')
            unit = ('Run No', 'last 3 digits')

        # Create a new vertical axis for the Q and Q**2 workspaces
        q_ws_axis = NumericAxis.create(len(input_workspace_names))
        q_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        q2_ws_axis = NumericAxis.create(len(input_workspace_names))
        q2_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        # Set the vertical axis values
        for idx in range(0, len(input_workspace_names)):
            if v_axis_is_sample:
                q_ws_axis.setValue(idx, float(sample_param[idx]))
                q2_ws_axis.setValue(idx, float(sample_param[idx]))
            else:
                q_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))
                q2_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))

        # Add the new vertical axis to each workspace
        self._q_workspace.replaceAxis(1, q_ws_axis)
        self._q2_workspace.replaceAxis(1, q2_ws_axis)

        progress.report('Creating ELF workspaces')
        transpose_alg = self.createChildAlgorithm("Transpose", enableLogging=False)
        sort_alg = self.createChildAlgorithm("SortXAxis", enableLogging=False)
        # Process the ELF workspace
        if self._elf_ws_name != '':
            logger.information('Creating ELF workspace')
            transpose_alg.setProperty("InputWorkspace", self._q_workspace)
            transpose_alg.setProperty("OutputWorkspace", self._elf_ws_name)
            transpose_alg.execute()

            sort_alg.setProperty("InputWorkspace", transpose_alg.getProperty("OutputWorkspace").value)
            sort_alg.setProperty("OutputWorkspace", self._elf_ws_name)
            sort_alg.execute()

            self.setProperty('OutputELF', sort_alg.getProperty("OutputWorkspace").value)

        # Do temperature normalisation
        if self._elt_ws_name != '':
            logger.information('Creating ELT workspace')

            # If the ELF workspace was not created, create the ELT workspace
            # from the Q workspace. Else, clone the ELF workspace.
            if self._elf_ws_name == '':
                transpose_alg.setProperty("InputWorkspace", self._q_workspace)
                transpose_alg.setProperty("OutputWorkspace", self._elt_ws_name)
                transpose_alg.execute()
                sort_alg.setProperty("InputWorkspace", self._elt_ws_name)
                sort_alg.setProperty("OutputWorkspace", self._elt_ws_name)
                sort_alg.execute()
                elt_workspace = sort_alg.getProperty("OutputWorkspace").value
            else:
                clone_alg = self.createChildAlgorithm("CloneWorkspace", enableLogging=False)
                clone_alg.setProperty("InputWorkspace", self.getProperty("OutputELF").value)
                clone_alg.setProperty("OutputWorkspace", self._elt_ws_name)
                clone_alg.execute()
                elt_workspace = clone_alg.getProperty("OutputWorkspace").value

            _normalize_by_index(elt_workspace, np.argmin(sample_param))

            self.setProperty('OutputELT', elt_workspace)

        # Set the output workspace
        self.setProperty('OutputInQ', self._q_workspace)
        self.setProperty('OutputInQSquared', self._q2_workspace)
Example #10
0
    def _get_sample_units(self, ws_name):
        """
        Gets the sample environment units for a given workspace.

        @param ws_name Name of workspace
        @returns sample in given units or None if not found
        """
        from IndirectCommon import getInstrRun

        instr, run_number = getInstrRun(ws_name)

        facility = config.getFacility()
        pad_num = facility.instrument(instr).zeroPadding(int(run_number))
        zero_padding = '0' * (pad_num - len(run_number))

        run_name = instr + zero_padding + run_number
        log_filename = run_name.upper() + '.log'

        run = mtd[ws_name].getRun()

        if self._sample_log_name == 'Position':
            # Look for sample changer position in logs in workspace
            if self._sample_log_name in run:
                tmp = run[self._sample_log_name].value
                value_action = {'last_value': lambda x: x[-1],
                                'average': lambda x: x.mean()}
                position = value_action['last_value'](tmp)
                if position == 0:
                    self._sample_log_name = 'Bot_Can_Top'
                if position == 1:
                    self._sample_log_name = 'Middle_Can_Top'
                if position == 2:
                    self._sample_log_name = 'Top_Can_Top'
            else:
                logger.information('Position not found in workspace.')

        if self._sample_log_name in run:
            # Look for sample unit in logs in workspace
            tmp = run[self._sample_log_name].value
            value_action = {'last_value': lambda x: x[-1],
                            'average': lambda x: x.mean()}
            sample = value_action[self._sample_log_value](tmp)
            unit = run[self._sample_log_name].units
            logger.information('%d %s found for run: %s' % (sample, unit, run_name))
            return sample, unit

        else:
            # Logs not in workspace, try loading from file
            logger.information('Log parameter not found in workspace. Searching for log file.')
            log_path = FileFinder.getFullPath(log_filename)

            if log_path != '':
                # Get temperature from log file
                LoadLog(Workspace=ws_name, Filename=log_path)
                run_logs = mtd[ws_name].getRun()
                if self._sample_log_name in run_logs:
                    tmp = run_logs[self._sample_log_name].value
                    sample = tmp[len(tmp) - 1]
                    unit = run[self._sample_log_name].units
                    logger.debug('%d %s found for run: %s' % (sample, unit, run_name))
                    return sample, unit
                else:
                    logger.warning('Log entry %s for run %s not found' % (self._sample_log_name, run_name))
            else:
                logger.warning('Log file for run %s not found' % run_name)

        # Can't find log file
        logger.warning('No sample units found for run: %s' % run_name)
        return None, None
Example #11
0
    def _get_sample_units(self, ws_name):
        """
        Gets the sample environment units for a given workspace.

        @param ws_name Name of workspace
        @returns sample in given units or None if not found
        """
        from IndirectCommon import getInstrRun

        instr, run_number = getInstrRun(ws_name)

        facility = config.getFacility()
        pad_num = facility.instrument(instr).zeroPadding(int(run_number))
        zero_padding = '0' * (pad_num - len(run_number))

        run_name = instr + zero_padding + run_number
        log_filename = run_name.upper() + '.log'

        run = mtd[ws_name].getRun()

        if self._sample_log_name == 'Position':
            # Look for sample changer position in logs in workspace
            if self._sample_log_name in run:
                tmp = run[self._sample_log_name].value
                value_action = {
                    'last_value': lambda x: x[-1],
                    'average': lambda x: x.mean()
                }
                position = value_action['last_value'](tmp)
                if position == 0:
                    self._sample_log_name = 'Bot_Can_Top'
                if position == 1:
                    self._sample_log_name = 'Middle_Can_Top'
                if position == 2:
                    self._sample_log_name = 'Top_Can_Top'
            else:
                logger.information('Position not found in workspace.')

        if self._sample_log_name in run:
            # Look for sample unit in logs in workspace
            tmp = run[self._sample_log_name].value
            value_action = {
                'last_value': lambda x: x[-1],
                'average': lambda x: x.mean()
            }
            sample = value_action[self._sample_log_value](tmp)
            unit = run[self._sample_log_name].units
            logger.information('%d %s found for run: %s' %
                               (sample, unit, run_name))
            return sample, unit

        else:
            # Logs not in workspace, try loading from file
            logger.information(
                'Log parameter not found in workspace. Searching for log file.'
            )
            log_path = FileFinder.getFullPath(log_filename)

            if log_path != '':
                # Get temperature from log file
                LoadLog(Workspace=ws_name, Filename=log_path)
                run_logs = mtd[ws_name].getRun()
                if self._sample_log_name in run_logs:
                    tmp = run_logs[self._sample_log_name].value
                    sample = tmp[len(tmp) - 1]
                    unit = run[self._sample_log_name].units
                    logger.debug('%d %s found for run: %s' %
                                 (sample, unit, run_name))
                    return sample, unit
                else:
                    logger.warning('Log entry %s for run %s not found' %
                                   (self._sample_log_name, run_name))
            else:
                logger.warning('Log file for run %s not found' % run_name)

        # Can't find log file
        logger.warning('No sample units found for run: %s' % run_name)
        return None, None
Example #12
0
    def PyExec(self):
        from IndirectCommon import getInstrRun

        # Do setup
        self._setup()

        # Get input workspaces
        input_workspace_names = self._input_workspaces.getNames()

        # Lists of input and output workspaces
        q_workspaces = list()
        q2_workspaces = list()
        run_numbers = list()
        sample_param = list()

        progress = Progress(self, 0.0, 0.05, 3)

        # Perform the ElasticWindow algorithms
        for input_ws in input_workspace_names:

            logger.information('Running ElasticWindow for workspace: %s' %
                               input_ws)
            progress.report('ElasticWindow for workspace: %s' % input_ws)

            q_ws = '__' + input_ws + '_q'
            q2_ws = '__' + input_ws + '_q2'

            elwin_alg = self.createChildAlgorithm("ElasticWindow",
                                                  enableLogging=False)
            elwin_alg.setProperty("InputWorkspace", input_ws)
            elwin_alg.setProperty("IntegrationRangeStart",
                                  self._integration_range_start)
            elwin_alg.setProperty("IntegrationRangeEnd",
                                  self._integration_range_end)
            elwin_alg.setProperty("OutputInQ", q_ws)
            elwin_alg.setProperty("OutputInQSquared", q2_ws)

            if self._background_range_start != Property.EMPTY_DBL and self._background_range_end != Property.EMPTY_DBL:
                elwin_alg.setProperty("BackgroundRangeStart",
                                      self._background_range_start)
                elwin_alg.setProperty("BackgroundRangeEnd",
                                      self._background_range_end)

            elwin_alg.execute()

            log_alg = self.createChildAlgorithm("Logarithm",
                                                enableLogging=False)
            log_alg.setProperty(
                "InputWorkspace",
                elwin_alg.getProperty("OutputInQSquared").value)
            log_alg.setProperty("OutputWorkspace", q2_ws)
            log_alg.execute()

            q_workspaces.append(elwin_alg.getProperty("OutputInQ").value)
            q2_workspaces.append(log_alg.getProperty("OutputWorkspace").value)

            # Get the run number
            run_no = getInstrRun(input_ws)[1]
            run_numbers.append(run_no)

            # Get the sample environment unit
            sample, unit = self._get_sample_units(input_ws)
            if sample is not None:
                sample_param.append(sample)
            else:
                # No need to output a temperature workspace if there are no temperatures
                self._elt_ws_name = ''

        logger.information('Creating Q and Q^2 workspaces')
        progress.report('Creating Q workspaces')

        if len(input_workspace_names) == 1:
            # Just rename single workspaces
            self._q_workspace = elwin_alg.getProperty("OutputInQ").value
            self._q2_workspace = log_alg.getProperty("OutputWorkspace").value
        else:
            # Append the spectra of the first two workspaces
            append_alg = self.createChildAlgorithm("AppendSpectra",
                                                   enableLogging=False)
            append_alg.setProperty("InputWorkspace1", q_workspaces[0])
            append_alg.setProperty("InputWorkspace2", q_workspaces[1])
            append_alg.setProperty("OutputWorkspace", self._q_workspace)
            append_alg.execute()
            self._q_workspace = append_alg.getProperty("OutputWorkspace").value
            append_alg.setProperty("InputWorkspace1", q2_workspaces[0])
            append_alg.setProperty("InputWorkspace2", q2_workspaces[1])
            append_alg.setProperty("OutputWorkspace", self._q2_workspace)
            append_alg.execute()
            self._q2_workspace = append_alg.getProperty(
                "OutputWorkspace").value

            # Append to the spectra of each remaining workspace
            for idx in range(2, len(input_workspace_names)):
                append_alg.setProperty("InputWorkspace1", self._q_workspace)
                append_alg.setProperty("InputWorkspace2", q_workspaces[idx])
                append_alg.setProperty("OutputWorkspace", self._q_workspace)
                append_alg.execute()
                self._q_workspace = append_alg.getProperty(
                    "OutputWorkspace").value
                append_alg.setProperty("InputWorkspace1", self._q2_workspace)
                append_alg.setProperty("InputWorkspace2", q2_workspaces[idx])
                append_alg.setProperty("OutputWorkspace", self._q2_workspace)
                append_alg.execute()
                self._q2_workspace = append_alg.getProperty(
                    "OutputWorkspace").value

        # Set the vertical axis units
        v_axis_is_sample = len(input_workspace_names) == len(sample_param)

        if v_axis_is_sample:
            logger.notice('Vertical axis is in units of %s' % unit)
            unit = (self._sample_log_name, unit)
        else:
            logger.notice('Vertical axis is in run number')
            unit = ('Run No', 'last 3 digits')

        # Create a new vertical axis for the Q and Q**2 workspaces
        q_ws_axis = NumericAxis.create(len(input_workspace_names))
        q_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        q2_ws_axis = NumericAxis.create(len(input_workspace_names))
        q2_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])

        # Set the vertical axis values
        for idx in range(0, len(input_workspace_names)):
            if v_axis_is_sample:
                q_ws_axis.setValue(idx, float(sample_param[idx]))
                q2_ws_axis.setValue(idx, float(sample_param[idx]))
            else:
                q_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))
                q2_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))

        # Add the new vertical axis to each workspace
        self._q_workspace.replaceAxis(1, q_ws_axis)
        self._q2_workspace.replaceAxis(1, q2_ws_axis)

        progress.report('Creating ELF workspaces')
        transpose_alg = self.createChildAlgorithm("Transpose",
                                                  enableLogging=False)
        sort_alg = self.createChildAlgorithm("SortXAxis", enableLogging=False)
        # Process the ELF workspace
        if self._elf_ws_name != '':
            logger.information('Creating ELF workspace')
            transpose_alg.setProperty("InputWorkspace", self._q_workspace)
            transpose_alg.setProperty("OutputWorkspace", self._elf_ws_name)
            transpose_alg.execute()

            sort_alg.setProperty(
                "InputWorkspace",
                transpose_alg.getProperty("OutputWorkspace").value)
            sort_alg.setProperty("OutputWorkspace", self._elf_ws_name)
            sort_alg.execute()

            self.setProperty('OutputELF',
                             sort_alg.getProperty("OutputWorkspace").value)

        # Do temperature normalisation
        if self._elt_ws_name != '':
            logger.information('Creating ELT workspace')

            # If the ELF workspace was not created, create the ELT workspace
            # from the Q workspace. Else, clone the ELF workspace.
            if self._elf_ws_name == '':
                transpose_alg.setProperty("InputWorkspace", self._q_workspace)
                transpose_alg.setProperty("OutputWorkspace", self._elt_ws_name)
                transpose_alg.execute()
                sort_alg.setProperty("InputWorkspace", self._elt_ws_name)
                sort_alg.setProperty("OutputWorkspace", self._elt_ws_name)
                sort_alg.execute()
                elt_workspace = sort_alg.getProperty("OutputWorkspace").value
            else:
                clone_alg = self.createChildAlgorithm("CloneWorkspace",
                                                      enableLogging=False)
                clone_alg.setProperty("InputWorkspace",
                                      self.getProperty("OutputELF").value)
                clone_alg.setProperty("OutputWorkspace", self._elt_ws_name)
                clone_alg.execute()
                elt_workspace = clone_alg.getProperty("OutputWorkspace").value

            _normalize_by_index(elt_workspace, np.argmin(sample_param))

            self.setProperty('OutputELT', elt_workspace)

        # Set the output workspace
        self.setProperty('OutputInQ', self._q_workspace)
        self.setProperty('OutputInQSquared', self._q2_workspace)