/
SANSDirectBeamTransmission.py
117 lines (95 loc) · 5.61 KB
/
SANSDirectBeamTransmission.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
# pylint: disable=no-init
from mantid.api import *
from mantid.kernel import *
import os
class SANSDirectBeamTransmission(PythonAlgorithm):
def category(self):
return "Workflow\\SANS\\UsesPropertyManager"
def name(self):
return "SANSDirectBeamTransmission"
def summary(self):
return "Compute transmission using the direct beam method"
def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty("InputWorkspace", "", direction=Direction.Input))
self.declareProperty(FileProperty("SampleDataFilename", "", action=FileAction.Load, extensions=["xml", "nxs", "nxs.h5"]))
self.declareProperty(FileProperty("EmptyDataFilename", "", action=FileAction.Load, extensions=["xml", "nxs", "nxs.h5"]))
self.declareProperty("BeamRadius", 3.0, "Beam radius [pixels]")
self.declareProperty("ThetaDependent", True, "If true, a theta-dependent correction will be applied")
self.declareProperty(FileProperty("DarkCurrentFilename", "", action=FileAction.OptionalLoad, extensions=["xml", "nxs", "nxs.h5"]))
self.declareProperty("UseSampleDarkCurrent", False, "If true, the sample dark current will be used")
self.declareProperty("BeamCenterX", 0.0, "Beam center position in X")
self.declareProperty("BeamCenterY", 0.0, "Beam center position in Y")
self.declareProperty(
"ReductionProperties",
"__sans_reduction_properties",
validator=StringMandatoryValidator(),
doc="Property manager name for the reduction",
)
self.declareProperty(
MatrixWorkspaceProperty("OutputWorkspace", "", direction=Direction.Output),
"Workspace containing the data corrected for the transmission.",
)
self.declareProperty(
MatrixWorkspaceProperty("TransmissionWorkspace", "", optional=PropertyMode.Optional, direction=Direction.Output),
"Workspace containing the fitted transmission distribution.",
)
self.declareProperty(
MatrixWorkspaceProperty("RawTransmissionWorkspace", "", optional=PropertyMode.Optional, direction=Direction.Output),
"Workspace containing the transmission distribution before fitting.",
)
self.declareProperty("MeasuredTransmission", 0.0, direction=Direction.Output)
self.declareProperty("MeasuredError", 0.0, direction=Direction.Output)
self.declareProperty("OutputMessage", "", direction=Direction.Output, doc="Output message")
def PyExec(self):
import TransmissionUtils
sample_file = self.getPropertyValue("SampleDataFilename")
empty_file = self.getPropertyValue("EmptyDataFilename")
property_manager_name = self.getProperty("ReductionProperties").value
property_manager = PropertyManagerDataService[property_manager_name]
# Build the name we are going to give the transmission workspace
sample_basename = os.path.basename(sample_file)
empty_basename = os.path.basename(empty_file)
entry_name = "Transmission%s%s" % (sample_basename, empty_basename)
trans_ws_name = "__transmission_fit_%s" % sample_basename
trans_ws = None
if property_manager.existsProperty(entry_name):
trans_ws_name = property_manager.getProperty(entry_name)
if AnalysisDataService.doesExist(trans_ws_name):
trans_ws = AnalysisDataService.retrieve(trans_ws_name)
if trans_ws is None:
# Load data files
sample_mon_ws, empty_mon_ws, first_det, output_str, monitor_det_ID = TransmissionUtils.load_monitors(self, property_manager)
trans_ws, raw_ws = TransmissionUtils.calculate_transmission(
self, sample_mon_ws, empty_mon_ws, first_det, trans_ws_name, monitor_det_ID
)
# 2- Apply correction (Note: Apply2DTransCorr)
if trans_ws is not None:
input_ws = self.getProperty("InputWorkspace").value
output_ws = TransmissionUtils.apply_transmission(self, input_ws, trans_ws)
trans = trans_ws.dataY(0)[0]
error = trans_ws.dataE(0)[0]
if len(trans_ws.dataY(0)) == 1:
self.setProperty("MeasuredTransmission", trans)
self.setProperty("MeasuredError", error)
output_str = "%s T = %6.2g += %6.2g\n" % (output_str, trans, error)
self.setProperty("OutputWorkspace", output_ws)
input_tr_name = self.getPropertyValue("TransmissionWorkspace")
if len(input_tr_name.strip()) == 0:
self.setPropertyValue("TransmissionWorkspace", trans_ws_name)
self.setProperty("TransmissionWorkspace", trans_ws)
if raw_ws is not None:
raw_ws_name = "__transmission_raw_%s" % sample_basename
self.setPropertyValue("RawTransmissionWorkspace", raw_ws_name)
self.setProperty("RawTransmissionWorkspace", raw_ws)
output_msg = "Transmission correction applied [%s]\n%s\n" % (trans_ws_name, output_str)
else:
output_msg = "Transmission correction had errors\n%s\n" % output_str
self.setPropertyValue("OutputMessage", output_msg)
#############################################################################################
AlgorithmFactory.subscribe(SANSDirectBeamTransmission)