source: sasview/calculator/instrument.py @ 19637b1

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 19637b1 was 19637b1, checked in by Jae Cho <jhjcho@…>, 13 years ago

fixed a small bug and removed unnecessary raise

  • Property mode set to 100644
File size: 8.3 KB
Line 
1"""
2This module is a small tool to allow user to
3control instrumental parameters
4"""
5import 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 = 30.0
20_INTENSITY = 368428
21_WAVE_LENGTH = 6.0
22_WAVE_SPREAD = 0.125
23_MASS = 1.67492729E-24 #[gr]
24X_VAL = numpy.linspace(0, 30, 310)
25_LAMBDA_ARRAY = [[0, 5, 10, 15, 20, 25, 30], [1, 1, 1, 1, 1, 1, 1]]
26
27class 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       
39    def set_source_size(self, size =[]):
40        """
41        Set the source aperture size
42        """
43        if len(size) == 0:
44            self.source_size = 0.0 
45        else:
46            self.source_size = size
47            validate(size[0])
48    def set_sample_size(self, size =[]):
49        """
50        Set the sample aperture size
51        """
52        if len(size) == 0:
53            self.sample_size = 0.0 
54        else:
55            self.sample_size = size
56            validate(size[0])
57           
58    def set_sample_distance(self, distance = []):
59        """
60        Set the sample aperture distance
61        """
62        if len(distance) == 0:
63            self.sample_distance = 0.0 
64        else:
65            self.sample_distance = distance
66            validate(distance[0])
67       
68   
69class Sample(object):
70    """
71    An object class that defines the sample variables
72    """
73    def __init__(self):
74       
75        # assumes that all aligned at the centers
76        # source2sample or sample2detector distance
77        self.distance = _SAMPLE_OFFSET
78        self.size = _SAMPLE_SIZE
79        self.thickness = _SAMPLE_THICKNESS
80
81   
82    def set_size(self, size =[]):
83        """
84        Set the sample size
85        """
86        if len(size) == 0:
87            self.sample_size = 0.0 
88        else:
89            self.sample_size = size
90            validate(size[0])
91           
92    def set_thickness(self, thickness = 0.0):
93        """
94        Set the sample thickness
95        """
96        self.thickness = thickness
97        validate(thickness)
98   
99    def set_distance(self, distance = []):
100        """
101        Set the sample distance
102        """
103        if len(distance) == 0:
104            self.distance = 0.0 
105        else:
106            self.distance = distance
107            if distance[0] != 0.0:
108                validate(distance[0])
109       
110 
111class 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       
125    def set_size(self, size =[]):
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           
135    def set_pix_size(self, size = []):
136        """
137        Set the detector pix_size
138        """
139        if len(size) == 0:
140            self.pix_size = 0 
141        else:
142            self.pix_size = size
143            validate(size[0])
144   
145    def set_distance(self, distance = []):
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])
154   
155       
156class Neutron(object):
157    """
158    An object that defines the wavelength variables
159    """
160    def __init__(self):
161       
162        # intensity in counts/sec
163        self.intensity = _INTENSITY
164        # neutron mass in cgs unit
165        self.mass = _MASS
166        # wavelength spectrum
167        self.spectrum = []
168        # wavelength
169        self.wavelength = _WAVE_LENGTH
170        # wavelength spread (FWHM)
171        self.wavelength_spread = _WAVE_SPREAD
172        # mean wavelength
173        self.mean = _WAVE_LENGTH
174        # wavelength = distribution after velocity selector
175        self.peak = []
176        # std of of the spectrum
177        self.std = None
178        # min max range of the spectrum
179        self.min = _MIN
180        self.max = _MAX
181        # x-range of the spectrum
182        self.x_val = X_VAL
183        # default distribution function
184        # ex., 'gaussian', or an array
185        self.func = _LAMBDA_ARRAY
186        # default unit of the thickness
187        self.wavelength_unit = 'A'
188       
189    def set_intensity(self, intensity = 368428):
190        """
191        Sets the intensity in counts/sec
192        """
193        self.intensity = intensity 
194        validate(intensity) 
195           
196    def set_wavelength(self, wavelength = _WAVE_LENGTH):
197        """
198        Sets the wavelength
199        """
200        self.wavelength = wavelength
201        validate(wavelength)
202
203    def set_mass(self, mass = _MASS):
204        """
205        Sets the wavelength
206        """
207        self.mass = mass
208        validate(mass)
209       
210    def set_wavelength_spread(self, spread = _WAVE_SPREAD):
211        """
212        Sets the wavelength spread
213        """
214        self.wavelength_spread = spread
215        if spread != 0.0:
216            validate(spread)
217       
218    def get_intensity(self):
219        """
220        To get the value of intensity
221        """
222        return self.intensity
223       
224    def get_wavelength(self):
225        """
226        To get the value of wavelength
227        """
228        return self.wavelength
229
230    def get_mass(self):
231        """
232        To get the neutron mass
233        """
234        return self.mass
235   
236    def get_wavelength_spread(self):
237        """
238        To get the value of wavelength spread
239        """
240        return self.wavelength_spread
241   
242    def get_ramdom_value(self):
243        """
244        To get the value of wave length
245        """
246        return self.wavelength
247   
248    def _set_mean(self):
249        """
250        To get mean value of wavelength
251        """
252        mean_value = numpy.mean(self.peak[0]*
253                                self.peak[1])
254        self.mean = mean_value
255
256       
257    def get_mean_peakvalue(self):
258        """
259        To get mean value of wavelength
260        """
261        mean_value = numpy.mean(self.peak[1])
262        return mean_value
263           
264    def get_spectrum(self):
265        """
266        To get the wavelength spectrum
267        """
268        return self.spectrum
269   
270    def plot_spectrum(self):
271        """
272        To plot the wavelength spactrum
273        : requirment: matplotlib.pyplot
274        """
275        try:
276            import matplotlib.pyplot as plt
277            plt.plot(self.x_val, self.spectrum, linewidth = 2, color = 'r')
278            plt.legend(['Spectrum'], loc = 'best')
279            plt.show()
280        except:
281            raise RuntimeError, "Can't import matplotlib required to plot..."
282       
283    def plot_peak(self):
284        """
285        To plot the wavelength peak spactrum
286        : requirment: matplotlib.pyplot
287        """
288        try:
289            min = self.mean * (1 - self.wavelength_spread)
290            max = self.mean * (1 + self.wavelength_spread)
291            x_val =   numpy.linspace(min, max, 310)
292            import matplotlib.pyplot as plt
293            plt.plot(x_val, self.peak, linewidth = 2, color = 'r')
294            plt.legend(['peak'], loc = 'best')
295            plt.show()
296        except:
297            raise RuntimeError, "Can't import matplotlib required to plot..."
298
299def validate(value = None):
300    """
301    Check if the value is folat > 0.0
302   
303    :return value: True / False
304    """
305    try:
306        val = float(value)
307        if val >= 0:
308            val = True
309        else:
310            val = False
311    except:
312        val = False
313    #if not val:
314    #    raise ValueError, "Got improper value..."
Note: See TracBrowser for help on using the repository browser.