Filter_combined_ZYP.py
· 7.9 KiB · Python
Bruto
#
# Filter_combined_ZYP.py
#
# ---------------------------------------------------------------------------
# This is a combined script for event filter by either sample log or time.
# The two ways of conducting event filter is not compatible with each other
# and users can choose which one to use by setting the `filter_by_time`
# variable in the 'Input section' below to either `True` (to enable filter by
# time) or `False` (to enable filter by sample log).
# ---------------------------------------------------------------------------
#
# ----------------------------
# Yuanpeng Zhang @ Nov-08-2024
# ----------------------------
#
from mantid.simpleapi import *
from datetime import datetime, timedelta
###############################################################################
# ------------------------------ Input section --------------------------------
###############################################################################
# Inputs for runs information.
title = 'output_th'
IPTS = 32368
run = 1532451
background = None
BackgroundScale = 1
vanadium = 1530874 # Run number or `None`
vanadium_IPTS = 23858
out_dir = "/SNS/users/y8z/Temp"
# Inputs for runs control.
normaliseBy = 'Monitor' # One on (None, Monitor, Time)
units = 'Theta' # One of (Theta, ElasticQ, ElasticDSpacing)
Binning = '5,125,1200' # Min,Max,Number_of_bins
log_binning = False
# Inputs concerning event filter. First, choose to filter by time or sample log,
# depending on the value of `filter_by_time`.
#
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# N.B. The option to filter by time and sample log is NOT compatible with each other.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
filter_by_time = True
# Inputs concerning event filter by time.
if filter_by_time:
start_time = '0'
stop_time = '900'
time_interval = '500'
unit_of_time = 'Seconds'
time_tolerance = 0
# Inputs concerning event filter by sample log.
LogName = 'HB2C:SE:SampleTemp'
LogValueInterval = 5
MinimumLogValue = None
MaximumLogValue = None
log_val_tolerance = LogValueInterval / 2.0
log_change_direction = 'Both'
###############################################################################
# ---------------------------- Execution section ------------------------------
###############################################################################
# Load vandium if not already loaded.
vanadium_ws = 'HB2C_{}'.format(vanadium)
if vanadium_ws not in mtd:
LoadWAND(IPTS=vanadium_IPTS,
RunNumbers=vanadium,
OutputWorkspace=vanadium_ws)
# Load background if needed and if not already loaded.
if background is not None:
background_ws = 'HB2C_{}'.format(background)
if background_ws not in mtd:
LoadEventNexus(Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(IPTS, background),
OutputWorkspace=background_ws,
LoadMonitors=True)
else:
background_ws = None
# Load data if not already loaded
ws = 'HB2C_{}'.format(run)
if ws not in mtd:
LoadEventNexus(Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(IPTS, run),
OutputWorkspace=ws,
LoadMonitors=True)
# Mask detectors to be the same as vanadium
MaskDetectors(ws, MaskedWorkspace=vanadium_ws)
# Filter events
if filter_by_time:
GenerateEventsFilter(InputWorkspace=ws,
OutputWorkspace='filter',
InformationWorkspace='info',
StartTime=start_time,
StopTime=stop_time,
TimeInterval=time_interval,
TimeTolerance=time_tolerance,
UnitOfTime=unit_of_time)
FilterEvents(InputWorkspace=ws,
SplitterWorkspace='filter',
OutputWorkspaceBaseName=ws + '_filtered',
InformationWorkspace='info',
GroupWorkspaces=True,
FilterByPulseTime=True,
OutputWorkspaceIndexedFrom1=True)
FilterEvents(InputWorkspace=ws + '_monitors',
SplitterWorkspace='filter',
OutputWorkspaceBaseName=ws + '_filtered_mon',
InformationWorkspace='info',
GroupWorkspaces=True,
FilterByPulseTime=True,
SpectrumWithoutDetector='Skip only if TOF correction',
OutputWorkspaceIndexedFrom1=True)
else:
GenerateEventsFilter(InputWorkspace=ws,
OutputWorkspace='filter',
InformationWorkspace='info',
LogName=LogName,
LogValueInterval=LogValueInterval,
MinimumLogValue=MinimumLogValue,
MaximumLogValue=MaximumLogValue,
LogValueTolerance=log_val_tolerance,
FilterLogValueByChangingDirection=log_change_direction
)
FilterEvents(InputWorkspace=ws,
SplitterWorkspace='filter',
OutputWorkspaceBaseName=ws + '_filtered',
InformationWorkspace='info',
GroupWorkspaces=True,
FilterByPulseTime=True,
OutputWorkspaceIndexedFrom1=True)
FilterEvents(InputWorkspace=ws + '_monitors',
SplitterWorkspace='filter',
OutputWorkspaceBaseName=ws + '_filtered_mon',
InformationWorkspace='info',
GroupWorkspaces=True,
FilterByPulseTime=True,
SpectrumWithoutDetector='Skip only if TOF correction',
OutputWorkspaceIndexedFrom1=True)
# Set the monitor count on filtered WS
for n in range(mtd[ws + '_filtered'].getNumberOfEntries()):
AddSampleLog(mtd[ws + '_filtered'].getItem(n),
LogName="gd_prtn_chrg",
LogType='Number',
NumberType='Double',
LogText=str(mtd[ws + '_filtered_mon'].getItem(n).getNumberEvents()))
if background is not None:
AddSampleLog(background_ws, LogName="gd_prtn_chrg",
LogType='Number',
NumberType='Double',
LogText=str(mtd[background_ws + '_monitors'].getNumberEvents()))
# Run powder diffraction reduction
xmin, xmax, bins = Binning.split(',')
WANDPowderReduction(ws + '_filtered',
CalibrationWorkspace=vanadium_ws,
BackgroundWorkspace=background_ws,
BackgroundScale=BackgroundScale,
XMin=xmin,
XMax=xmax,
NumberBins=bins,
Target=units,
LogBinning=log_binning,
NormaliseBy=normaliseBy,
OutputWorkspace=title)
run_start = mtd[ws].getRun().getLogData("run_start").value
run_start = run_start.split(".")[0] + "." + run_start.split(".")[1][:6]
starting_time = datetime.strptime(run_start, '%Y-%m-%dT%H:%M:%S.%f')
for count in range(mtd[ws + '_filtered'].getNumberOfEntries()):
out_wksp = mtd[title].getItem(count)
if filter_by_time:
tname_part = "{:d}".format(int(start_time) + count * int(time_interval))
out_wksp_new = f"{str(out_wksp)}_{tname_part}s"
RenameWorkspace(
InputWorkspace=out_wksp,
OutputWorkspace=out_wksp_new
)
fn_tmp = f"{title}_{tname_part}s_"
duration = count * int(time_interval)
stop_time = starting_time + timedelta(seconds=duration)
stop_time = stop_time.strftime('%Y-%m-%dT%H:%M:%S.%f')
fn_tmp += f"{stop_time}.dat"
SaveAscii(
InputWorkspace=out_wksp_new,
Filename=os.path.join(
out_dir,
fn_tmp
)
)
else:
SaveAscii(
InputWorkspace=out_wksp,
Filename=os.path.join(
out_dir,
f"{title}_{item}.dat"
)
)
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 | # |
16 | from mantid.simpleapi import * |
17 | from datetime import datetime, timedelta |
18 | |
19 | ############################################################################### |
20 | # ------------------------------ Input section -------------------------------- |
21 | ############################################################################### |
22 | |
23 | # Inputs for runs information. |
24 | title = 'output_th' |
25 | IPTS = 32368 |
26 | run = 1532451 |
27 | background = None |
28 | BackgroundScale = 1 |
29 | vanadium = 1530874 # Run number or `None` |
30 | vanadium_IPTS = 23858 |
31 | out_dir = "/SNS/users/y8z/Temp" |
32 | |
33 | # Inputs for runs control. |
34 | normaliseBy = 'Monitor' # One on (None, Monitor, Time) |
35 | units = 'Theta' # One of (Theta, ElasticQ, ElasticDSpacing) |
36 | Binning = '5,125,1200' # Min,Max,Number_of_bins |
37 | log_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 | # |
46 | filter_by_time = True |
47 | |
48 | # Inputs concerning event filter by time. |
49 | if 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. |
57 | LogName = 'HB2C:SE:SampleTemp' |
58 | LogValueInterval = 5 |
59 | MinimumLogValue = None |
60 | MaximumLogValue = None |
61 | log_val_tolerance = LogValueInterval / 2.0 |
62 | log_change_direction = 'Both' |
63 | |
64 | ############################################################################### |
65 | # ---------------------------- Execution section ------------------------------ |
66 | ############################################################################### |
67 | |
68 | # Load vandium if not already loaded. |
69 | vanadium_ws = 'HB2C_{}'.format(vanadium) |
70 | |
71 | if 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. |
77 | if 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) |
83 | else: |
84 | background_ws = None |
85 | |
86 | # Load data if not already loaded |
87 | ws = 'HB2C_{}'.format(run) |
88 | |
89 | if 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 |
97 | if 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) |
121 | else: |
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 |
149 | for 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 | |
156 | if 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 |
163 | xmin, xmax, bins = Binning.split(',') |
164 | WANDPowderReduction(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 | |
176 | run_start = mtd[ws].getRun().getLogData("run_start").value |
177 | run_start = run_start.split(".")[0] + "." + run_start.split(".")[1][:6] |
178 | starting_time = datetime.strptime(run_start, '%Y-%m-%dT%H:%M:%S.%f') |
179 | for 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 |