1 | """ |
---|
2 | This module is a small tool to allow user to |
---|
3 | control instrumental parameters |
---|
4 | """ |
---|
5 | import numpy |
---|
6 | |
---|
7 | # defaults in cgs unit |
---|
8 | _SAMPLE_A_SIZE = [1.27] |
---|
9 | _SOURCE_A_SIZE = [3.81] |
---|
10 | _SAMPLE_DISTANCE = [1627, 0] |
---|
11 | _SAMPLE_OFFSET = [0, 0] |
---|
12 | _SAMPLE_SIZE = [2.54] |
---|
13 | _SAMPLE_THICKNESS = 0.2 |
---|
14 | _D_DISTANCE = [1000, 0] |
---|
15 | _D_SIZE = [128, 128] |
---|
16 | _D_PIX_SIZE = [0.5, 0.5] |
---|
17 | |
---|
18 | _MIN = 0.0 |
---|
19 | _MAX = 50.0 |
---|
20 | _INTENSITY = 368428 |
---|
21 | _WAVE_LENGTH = 6.0 |
---|
22 | _WAVE_SPREAD = 0.125 |
---|
23 | _MASS = 1.67492729E-24 #[gr] |
---|
24 | _LAMBDA_ARRAY = [[0, 1e+16],[_INTENSITY, _INTENSITY]] |
---|
25 | |
---|
26 | class Aperture(object): |
---|
27 | """ |
---|
28 | An object class that defines the aperture variables |
---|
29 | """ |
---|
30 | def __init__(self): |
---|
31 | |
---|
32 | # assumes that all aligned at the centers |
---|
33 | # aperture_size [diameter] for pinhole, [dx, dy] for rectangular |
---|
34 | self.sample_size = _SAMPLE_A_SIZE |
---|
35 | self.source_size = _SOURCE_A_SIZE |
---|
36 | self.sample_distance = _SAMPLE_DISTANCE |
---|
37 | |
---|
38 | def set_source_size(self, size =[]): |
---|
39 | """ |
---|
40 | Set the source aperture size |
---|
41 | """ |
---|
42 | if len(size) == 0: |
---|
43 | self.source_size = 0.0 |
---|
44 | else: |
---|
45 | self.source_size = size |
---|
46 | validate(size[0]) |
---|
47 | def set_sample_size(self, size =[]): |
---|
48 | """ |
---|
49 | Set the sample aperture size |
---|
50 | """ |
---|
51 | if len(size) == 0: |
---|
52 | self.sample_size = 0.0 |
---|
53 | else: |
---|
54 | self.sample_size = size |
---|
55 | validate(size[0]) |
---|
56 | |
---|
57 | def set_sample_distance(self, distance = []): |
---|
58 | """ |
---|
59 | Set the sample aperture distance |
---|
60 | """ |
---|
61 | if len(distance) == 0: |
---|
62 | self.sample_distance = 0.0 |
---|
63 | else: |
---|
64 | self.sample_distance = distance |
---|
65 | validate(distance[0]) |
---|
66 | |
---|
67 | |
---|
68 | class Sample(object): |
---|
69 | """ |
---|
70 | An object class that defines the sample variables |
---|
71 | """ |
---|
72 | def __init__(self): |
---|
73 | |
---|
74 | # assumes that all aligned at the centers |
---|
75 | # source2sample or sample2detector distance |
---|
76 | self.distance = _SAMPLE_OFFSET |
---|
77 | self.size = _SAMPLE_SIZE |
---|
78 | self.thickness = _SAMPLE_THICKNESS |
---|
79 | |
---|
80 | |
---|
81 | def set_size(self, size =[]): |
---|
82 | """ |
---|
83 | Set the sample size |
---|
84 | """ |
---|
85 | if len(size) == 0: |
---|
86 | self.sample_size = 0.0 |
---|
87 | else: |
---|
88 | self.sample_size = size |
---|
89 | validate(size[0]) |
---|
90 | |
---|
91 | def set_thickness(self, thickness = 0.0): |
---|
92 | """ |
---|
93 | Set the sample thickness |
---|
94 | """ |
---|
95 | self.thickness = thickness |
---|
96 | validate(thickness) |
---|
97 | |
---|
98 | def set_distance(self, distance = []): |
---|
99 | """ |
---|
100 | Set the sample distance |
---|
101 | """ |
---|
102 | if len(distance) == 0: |
---|
103 | self.distance = 0.0 |
---|
104 | else: |
---|
105 | self.distance = distance |
---|
106 | if distance[0] != 0.0: |
---|
107 | validate(distance[0]) |
---|
108 | |
---|
109 | |
---|
110 | class Detector(object): |
---|
111 | """ |
---|
112 | An object class that defines the detector variables |
---|
113 | """ |
---|
114 | def __init__(self): |
---|
115 | |
---|
116 | # assumes that all aligned at the centers |
---|
117 | # source2sample or sample2detector distance |
---|
118 | self.distance = _D_DISTANCE |
---|
119 | self.size = _D_SIZE |
---|
120 | self.pix_size = _D_PIX_SIZE |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | def set_size(self, size =[]): |
---|
125 | """ |
---|
126 | Set the detector size |
---|
127 | """ |
---|
128 | if len(size) == 0: |
---|
129 | self.size = 0 |
---|
130 | else: |
---|
131 | self.size = size |
---|
132 | validate(size[0]) |
---|
133 | |
---|
134 | def set_pix_size(self, size = []): |
---|
135 | """ |
---|
136 | Set the detector pix_size |
---|
137 | """ |
---|
138 | if len(size) == 0: |
---|
139 | self.pix_size = 0 |
---|
140 | else: |
---|
141 | self.pix_size = size |
---|
142 | validate(size[0]) |
---|
143 | |
---|
144 | def set_distance(self, distance = []): |
---|
145 | """ |
---|
146 | Set the detector distance |
---|
147 | """ |
---|
148 | if len(distance) == 0: |
---|
149 | self.distance = 0 |
---|
150 | else: |
---|
151 | self.distance = distance |
---|
152 | validate(distance[0]) |
---|
153 | |
---|
154 | |
---|
155 | class Neutron(object): |
---|
156 | """ |
---|
157 | An object that defines the wavelength variables |
---|
158 | """ |
---|
159 | def __init__(self): |
---|
160 | |
---|
161 | # neutron mass in cgs unit |
---|
162 | self.mass = _MASS |
---|
163 | |
---|
164 | # wavelength |
---|
165 | self.wavelength = _WAVE_LENGTH |
---|
166 | # wavelength spread (FWHM) |
---|
167 | self.wavelength_spread = _WAVE_SPREAD |
---|
168 | # wavelength spectrum |
---|
169 | self.spectrum = self.get_default_spectrum() |
---|
170 | # intensity in counts/sec |
---|
171 | self.intensity = numpy.interp(self.wavelength, |
---|
172 | self.spectrum[0], |
---|
173 | self.spectrum[1], |
---|
174 | 0.0, |
---|
175 | 0.0) |
---|
176 | # min max range of the spectrum |
---|
177 | self.min = min(self.spectrum[0]) |
---|
178 | self.max = max(self.spectrum[0]) |
---|
179 | # wavelength band |
---|
180 | self.band = [self.min, self.max] |
---|
181 | |
---|
182 | # default unit of the thickness |
---|
183 | self.wavelength_unit = 'A' |
---|
184 | |
---|
185 | def set_full_band(self): |
---|
186 | """ |
---|
187 | set band to default value |
---|
188 | """ |
---|
189 | self.band = self.spectrum |
---|
190 | def set_spectrum(self, spectrum): |
---|
191 | """ |
---|
192 | Set spectrum |
---|
193 | |
---|
194 | :param spectrum: numpy array |
---|
195 | """ |
---|
196 | self.spectrum = spectrum |
---|
197 | self.setup_spectrum() |
---|
198 | |
---|
199 | def setup_spectrum(self): |
---|
200 | """ |
---|
201 | To set the wavelength spectrum, and intensity, assumes |
---|
202 | wavelength is already within the spectrum |
---|
203 | """ |
---|
204 | spectrum = self.spectrum |
---|
205 | intensity = numpy.interp(self.wavelength, |
---|
206 | spectrum[0], |
---|
207 | spectrum[1], |
---|
208 | 0.0, |
---|
209 | 0.0) |
---|
210 | self.set_intensity(intensity) |
---|
211 | # min max range of the spectrum |
---|
212 | self.min = min(self.spectrum[0]) |
---|
213 | self.max = max(self.spectrum[0]) |
---|
214 | # set default band |
---|
215 | self.set_band([self.min,self.max]) |
---|
216 | |
---|
217 | def set_band(self, band=[]): |
---|
218 | """ |
---|
219 | To set the wavelength band |
---|
220 | |
---|
221 | :param band: array of [min, max] |
---|
222 | """ |
---|
223 | # check if the wavelength is in range |
---|
224 | if min(band) < self.min or\ |
---|
225 | max(band) > self.max: |
---|
226 | raise |
---|
227 | self.band = band |
---|
228 | |
---|
229 | |
---|
230 | def set_intensity(self, intensity = 368428): |
---|
231 | """ |
---|
232 | Sets the intensity in counts/sec |
---|
233 | """ |
---|
234 | self.intensity = intensity |
---|
235 | validate(intensity) |
---|
236 | |
---|
237 | def set_wavelength(self, wavelength = _WAVE_LENGTH): |
---|
238 | """ |
---|
239 | Sets the wavelength |
---|
240 | """ |
---|
241 | # check if the wavelength is in range |
---|
242 | if wavelength < min(self.band) or\ |
---|
243 | wavelength > max(self.band): |
---|
244 | raise |
---|
245 | self.wavelength = wavelength |
---|
246 | validate(wavelength) |
---|
247 | self.intensity = numpy.interp(self.wavelength, |
---|
248 | self.spectrum[0], |
---|
249 | self.spectrum[1], |
---|
250 | 0.0, |
---|
251 | 0.0) |
---|
252 | |
---|
253 | |
---|
254 | def set_mass(self, mass = _MASS): |
---|
255 | """ |
---|
256 | Sets the wavelength |
---|
257 | """ |
---|
258 | self.mass = mass |
---|
259 | validate(mass) |
---|
260 | |
---|
261 | def set_wavelength_spread(self, spread = _WAVE_SPREAD): |
---|
262 | """ |
---|
263 | Sets the wavelength spread |
---|
264 | """ |
---|
265 | self.wavelength_spread = spread |
---|
266 | if spread != 0.0: |
---|
267 | validate(spread) |
---|
268 | |
---|
269 | def get_intensity(self): |
---|
270 | """ |
---|
271 | To get the value of intensity |
---|
272 | """ |
---|
273 | return self.intensity |
---|
274 | |
---|
275 | def get_wavelength(self): |
---|
276 | """ |
---|
277 | To get the value of wavelength |
---|
278 | """ |
---|
279 | return self.wavelength |
---|
280 | |
---|
281 | def get_mass(self): |
---|
282 | """ |
---|
283 | To get the neutron mass |
---|
284 | """ |
---|
285 | return self.mass |
---|
286 | |
---|
287 | def get_wavelength_spread(self): |
---|
288 | """ |
---|
289 | To get the value of wavelength spread |
---|
290 | """ |
---|
291 | return self.wavelength_spread |
---|
292 | |
---|
293 | def get_ramdom_value(self): |
---|
294 | """ |
---|
295 | To get the value of wave length |
---|
296 | """ |
---|
297 | return self.wavelength |
---|
298 | |
---|
299 | def get_spectrum(self): |
---|
300 | """ |
---|
301 | To get the wavelength spectrum |
---|
302 | """ |
---|
303 | return self.spectrum |
---|
304 | def get_default_spectrum(self): |
---|
305 | """ |
---|
306 | get default spectrum |
---|
307 | """ |
---|
308 | return numpy.array(_LAMBDA_ARRAY) |
---|
309 | def get_band(self): |
---|
310 | """ |
---|
311 | To get the wavelength band |
---|
312 | """ |
---|
313 | return self.band |
---|
314 | |
---|
315 | def plot_spectrum(self): |
---|
316 | """ |
---|
317 | To plot the wavelength spactrum |
---|
318 | : requirment: matplotlib.pyplot |
---|
319 | """ |
---|
320 | try: |
---|
321 | import matplotlib.pyplot as plt |
---|
322 | plt.plot(self.spectrum[0], self.spectrum[1], linewidth = 2, color = 'r') |
---|
323 | plt.legend(['Spectrum'], loc = 'best') |
---|
324 | plt.show() |
---|
325 | except: |
---|
326 | raise RuntimeError, "Can't import matplotlib required to plot..." |
---|
327 | |
---|
328 | |
---|
329 | class TOF(Neutron): |
---|
330 | """ |
---|
331 | TOF: make list of wavelength and wave length spreads |
---|
332 | """ |
---|
333 | def __init__(self): |
---|
334 | """ |
---|
335 | Init |
---|
336 | """ |
---|
337 | Neutron.__init__(self) |
---|
338 | #self.switch = switch |
---|
339 | self.wavelength_list = [self.wavelength] |
---|
340 | self.wavelength_spread_list = [self.wavelength_spread] |
---|
341 | self.intensity_list = self.get_intensity_list() |
---|
342 | |
---|
343 | def get_intensity_list(self): |
---|
344 | """ |
---|
345 | get list of the intensity wrt wavelength_list |
---|
346 | """ |
---|
347 | out = numpy.interp(self.wavelength_list, |
---|
348 | self.spectrum[0], |
---|
349 | self.spectrum[1], |
---|
350 | 0.0, |
---|
351 | 0.0) |
---|
352 | return out |
---|
353 | |
---|
354 | def get_wave_list(self): |
---|
355 | """ |
---|
356 | Get wavelength and wavelength_spread list |
---|
357 | """ |
---|
358 | return self.wavelength_list, self.wavelengthspread_list |
---|
359 | |
---|
360 | def set_wave_list(self, wavelength=[]): |
---|
361 | """ |
---|
362 | Set wavelength list |
---|
363 | |
---|
364 | :param wavelength: list of wavelengths |
---|
365 | """ |
---|
366 | self.wavelength_list = wavelength |
---|
367 | |
---|
368 | def set_wave_spread_list(self, wavelength_spread=[]): |
---|
369 | """ |
---|
370 | Set wavelength_spread list |
---|
371 | |
---|
372 | :param wavelength_spread: list of wavelength spreads |
---|
373 | """ |
---|
374 | self.wavelengthspread_list = wavelength_spread |
---|
375 | |
---|
376 | |
---|
377 | def validate(value = None): |
---|
378 | """ |
---|
379 | Check if the value is folat > 0.0 |
---|
380 | |
---|
381 | :return value: True / False |
---|
382 | """ |
---|
383 | try: |
---|
384 | val = float(value) |
---|
385 | if val >= 0: |
---|
386 | val = True |
---|
387 | else: |
---|
388 | val = False |
---|
389 | except: |
---|
390 | val = False |
---|
391 | #if not val: |
---|
392 | # raise ValueError, "Got improper value..." |
---|