Changeset a58706d in sasview for src/sas/sasgui/perspectives/file_converter/converter_panel.py
- Timestamp:
- Jul 13, 2016 5:56:54 AM (8 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 5afe77f
- Parents:
- 77d92cd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/file_converter/converter_panel.py
r77d92cd ra58706d 5 5 import wx 6 6 import sys 7 import numpy as np 7 8 from wx.lib.scrolledpanel import ScrolledPanel 8 9 from sas.sasgui.guiframe.panel_base import PanelBase 9 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 10 14 11 15 # Panel size … … 14 18 _STATICBOX_WIDTH = 410 15 19 _BOX_WIDTH = 200 16 PANEL_SIZE = 4 4020 PANEL_SIZE = 460 17 21 FONT_VARIANT = 0 18 22 else: … … 20 24 _STATICBOX_WIDTH = 430 21 25 _BOX_WIDTH = 200 22 PANEL_SIZE = 4 6026 PANEL_SIZE = 480 23 27 FONT_VARIANT = 1 24 28 … … 28 32 ScrolledPanel.__init__(self, parent, *args, **kwargs) 29 33 PanelBase.__init__(self) 30 34 self.SetupScrolling() 31 35 self.SetWindowVariant(variant=FONT_VARIANT) 32 36 … … 34 38 self.parent = parent 35 39 36 self._do_layout 40 self.q_input = None 41 self.iq_input = None 42 self.output = None 43 44 self._do_layout() 37 45 self.SetAutoLayout(True) 38 46 self.Layout() 39 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 40 88 def _do_layout(self): 41 # TODO: Get this working 42 vbox = wx.BoxSizer(wx.VERTICAL) 89 vbox = wx.GridBagSizer(wx.VERTICAL) 43 90 44 inputsection = wx.StaticBox(self, -1, "Input File") 45 inputsection_sizer = wx.StaticBoxSizer(inputsection, wx.VERTICAL) 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)) 46 102 47 103 input_grid = wx.GridBagSizer(5, 5) 48 104 49 input_box = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1))50 input_grid.Add( input_box, (0,0), (1,1), wx.LEFT, 5)105 q_label = wx.StaticText(self, -1, "Q Axis: ") 106 input_grid.Add(q_label, (0,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) 51 107 52 inputsection_sizer.Add(input_grid) 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) 53 112 54 vbox.Add(input_section, (0,0), (0,1), wx.TOP, 15) 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) 55 138 56 139 vbox.Fit(self)
Note: See TracChangeset
for help on using the changeset viewer.