def _save_output(self): """ Save the output workspace to the user's default working directory """ from IndirectCommon import getDefaultWorkingDirectory workdir = getDefaultWorkingDirectory() file_path = os.path.join(workdir, self._output_workspace + '.nxs') SaveNexusProcessed(InputWorkspace=self._output_workspace, Filename=file_path) logger.information('Output file : ' + file_path)
def _save_output(self): """ Save the output workspace to the user's default working directory """ from IndirectCommon import getDefaultWorkingDirectory workdir = getDefaultWorkingDirectory() file_path = os.path.join(workdir, self._output_workspace + '.nxs') SaveNexusProcessed(InputWorkspace=self._output_workspace, Filename=file_path) if self._verbose: logger.notice('Output file : ' + file_path)
def _process_output(self, workspace): if self._save: from mantid.simpleapi import SaveNexusProcessed from IndirectCommon import getDefaultWorkingDirectory workdir = getDefaultWorkingDirectory() fit_path = os.path.join(workdir, workspace + '.nxs') SaveNexusProcessed(InputWorkspace=workspace, Filename=fit_path) logger.information('Fit file is ' + fit_path) if self._plot: from IndirectImport import import_mantidplot mtd_plot = import_mantidplot() mtd_plot.plotSpectrum(workspace, [0, 1, 2], True)
def PyExec(self): from IndirectCommon import CheckHistZero, CheckElimits, StartTime, EndTime, getDefaultWorkingDirectory sample_workspace = self.getPropertyValue('Sample') output_workspace = self.getPropertyValue('OutputWorkspace') factor = self.getProperty('Scale').value emin = self.getProperty('EnergyMin').value emax = self.getProperty('EnergyMax').value erange = [emin, emax] Verbose = self.getProperty('Verbose').value Plot = self.getProperty('Plot').value Save = self.getProperty('Save').value StartTime('SofQWMoments') num_spectra,num_w = CheckHistZero(sample_workspace) if Verbose: text = 'Sample %s has %d Q values & %d w values' % (sample_workspace, num_spectra, num_w) logger.notice(text) x = np.asarray(mtd[sample_workspace].readX(0)) CheckElimits(erange,x) samWS = '__temp_sqw_moments_cropped' CropWorkspace(InputWorkspace=sample_workspace, OutputWorkspace=samWS, XMin=erange[0], XMax=erange[1]) if Verbose: logger.notice('Energy range is %f to %f' % (erange[0], erange[1])) if factor > 0.0: Scale(InputWorkspace=samWS, OutputWorkspace=samWS, Factor=factor, Operation='Multiply') if Verbose: logger.notice('y(q,w) scaled by %f' % factor) #calculate delta x ConvertToPointData(InputWorkspace=samWS, OutputWorkspace=samWS) x = np.asarray(mtd[samWS].readX(0)) x_workspace = CreateWorkspace(OutputWorkspace="__temp_sqw_moments_x", DataX=x, DataY=x, UnitX="DeltaE") #calculate moments m0 = output_workspace + '_M0' m1 = output_workspace + '_M1' m2 = output_workspace + '_M2' m3 = output_workspace + '_M3' m4 = output_workspace + '_M4' Multiply(x_workspace, samWS, OutputWorkspace=m1) Multiply(x_workspace, m1, OutputWorkspace=m2) Multiply(x_workspace, m2, OutputWorkspace=m3) Multiply(x_workspace, m3, OutputWorkspace=m4) DeleteWorkspace(m3) ConvertToHistogram(InputWorkspace=samWS, OutputWorkspace=samWS) Integration(samWS, OutputWorkspace=m0) moments = [m1, m2, m4] for moment_ws in moments: ConvertToHistogram(InputWorkspace=moment_ws, OutputWorkspace=moment_ws) Integration(moment_ws, OutputWorkspace=moment_ws) Divide(moment_ws, m0, OutputWorkspace=moment_ws) DeleteWorkspace(samWS) DeleteWorkspace(x_workspace) #create output workspace extensions = ['_M0', '_M1', '_M2', '_M4'] for ext in extensions: ws_name = output_workspace+ext Transpose(InputWorkspace=ws_name, OutputWorkspace=ws_name) ConvertToHistogram(InputWorkspace=ws_name, OutputWorkspace=ws_name) ConvertUnits(InputWorkspace=ws_name, OutputWorkspace=ws_name, Target='MomentumTransfer', EMode='Indirect') CopyLogs(InputWorkspace=sample_workspace, OutputWorkspace=ws_name) AddSampleLog(Workspace=ws_name, LogName="energy_min", LogType="Number", LogText=str(emin)) AddSampleLog(Workspace=ws_name, LogName="energy_max", LogType="Number", LogText=str(emax)) AddSampleLog(Workspace=ws_name, LogName="scale_factor", LogType="Number", LogText=str(factor)) #group ouput workspace group_workspaces = ','.join([output_workspace+ext for ext in extensions]) GroupWorkspaces(InputWorkspaces=group_workspaces,OutputWorkspace=output_workspace) if Save: workdir = getDefaultWorkingDirectory() opath = os.path.join(workdir,output_workspace+'.nxs') SaveNexusProcessed(InputWorkspace=output_workspace, Filename=opath) if Verbose: logger.notice('Output file : ' + opath) if Plot: self._plot_moments(output_workspace) self.setProperty("OutputWorkspace", output_workspace) EndTime('SofQWMoments')
def PyExec(self): from IndirectCommon import CheckHistZero, CheckElimits, getDefaultWorkingDirectory workflow_prog = Progress(self, start=0.0, end=1.0, nreports=20) workflow_prog.report('Setting up algorithm') sample_workspace = self.getPropertyValue('Sample') output_workspace = self.getPropertyValue('OutputWorkspace') factor = self.getProperty('Scale').value emin = self.getProperty('EnergyMin').value emax = self.getProperty('EnergyMax').value erange = [emin, emax] Plot = self.getProperty('Plot').value Save = self.getProperty('Save').value workflow_prog.report('Validating input') num_spectra,num_w = CheckHistZero(sample_workspace) logger.information('Sample %s has %d Q values & %d w values' % (sample_workspace, num_spectra, num_w)) x_data = np.asarray(mtd[sample_workspace].readX(0)) CheckElimits(erange,x_data) workflow_prog.report('Cropping Workspace') samWS = '__temp_sqw_moments_cropped' CropWorkspace(InputWorkspace=sample_workspace, OutputWorkspace=samWS, XMin=erange[0], XMax=erange[1]) logger.information('Energy range is %f to %f' % (erange[0], erange[1])) if factor > 0.0: workflow_prog.report('Scaling Workspace by factor %f' % factor) Scale(InputWorkspace=samWS, OutputWorkspace=samWS, Factor=factor, Operation='Multiply') logger.information('y(q,w) scaled by %f' % factor) #calculate delta x workflow_prog.report('Converting to point data') ConvertToPointData(InputWorkspace=samWS, OutputWorkspace=samWS) x_data = np.asarray(mtd[samWS].readX(0)) workflow_prog.report('Creating temporary data workspace') x_workspace = CreateWorkspace(OutputWorkspace="__temp_sqw_moments_x", DataX=x_data, DataY=x_data, UnitX="DeltaE") #calculate moments moments_0 = output_workspace + '_M0' moments_1 = output_workspace + '_M1' moments_2 = output_workspace + '_M2' moments_3 = output_workspace + '_M3' moments_4 = output_workspace + '_M4' workflow_prog.report('Multiplying Workspaces by moments') Multiply(x_workspace, samWS, OutputWorkspace=moments_1) Multiply(x_workspace, moments_1, OutputWorkspace=moments_2) Multiply(x_workspace, moments_2, OutputWorkspace=moments_3) Multiply(x_workspace, moments_3, OutputWorkspace=moments_4) DeleteWorkspace(moments_3) workflow_prog.report('Converting to Histogram') ConvertToHistogram(InputWorkspace=samWS, OutputWorkspace=samWS) workflow_prog.report('Intergrating result') Integration(samWS, OutputWorkspace=moments_0) moments = [moments_1, moments_2, moments_4] for moment_ws in moments: workflow_prog.report('Processing workspace %s' % moment_ws) ConvertToHistogram(InputWorkspace=moment_ws, OutputWorkspace=moment_ws) Integration(moment_ws, OutputWorkspace=moment_ws) Divide(moment_ws, moments_0, OutputWorkspace=moment_ws) workflow_prog.report('Deleting Workspaces') DeleteWorkspace(samWS) DeleteWorkspace(x_workspace) #create output workspace extensions = ['_M0', '_M1', '_M2', '_M4'] for ext in extensions: ws_name = output_workspace+ext workflow_prog.report('Processing Workspace %s' % ext) Transpose(InputWorkspace=ws_name, OutputWorkspace=ws_name) ConvertToHistogram(InputWorkspace=ws_name, OutputWorkspace=ws_name) ConvertUnits(InputWorkspace=ws_name, OutputWorkspace=ws_name, Target='MomentumTransfer', EMode='Indirect') CopyLogs(InputWorkspace=sample_workspace, OutputWorkspace=ws_name) workflow_prog.report('Adding Sample logs to %s' % ws_name) AddSampleLog(Workspace=ws_name, LogName="energy_min", LogType="Number", LogText=str(emin)) AddSampleLog(Workspace=ws_name, LogName="energy_max", LogType="Number", LogText=str(emax)) AddSampleLog(Workspace=ws_name, LogName="scale_factor", LogType="Number", LogText=str(factor)) # Group output workspace workflow_prog.report('Grouping OutputWorkspace') group_workspaces = ','.join([output_workspace+ext for ext in extensions]) GroupWorkspaces(InputWorkspaces=group_workspaces, OutputWorkspace=output_workspace) if Save: workflow_prog.report('Saving Workspace') workdir = getDefaultWorkingDirectory() opath = os.path.join(workdir,output_workspace+'.nxs') SaveNexusProcessed(InputWorkspace=output_workspace, Filename=opath) logger.information('Output file : ' + opath) if Plot: workflow_prog.report('Plotting Workspace') self._plot_moments(output_workspace) self.setProperty("OutputWorkspace", output_workspace) workflow_prog.report('Algorithm complete')