Dernière activité 1731088877

Révision eb22736a53c6921ae982487fff5732f3759a0372

Filter_combined_ZYP.py Brut
1#
2# Filter_combined_ZYP.py
3#
4# ---------------------------------------------------------------------------
5# This is a combined script for event filter by either sample log or time.
6# The two ways of conducting event filter is not compatible with each other
7# and users can choose which one to use by setting the `filter_by_time`
8# variable in the 'Input section' below to either `True` (to enable filter by
9# time) or `False` (to enable filter by sample log).
10# ---------------------------------------------------------------------------
11#
12# ----------------------------
13# Yuanpeng Zhang @ Nov-08-2024
14# ----------------------------
15#
16from mantid.simpleapi import *
17from datetime import datetime, timedelta
18
19###############################################################################
20# ------------------------------ Input section --------------------------------
21###############################################################################
22
23# Inputs for runs information.
24title = 'output_th'
25IPTS = 32368
26run = 1532451
27background = None
28BackgroundScale = 1
29vanadium = 1530874 # Run number or `None`
30vanadium_IPTS = 23858
31out_dir = "/SNS/users/y8z/Temp"
32
33# Inputs for runs control.
34normaliseBy = 'Monitor' # One on (None, Monitor, Time)
35units = 'Theta' # One of (Theta, ElasticQ, ElasticDSpacing)
36Binning = '5,125,1200' # Min,Max,Number_of_bins
37log_binning = False
38
39# Inputs concerning event filter. First, choose to filter by time or sample log,
40# depending on the value of `filter_by_time`.
41#
42# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43# N.B. The option to filter by time and sample log is NOT compatible with each other.
44# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
45#
46filter_by_time = True
47
48# Inputs concerning event filter by time.
49if filter_by_time:
50 start_time = '0'
51 stop_time = '900'
52 time_interval = '500'
53 unit_of_time = 'Seconds'
54 time_tolerance = 0
55
56# Inputs concerning event filter by sample log.
57LogName = 'HB2C:SE:SampleTemp'
58LogValueInterval = 5
59MinimumLogValue = None
60MaximumLogValue = None
61log_val_tolerance = LogValueInterval / 2.0
62log_change_direction = 'Both'
63
64###############################################################################
65# ---------------------------- Execution section ------------------------------
66###############################################################################
67
68# Load vandium if not already loaded.
69vanadium_ws = 'HB2C_{}'.format(vanadium)
70
71if vanadium_ws not in mtd:
72 LoadWAND(IPTS=vanadium_IPTS,
73 RunNumbers=vanadium,
74 OutputWorkspace=vanadium_ws)
75
76# Load background if needed and if not already loaded.
77if background is not None:
78 background_ws = 'HB2C_{}'.format(background)
79 if background_ws not in mtd:
80 LoadEventNexus(Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(IPTS, background),
81 OutputWorkspace=background_ws,
82 LoadMonitors=True)
83else:
84 background_ws = None
85
86# Load data if not already loaded
87ws = 'HB2C_{}'.format(run)
88
89if ws not in mtd:
90 LoadEventNexus(Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(IPTS, run),
91 OutputWorkspace=ws,
92 LoadMonitors=True)
93 # Mask detectors to be the same as vanadium
94 MaskDetectors(ws, MaskedWorkspace=vanadium_ws)
95
96# Filter events
97if filter_by_time:
98 GenerateEventsFilter(InputWorkspace=ws,
99 OutputWorkspace='filter',
100 InformationWorkspace='info',
101 StartTime=start_time,
102 StopTime=stop_time,
103 TimeInterval=time_interval,
104 TimeTolerance=time_tolerance,
105 UnitOfTime=unit_of_time)
106 FilterEvents(InputWorkspace=ws,
107 SplitterWorkspace='filter',
108 OutputWorkspaceBaseName=ws + '_filtered',
109 InformationWorkspace='info',
110 GroupWorkspaces=True,
111 FilterByPulseTime=True,
112 OutputWorkspaceIndexedFrom1=True)
113 FilterEvents(InputWorkspace=ws + '_monitors',
114 SplitterWorkspace='filter',
115 OutputWorkspaceBaseName=ws + '_filtered_mon',
116 InformationWorkspace='info',
117 GroupWorkspaces=True,
118 FilterByPulseTime=True,
119 SpectrumWithoutDetector='Skip only if TOF correction',
120 OutputWorkspaceIndexedFrom1=True)
121else:
122 GenerateEventsFilter(InputWorkspace=ws,
123 OutputWorkspace='filter',
124 InformationWorkspace='info',
125 LogName=LogName,
126 LogValueInterval=LogValueInterval,
127 MinimumLogValue=MinimumLogValue,
128 MaximumLogValue=MaximumLogValue,
129 LogValueTolerance=log_val_tolerance,
130 FilterLogValueByChangingDirection=log_change_direction
131 )
132 FilterEvents(InputWorkspace=ws,
133 SplitterWorkspace='filter',
134 OutputWorkspaceBaseName=ws + '_filtered',
135 InformationWorkspace='info',
136 GroupWorkspaces=True,
137 FilterByPulseTime=True,
138 OutputWorkspaceIndexedFrom1=True)
139 FilterEvents(InputWorkspace=ws + '_monitors',
140 SplitterWorkspace='filter',
141 OutputWorkspaceBaseName=ws + '_filtered_mon',
142 InformationWorkspace='info',
143 GroupWorkspaces=True,
144 FilterByPulseTime=True,
145 SpectrumWithoutDetector='Skip only if TOF correction',
146 OutputWorkspaceIndexedFrom1=True)
147
148# Set the monitor count on filtered WS
149for n in range(mtd[ws + '_filtered'].getNumberOfEntries()):
150 AddSampleLog(mtd[ws + '_filtered'].getItem(n),
151 LogName="gd_prtn_chrg",
152 LogType='Number',
153 NumberType='Double',
154 LogText=str(mtd[ws + '_filtered_mon'].getItem(n).getNumberEvents()))
155
156if background is not None:
157 AddSampleLog(background_ws, LogName="gd_prtn_chrg",
158 LogType='Number',
159 NumberType='Double',
160 LogText=str(mtd[background_ws + '_monitors'].getNumberEvents()))
161
162# Run powder diffraction reduction
163xmin, xmax, bins = Binning.split(',')
164WANDPowderReduction(ws + '_filtered',
165 CalibrationWorkspace=vanadium_ws,
166 BackgroundWorkspace=background_ws,
167 BackgroundScale=BackgroundScale,
168 XMin=xmin,
169 XMax=xmax,
170 NumberBins=bins,
171 Target=units,
172 LogBinning=log_binning,
173 NormaliseBy=normaliseBy,
174 OutputWorkspace=title)
175
176run_start = mtd[ws].getRun().getLogData("run_start").value
177run_start = run_start.split(".")[0] + "." + run_start.split(".")[1][:6]
178starting_time = datetime.strptime(run_start, '%Y-%m-%dT%H:%M:%S.%f')
179for count in range(mtd[ws + '_filtered'].getNumberOfEntries()):
180 out_wksp = mtd[title].getItem(count)
181 if filter_by_time:
182 tname_part = "{:d}".format(int(start_time) + count * int(time_interval))
183 out_wksp_new = f"{str(out_wksp)}_{tname_part}s"
184 RenameWorkspace(
185 InputWorkspace=out_wksp,
186 OutputWorkspace=out_wksp_new
187 )
188 fn_tmp = f"{title}_{tname_part}s_"
189 duration = count * int(time_interval)
190 stop_time = starting_time + timedelta(seconds=duration)
191 stop_time = stop_time.strftime('%Y-%m-%dT%H:%M:%S.%f')
192 fn_tmp += f"{stop_time}.dat"
193 SaveAscii(
194 InputWorkspace=out_wksp_new,
195 Filename=os.path.join(
196 out_dir,
197 fn_tmp
198 )
199 )
200 else:
201 SaveAscii(
202 InputWorkspace=out_wksp,
203 Filename=os.path.join(
204 out_dir,
205 f"{title}_{item}.dat"
206 )
207 )
208