def chop_workspace(workspace, monitor_index): """ Chops the specified workspace if its maximum x-value exceeds its instrument parameter, 'Workflow.ChopDataIfGreaterThan'. :param workspace: The workspace to chop :param monitor_index: The index of the monitor spectra in the workspace. :return: A tuple of the list of output workspace names and a boolean specifying whether the workspace was chopped. """ from mantid.simpleapi import ChopData workspace_name = workspace.getName() # Chop data if required try: chop_threshold = workspace.getInstrument().getNumberParameter('Workflow.ChopDataIfGreaterThan')[0] x_max = workspace.readX(0)[-1] chopped_data = x_max > chop_threshold except IndexError: logger.warning("Chop threshold not found in instrument parameters") chopped_data = False logger.information('Workspace {0} need data chop: {1}'.format(workspace_name, str(chopped_data))) if chopped_data: ChopData(InputWorkspace=workspace, OutputWorkspace=workspace_name, MonitorWorkspaceIndex=monitor_index, IntegrationRangeLower=5000.0, IntegrationRangeUpper=10000.0, NChops=5) return mtd[workspace_name].getNames(), True else: return [workspace_name], False
def load_files(data_files, ipf_filename, spec_min, spec_max, sum_files=False, load_logs=True, load_opts=None): """ Loads a set of files and extracts just the spectra we care about (i.e. detector range and monitor). @param data_files List of data file names @param ipf_filename File path/name for the instrument parameter file to load @param spec_min Minimum spectra ID to load @param spec_max Maximum spectra ID to load @param sum_files Sum loaded files @param load_logs Load log files when loading runs @param load_opts Additional options to be passed to load algorithm @return List of loaded workspace names and flag indicating chopped data """ from mantid.simpleapi import (Load, LoadVesuvio, LoadParameterFile, ChopData, ExtractSingleSpectrum, CropWorkspace) if load_opts is None: load_opts = {} workspace_names = [] for filename in data_files: # The filename without path and extension will be the workspace name ws_name = os.path.splitext(os.path.basename(str(filename)))[0] logger.debug('Loading file %s as workspace %s' % (filename, ws_name)) if 'VESUVIO' in ipf_filename: # Load all spectra. They are cropped later LoadVesuvio(Filename=str(filename), OutputWorkspace=ws_name, SpectrumList='1-198', **load_opts) else: Load(Filename=filename, OutputWorkspace=ws_name, LoadLogFiles=load_logs, **load_opts) # Load the instrument parameters LoadParameterFile(Workspace=ws_name, Filename=ipf_filename) # Add the workspace to the list of workspaces workspace_names.append(ws_name) # Get the spectrum number for the monitor instrument = mtd[ws_name].getInstrument() monitor_index = int(instrument.getNumberParameter('Workflow.Monitor1-SpectrumNumber')[0]) logger.debug('Workspace %s monitor 1 spectrum number :%d' % (ws_name, monitor_index)) # Chop data if required try: chop_threshold = mtd[ws_name].getInstrument().getNumberParameter('Workflow.ChopDataIfGreaterThan')[0] x_max = mtd[ws_name].readX(0)[-1] chopped_data = x_max > chop_threshold except IndexError: chopped_data = False logger.information('Workspace {0} need data chop: {1}'.format(ws_name, str(chopped_data))) workspaces = [ws_name] if chopped_data: ChopData(InputWorkspace=ws_name, OutputWorkspace=ws_name, MonitorWorkspaceIndex=monitor_index, IntegrationRangeLower=5000.0, IntegrationRangeUpper=10000.0, NChops=5) workspaces = mtd[ws_name].getNames() for chop_ws_name in workspaces: # Get the monitor spectrum monitor_ws_name = chop_ws_name + '_mon' ExtractSingleSpectrum(InputWorkspace=chop_ws_name, OutputWorkspace=monitor_ws_name, WorkspaceIndex=monitor_index) # Crop to the detectors required chop_ws = mtd[chop_ws_name] CropWorkspace(InputWorkspace=chop_ws_name, OutputWorkspace=chop_ws_name, StartWorkspaceIndex=chop_ws.getIndexFromSpectrumNumber(int(spec_min)), EndWorkspaceIndex=chop_ws.getIndexFromSpectrumNumber(int(spec_max))) logger.information('Loaded workspace names: %s' % (str(workspace_names))) logger.information('Chopped data: %s' % (str(chopped_data))) # Sum files if needed if sum_files and len(data_files) > 1: if chopped_data: workspace_names = sum_chopped_runs(workspace_names) else: workspace_names = sum_regular_runs(workspace_names) logger.information('Summed workspace names: %s' % (str(workspace_names))) return workspace_names, chopped_data