def lowTResolution(model, simulated, resolution, convolved, expdata=None, costfile=None, **kwargs): """Convolve a simulated S(Q,E) with a resolution file Arguments: model: beamline model file (background, elastic line, convolution). simulated: Nexus file containing S(Q,E) from a simulation resolution: Nexus file containing the resolution. This will be used to produce a elastic line. convolved: Output Nexus file containing the convolution of the simulated S(Q,E) with the model beamline. expdata: Optional, experimental nexus file. If passed, output convolved will be binned as expdata. costfile: Optional, file to store cost. If passed, the cost of comparing convolved and expdata will be saved. [**kwargs]: extra options for the Mantid algorithms producing S(Q,E). For example: kwargs={'Fit':{'StartX'=-50.0, 'EndX'=50.0}, } Returns: Groupworkspace containing the convolved S(Q,E) """ from mantid.simpleapi import (LoadNexus, ScaleX, Fit, ExtractSingleSpectrum, RenameWorkspace, AppendSpectra, SaveNexus) from mantidhelper.algorithm import findopts from mantidhelper.fitalg import parse_results from fitalg import parse_results algs_opt=locals()['kwargs'] funcStr=open(model,'r').readline().strip() #read the model string wsr=LoadNexus(Filename=resolution,OutputWorkspace='resolutions') wse=ScaleX(InputWorkspace=wsr, OutputWorkspace='elastics',factor=-1) wss=LoadNexus(Filename=simulated,OutputWorkspace='simulateds') wsx=wss # wsx will be our reference to compare convolved in Fit algorithm if expdata: wse=LoadNexus(Filename=expdata,OutputWorkspace='expdatas') wsx=wse # the reference will be the experimental data wsc=None # output convolved spectra cost=0 for iw in range(wss.getNumberHistograms()): ExtractSingleSpectrum(wsr,OutputWorkspace='resolution',WorkspaceIndex=iw) ExtractSingleSpectrum(wse,OutputWorkspace='elastic',WorkspaceIndex=iw) ExtractSingleSpectrum(wss,OutputWorkspace='simulated',WorkspaceIndex=iw) ExtractSingleSpectrum(wsx,OutputWorkspace='reference',WorkspaceIndex=iw) r=Fit( funcStr, InputWorkspace='reference', CreateOutput='1', Output='fitted', MaxIterations=0, **findopts('Fit',algs_opt) ) parsed=parse_results(r) cost+=parsed['Cost'] RenameWorkspace(parsed['Calc'],OutputWorkspace='convolved') #ws=parse_results(r)['fitted_Workspace'] if not wsc: wsc='convolveds' RenameWorkspace(InputWorkspace='convolved',OutputWorkspace=wsc) else: AppendSpectra(InputWorkspace1=wsc, InputWorkspace2='convolved', OutputWorkspace=wsc) SaveNexus(InputWorkspace=wsc, Filename=convolved) if costfile: open(costfile,'w').write(str(cost)+' obj-fn\n') return cost
def genSQE(hdfname,nxsname,wsname=None,indexes=[],rebinQ=None,scale=1.0, **kwargs): """ Generate S(Q,E) Loads Sassena output (HDF5 files) and generates a Nexus file containing S(Q,E) in a Workspace2D. Options to LoadSasena and SassenaFFT algorithms are lumped into optional 'options' parameter Args: hsdfname: path to sassena output hdf5 files for the incoherent factors. If more than one, enclosed then in quotes and separate with space(s). The output S(Q,E) will be the Fourier transform of the summ of the incoherent factors. nxsname: path to output Nexus file [wsname]: root name for the GroupWorkspace created when Sassena output is loaded [rebinQ]: rebin in Q. Useful when reported experimental S(Q,E) was obtained integrating over different [Q-dQ,Q+dQ] ranges. Format is "Qmin Qwidth Qmax". [indexes]: save only spectra with indexes given by indexes list. If indexes is empty, all spectra are saved. [scale]: multipy the generated S(Q,E) by this scaling factor [**kwargs]: extra options for the Mantid algorithms producing S(Q,E). For example: kwargs={'LoadSassena':{'TimeUnit':0.1,}, 'SassenaFFT':{'Temp':290,}, 'NormaliseToUnity:{'RangeLower'=50.0,'RangeUpper':50.0}' } Returns: GroupWorkspace containing I(Q,t), S(Q,E), and Q-vectors Worskpaces, among others. For example: rooname_ |_rootname_qvectors |_rootname_fqt.Re |_rootname_fqt.Im |_rootname_sqw Raises: None """ from mantidhelper.algorithm import findopts from mantidhelper.workspace import prunespectra from os.path import basename,splitext wsname=wsname or splitext(basename(nxsname))[0] algs_opt=locals()['kwargs'] hdfs=hdfname.split() # list of sassena output files serving as input sassopt=findopts('LoadSassena',algs_opt).copy(); sassopt.update({'OutputWorkspace':wsname}) ws = sortQvectors( hdfs[0], sassopt) if len(hdfs)>1: # add remaining sassena output files for hdf in hdfs[1:]: ws1 = sortQvectors( hdf, findopts('LoadSassena',algs_opt) ) if mti.CheckWorkspacesMatch(Workspace1=wsname+'_qvectors',Workspace2=ws1.getName()+'_qvectors'): for wstype in ('_fq0','_fqt.Re','_fqt.Im'): mti.Plus(LHSWorkspace=wsname+wstype,RHSWorkspace=ws1.getName()+wstype,OutputWorkspace=wsname+wstype) else: print 'Workspaces do not match' if rebinQ: # rebin in Q space rebinQ=','.join(rebinQ.split()) #substitute separators, from space to comma mti.Rebin(InputWorkspace=wsname+'_fq0',Params=rebinQ,OutputWorkspace=wsname+'_fq0') for wstype in ('_fqt.Re','_fqt.Im'): mti.Transpose(InputWorkspace=wsname+wstype,OutputWorkspace=wsname+wstype) mti.Rebin(InputWorkspace=wsname+wstype,Params=rebinQ,OutputWorkspace=wsname+wstype) mti.Transpose(InputWorkspace=wsname+wstype,OutputWorkspace=wsname+wstype) mti.SassenaFFT(ws,**findopts('SassenaFFT',algs_opt)) wss=wsname+'_sqw' if 'NormaliseToUnity' in algs_opt.keys(): mti.ConvertToHistogram(InputWorkspace=wss,OutputWorkspace=wss) mti.NormaliseToUnity(InputWorkspace=wss,OutputWorkspace=wss,**findopts('NormaliseToUnity',algs_opt)) prunespectra(InputWorkspace=wss,indexes=indexes) # does nothing in indexes is empty if scale!=1.0: wss=mti.Scale(wss,Factor=scale,Operation='Multiply') mti.SaveNexus(InputWorkspace=wss, Filename=nxsname, **findopts('SaveNexus',algs_opt)) return ws