Последняя активность 1731088877

yuanpeng's Avatar yuanpeng ревизий этого фрагмента 1731088877. К ревизии

1 file changed, 207 insertions

Filter_combined_ZYP.py(файл создан)

@@ -0,0 +1,207 @@
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 + )
Новее Позже