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