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.guiframe.events import StatusEvent |
---|
12 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
13 | from sas.sascalc.dataloader.readers.cansas_reader import Reader as CansasReader |
---|
14 | |
---|
15 | # Panel size |
---|
16 | if sys.platform.count("win32") > 0: |
---|
17 | PANEL_TOP = 0 |
---|
18 | _STATICBOX_WIDTH = 410 |
---|
19 | _BOX_WIDTH = 200 |
---|
20 | PANEL_SIZE = 460 |
---|
21 | FONT_VARIANT = 0 |
---|
22 | else: |
---|
23 | PANEL_TOP = 60 |
---|
24 | _STATICBOX_WIDTH = 430 |
---|
25 | _BOX_WIDTH = 200 |
---|
26 | PANEL_SIZE = 480 |
---|
27 | FONT_VARIANT = 1 |
---|
28 | |
---|
29 | class ConverterPanel(ScrolledPanel, PanelBase): |
---|
30 | |
---|
31 | def __init__(self, parent, base=None, *args, **kwargs): |
---|
32 | ScrolledPanel.__init__(self, parent, *args, **kwargs) |
---|
33 | PanelBase.__init__(self) |
---|
34 | self.SetupScrolling() |
---|
35 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
36 | |
---|
37 | self.base = base |
---|
38 | self.parent = parent |
---|
39 | |
---|
40 | self.q_input = None |
---|
41 | self.iq_input = None |
---|
42 | self.output = None |
---|
43 | |
---|
44 | self._do_layout() |
---|
45 | self.SetAutoLayout(True) |
---|
46 | self.Layout() |
---|
47 | |
---|
48 | def convert_to_cansas(self, data, filename): |
---|
49 | reader = CansasReader() |
---|
50 | reader.write(filename, data) |
---|
51 | |
---|
52 | def extract_data(self, filename): |
---|
53 | data = np.loadtxt(filename, dtype=str) |
---|
54 | |
---|
55 | is_float = True |
---|
56 | try: |
---|
57 | float(data[0]) |
---|
58 | except: |
---|
59 | is_float = False |
---|
60 | |
---|
61 | if not is_float: |
---|
62 | end_char = data[0][-1] |
---|
63 | # If lines end with comma or semi-colon, trim the last character |
---|
64 | if end_char == ',' or end_char == ';': |
---|
65 | data = map(lambda s: s[0:-1], data) |
---|
66 | else: |
---|
67 | msg = ("Error reading {}: Lines must end with a digit, comma " |
---|
68 | "or semi-colon").format(filename.split('\\')[-1]) |
---|
69 | raise Exception(msg) |
---|
70 | |
---|
71 | return np.array(data, dtype=np.float32) |
---|
72 | |
---|
73 | def on_convert(self, event): |
---|
74 | try: |
---|
75 | qdata = self.extract_data(self.q_input.GetPath()) |
---|
76 | iqdata = self.extract_data(self.iq_input.GetPath()) |
---|
77 | except Exception as ex: |
---|
78 | msg = str(ex) |
---|
79 | wx.PostEvent(self.parent.manager.parent, |
---|
80 | StatusEvent(status=msg, info='error')) |
---|
81 | return |
---|
82 | |
---|
83 | data = Data1D(x=qdata, y=iqdata) |
---|
84 | self.convert_to_cansas(data, self.output.GetPath()) |
---|
85 | wx.PostEvent(self.parent.manager.parent, |
---|
86 | StatusEvent(status="Conversion completed.")) |
---|
87 | |
---|
88 | def _do_layout(self): |
---|
89 | vbox = wx.GridBagSizer(wx.VERTICAL) |
---|
90 | |
---|
91 | instructions = ("Select the 1 column ASCII files containing the Q Axis" |
---|
92 | "and Intensity data, chose where to save the converted file, then " |
---|
93 | "click Convert to convert them to CanSAS XML format") |
---|
94 | instruction_label = wx.StaticText(self, -1, "", |
---|
95 | size=(_STATICBOX_WIDTH+40, 28)) |
---|
96 | instruction_label.SetLabel(instructions) |
---|
97 | vbox.Add(instruction_label, (0,0), (1,1), wx.ALL, 5) |
---|
98 | |
---|
99 | section = wx.StaticBox(self, -1) |
---|
100 | section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL) |
---|
101 | section_sizer.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
102 | |
---|
103 | input_grid = wx.GridBagSizer(5, 5) |
---|
104 | |
---|
105 | q_label = wx.StaticText(self, -1, "Q Axis: ") |
---|
106 | input_grid.Add(q_label, (0,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
107 | |
---|
108 | self.q_input = wx.FilePickerCtrl(self, -1, |
---|
109 | size=(_STATICBOX_WIDTH-80, -1), |
---|
110 | message="Chose the Q Axis data file.") |
---|
111 | input_grid.Add(self.q_input, (0,1), (1,1), wx.ALL, 5) |
---|
112 | |
---|
113 | iq_label = wx.StaticText(self, -1, "Intensity Data: ") |
---|
114 | input_grid.Add(iq_label, (1,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
115 | |
---|
116 | self.iq_input = wx.FilePickerCtrl(self, -1, |
---|
117 | size=(_STATICBOX_WIDTH-80, -1), |
---|
118 | message="Chose the Intensity data file.") |
---|
119 | input_grid.Add(self.iq_input, (1,1), (1,1), wx.ALL, 5) |
---|
120 | |
---|
121 | output_label = wx.StaticText(self, -1, "Output File: ") |
---|
122 | input_grid.Add(output_label, (2,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
123 | |
---|
124 | self.output = wx.FilePickerCtrl(self, -1, |
---|
125 | size=(_STATICBOX_WIDTH-80, -1), |
---|
126 | message="Chose the Intensity data file.", |
---|
127 | style=wx.FLP_SAVE | wx.FLP_OVERWRITE_PROMPT | wx.FLP_USE_TEXTCTRL, |
---|
128 | wildcard="*.xml") |
---|
129 | input_grid.Add(self.output, (2,1), (1,1), wx.ALL, 5) |
---|
130 | |
---|
131 | convert_btn = wx.Button(self, -1, "Convert") |
---|
132 | input_grid.Add(convert_btn, (3,0), (1,1), wx.ALL, 5) |
---|
133 | convert_btn.Bind(wx.EVT_BUTTON, self.on_convert) |
---|
134 | |
---|
135 | section_sizer.Add(input_grid) |
---|
136 | |
---|
137 | vbox.Add(section_sizer, (1,0), (1,1), wx.ALL, 5) |
---|
138 | |
---|
139 | vbox.Fit(self) |
---|
140 | self.SetSizer(vbox) |
---|
141 | |
---|
142 | class ConverterWindow(widget.CHILD_FRAME): |
---|
143 | |
---|
144 | def __init__(self, parent=None, title='File Converter', base=None, |
---|
145 | manager=None, size=(PANEL_SIZE * 1.05, PANEL_SIZE / 1.55), |
---|
146 | *args, **kwargs): |
---|
147 | kwargs['title'] = title |
---|
148 | kwargs['size'] = size |
---|
149 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwargs) |
---|
150 | |
---|
151 | self.manager = manager |
---|
152 | self.panel = ConverterPanel(self, base=None) |
---|
153 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
154 | self.SetPosition((wx.LEFT, PANEL_TOP)) |
---|
155 | self.Show(True) |
---|
156 | |
---|
157 | def on_close(self, event): |
---|
158 | if self.manager is not None: |
---|
159 | self.manager.converter_frame = None |
---|
160 | self.Destroy() |
---|