forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortDetectors.py
89 lines (75 loc) · 3.19 KB
/
SortDetectors.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
# 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,invalid-name
from mantid.api import PythonAlgorithm, AlgorithmFactory
from mantid.kernel import Direction,IntArrayProperty, FloatArrayProperty
import mantid
import math
import numpy
class SortDetectors(PythonAlgorithm):
""" Sort detectors by distance
"""
def category(self):
""" Return category
"""
return "Utility\\Sorting"
def seeAlso(self):
return [ "SortByQVectors","SortXAxis" ]
def name(self):
""" Return name
"""
return "SortDetectors"
def summary(self):
""" Return summary
"""
return "Algorithm to sort detectors by distance."
def PyInit(self):
""" Declare properties
"""
self.declareProperty(mantid.api.WorkspaceProperty( "Workspace", "",
direction=mantid.kernel.Direction.Input,
validator=mantid.api.InstrumentValidator()),
"Input workspace")
self.declareProperty(IntArrayProperty("UpstreamSpectra", Direction.Output))
self.declareProperty(FloatArrayProperty("UpstreamDetectorDistances", Direction.Output))
self.declareProperty(IntArrayProperty("DownstreamSpectra", Direction.Output))
self.declareProperty(FloatArrayProperty("DownstreamDetectorDistances", Direction.Output))
def PyExec(self):
""" Main execution body
"""
workspace = self.getProperty("Workspace").value
samplePos = workspace.getInstrument().getSample().getPos()
moderatorPos = workspace.getInstrument().getSource().getPos()
incident = samplePos - moderatorPos
upstream=[]
upinds=[]
updist=[]
downstream=[]
downinds=[]
downdist=[]
for i in range(workspace.getNumberHistograms()):
detPos=workspace.getDetector(i).getPos()
scattered=detPos-samplePos
if abs(scattered.angle(incident))>0.999*math.pi:
upstream.append((i,scattered.norm()))
else:
downstream.append((i,scattered.norm()))
if len(upstream)>0:
upstream.sort(key=lambda x: x[1])
zipped_upstream = list(zip(*upstream))
upinds=zipped_upstream[0]
updist=zipped_upstream[1]
if len(downstream)>0:
downstream.sort(key=lambda x: x[1])
zipped_downstream = list(zip(*downstream))
downinds=zipped_downstream[0]
downdist=zipped_downstream[1]
self.setProperty("UpstreamSpectra", numpy.array(upinds))
self.setProperty("UpstreamDetectorDistances", numpy.array(updist))
self.setProperty("DownstreamSpectra", numpy.array(downinds))
self.setProperty("DownstreamDetectorDistances", numpy.array(downdist))
AlgorithmFactory.subscribe(SortDetectors)