1 | """ |
---|
2 | This software was developed by the University of Tennessee as part of the |
---|
3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | project funded by the US National Science Foundation. |
---|
5 | |
---|
6 | See the license text in license.txt |
---|
7 | |
---|
8 | copyright 2008, 2009, University of Tennessee |
---|
9 | """ |
---|
10 | |
---|
11 | import wx |
---|
12 | import sys |
---|
13 | import os |
---|
14 | |
---|
15 | #from DataLoader.readers.ascii_reader import Reader |
---|
16 | from sans.guicomm.events import StatusEvent |
---|
17 | from sans.calculator.slit_length_calculator import SlitlengthCalculator |
---|
18 | from calculator_widgets import OutputTextCtrl |
---|
19 | from calculator_widgets import InterActiveOutputTextCtrl |
---|
20 | |
---|
21 | _BOX_WIDTH = 76 |
---|
22 | #Slit length panel size |
---|
23 | if sys.platform.count("win32") > 0: |
---|
24 | PANEL_WIDTH = 500 |
---|
25 | PANEL_HEIGHT = 200 |
---|
26 | FONT_VARIANT = 0 |
---|
27 | else: |
---|
28 | PANEL_WIDTH = 530 |
---|
29 | PANEL_HEIGHT = 230 |
---|
30 | FONT_VARIANT = 1 |
---|
31 | |
---|
32 | class SlitLengthCalculatorPanel(wx.Panel): |
---|
33 | """ |
---|
34 | Provides the slit length calculator GUI. |
---|
35 | """ |
---|
36 | ## Internal nickname for the window, used by the AUI manager |
---|
37 | window_name = "Slit Size Calculator" |
---|
38 | ## Name to appear on the window title bar |
---|
39 | window_caption = "Slit Size Calculator" |
---|
40 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
41 | CENTER_PANE = True |
---|
42 | |
---|
43 | def __init__(self, parent, *args, **kwds): |
---|
44 | wx.Panel.__init__(self, parent, *args, **kwds) |
---|
45 | #Font size |
---|
46 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
47 | #thread to read data |
---|
48 | self.reader = None |
---|
49 | # Default location |
---|
50 | self._default_save_location = os.getcwd() |
---|
51 | # Object that receive status event |
---|
52 | self.parent = parent |
---|
53 | self._do_layout() |
---|
54 | |
---|
55 | def _define_structure(self): |
---|
56 | """ |
---|
57 | Define the main sizers building to build this application. |
---|
58 | """ |
---|
59 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
60 | self.box_source = wx.StaticBox(self, -1,str("Slit Size Calculator")) |
---|
61 | self.boxsizer_source = wx.StaticBoxSizer(self.box_source, |
---|
62 | wx.VERTICAL) |
---|
63 | self.data_name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
64 | self.slit_size_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
65 | self.hint_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
66 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
67 | |
---|
68 | def _layout_data_name(self): |
---|
69 | """ |
---|
70 | Fill the sizer containing data's name |
---|
71 | """ |
---|
72 | data_name_txt = wx.StaticText(self, -1, 'Data: ') |
---|
73 | self.data_name_tcl = OutputTextCtrl(self, -1, |
---|
74 | size=(_BOX_WIDTH*4, -1)) |
---|
75 | data_hint = "Loaded data" |
---|
76 | self.data_name_tcl.SetToolTipString(data_hint) |
---|
77 | #control that triggers importing data |
---|
78 | id = wx.NewId() |
---|
79 | self.browse_button = wx.Button(self, id, "Browse") |
---|
80 | hint_on_browse = "Click on this button to import data in this panel." |
---|
81 | self.browse_button.SetToolTipString(hint_on_browse) |
---|
82 | self.Bind(wx.EVT_BUTTON, self.on_load_data, id=id) |
---|
83 | self.data_name_sizer.AddMany([(data_name_txt, 0, wx.LEFT, 15), |
---|
84 | (self.data_name_tcl, 0, wx.LEFT, 10), |
---|
85 | (self.browse_button, 0, wx.LEFT, 10)]) |
---|
86 | def _layout_slit_size(self): |
---|
87 | """ |
---|
88 | Fill the sizer containing slit size information |
---|
89 | """ |
---|
90 | slit_size_txt = wx.StaticText(self, -1, 'Slit Size (FWHM/2): ') |
---|
91 | self.slit_size_tcl = InterActiveOutputTextCtrl(self, -1, |
---|
92 | size=(_BOX_WIDTH, -1)) |
---|
93 | slit_size_hint = " Estimated full slit size" |
---|
94 | self.slit_size_tcl.SetToolTipString(slit_size_hint) |
---|
95 | slit_size_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
96 | self.slit_size_unit_tcl = OutputTextCtrl(self, -1, |
---|
97 | size=(_BOX_WIDTH, -1)) |
---|
98 | slit_size_unit_hint = "Full slit size's unit" |
---|
99 | self.slit_size_unit_tcl.SetToolTipString(slit_size_unit_hint) |
---|
100 | self.slit_size_sizer.AddMany([(slit_size_txt, 0, wx.LEFT, 15), |
---|
101 | (self.slit_size_tcl, 0, wx.LEFT, 10), |
---|
102 | (slit_size_unit_txt, 0, wx.LEFT, 10), |
---|
103 | (self.slit_size_unit_tcl, 0, wx.LEFT, 10)]) |
---|
104 | |
---|
105 | def _layout_hint(self): |
---|
106 | """ |
---|
107 | Fill the sizer containing hint |
---|
108 | """ |
---|
109 | hint_msg = "This calculation works only for SAXSess beam profile data." |
---|
110 | self.hint_txt = wx.StaticText(self, -1, hint_msg) |
---|
111 | self.hint_sizer.AddMany([(self.hint_txt, 0, wx.LEFT, 15)]) |
---|
112 | |
---|
113 | def _layout_button(self): |
---|
114 | """ |
---|
115 | Do the layout for the button widgets |
---|
116 | """ |
---|
117 | self.bt_close = wx.Button(self, wx.ID_CANCEL,'Close') |
---|
118 | self.bt_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
119 | self.bt_close.SetToolTipString("Close this window.") |
---|
120 | self.button_sizer.AddMany([(self.bt_close, 0, wx.LEFT, 390)]) |
---|
121 | |
---|
122 | def _do_layout(self): |
---|
123 | """ |
---|
124 | Draw window content |
---|
125 | """ |
---|
126 | self._define_structure() |
---|
127 | self._layout_data_name() |
---|
128 | self._layout_slit_size() |
---|
129 | self._layout_hint() |
---|
130 | self._layout_button() |
---|
131 | self.boxsizer_source.AddMany([(self.data_name_sizer, 0, |
---|
132 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
133 | (self.slit_size_sizer, 0, |
---|
134 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
135 | (self.hint_sizer, 0, |
---|
136 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
137 | self.main_sizer.AddMany([(self.boxsizer_source, 0, wx.ALL, 10), |
---|
138 | (self.button_sizer, 0, |
---|
139 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
140 | self.SetSizer(self.main_sizer) |
---|
141 | self.SetAutoLayout(True) |
---|
142 | |
---|
143 | def choose_data_file(self, location=None): |
---|
144 | path = None |
---|
145 | filename = '' |
---|
146 | if location == None: |
---|
147 | location = os.getcwd() |
---|
148 | |
---|
149 | wildcard = "SAXSess Data 1D (*.DAT, *.dat)|*.DAT" |
---|
150 | |
---|
151 | dlg = wx.FileDialog(self, "Choose a file", location, |
---|
152 | "", wildcard, wx.OPEN) |
---|
153 | if dlg.ShowModal() == wx.ID_OK: |
---|
154 | path = dlg.GetPath() |
---|
155 | filename = os.path.basename(path) |
---|
156 | dlg.Destroy() |
---|
157 | |
---|
158 | return path |
---|
159 | |
---|
160 | def on_close(self, event): |
---|
161 | """ |
---|
162 | close the window containing this panel |
---|
163 | """ |
---|
164 | self.parent.Close() |
---|
165 | |
---|
166 | def on_load_data(self, event): |
---|
167 | """ |
---|
168 | Open a file dialog to allow the user to select a given file. |
---|
169 | The user is only allow to load file with extension .DAT or .dat. |
---|
170 | Display the slit size corresponding to the loaded data. |
---|
171 | """ |
---|
172 | path = self.choose_data_file(location=self._default_save_location) |
---|
173 | |
---|
174 | if path is None: |
---|
175 | return |
---|
176 | self._default_save_location = path |
---|
177 | try: |
---|
178 | #Load data |
---|
179 | from load_thread import DataReader |
---|
180 | ## If a thread is already started, stop it |
---|
181 | if self.reader is not None and self.reader.isrunning(): |
---|
182 | self.reader.stop() |
---|
183 | if self.parent.parent is not None: |
---|
184 | wx.PostEvent(self.parent.parent, |
---|
185 | StatusEvent(status="Loading...", |
---|
186 | type="progress")) |
---|
187 | self.reader = DataReader(path=path, |
---|
188 | completefn=self.complete_loading, |
---|
189 | updatefn=None) |
---|
190 | self.reader.queue() |
---|
191 | except: |
---|
192 | if self.parent.parent is None: |
---|
193 | return |
---|
194 | msg = "Slit Length Calculator: %s" % (sys.exc_value) |
---|
195 | wx.PostEvent(self.parent.parent, |
---|
196 | StatusEvent(status=msg, type='stop')) |
---|
197 | return |
---|
198 | |
---|
199 | def complete_loading(self, data=None, filename=''): |
---|
200 | """ |
---|
201 | Complete the loading and compute the slit size |
---|
202 | """ |
---|
203 | if data is None or data.__class__.__name__ == 'Data2D': |
---|
204 | if self.parent.parent is None: |
---|
205 | return |
---|
206 | msg = "Slit Length cannot be computed for 2D Data" |
---|
207 | wx.PostEvent(self.parent.parent, |
---|
208 | StatusEvent(status=msg, type='stop')) |
---|
209 | return |
---|
210 | self.data_name_tcl.SetValue(str(data.filename)) |
---|
211 | #compute the slit size |
---|
212 | try: |
---|
213 | x = data.x |
---|
214 | y = data.y |
---|
215 | if x == [] or x is None or y == [] or y is None: |
---|
216 | msg = "The current data is empty please check x and y" |
---|
217 | raise ValueError, msg |
---|
218 | slit_length_calculator = SlitlengthCalculator() |
---|
219 | slit_length_calculator.set_data(x=x, y=y) |
---|
220 | slit_length = slit_length_calculator.calculate_slit_length() |
---|
221 | except: |
---|
222 | if self.parent.parent is None: |
---|
223 | return |
---|
224 | msg = "Slit Size Calculator: %s" % (sys.exc_value) |
---|
225 | wx.PostEvent(self.parent.parent, |
---|
226 | StatusEvent(status=msg, type='stop')) |
---|
227 | return |
---|
228 | self.slit_size_tcl.SetValue(str(slit_length)) |
---|
229 | #Display unit |
---|
230 | self.slit_size_unit_tcl.SetValue('[Unknown]') |
---|
231 | if self.parent.parent is None: |
---|
232 | return |
---|
233 | msg = "Load Complete" |
---|
234 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop')) |
---|
235 | |
---|
236 | |
---|
237 | class SlitLengthCalculatorWindow(wx.Frame): |
---|
238 | """ |
---|
239 | """ |
---|
240 | def __init__(self, parent=None, title="Slit Size Calculator"): |
---|
241 | """ |
---|
242 | """ |
---|
243 | wx.Frame.__init__(self, parent, title, |
---|
244 | size=(PANEL_WIDTH,PANEL_HEIGHT)) |
---|
245 | self.parent = parent |
---|
246 | self.panel = SlitLengthCalculatorPanel(parent=self) |
---|
247 | self.Centre() |
---|
248 | self.Show(True) |
---|
249 | |
---|
250 | if __name__ == "__main__": |
---|
251 | app = wx.PySimpleApp() |
---|
252 | frame = SlitLengthCalculatorWindow() |
---|
253 | frame.Show(True) |
---|
254 | app.MainLoop() |
---|