forked from neutrons/mandi_autoreduction_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandi_singlerun.py
142 lines (133 loc) · 6.44 KB
/
mandi_singlerun.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
133
134
135
136
137
138
139
140
141
142
from __future__ import (absolute_import, division, print_function)
import os
import sys
import ReduceDictionary
import matplotlib
import numpy as np
matplotlib.use('Agg') # autoanalyzer1 fails if we dont set this
sys.path.insert(0, "/opt/mantidnightly/bin")
from mantid.simpleapi import * # noqa: F402, F403
from mantid.api import * # noqa: F402, F403
def doIntegration(d, nxsFilename, out_dir, run_number):
rA = d['peak_radius']
rB = d['bkg_inner_radius']
rC = d['bkg_outer_radius']
min_d = d['min_d']
max_d = d['max_d']
tolerance = d['tolerance']
moderatorFile = d['moderator_file']
predictPeaks = d['integrate_predicted_peaks']
min_pred_dspacing = d['min_pred_dspacing']
max_pred_dspacing = d['max_pred_dspacing']
min_pred_wl = d['min_pred_wl']
max_pred_wl = d['max_pred_wl']
StrongPeaksParamsFile = d['strong_peaks_params_file']
IntensityCutoff = d['intensity_cutoff']
EdgeCutoff = d['edge_cutoff']
FracStop = d['frac_stop']
MinpplFrac = d['min_ppl_frac']
MaxpplFrac = d['max_ppl_frac']
DQMax = d['dq_max']
a = d['unitcell_a']
b = d['unitcell_b']
c = d['unitcell_c']
alpha = d['unitcell_alpha']
beta = d['unitcell_beta']
gamma = d['unitcell_gamma']
num_peaks_to_find = d['num_peaks_to_find']
DetCalFile = d['calibration_file_1']
outputFilenameTemplate = out_dir + '%s_ws_%i_mandi_autoreduced.%s'
try:
event_ws = Load(Filename=nxsFilename, OutputWorkspace='event_ws')
if DetCalFile is not None:
print('Loading DetCal file %s' % DetCalFile)
LoadIsawDetCal(InputWorkspace=event_ws, Filename=DetCalFile)
ConvertToMD(InputWorkspace='event_ws', QDimensions='Q3D',
dEAnalysisMode='Elastic', Q3DFrames='Q_lab',
OutputWorkspace='MDdata', MinValues='-5,-5,-5',
MaxValues='5,5,5', MaxRecursionDepth=10)
peaks_ws = FindPeaksMD(InputWorkspace='MDdata',
MaxPeaks=num_peaks_to_find,
DensityThresholdFactor=500,
OutputWorkspace='peaks_ws')
if np.max([a, b, c] > 150):
SetInstrumentParameter(Workspace='peaks_ws',
ParameterName='fracHKL',
ParameterType='Number',
Value='0.4')
try:
FindUBUsingFFT(PeaksWorkspace='peaks_ws', MinD=min_d, MaxD=max_d,
Tolerance=tolerance, Iterations=10,
DegreesPerStep=1.0)
except:
FindUBUsingLatticeParameters(PeaksWorkspace='peaks_ws',
a=a, b=b, c=c,
alpha=alpha, beta=beta, gamma=gamma,
NumInitial=50, Tolerance=tolerance,
Iterations=1000)
IndexPeaks(PeaksWorkspace='peaks_ws')
mtd.remove('event_ws') # Free up memory
if predictPeaks:
print("PREDICTING peaks to integrate....")
peaks_ws = PredictPeaks(InputWorkspace=peaks_ws,
WavelengthMin=min_pred_wl,
WavelengthMax=max_pred_wl,
MinDSpacing=min_pred_dspacing,
MaxDSpacing=max_pred_dspacing,
ReflectionCondition='Primitive')
IntegratePeaksMD(InputWorkspace='MDdata', PeakRadius=rA,
BackgroundInnerRadius=rB, BackgroundOuterRadius=rC,
PeaksWorkspace='peaks_ws', OutputWorkspace='peaks_ws',
CylinderLength=0.4, PercentBackground=20,
ProfileFunction='IkedaCarpenterPV')
IntegratePeaksProfileFitting(OutputPeaksWorkspace='peaks_ws_out',
OutputParamsWorkspace='params_ws',
InputWorkspace='MDdata',
PeaksWorkspace='peaks_ws',
ModeratorCoefficientsFile=moderatorFile,
DQMax=DQMax, MinpplFrac=MinpplFrac,
MaxpplFrac=MaxpplFrac, FracStop=FracStop,
EdgeCutoff=EdgeCutoff,
IntensityCutoff=IntensityCutoff,
StrongPeakParamsFile=StrongPeaksParamsFile) # noqa: E501
paramsFileName = outputFilenameTemplate % ('params', run_number, 'nxs')
peaksFileName = outputFilenameTemplate % ('peaks',
run_number, 'integrate')
peaksPFFileName = outputFilenameTemplate % ('peaks_profileFitted',
run_number, 'integrate')
matFileName = outputFilenameTemplate % ('UB', run_number, 'mat')
if os.path.isfile(paramsFileName):
os.remove(paramsFileName)
if os.path.isfile(peaksFileName):
os.remove(peaksFileName)
if os.path.isfile(peaksPFFileName):
os.remove(peaksPFFileName)
if os.path.isfile(peaksPFFileName):
os.remove(matFileName)
SaveNexus(InputWorkspace='params_ws', Filename=paramsFileName)
SaveIsawPeaks(InputWorkspace='peaks_ws', Filename=peaksFileName)
SaveIsawPeaks(InputWorkspace='peaks_ws_out', Filename=peaksPFFileName)
SaveIsawUB(InputWorkspace='peaks_ws', Filename=matFileName)
mtd.clear()
except:
raise
# raise UserWarning('ERROR WITH RUN %i'%run_number)
if __name__ == '__main__':
# arg1=script name, arg2=config filename, arg3=nxs filename,
# arg4=outputdir, arg5=run_number
if (len(sys.argv) != 5):
logger.error('{0} must take 4 arguments!'.format(sys.argv[0]))
sys.exit()
if not(os.path.isfile(sys.argv[1])):
logger.error("config file " + sys.argv[1] + " not found")
sys.exit()
if not(os.path.isfile(sys.argv[2])):
logger.error("data file " + sys.argv[2] + " not found")
sys.exit()
else:
config_file_name = str(sys.argv[1])
nxs_name = str(sys.argv[2])
out_dir = str(sys.argv[3])
run_number = int(sys.argv[4])
params_dictionary = ReduceDictionary.LoadDictionary(config_file_name)
doIntegration(params_dictionary, nxs_name, out_dir, run_number)