1 | """ |
---|
2 | This module provides a GUI for the file converter |
---|
3 | """ |
---|
4 | |
---|
5 | import wx |
---|
6 | import sys |
---|
7 | import numpy as np |
---|
8 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
9 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
10 | from sas.sasgui.perspectives.calculator import calculator_widgets as widget |
---|
11 | from sas.sasgui.perspectives.file_converter.converter_widgets import VectorInput |
---|
12 | from sas.sasgui.perspectives.file_converter.meta_panels import MetadataWindow |
---|
13 | from sas.sasgui.perspectives.file_converter.meta_panels import DetectorPanel |
---|
14 | from sas.sasgui.perspectives.file_converter.meta_panels import SamplePanel |
---|
15 | from sas.sasgui.guiframe.events import StatusEvent |
---|
16 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
17 | from sas.sasgui.guiframe.utils import check_float |
---|
18 | from sas.sascalc.dataloader.readers.cansas_reader import Reader as CansasReader |
---|
19 | from sas.sasgui.perspectives.file_converter.bsl_loader import BSLLoader |
---|
20 | from sas.sascalc.dataloader.data_info import Detector |
---|
21 | from sas.sascalc.dataloader.data_info import Sample |
---|
22 | from sas.sascalc.dataloader.data_info import Vector |
---|
23 | |
---|
24 | # Panel size |
---|
25 | if sys.platform.count("win32") > 0: |
---|
26 | PANEL_TOP = 0 |
---|
27 | _STATICBOX_WIDTH = 410 |
---|
28 | _BOX_WIDTH = 200 |
---|
29 | PANEL_SIZE = 480 |
---|
30 | FONT_VARIANT = 0 |
---|
31 | else: |
---|
32 | PANEL_TOP = 60 |
---|
33 | _STATICBOX_WIDTH = 430 |
---|
34 | _BOX_WIDTH = 200 |
---|
35 | PANEL_SIZE = 500 |
---|
36 | FONT_VARIANT = 1 |
---|
37 | |
---|
38 | class ConverterPanel(ScrolledPanel, PanelBase): |
---|
39 | |
---|
40 | def __init__(self, parent, base=None, *args, **kwargs): |
---|
41 | ScrolledPanel.__init__(self, parent, *args, **kwargs) |
---|
42 | PanelBase.__init__(self) |
---|
43 | self.SetupScrolling() |
---|
44 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
45 | |
---|
46 | self.base = base |
---|
47 | self.parent = parent |
---|
48 | self.meta_frames = [] |
---|
49 | |
---|
50 | self.q_input = None |
---|
51 | self.iq_input = None |
---|
52 | self.output = None |
---|
53 | self.data_type = "ascii" |
---|
54 | |
---|
55 | self.metadata = { |
---|
56 | 'title': None, |
---|
57 | 'run': None, |
---|
58 | 'run_name': None, |
---|
59 | 'instrument': None, |
---|
60 | 'detector': [Detector()], |
---|
61 | 'sample': Sample() |
---|
62 | } |
---|
63 | |
---|
64 | self.metadata['detector'][0].name = '' |
---|
65 | |
---|
66 | self._do_layout() |
---|
67 | self.SetAutoLayout(True) |
---|
68 | self.Layout() |
---|
69 | |
---|
70 | def convert_to_cansas(self, data, filename): |
---|
71 | reader = CansasReader() |
---|
72 | reader.write(filename, data) |
---|
73 | |
---|
74 | def extract_data(self, filename): |
---|
75 | data = np.loadtxt(filename, dtype=str) |
---|
76 | |
---|
77 | if len(data.shape) != 1: |
---|
78 | msg = "Error reading {}: Only one column of data is allowed" |
---|
79 | raise Exception(msg.format(filename.split('\\')[-1])) |
---|
80 | |
---|
81 | is_float = True |
---|
82 | try: |
---|
83 | float(data[0]) |
---|
84 | except: |
---|
85 | is_float = False |
---|
86 | |
---|
87 | if not is_float: |
---|
88 | end_char = data[0][-1] |
---|
89 | # If lines end with comma or semi-colon, trim the last character |
---|
90 | if end_char == ',' or end_char == ';': |
---|
91 | data = map(lambda s: s[0:-1], data) |
---|
92 | else: |
---|
93 | msg = ("Error reading {}: Lines must end with a digit, comma " |
---|
94 | "or semi-colon").format(filename.split('\\')[-1]) |
---|
95 | raise Exception(msg) |
---|
96 | |
---|
97 | return np.array(data, dtype=np.float32) |
---|
98 | |
---|
99 | def on_convert(self, event): |
---|
100 | if not self.validate_inputs(): |
---|
101 | return |
---|
102 | |
---|
103 | try: |
---|
104 | if self.data_type == 'ascii': |
---|
105 | qdata = self.extract_data(self.q_input.GetPath()) |
---|
106 | iqdata = self.extract_data(self.iq_input.GetPath()) |
---|
107 | else: # self.data_type == 'bsl' |
---|
108 | loader = BSLLoader(self.q_input.GetPath(), |
---|
109 | self.iq_input.GetPath()) |
---|
110 | bsl_data = loader.load_bsl_data() |
---|
111 | qdata = bsl_data.q_axis.data[0] |
---|
112 | iqdata = bsl_data.data_axis.data[0] |
---|
113 | except Exception as ex: |
---|
114 | msg = str(ex) |
---|
115 | wx.PostEvent(self.parent.manager.parent, |
---|
116 | StatusEvent(status=msg, info='error')) |
---|
117 | return |
---|
118 | |
---|
119 | output_path = self.output.GetPath() |
---|
120 | data = Data1D(x=qdata, y=iqdata) |
---|
121 | data.filename = output_path.split('\\')[-1] |
---|
122 | |
---|
123 | if self.metadata['run'] is not None: |
---|
124 | run = self.metadata['run'] |
---|
125 | run_name = self.metadata['run_name'] |
---|
126 | |
---|
127 | if not isinstance(run, list) and run is not None: |
---|
128 | self.metadata['run'] = [run] |
---|
129 | else: |
---|
130 | run = run[0] |
---|
131 | |
---|
132 | if not isinstance(run_name, dict): |
---|
133 | if run_name is not None: |
---|
134 | self.metadata['run_name'] = { run: run_name } |
---|
135 | else: |
---|
136 | self.metadata['run_name'] = {} |
---|
137 | elif run_name != {}: |
---|
138 | self.metadata['run_name'][run] = run_name.values()[0] |
---|
139 | else: |
---|
140 | self.metadata['run'] = [] |
---|
141 | self.metadata['run_name'] = {} |
---|
142 | |
---|
143 | for attr, value in self.metadata.iteritems(): |
---|
144 | if value is not None: |
---|
145 | setattr(data, attr, value) |
---|
146 | |
---|
147 | self.convert_to_cansas(data, output_path) |
---|
148 | wx.PostEvent(self.parent.manager.parent, |
---|
149 | StatusEvent(status="Conversion completed.")) |
---|
150 | |
---|
151 | def validate_inputs(self): |
---|
152 | msg = "You must select a" |
---|
153 | if self.q_input.GetPath() == '': |
---|
154 | msg += " Q Axis input file." |
---|
155 | elif self.iq_input.GetPath() == '': |
---|
156 | msg += "n Intensity input file." |
---|
157 | elif self.output.GetPath() == '': |
---|
158 | msg += "destination for the converted file." |
---|
159 | if msg != "You must select a": |
---|
160 | wx.PostEvent(self.parent.manager.parent, |
---|
161 | StatusEvent(status=msg, info='error')) |
---|
162 | return |
---|
163 | |
---|
164 | return True |
---|
165 | |
---|
166 | def show_detector_window(self, event): |
---|
167 | if self.meta_frames != []: |
---|
168 | for frame in self.meta_frames: |
---|
169 | frame.panel.on_close() |
---|
170 | detector_frame = MetadataWindow(DetectorPanel, |
---|
171 | parent=self.parent.manager.parent, manager=self, |
---|
172 | metadata=self.metadata['detector'][0], title='Detector Metadata') |
---|
173 | self.meta_frames.append(detector_frame) |
---|
174 | self.parent.manager.put_icon(detector_frame) |
---|
175 | detector_frame.Show(True) |
---|
176 | |
---|
177 | def show_sample_window(self, event): |
---|
178 | if self.meta_frames != []: |
---|
179 | for frame in self.meta_frames: |
---|
180 | frame.panel.on_close() |
---|
181 | sample_frame = MetadataWindow(SamplePanel, |
---|
182 | parent=self.parent.manager.parent, manager=self, |
---|
183 | metadata=self.metadata['sample'], title='Sample Metadata') |
---|
184 | self.meta_frames.append(sample_frame) |
---|
185 | self.parent.manager.put_icon(sample_frame) |
---|
186 | sample_frame.Show(True) |
---|
187 | |
---|
188 | def datatype_changed(self, event): |
---|
189 | event.Skip() |
---|
190 | dtype = event.GetEventObject().GetName() |
---|
191 | self.data_type = dtype |
---|
192 | |
---|
193 | def metadata_changed(self, event): |
---|
194 | event.Skip() |
---|
195 | textbox = event.GetEventObject() |
---|
196 | attr = textbox.GetName() |
---|
197 | value = textbox.GetValue().strip() |
---|
198 | |
---|
199 | if value == '': |
---|
200 | self.metadata[attr] = None |
---|
201 | else: |
---|
202 | self.metadata[attr] = value |
---|
203 | |
---|
204 | |
---|
205 | def _do_layout(self): |
---|
206 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
207 | |
---|
208 | instructions = ("Select either 1 column ASCII files or BSL files " |
---|
209 | "containing the Q Axis and Intensity data, chose where to save " |
---|
210 | "the converted file, then click Convert to convert them to CanSAS " |
---|
211 | "XML format. If required, metadata can also be input below.") |
---|
212 | instruction_label = wx.StaticText(self, -1, instructions, |
---|
213 | size=(_STATICBOX_WIDTH+40, -1)) |
---|
214 | instruction_label.Wrap(_STATICBOX_WIDTH+40) |
---|
215 | vbox.Add(instruction_label, flag=wx.TOP | wx.LEFT | wx.RIGHT, border=5) |
---|
216 | |
---|
217 | section = wx.StaticBox(self, -1) |
---|
218 | section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL) |
---|
219 | section_sizer.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
220 | |
---|
221 | input_grid = wx.GridBagSizer(5, 5) |
---|
222 | |
---|
223 | y = 0 |
---|
224 | |
---|
225 | q_label = wx.StaticText(self, -1, "Q Axis: ") |
---|
226 | input_grid.Add(q_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
227 | |
---|
228 | self.q_input = wx.FilePickerCtrl(self, -1, |
---|
229 | size=(_STATICBOX_WIDTH-80, -1), |
---|
230 | message="Chose the Q Axis data file.") |
---|
231 | input_grid.Add(self.q_input, (y,1), (1,1), wx.ALL, 5) |
---|
232 | y += 1 |
---|
233 | |
---|
234 | iq_label = wx.StaticText(self, -1, "Intensity Data: ") |
---|
235 | input_grid.Add(iq_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
236 | |
---|
237 | self.iq_input = wx.FilePickerCtrl(self, -1, |
---|
238 | size=(_STATICBOX_WIDTH-80, -1), |
---|
239 | message="Chose the Intensity data file.") |
---|
240 | input_grid.Add(self.iq_input, (y,1), (1,1), wx.ALL, 5) |
---|
241 | y += 1 |
---|
242 | |
---|
243 | data_type_label = wx.StaticText(self, -1, "Input Format: ") |
---|
244 | input_grid.Add(data_type_label, (y,0), (1,1), |
---|
245 | wx.ALIGN_CENTER_VERTICAL, 5) |
---|
246 | radio_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
247 | ascii_btn = wx.RadioButton(self, -1, "ASCII", name="ascii", |
---|
248 | style=wx.RB_GROUP) |
---|
249 | ascii_btn.Bind(wx.EVT_RADIOBUTTON, self.datatype_changed) |
---|
250 | radio_sizer.Add(ascii_btn) |
---|
251 | bsl_btn = wx.RadioButton(self, -1, "BSL", name="bsl") |
---|
252 | bsl_btn.Bind(wx.EVT_RADIOBUTTON, self.datatype_changed) |
---|
253 | radio_sizer.Add(bsl_btn) |
---|
254 | input_grid.Add(radio_sizer, (y,1), (1,1), wx.ALL, 5) |
---|
255 | y += 1 |
---|
256 | |
---|
257 | output_label = wx.StaticText(self, -1, "Output File: ") |
---|
258 | input_grid.Add(output_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
259 | |
---|
260 | self.output = wx.FilePickerCtrl(self, -1, |
---|
261 | size=(_STATICBOX_WIDTH-80, -1), |
---|
262 | message="Chose the Intensity data file.", |
---|
263 | style=wx.FLP_SAVE | wx.FLP_OVERWRITE_PROMPT | wx.FLP_USE_TEXTCTRL, |
---|
264 | wildcard="*.xml") |
---|
265 | input_grid.Add(self.output, (y,1), (1,1), wx.ALL, 5) |
---|
266 | y += 1 |
---|
267 | |
---|
268 | convert_btn = wx.Button(self, wx.ID_OK, "Convert") |
---|
269 | input_grid.Add(convert_btn, (y,0), (1,1), wx.ALL, 5) |
---|
270 | convert_btn.Bind(wx.EVT_BUTTON, self.on_convert) |
---|
271 | |
---|
272 | section_sizer.Add(input_grid) |
---|
273 | |
---|
274 | vbox.Add(section_sizer, flag=wx.ALL, border=5) |
---|
275 | |
---|
276 | metadata_section = wx.CollapsiblePane(self, -1, "Metadata", |
---|
277 | size=(_STATICBOX_WIDTH+40, -1), style=wx.WS_EX_VALIDATE_RECURSIVELY) |
---|
278 | metadata_pane = metadata_section.GetPane() |
---|
279 | metadata_grid = wx.GridBagSizer(5, 5) |
---|
280 | |
---|
281 | y = 0 |
---|
282 | for item in self.metadata.keys(): |
---|
283 | if item == 'detector' or item == 'sample': continue |
---|
284 | label_txt = item.replace('_', ' ').capitalize() |
---|
285 | label = wx.StaticText(metadata_pane, -1, label_txt, |
---|
286 | style=wx.ALIGN_CENTER_VERTICAL) |
---|
287 | input_box = wx.TextCtrl(metadata_pane, name=item, |
---|
288 | size=(_STATICBOX_WIDTH-80, -1)) |
---|
289 | input_box.Bind(wx.EVT_TEXT, self.metadata_changed) |
---|
290 | metadata_grid.Add(label, (y,0), (1,1), |
---|
291 | wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5) |
---|
292 | metadata_grid.Add(input_box, (y,1), (1,2), wx.EXPAND) |
---|
293 | y += 1 |
---|
294 | |
---|
295 | detector_label = wx.StaticText(metadata_pane, -1, |
---|
296 | "Detector:") |
---|
297 | metadata_grid.Add(detector_label, (y, 0), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
298 | detector_btn = wx.Button(metadata_pane, -1, "Enter Detector Metadata") |
---|
299 | metadata_grid.Add(detector_btn, (y, 1), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
300 | detector_btn.Bind(wx.EVT_BUTTON, self.show_detector_window) |
---|
301 | y += 1 |
---|
302 | |
---|
303 | sample_label = wx.StaticText(metadata_pane, -1, "Sample: ") |
---|
304 | metadata_grid.Add(sample_label, (y,0), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
305 | sample_btn = wx.Button(metadata_pane, -1, "Enter Sample Metadata") |
---|
306 | metadata_grid.Add(sample_btn, (y,1), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
307 | sample_btn.Bind(wx.EVT_BUTTON, self.show_sample_window) |
---|
308 | y += 1 |
---|
309 | |
---|
310 | metadata_pane.SetSizer(metadata_grid) |
---|
311 | |
---|
312 | vbox.Add(metadata_section, proportion=0, flag=wx.ALL, border=5) |
---|
313 | |
---|
314 | vbox.Fit(self) |
---|
315 | self.SetSizer(vbox) |
---|
316 | |
---|
317 | class ConverterWindow(widget.CHILD_FRAME): |
---|
318 | |
---|
319 | def __init__(self, parent=None, title='File Converter', base=None, |
---|
320 | manager=None, size=(PANEL_SIZE * 1.05, PANEL_SIZE / 1.55), |
---|
321 | *args, **kwargs): |
---|
322 | kwargs['title'] = title |
---|
323 | kwargs['size'] = size |
---|
324 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwargs) |
---|
325 | |
---|
326 | self.manager = manager |
---|
327 | self.panel = ConverterPanel(self, base=None) |
---|
328 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
329 | self.SetPosition((wx.LEFT, PANEL_TOP)) |
---|
330 | self.Show(True) |
---|
331 | |
---|
332 | def on_close(self, event): |
---|
333 | if self.manager is not None: |
---|
334 | self.manager.converter_frame = None |
---|
335 | self.Destroy() |
---|