Newer
Older
from mantid.simpleapi import *
from mantid.api import *
from mantid.kernel import Direction, FloatArrayProperty
import mantid
COMPRESS_TOL_TOF = .01
class PDToPDFgetN(DataProcessorAlgorithm):
def category(self):
return "Workflow\\Diffraction;PythonAlgorithms"
def name(self):
return "PDToPDFgetN"
def summary(self):
return "The algorithm used converting raw data to pdfgetn input files"
def PyInit(self):
self.declareProperty(FileProperty(name="Filename",
defaultValue="", action=FileAction.Load,
extensions=["_event.nxs", ".nxs.h5"]),
"Event file")
self.declareProperty("MaxChunkSize", 0.0,
"Specify maximum Gbytes of file to read in one chunk. Default is whole file.")
self.declareProperty("FilterBadPulses", 95.,
doc="Filter out events measured while proton " +
"charge is more than 5% below average")
self.declareProperty(MatrixWorkspaceProperty("InputWorkspace", "",
direction=Direction.Input,
optional=PropertyMode.Optional),
doc="Handle to reduced workspace")
self.setPropertyGroup("Filename", group)
self.setPropertyGroup("MaxChunkSize", group)
self.setPropertyGroup("FilterBadPulses", group)
self.setPropertyGroup("InputWorkspace", group)
group = "Output"
self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", "",
direction=Direction.Output),
doc="Handle to reduced workspace")
self.declareProperty(FileProperty(name="PDFgetNFile", defaultValue="", action=FileAction.Save,
extensions=[".getn"]), "Output filename")
self.setPropertyGroup("OutputWorkspace", group)
self.setPropertyGroup("PDFgetNFile", group)
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
self.declareProperty(FileProperty(name="CalibrationFile",
defaultValue="", action=FileAction.OptionalLoad,
extensions=[".h5", ".hd5", ".hdf", ".cal"]))
self.declareProperty(FileProperty(name="CharacterizationRunsFile", defaultValue="",
action=FileAction.OptionalLoad,
extensions=["txt"]),
"File with characterization runs denoted")
self.declareProperty("RemovePromptPulseWidth", 0.0,
"Width of events (in microseconds) near the prompt pulse to remove. 0 disables")
self.declareProperty("CropWavelengthMin", 0.,
"Crop the data at this minimum wavelength.")
self.declareProperty("CropWavelengthMax", 0.,
"Crop the data at this maximum wavelength.")
self.declareProperty(FloatArrayProperty("Binning", values=[0., 0., 0.],
direction=Direction.Input), "Positive is linear bins, negative is logorithmic")
self.declareProperty("ResampleX", 0,
"Number of bins in x-axis. Non-zero value overrides \"Params\" property. " +
"Negative value means logorithmic binning.")
def _loadCharacterizations(self):
self._focusPos = {}
self._iparmFile = None
charFilename = self.getProperty("CharacterizationRunsFile").value
if charFilename is None or len(charFilename) <= 0:
return
results = PDLoadCharacterizations(Filename=charFilename,
OutputWorkspace="characterizations")
self._iparmFile = results[1]
self._focusPos['PrimaryFlightPath'] = results[2]
self._focusPos['SpectrumIDs'] = results[3]
self._focusPos['L2'] = results[4]
self._focusPos['Polar'] = results[5]
self._focusPos['Azimuthal'] = results[6]
def PyExec(self):
self._loadCharacterizations()
wksp = self.getProperty("InputWorkspace").value
if wksp is None:
wksp = LoadEventAndCompress(Filename=self.getProperty("Filename").value,
OutputWorkspace=self.getPropertyValue("OutputWorkspace"),
MaxChunkSize=self.getProperty("MaxChunkSize").value,
FilterBadPulses=self.getProperty("FilterBadPulses").value,
CompressTOFTolerance=COMPRESS_TOL_TOF)
else:
self.log().information("Using input workspace. Ignoring properties 'Filename', " +
"'OutputWorkspace', 'MaxChunkSize', and 'FilterBadPulses'")
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
charac = ""
if mtd.doesExist("characterizations"):
charac = "characterizations"
# get the correct row of the table
PDDetermineCharacterizations(InputWorkspace=wksp,
Characterizations=charac,
ReductionProperties="__snspowderreduction")
wksp = AlignAndFocusPowder(InputWorkspace=wksp, OutputWorkspace=wksp,
CalFileName=self.getProperty("CalibrationFile").value,
Params=self.getProperty("Binning").value,
ResampleX=self.getProperty("ResampleX").value, Dspacing=True,
PreserveEvents=False,
RemovePromptPulseWidth=self.getProperty("RemovePromptPulseWidth").value,
CompressTolerance=COMPRESS_TOL_TOF,
CropWavelengthMin=self.getProperty("CropWavelengthMin").value,
CropWavelengthMax=self.getProperty("CropWavelengthMax").value,
ReductionProperties="__snspowderreduction",
**(self._focusPos))
wksp = NormaliseByCurrent(InputWorkspace=wksp, OutputWorkspace=wksp)
wksp.getRun()['gsas_monitor'] = 1
if self._iparmFile is not None:
wksp.getRun()['iparm_file'] = self._iparmFile
wksp = SetUncertainties(InputWorkspace=wksp, OutputWorkspace=wksp,
SetError="sqrt")
SaveGSS(InputWorkspace=wksp,
Filename=self.getProperty("PDFgetNFile").value,
SplitFiles=False, Append=False,
MultiplyByBinWidth=False,
Bank=mantid.pmds["__snspowderreduction"]["bank"].value,
Format="SLOG", ExtendedHeader=True)
self.setProperty("OutputWorkspace", wksp)
# Register algorithm with Mantid.
AlgorithmFactory.subscribe(PDToPDFgetN)