forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaskWorkspaceToCalFile.py
105 lines (82 loc) · 3.91 KB
/
MaskWorkspaceToCalFile.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
# 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=invalid-name, no-init
from mantid.kernel import *
from mantid.api import *
from mantid.simpleapi import *
#pylint: disable=too-few-public-methods
class QueryFlag(object):
def isMasked(self, specInfo, index, dummy_yValue):
return specInfo.isMasked(index)
#pylint: disable=too-few-public-methods
class QueryValue(object):
def isMasked(self, dummy_specInfo, dummy_index, yValue):
return yValue == 1
class MaskWorkspaceToCalFile(PythonAlgorithm):
def category(self):
return "DataHandling\\Text;Diffraction\\DataHandling;Diffraction\\Masking"
def seeAlso(self):
return [ "ReadGroupsFromFile","CreateDummyCalFile","CreateCalFileByNames",
"AlignDetectors","DiffractionFocussing","LoadCalFile","SaveCalFile","MergeCalFiles" ]
def name(self):
return "MaskWorkspaceToCalFile"
def summary(self):
return "Saves the masking information in a workspace to a Cal File."
def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty("InputWorkspace", "", Direction.Input),
"The workspace containing the Masking to extract.")
self.declareProperty(FileProperty(name="OutputFile",defaultValue="",
action=FileAction.Save,extensions=['cal']), "The file for the results.")
self.declareProperty("Invert", False, "If True, masking is inverted in the input workspace. Default: False")
def PyExec(self):
#extract settings
inputWorkspace = mtd[self.getPropertyValue("InputWorkspace")]
outputFileName = self.getProperty("OutputFile").value
invert = self.getProperty("Invert").value
mask_query = QueryFlag()
if inputWorkspace.id() == "MaskWorkspace":
mask_query = QueryValue()
#check for consistency
if len(inputWorkspace.readX(0)) < 1:
raise RuntimeError('The input workspace is empty.')
#define flags for masking and not-masking
masking_flag = 0
not_masking_flag = 1
if invert:
masking_flag, not_masking_flag = not_masking_flag, masking_flag
calFile = open(outputFileName,"w")
#write a header
instrumentName = inputWorkspace.getInstrument().getName()
calFile.write('# '+instrumentName+' detector file\n')
calFile.write('# Format: number UDET offset select group\n')
#save the grouping
specInfo = inputWorkspace.spectrumInfo()
for i in range(inputWorkspace.getNumberHistograms()):
try:
det = inputWorkspace.getDetector(i)
y_value = inputWorkspace.readY(i)[0]
if mask_query.isMasked(specInfo, i, y_value): #check if masked
group = masking_flag
else:
group = not_masking_flag
detIDs = []
try:
detIDs = det.getDetectorIDs()
except AttributeError:
detIDs = [det.getID()]
for did in detIDs:
calFile.write(self.FormatLine(i,did,0.0,group,group))
except RuntimeError:
# no detector for this spectra
pass
calFile.close()
#pylint: disable=too-many-arguments
def FormatLine(self,number,UDET,offset,select,group):
line = "{0:9d}{1:16d}{2:16.7f}{3:9d}{4:9d}\n".format(number,UDET,offset,select,group)
return line
#############################################################################################
AlgorithmFactory.subscribe(MaskWorkspaceToCalFile())