Skip to content

Commit

Permalink
Refs #4867 - Additions to IndirectCommon.py
Browse files Browse the repository at this point in the history
* Some convenience methods.
* Much simpler reworking of getEFixed.
* Changes to support new Python API.
* GetWSangles and GetThetaQ.
  • Loading branch information
PeterParker committed Apr 4, 2012
1 parent 51cbfc3 commit 53dda17
Showing 1 changed file with 59 additions and 19 deletions.
78 changes: 59 additions & 19 deletions Code/Mantid/scripts/Inelastic/IndirectCommon.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
from mantidsimple import *
import os.path
import math
from mantid import config, logger
import os.path, math, datetime

def StartTime(prog):
logger.notice('----------')
message = 'Program ' + prog +' started @ ' + str(datetime.datetime.now())
logger.notice(message)

def EndTime(prog):
message = 'Program ' + prog +' ended @ ' + str(datetime.datetime.now())
logger.notice(message)
logger.notice('----------')

def loadInst(instrument):
ws = '__empty_' + instrument
if (mtd[ws] == None):
idf_dir = config['instrumentDefinition.directory']
idf = idf_dir + instrument + '_Definition.xml'
LoadEmptyInstrument(Filename=idf, OutputWorkspace=ws)

def loadNexus(filename):
'''Loads a Nexus file into a workspace with the name based on the
filename. Convenience function for not having to play around with paths
in every function.'''
name = os.path.splitext( os.path.split(filename)[1] )[0]
LoadNexus(filename, name)
LoadNexus(Filename=filename, OutputWorkspace=name)
return name

def getWSprefix(workspace):
Expand All @@ -29,14 +46,8 @@ def getWSprefix(workspace):
return prefix

def getEfixed(workspace, detIndex=0):
det = mtd[workspace].getDetector(detIndex)
try:
efixed = det.getNumberParameter('Efixed')[0]
except AttributeError:
ids = det.getDetectorIDs()
det = mtd[workspace].getInstrument().getDetector(ids[0])
efixed = det.getNumberParameter('Efixed')[0]
return efixed
inst = mtd[workspace].getInstrument()
return inst.getNumberParameter("efixed-val")[0]

def getRunTitle(workspace):
ws = mtd[workspace]
Expand All @@ -49,6 +60,12 @@ def getRunTitle(workspace):
title = isn + runNo + '-' + title
return title

def getDetectorTwoTheta(detector, samplePos, beamPos): #fix 'cos getTwoTheta is incorrectly gettwoTheta in new API
if hasattr(detector, 'getTwotheta'):
return detector.getTwotheta(samplePos, beamPos)
else:
return detector.getTwoTheta(samplePos, beamPos)

def createQaxis(inputWS):
result = []
ws = mtd[inputWS]
Expand All @@ -60,7 +77,7 @@ def createQaxis(inputWS):
for i in range(0,nHist):
efixed = getEfixed(inputWS, i)
detector = ws.getDetector(i)
theta = detector.getTwoTheta(samplePos, beamPos) / 2
theta = getDetectorTwoTheta(detector, samplePos, beamPos) / 2
lamda = math.sqrt(81.787/efixed)
q = 4 * math.pi * math.sin(theta) / lamda
result.append(q)
Expand All @@ -79,10 +96,33 @@ def createQaxis(inputWS):
result.append(float(axis.label(i)))
return result

def loadInst(instrument):
ws = '__empty_' + instrument
if (mtd[ws] == None):
idf_dir = mantid.getConfigProperty('instrumentDefinition.directory')
idf = idf_dir + instrument + '_Definition.xml'
LoadEmptyInstrument(idf, ws)

def GetWSangles(inWS,verbose=False):
nhist = mtd[inWS].getNumberHistograms() # get no. of histograms/groups
sourcePos = mtd[inWS].getInstrument().getSource().getPos()
samplePos = mtd[inWS].getInstrument().getSample().getPos()
beamPos = samplePos - sourcePos
angles = [] # will be list of angles
for index in range(0, nhist):
detector = mtd[inWS].getDetector(index) # get index
twoTheta = getDetectorTwoTheta(detector, samplePos, beamPos)*180.0/math.pi # calc angle
angles.append(twoTheta) # add angle
return angles

def GetThetaQ(inWS):
nhist = mtd[inWS].getNumberHistograms() # get no. of histograms/groups
efixed = getEfixed(inWS)
wavelas = math.sqrt(81.787/efixed) # elastic wavelength
k0 = 4.0*math.pi/wavelas
d2r = math.pi/180.0
sourcePos = mtd[inWS].getInstrument().getSource().getPos()
samplePos = mtd[inWS].getInstrument().getSample().getPos()
beamPos = samplePos - sourcePos
theta = []
Q = []
for index in range(0,nhist):
detector = mtd[inWS].getDetector(index) # get index
twoTheta = getDetectorTwoTheta(detector, samplePos, beamPos)*180.0/math.pi # calc angle
theta.append(twoTheta) # add angle
Q.append(k0*math.sin(0.5*twoTheta*d2r))
return theta,Q

0 comments on commit 53dda17

Please sign in to comment.