66
77from pdb import set_trace as trace # uncomment only for debugging purposes
88import os
9+ import mantid .simpleapi as mti
910
1011sassexec = None
1112sassdb = None
@@ -125,6 +126,32 @@ def orderByQmodulus(filename,outfile=None):
125126 os .system ('/bin/mv %s %s' % (outfile ,filename ))
126127 return None
127128
129+
130+ def sortQvectors (hdfile , args ):
131+ """Sort rows by Qvector modulus
132+
133+ If python version < 2.7, then use Mantid algorithm
134+ SortByQVectors. Otherwise check if the rows are already ordered and
135+ use orderByQmodulus if needed.
136+
137+ Arguments:
138+ hdfile (string) HDF5 file
139+ args (dictionary) extra arguments for LoadSassena algorithm
140+
141+ Returns:
142+ ws1 (mantid group workspace) Workspace holding the contents of the HDF5 file
143+ """
144+ trace ()
145+ from sys import version_info # python version
146+ if version_info < (2 ,7 ):
147+ ws1 = mti .LoadSassena ( Filename = hdfile , ** args )
148+ mti .SortByQVectors (ws1 )
149+ else :
150+ if not isOrderedByQmodulus (hdfile ): orderByQmodulus (hdf )
151+ ws1 = mti .LoadSassena ( Filename = hdfile , ** args )
152+ return ws1
153+
154+
128155def calculateIQ (qlist , pdbfile ):
129156 """Calculate both the static coherent and static incoherent intermediate structure factors
130157 Arguments:
@@ -137,7 +164,6 @@ def calculateIQ(qlist, pdbfile):
137164 import sys
138165 from tempfile import mkdtemp
139166 from os .path import exists
140- from mantid .simpleapi import LoadSassena ,SortByQVectors ,mtd
141167
142168 inc_template = '''<root>
143169<sample>
@@ -280,13 +306,13 @@ def calculateIQ(qlist, pdbfile):
280306 os .system ('%s --config=%s/sassena_coh.xml' % (sassexec ,workdir ))
281307 addVersionStamp (os .path .join (workdir ,'fq_coh.h5' ),'1.4.1' )
282308
283- LoadSassena (Filename = os .path .join (workdir ,'fq_inc.h5' ),OutputWorkspace = 'inc' )
284- SortByQVectors (InputWorkspace = 'inc' )
285- LoadSassena (Filename = os .path .join (workdir ,'fq_coh.h5' ),OutputWorkspace = 'coh' )
286- SortByQVectors (InputWorkspace = 'coh' )
309+ mti . LoadSassena (Filename = os .path .join (workdir ,'fq_inc.h5' ),OutputWorkspace = 'inc' )
310+ mti . SortByQVectors (InputWorkspace = 'inc' )
311+ mti . LoadSassena (Filename = os .path .join (workdir ,'fq_coh.h5' ),OutputWorkspace = 'coh' )
312+ mti . SortByQVectors (InputWorkspace = 'coh' )
287313
288314 os .system ('/bin/rm -rf ' + workdir )
289- return mtd ['inc' ],mtd ['coh' ]
315+ return mti . mtd ['inc' ],mti . mtd ['coh' ]
290316
291317def genSQE (hdfname ,nxsname ,wsname = None ,indexes = [],rebinQ = None ,scale = 1.0 , ** kwargs ):
292318 """ Generate S(Q,E)
@@ -328,47 +354,39 @@ def genSQE(hdfname,nxsname,wsname=None,indexes=[],rebinQ=None,scale=1.0, **kwarg
328354 from mantidhelper .algorithm import findopts
329355 from mantidhelper .workspace import prunespectra
330356 from os .path import basename ,splitext
331- from mantid .simpleapi import LoadSassena ,SortByQVectors ,CheckWorkspacesMatch ,Plus ,SassenaFFT ,SaveNexus ,Scale
332357 wsname = wsname or splitext (basename (nxsname ))[0 ]
333358 algs_opt = locals ()['kwargs' ]
334359 hdfs = hdfname .split () # list of sassena output files serving as input
335- if not isOrderedByQmodulus (hdfs [0 ]):
336- orderByQmodulus (hdfs [0 ])
337- ws = LoadSassena (Filename = hdfs [0 ], OutputWorkspace = wsname , ** findopts ('LoadSassena' ,algs_opt )) # initialize the first
338- #SortByQVectors(ws)
360+ #trace()
361+ ws = sortQvectors ( hdfs [0 ], findopts ('LoadSassena' ,algs_opt ) )
339362
340363 if len (hdfs )> 1 : # add remaining sassena output files
341364 for hdf in hdfs [1 :]:
342- if not isOrderedByQmodulus (hdf ):
343- orderByQmodulus (hdf )
344- ws1 = LoadSassena (Filename = hdf , ** findopts ('LoadSassena' ,algs_opt ))
345- #SortByQVectors(ws1)
346- if CheckWorkspacesMatch (Workspace1 = wsname + '_qvectors' ,Workspace2 = ws1 .getName ()+ '_qvectors' ):
365+ ws1 = sortQvectors ( hdf , ** findopts ('LoadSassena' ,algs_opt ) )
366+ if mti .CheckWorkspacesMatch (Workspace1 = wsname + '_qvectors' ,Workspace2 = ws1 .getName ()+ '_qvectors' ):
347367 for wstype in ('_fq0' ,'_fqt.Re' ,'_fqt.Im' ):
348- Plus (LHSWorkspace = wsname + wstype ,RHSWorkspace = ws1 .getName ()+ wstype ,OutputWorkspace = wsname + wstype )
368+ mti . Plus (LHSWorkspace = wsname + wstype ,RHSWorkspace = ws1 .getName ()+ wstype ,OutputWorkspace = wsname + wstype )
349369 else :
350370 print 'Workspaces do not match'
351371
352372 if rebinQ : # rebin in Q space
353373 rebinQ = ',' .join (rebinQ .split ()) #substitute separators, from space to comma
354- from mantid .simpleapi import (Transpose , Rebin )
355- Rebin (InputWorkspace = wsname + '_fq0' ,Params = rebinQ ,OutputWorkspace = wsname + '_fq0' )
374+ mti .Rebin (InputWorkspace = wsname + '_fq0' ,Params = rebinQ ,OutputWorkspace = wsname + '_fq0' )
356375 for wstype in ('_fqt.Re' ,'_fqt.Im' ):
357- Transpose (InputWorkspace = wsname + wstype ,OutputWorkspace = wsname + wstype )
358- Rebin (InputWorkspace = wsname + wstype ,Params = rebinQ ,OutputWorkspace = wsname + wstype )
359- Transpose (InputWorkspace = wsname + wstype ,OutputWorkspace = wsname + wstype )
360- SassenaFFT (ws ,** findopts ('SassenaFFT' ,algs_opt ))
376+ mti . Transpose (InputWorkspace = wsname + wstype ,OutputWorkspace = wsname + wstype )
377+ mti . Rebin (InputWorkspace = wsname + wstype ,Params = rebinQ ,OutputWorkspace = wsname + wstype )
378+ mti . Transpose (InputWorkspace = wsname + wstype ,OutputWorkspace = wsname + wstype )
379+ mti . SassenaFFT (ws ,** findopts ('SassenaFFT' ,algs_opt ))
361380 wss = wsname + '_sqw'
362381
363382 if 'NormaliseToUnity' in algs_opt .keys ():
364- from mantid .simpleapi import (ConvertToHistogram , NormaliseToUnity )
365- ConvertToHistogram (InputWorkspace = wss ,OutputWorkspace = wss )
366- NormaliseToUnity (InputWorkspace = wss ,OutputWorkspace = wss ,** findopts ('NormaliseToUnity' ,algs_opt ))
383+ mti .ConvertToHistogram (InputWorkspace = wss ,OutputWorkspace = wss )
384+ mti .NormaliseToUnity (InputWorkspace = wss ,OutputWorkspace = wss ,** findopts ('NormaliseToUnity' ,algs_opt ))
367385
368386 prunespectra (InputWorkspace = wss ,indexes = indexes ) # does nothing in indexes is empty
369- if scale != 1.0 : wss = Scale (wss ,Factor = scale ,Operation = 'Multiply' )
387+ if scale != 1.0 : wss = mti . Scale (wss ,Factor = scale ,Operation = 'Multiply' )
370388
371- SaveNexus (InputWorkspace = wss , Filename = nxsname , ** findopts ('SaveNexus' ,algs_opt ))
389+ mti . SaveNexus (InputWorkspace = wss , Filename = nxsname , ** findopts ('SaveNexus' ,algs_opt ))
372390 #trace()
373391 return ws
374392
0 commit comments