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