|
13 | 13 | from IPython.core.display import HTML |
14 | 14 | import pandas as pd |
15 | 15 | import matplotlib.pyplot as plt |
16 | | -from enum import Enum |
17 | | -from scipy.constants import h, c, electron_volt, m_n |
| 16 | +# from enum import Enum |
| 17 | +# from scipy.constants import h, c, electron_volt, m_n |
| 18 | + |
| 19 | +from __code.normalization_tof.units import convert_array_from_time_to_lambda, convert_array_from_time_to_energy |
| 20 | +from __code.normalization_tof.units import TimeUnitOptions, DistanceUnitOptions, EnergyUnitOptions |
18 | 21 |
|
19 | 22 | LOG_PATH = "/SNS/VENUS/shared/log/" |
20 | 23 | LOAD_DTYPE = np.uint16 |
@@ -84,194 +87,6 @@ def retrieve_list_of_tif(folder: str) -> list: |
84 | 87 | return list_tif |
85 | 88 |
|
86 | 89 |
|
87 | | -class TimeUnitOptions(str, Enum): |
88 | | - s = "s" |
89 | | - ms = "ms" |
90 | | - us = "us" |
91 | | - ns = "ns" |
92 | | - ps = "ps" |
93 | | - |
94 | | - |
95 | | -class DistanceUnitOptions(str, Enum): |
96 | | - cm = "cm" |
97 | | - nm = "nm" |
98 | | - pm = "pm" |
99 | | - m = "m" |
100 | | - angstrom = "angstrom" |
101 | | - |
102 | | - |
103 | | -class EnergyUnitOptions(str, Enum): |
104 | | - meV = "meV" |
105 | | - eV = "eV" |
106 | | - keV = "keV" |
107 | | - MeV = "MeV" |
108 | | - GeV = "GeV" |
109 | | - J = "J" |
110 | | - |
111 | | - |
112 | | -def convert_time_units(from_unit, to_unit): |
113 | | - """Convert time from one unit to another unit |
114 | | - based on TimeUnitOptions options |
115 | | - |
116 | | - Args: |
117 | | - from_unit (TimeUnitOptions): Unit to convert from. |
118 | | - to_unit (TimeUnitOptions): Unit to convert to. |
119 | | - |
120 | | - Returns: |
121 | | - float: Time in the new unit. |
122 | | - """ |
123 | | - |
124 | | - # Conversion factors |
125 | | - conversion_factors = { |
126 | | - TimeUnitOptions.s: 1, |
127 | | - TimeUnitOptions.ms: 1e-3, |
128 | | - TimeUnitOptions.us: 1e-6, |
129 | | - TimeUnitOptions.ns: 1e-9, |
130 | | - TimeUnitOptions.ps: 1e-12, |
131 | | - } |
132 | | - |
133 | | - return conversion_factors[from_unit] / conversion_factors[to_unit] |
134 | | - |
135 | | - |
136 | | -def convert_to_energy(from_unit, to_unit): |
137 | | - """Convert energy from one unit to another unit |
138 | | - based on EnergyUnitOptions options |
139 | | - |
140 | | - Args: |
141 | | - from_unit (EnergyUnitOptions): Unit to convert from. |
142 | | - to_unit (EnergyUnitOptions): Unit to convert to. |
143 | | - |
144 | | - Returns: |
145 | | - float: Energy in the new unit. |
146 | | - """ |
147 | | - |
148 | | - # Conversion factors |
149 | | - conversion_factors = { |
150 | | - EnergyUnitOptions.meV: 1e-3, |
151 | | - EnergyUnitOptions.eV: 1, |
152 | | - EnergyUnitOptions.keV: 1e3, |
153 | | - EnergyUnitOptions.MeV: 1e6, |
154 | | - EnergyUnitOptions.GeV: 1e9, |
155 | | - # EnergyUnitOptions.J: 6.242e12, |
156 | | - EnergyUnitOptions.J: 1/electron_volt, |
157 | | - } |
158 | | - |
159 | | - return conversion_factors[from_unit] / conversion_factors[to_unit] |
160 | | - |
161 | | - |
162 | | -def convert_distance_units(from_unit, to_unit): |
163 | | - """Convert distance from one unit to another unit |
164 | | - based on DistanceUnitOptions options |
165 | | - |
166 | | - Args: |
167 | | - from_unit (DistanceUnitOptions): Unit to convert from. |
168 | | - to_unit (DistanceUnitOptions): Unit to convert to. |
169 | | - |
170 | | - Returns: |
171 | | - float: distance in the new unit. |
172 | | - """ |
173 | | - |
174 | | - # Conversion factors |
175 | | - conversion_factors = { |
176 | | - DistanceUnitOptions.nm: 1e-9, |
177 | | - DistanceUnitOptions.cm: 1e-2, |
178 | | - DistanceUnitOptions.pm: 1e-12, |
179 | | - DistanceUnitOptions.m: 1, |
180 | | - DistanceUnitOptions.angstrom: 1e-10, |
181 | | - } |
182 | | - |
183 | | - return conversion_factors[from_unit] / conversion_factors[to_unit] |
184 | | - |
185 | | - |
186 | | -def convert_array_from_time_to_lambda(time_array: np.ndarray, |
187 | | - time_unit: TimeUnitOptions, |
188 | | - distance_source_detector: float, |
189 | | - distance_source_detector_unit: DistanceUnitOptions, |
190 | | - detector_offset: float, |
191 | | - detector_offset_unit: DistanceUnitOptions, |
192 | | - lambda_unit: DistanceUnitOptions) -> np.ndarray: |
193 | | - """Convert an array of time values to wavelength values. |
194 | | - |
195 | | - Args: |
196 | | - time_array (np.ndarray): Array of time values. |
197 | | - time_unit (TimeUnitOptions): Unit of the input time. |
198 | | - distance_source_detector (float): Distance from the source to the detector. |
199 | | - distance_source_detector_unit (DistanceUnitOptions): Unit of the distance. |
200 | | - detector_offset (float): Offset of the detector. |
201 | | - detector_offset_unit (DistanceUnitOptions): Unit of the offset. |
202 | | - lambda_unit (DistanceUnitOptions): Unit of the output wavelength. |
203 | | - |
204 | | - This is using the formula: lambda_m = h/(m_n * distance_source_detector_m) * (time_array_s + detector_offset_s) |
205 | | - |
206 | | - Returns: |
207 | | - np.ndarray: Array of wavelength values. |
208 | | - """ |
209 | | - time_array_s = time_array * convert_time_units(time_unit, TimeUnitOptions.s) |
210 | | - detector_offset_s = detector_offset * convert_time_units(detector_offset_unit, TimeUnitOptions.s) |
211 | | - distance_source_detector_m = distance_source_detector * convert_distance_units(distance_source_detector_unit, DistanceUnitOptions.m) |
212 | | - |
213 | | - h_over_mn = h / m_n |
214 | | - lambda_m = h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m |
215 | | - |
216 | | - lambda_converted = lambda_m * convert_distance_units(DistanceUnitOptions.m, lambda_unit) |
217 | | - |
218 | | - return lambda_converted |
219 | | - |
220 | | - |
221 | | -def convert_from_wavelength_to_energy_ev(wavelength, |
222 | | - unit_from=DistanceUnitOptions.angstrom): |
223 | | - """Convert wavelength to energy based on the given units. |
224 | | - |
225 | | - Args: |
226 | | - wavelength (float): Wavelength value. |
227 | | - unit_from (WavelengthUnitOptions): Unit of the input wavelength. |
228 | | - |
229 | | - Returns: |
230 | | - float: Energy in the new unit. |
231 | | - """ |
232 | | - wavelength_m = wavelength * convert_distance_units(unit_from, DistanceUnitOptions.m) |
233 | | - energy = h * c / wavelength_m |
234 | | - energy = energy * convert_to_energy(EnergyUnitOptions.J, EnergyUnitOptions.eV) |
235 | | - return energy |
236 | | - |
237 | | - |
238 | | -def convert_array_from_time_to_energy(time_array: np.ndarray, |
239 | | - time_unit: TimeUnitOptions, |
240 | | - distance_source_detector: float, |
241 | | - distance_source_detector_unit: DistanceUnitOptions, |
242 | | - detector_offset: float, |
243 | | - detector_offset_unit: DistanceUnitOptions, |
244 | | - energy_unit: EnergyUnitOptions) -> np.ndarray: |
245 | | - """Convert an array of time values to energy values. |
246 | | - |
247 | | - Args: |
248 | | - time_array (np.ndarray): Array of time values. |
249 | | - time_unit (TimeUnitOptions): Unit of the input time. |
250 | | - distance_source_detector (float): Distance from the source to the detector. |
251 | | - distance_source_detector_unit (DistanceUnitOptions): Unit of the distance. |
252 | | - detector_offset (float): Offset of the detector. |
253 | | - detector_offset_unit (DistanceUnitOptions): Unit of the offset. |
254 | | - energy_unit (EnergyUnitOptions): Unit of the output energy. |
255 | | - |
256 | | - This is using the formula: E = h/(m_n * distance_source_detector_m) * (time_array_s + detector_offset_s) |
257 | | - |
258 | | - Returns: |
259 | | - np.ndarray: Array of energy values. |
260 | | - """ |
261 | | - lambda_in_angstroms = convert_array_from_time_to_lambda(time_array, |
262 | | - time_unit, |
263 | | - distance_source_detector, |
264 | | - distance_source_detector_unit, |
265 | | - detector_offset, |
266 | | - detector_offset_unit, |
267 | | - DistanceUnitOptions.angstrom) |
268 | | - |
269 | | - energy_array_ev = [convert_from_wavelength_to_energy_ev(l, DistanceUnitOptions.angstrom) for l in lambda_in_angstroms] |
270 | | - energy_array = np.array(energy_array_ev) |
271 | | - |
272 | | - return energy_array |
273 | | - |
274 | | - |
275 | 90 | def normalization_with_list_of_runs(sample_run_numbers: list = None, |
276 | 91 | ob_run_numbers: list = None, |
277 | 92 | output_folder: str ="./", |
@@ -448,12 +263,12 @@ def normalization_with_list_of_runs(sample_run_numbers: list = None, |
448 | 263 | detector_offset_unit=TimeUnitOptions.us, |
449 | 264 | lambda_unit=DistanceUnitOptions.angstrom) |
450 | 265 | energy_array = convert_array_from_time_to_energy(time_array=time_spectra, |
451 | | - time_unit=TimeUnitOptions.s, |
452 | | - distance_source_detector=distance_source_detector_m, |
453 | | - distance_source_detector_unit=DistanceUnitOptions.m, |
454 | | - detector_offset=detector_delay_us, |
455 | | - detector_offset_unit=TimeUnitOptions.us, |
456 | | - energy_unit=EnergyUnitOptions.eV) |
| 266 | + time_unit=TimeUnitOptions.s, |
| 267 | + distance_source_detector=distance_source_detector_m, |
| 268 | + distance_source_detector_unit=DistanceUnitOptions.m, |
| 269 | + detector_offset=detector_delay_us, |
| 270 | + detector_offset_unit=TimeUnitOptions.us, |
| 271 | + energy_unit=EnergyUnitOptions.eV) |
457 | 272 |
|
458 | 273 | if preview: |
459 | 274 |
|
|
0 commit comments