-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadWAND.py
132 lines (109 loc) · 6.99 KB
/
LoadWAND.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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 +
from mantid.api import DataProcessorAlgorithm, AlgorithmFactory, MultipleFileProperty, FileAction, WorkspaceProperty
from mantid.kernel import Direction, UnitConversion, Elastic, Property, IntArrayProperty, StringListValidator
from mantid.simpleapi import (mtd, SetGoniometer, AddSampleLog, MaskBTP, RenameWorkspace, GroupWorkspaces,
CreateWorkspace, LoadNexusLogs, LoadInstrument)
import numpy as np
import h5py
class LoadWAND(DataProcessorAlgorithm):
def name(self):
return 'LoadWAND'
def category(self):
return 'DataHandling\\Nexus'
def summary(self):
return 'Loads Event Nexus file, integrates events, sets wavelength, mask, and goniometer, and sets proton charge to monitor counts'
def PyInit(self):
self.declareProperty(MultipleFileProperty(name="Filename", action=FileAction.OptionalLoad, extensions=[".nxs.h5"]), "Files to Load")
self.declareProperty('IPTS', Property.EMPTY_INT, "IPTS number to load from")
self.declareProperty(IntArrayProperty("RunNumbers", []), 'Run numbers to load')
self.declareProperty("Wavelength", 1.488, doc="Wavelength to set the workspace")
self.declareProperty("ApplyMask", True, "If True standard masking will be applied to the workspace")
self.declareProperty("Grouping", 'None', StringListValidator(['None', '2x2', '4x4']), "Group pixels")
self.declareProperty(WorkspaceProperty(name="OutputWorkspace", defaultValue="", direction=Direction.Output))
def validateInputs(self):
issues = dict()
if not self.getProperty("Filename").value:
if (self.getProperty("IPTS").value == Property.EMPTY_INT) or len(self.getProperty("RunNumbers").value) == 0:
issues["Filename"] = 'Must specify either Filename or IPTS AND RunNumbers'
return issues
def PyExec(self):
runs = self.getProperty("Filename").value
if not runs:
ipts = self.getProperty("IPTS").value
runs = ['/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(ipts, run) for run in self.getProperty("RunNumbers").value]
wavelength = self.getProperty("wavelength").value
outWS = self.getPropertyValue("OutputWorkspace")
group_names = []
grouping = self.getProperty("Grouping").value
if grouping == 'None':
grouping = 1
else:
grouping = 2 if grouping == '2x2' else 4
for i, run in enumerate(runs):
data = np.zeros((512*480*8),dtype=np.int64)
with h5py.File(run, 'r') as f:
monitor_count = f['/entry/monitor1/total_counts'].value[0]
duration = f['/entry/duration'].value[0]
run_number = f['/entry/run_number'].value[0]
for b in range(8):
data += np.bincount(f['/entry/bank'+str(b+1)+'_events/event_id'].value,minlength=512*480*8)
data = data.reshape((480*8, 512))
if grouping == 2:
data = data[::2,::2] + data[1::2,::2] + data[::2,1::2] + data[1::2,1::2]
elif grouping == 4:
data = (data[::4,::4] + data[1::4,::4] + data[2::4,::4] + data[3::4,::4]
+ data[::4,1::4] + data[1::4,1::4] + data[2::4,1::4] + data[3::4,1::4]
+ data[::4,2::4] + data[1::4,2::4] + data[2::4,2::4] + data[3::4,2::4]
+ data[::4,3::4] + data[1::4,3::4] + data[2::4,3::4] + data[3::4,3::4])
CreateWorkspace(DataX=[wavelength-0.001, wavelength+0.001],
DataY=data,
DataE=np.sqrt(data),
UnitX='Wavelength',
YUnitLabel='Counts',
NSpec=1966080//grouping**2,
OutputWorkspace='__tmp_load', EnableLogging=False)
LoadNexusLogs('__tmp_load', Filename=run, EnableLogging=False)
AddSampleLog('__tmp_load', LogName="monitor_count", LogType='Number', NumberType='Double',
LogText=str(monitor_count), EnableLogging=False)
AddSampleLog('__tmp_load', LogName="gd_prtn_chrg", LogType='Number', NumberType='Double',
LogText=str(monitor_count), EnableLogging=False)
AddSampleLog('__tmp_load', LogName="Wavelength", LogType='Number', NumberType='Double',
LogText=str(wavelength), EnableLogging=False)
AddSampleLog('__tmp_load', LogName="Ei", LogType='Number', NumberType='Double',
LogText=str(UnitConversion.run('Wavelength', 'Energy', wavelength, 0, 0, 0, Elastic, 0)), EnableLogging=False)
AddSampleLog('__tmp_load', LogName="run_number", LogText=run_number, EnableLogging=False)
AddSampleLog('__tmp_load', LogName="duration", LogType='Number',
LogText=str(duration), NumberType='Double', EnableLogging=False)
if grouping > 1: # Fix detector IDs per spectrum before loading instrument
__tmp_load = mtd['__tmp_load']
for n in range(__tmp_load.getNumberHistograms()):
s=__tmp_load.getSpectrum(n)
for i in range(grouping):
for j in range(grouping):
s.addDetectorID(int(n*grouping%512 + n//(512/grouping)*512*grouping + j + i*512))
LoadInstrument('__tmp_load', InstrumentName='WAND', RewriteSpectraMap=False, EnableLogging=False)
else:
LoadInstrument('__tmp_load', InstrumentName='WAND', RewriteSpectraMap=True, EnableLogging=False)
SetGoniometer('__tmp_load', Axis0="HB2C:Mot:s1,0,1,0,1", EnableLogging=False)
if self.getProperty("ApplyMask").value:
MaskBTP('__tmp_load', Pixel='1,2,511,512', EnableLogging=False)
if mtd['__tmp_load'].getRunNumber() > 26600: # They changed pixel mapping and bank name order here
MaskBTP('__tmp_load', Bank='1', Tube='479-480', EnableLogging=False)
MaskBTP('__tmp_load', Bank='8', Tube='1-2', EnableLogging=False)
else:
MaskBTP('__tmp_load', Bank='8', Tube='475-480', EnableLogging=False)
if len(runs) == 1:
RenameWorkspace('__tmp_load', outWS, EnableLogging=False)
else:
outName = outWS+"_"+str(mtd['__tmp_load'].getRunNumber())
group_names.append(outName)
RenameWorkspace('__tmp_load', outName, EnableLogging=False)
if len(runs) > 1:
GroupWorkspaces(group_names, OutputWorkspace=outWS, EnableLogging=False)
self.setProperty('OutputWorkspace', outWS)
AlgorithmFactory.subscribe(LoadWAND)