source: sasview/src/sas/sasgui/perspectives/file_converter/converter_panel.py @ 346d56e

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 346d56e was 346d56e, checked in by lewis, 8 years ago

Allow adding metadata to 2d files and ensure it's written correctly

  • Property mode set to 100644
File size: 24.5 KB
RevLine 
[77d92cd]1"""
2This module provides a GUI for the file converter
3"""
4
5import wx
6import sys
[05595c4]7import os
[a58706d]8import numpy as np
[77d92cd]9from wx.lib.scrolledpanel import ScrolledPanel
10from sas.sasgui.guiframe.panel_base import PanelBase
11from sas.sasgui.perspectives.calculator import calculator_widgets as widget
[0e11ec7]12from sas.sasgui.perspectives.file_converter.converter_widgets import FileInput
[de0df2c]13from sas.sasgui.perspectives.file_converter.meta_panels import MetadataWindow
14from sas.sasgui.perspectives.file_converter.meta_panels import DetectorPanel
[2a7722b]15from sas.sasgui.perspectives.file_converter.meta_panels import SamplePanel
[55bc56bc]16from sas.sasgui.perspectives.file_converter.meta_panels import SourcePanel
[eb8da5f]17from sas.sasgui.perspectives.file_converter.frame_select_dialog import FrameSelectDialog
[a58706d]18from sas.sasgui.guiframe.events import StatusEvent
[11794f2]19from sas.sasgui.guiframe.documentation_window import DocumentationWindow
[a58706d]20from sas.sasgui.guiframe.dataFitting import Data1D
[e9e777c]21from sas.sascalc.dataloader.data_info import Data2D
[fdbea3c]22from sas.sasgui.guiframe.utils import check_float
[0b1a677]23from sas.sascalc.file_converter.cansas_writer import CansasWriter
24from sas.sascalc.file_converter.otoko_loader import OTOKOLoader
25from sas.sascalc.file_converter.bsl_loader import BSLLoader
[e9e777c]26from sas.sascalc.file_converter.nxcansas_writer import NXcanSASWriter
[f2b3f28]27from sas.sascalc.dataloader.data_info import Detector
[2a7722b]28from sas.sascalc.dataloader.data_info import Sample
[55bc56bc]29from sas.sascalc.dataloader.data_info import Source
[fdbea3c]30from sas.sascalc.dataloader.data_info import Vector
[77d92cd]31
32# Panel size
33if sys.platform.count("win32") > 0:
34    PANEL_TOP = 0
35    _STATICBOX_WIDTH = 410
36    _BOX_WIDTH = 200
[36f4debb]37    PANEL_SIZE = 480
[77d92cd]38    FONT_VARIANT = 0
39else:
40    PANEL_TOP = 60
41    _STATICBOX_WIDTH = 430
42    _BOX_WIDTH = 200
[36f4debb]43    PANEL_SIZE = 500
[77d92cd]44    FONT_VARIANT = 1
45
46class ConverterPanel(ScrolledPanel, PanelBase):
[ba65aff]47    """
48    This class provides the File Converter GUI
49    """
[77d92cd]50
51    def __init__(self, parent, base=None, *args, **kwargs):
52        ScrolledPanel.__init__(self, parent, *args, **kwargs)
53        PanelBase.__init__(self)
[a58706d]54        self.SetupScrolling()
[77d92cd]55        self.SetWindowVariant(variant=FONT_VARIANT)
56
57        self.base = base
58        self.parent = parent
[de0df2c]59        self.meta_frames = []
[77d92cd]60
[ba65aff]61        # GUI inputs
[a58706d]62        self.q_input = None
63        self.iq_input = None
64        self.output = None
[d6bf064]65        self.radiation_input = None
[35488b2]66        self.convert_btn = None
[d6bf064]67        self.metadata_section = None
[ba65aff]68
[4b862c4]69        self.data_type = "ascii"
[a58706d]70
[ba65aff]71        # Metadata values
[94667e27]72        self.title = ''
73        self.run = ''
74        self.run_name = ''
75        self.instrument = ''
[503cc34]76        self.detector = Detector()
77        self.sample = Sample()
78        self.source = Source()
79        self.properties = ['title', 'run', 'run_name', 'instrument']
80
81        self.detector.name = ''
82        self.source.radiation = 'neutron'
[4b862c4]83
[a58706d]84        self._do_layout()
[77d92cd]85        self.SetAutoLayout(True)
86        self.Layout()
87
[9e9f848]88    def convert_to_cansas(self, frame_data, filepath, single_file):
[ba65aff]89        """
90        Saves an array of Data1D objects to a single CanSAS file with multiple
91        <SasData> elements, or to multiple CanSAS files, each with one
92        <SasData> element.
93
[9e9f848]94        :param frame_data: If single_file is true, an array of Data1D objects.
95        If single_file is false, a dictionary of the form frame_number: Data1D.
96        :param filepath: Where to save the CanSAS file
[ba65aff]97        :param single_file: If true, array is saved as a single file, if false,
98        each item in the array is saved to it's own file
99        """
100        writer = CansasWriter()
[ea2a7348]101        entry_attrs = None
[94667e27]102        if self.run_name != '':
[ea2a7348]103            entry_attrs = { 'name': self.run_name }
[5451a78]104
[94f4518]105        if single_file:
[9e9f848]106            writer.write(filepath, frame_data,
[94f4518]107                sasentry_attrs=entry_attrs)
108        else:
[9e9f848]109            # Folder and base filename
110            [group_path, group_name] = os.path.split(filepath)
111            ext = "." + group_name.split('.')[-1] # File extension
112            for frame_number, frame_data in frame_data.iteritems():
113                # Append frame number to base filename
114                filename = group_name.replace(ext, str(frame_number)+ext)
115                destination = os.path.join(group_path, filename)
116                writer.write(destination, [frame_data],
[94f4518]117                    sasentry_attrs=entry_attrs)
[a58706d]118
[535e181]119    def extract_ascii_data(self, filename):
[ba65aff]120        """
121        Extracts data from a single-column ASCII file
122
123        :param filename: The file to load data from
124        :return: A numpy array containing the extracted data
125        """
[514a00e]126        try:
127            data = np.loadtxt(filename, dtype=str)
128        except:
129            is_bsl = False
130            # Check if file is a BSL or OTOKO header file
131            f = open(filename, 'r')
132            f.readline()
133            f.readline()
134            bsl_metadata = f.readline().strip().split()
135            f.close()
136            if len(bsl_metadata) == 10:
137                msg = ("Error parsing ASII data. {} looks like a BSL or OTOKO "
138                    "header file.")
139                raise Exception(msg.format(os.path.split(filename)[-1]))
[a58706d]140
[ff790b3]141        if len(data.shape) != 1:
142            msg = "Error reading {}: Only one column of data is allowed"
143            raise Exception(msg.format(filename.split('\\')[-1]))
144
[a58706d]145        is_float = True
146        try:
147            float(data[0])
148        except:
149            is_float = False
150
151        if not is_float:
152            end_char = data[0][-1]
153            # If lines end with comma or semi-colon, trim the last character
154            if end_char == ',' or end_char == ';':
155                data = map(lambda s: s[0:-1], data)
156            else:
157                msg = ("Error reading {}: Lines must end with a digit, comma "
158                    "or semi-colon").format(filename.split('\\')[-1])
159                raise Exception(msg)
160
161        return np.array(data, dtype=np.float32)
162
[535e181]163    def extract_otoko_data(self, filename):
[ba65aff]164        """
165        Extracts data from a 1D OTOKO file
166
167        :param filename: The OTOKO file to load the data from
168        :return: A numpy array containing the extracted data
169        """
[535e181]170        loader = OTOKOLoader(self.q_input.GetPath(),
171            self.iq_input.GetPath())
[9c500ab]172        otoko_data = loader.load_otoko_data()
173        qdata = otoko_data.q_axis.data
174        iqdata = otoko_data.data_axis.data
[535e181]175        if len(qdata) > 1:
176            msg = ("Q-Axis file has multiple frames. Only 1 frame is "
177                "allowed for the Q-Axis")
178            wx.PostEvent(self.parent.manager.parent,
179                StatusEvent(status=msg, info="error"))
180            return
181        else:
182            qdata = qdata[0]
183
184        return qdata, iqdata
185
[0b1a677]186    def extract_bsl_data(self, filename):
187        """
188        Extracts data from a 2D BSL file
189
190        :param filename: The header file to extract the data from
191        :return x_data: A 1D array containing all the x coordinates of the data
192        :return y_data: A 1D array containing all the y coordinates of the data
193        :return frame_data: A dictionary of the form frame_number: data, where
194        data is a 2D numpy array containing the intensity data
195        """
196        loader = BSLLoader(filename)
197        frames = [0]
198        should_continue = True
199
200        if loader.n_frames > 1:
201            params = self.ask_frame_range(loader.n_frames)
202            frames = params['frames']
203            if len(frames) == 0:
204                should_continue = False
205        elif loader.n_rasters == 1 and loader.n_frames == 1:
206            message = ("The selected file is an OTOKO file. Please select the "
207            "'OTOKO 1D' option if you wish to convert it.")
208            dlg = wx.MessageDialog(self,
209            message,
210            'Error!',
211            wx.OK | wx.ICON_WARNING)
212            dlg.ShowModal()
213            should_continue = False
214            dlg.Destroy()
215        else:
216            message = ("The selected data file only has 1 frame, it might be"
217                " a multi-frame OTOKO file.\nContinue conversion?")
218            dlg = wx.MessageDialog(self,
219            message,
220            'Warning!',
221            wx.YES_NO | wx.ICON_WARNING)
222            should_continue = (dlg.ShowModal() == wx.ID_YES)
223            dlg.Destroy()
224
225        if not should_continue:
[fe498b83]226            return None
[0b1a677]227
[fe498b83]228        frame_data = loader.load_frames(frames)
[0b1a677]229
[fe498b83]230        return frame_data
[0b1a677]231
[eb8da5f]232    def ask_frame_range(self, n_frames):
[ba65aff]233        """
234        Display a dialog asking the user to input the range of frames they
235        would like to export
236
237        :param n_frames: How many frames the loaded data file has
238        :return: A dictionary containing the parameters input by the user
239        """
[eb8da5f]240        valid_input = False
[05595c4]241        is_bsl = (self.data_type == 'bsl')
242        dlg = FrameSelectDialog(n_frames, is_bsl)
[eb8da5f]243        frames = None
244        increment = None
[94f4518]245        single_file = True
[eb8da5f]246        while not valid_input:
247            if dlg.ShowModal() == wx.ID_OK:
248                msg = ""
249                try:
250                    first_frame = int(dlg.first_input.GetValue())
251                    last_frame = int(dlg.last_input.GetValue())
252                    increment = int(dlg.increment_input.GetValue())
[05595c4]253                    if not is_bsl:
254                        single_file = dlg.single_btn.GetValue()
255
[eb8da5f]256                    if last_frame < 0 or first_frame < 0:
257                        msg = "Frame values must be positive"
258                    elif increment < 1:
259                        msg = "Increment must be greater than or equal to 1"
260                    elif first_frame > last_frame:
261                        msg = "First frame must be less than last frame"
[05595c4]262                    elif last_frame >= n_frames:
[eb8da5f]263                        msg = "Last frame must be less than {}".format(n_frames)
264                    else:
265                        valid_input = True
266                except:
267                    valid_input = False
268                    msg = "Please enter valid integer values"
269
270                if not valid_input:
271                    wx.PostEvent(self.parent.manager.parent,
272                        StatusEvent(status=msg))
273            else:
[94f4518]274                return { 'frames': [], 'inc': None, 'file': single_file }
[18544f8]275        frames = range(first_frame, last_frame + 1, increment)
[94f4518]276        return { 'frames': frames, 'inc': increment, 'file': single_file }
[eb8da5f]277
[edf0e06]278    def get_metadata(self):
279        # Prepare the metadata for writing to a file
280        if self.run is None:
281            self.run = []
282        elif not isinstance(self.run, list):
283            self.run = [self.run]
[0b1a677]284
[edf0e06]285        if self.title is None:
286            self.title = ''
[e9e777c]287
[edf0e06]288        metadata = {
289            'title': self.title,
290            'run': self.run,
291            'instrument': self.instrument,
292            'detector': [self.detector],
293            'sample': self.sample,
294            'source': self.source
295        }
[535e181]296
[edf0e06]297        return metadata
[a58706d]298
[edf0e06]299    def convert_1d_data(self, qdata, iqdata):
300        """
301        Formats a 1D array of q_axis data and a 2D array of I axis data (where
302        each row of iqdata is a separate row), into an array of Data1D objects
303        """
[535e181]304        frames = []
305        increment = 1
306        single_file = True
[05595c4]307        n_frames = iqdata.shape[0]
[535e181]308        # Standard file has 3 frames: SAS, calibration and WAS
[05595c4]309        if n_frames > 3:
[ba65aff]310            # File has multiple frames - ask the user which ones they want to
311            # export
[05595c4]312            params = self.ask_frame_range(n_frames)
[535e181]313            frames = params['frames']
314            increment = params['inc']
315            single_file = params['file']
316            if frames == []: return
317        else: # Only interested in SAS data
318            frames = [0]
319
[f2b3f28]320        output_path = self.output.GetPath()
[edf0e06]321        metadata = self.get_metadata()
[f2b3f28]322
[9e9f848]323        frame_data = {}
[eb8da5f]324        for i in frames:
325            data = Data1D(x=qdata, y=iqdata[i])
[9e9f848]326            frame_data[i] = data
[94f4518]327        if single_file:
328            # Only need to set metadata on first Data1D object
[9e9f848]329            frame_data = frame_data.values() # Don't need to know frame numbers
[94f4518]330            frame_data[0].filename = output_path.split('\\')[-1]
331            for key, value in metadata.iteritems():
332                setattr(frame_data[0], key, value)
333        else:
334            # Need to set metadata for all Data1D objects
[9e9f848]335            for datainfo in frame_data.values():
[94f4518]336                datainfo.filename = output_path.split('\\')[-1]
337                for key, value in metadata.iteritems():
338                    setattr(datainfo, key, value)
339
340        self.convert_to_cansas(frame_data, output_path, single_file)
[edf0e06]341
342    def on_convert(self, event):
343        """Called when the Convert button is clicked"""
344        if not self.validate_inputs():
345            return
346
347        self.sample.ID = self.title
348
349        try:
350            if self.data_type == 'ascii':
351                qdata = self.extract_ascii_data(self.q_input.GetPath())
352                iqdata = np.array([self.extract_ascii_data(self.iq_input.GetPath())])
353                self.convert_1d_data(qdata, iqdata)
354            elif self.data_type == 'otoko':
355                qdata, iqdata = self.extract_otoko_data(self.q_input.GetPath())
356                self.convert_1d_data(qdata, iqdata)
357            else: # self.data_type == 'bsl'
358                dataset = self.extract_bsl_data(self.iq_input.GetPath())
359                if dataset is None:
360                    # Cancelled by user
361                    return
362
363                metadata = self.get_metadata()
364                for key, value in metadata.iteritems():
365                    setattr(dataset[0], key, value)
[346d56e]366                if self.run != []:
367                    run_number = self.run[0]
368                    dataset[0].run_name[run_number] = self.run_name
[edf0e06]369
370                w = NXcanSASWriter()
371                w.write(dataset, self.output.GetPath())
372        except Exception as ex:
373            msg = str(ex)
374            wx.PostEvent(self.parent.manager.parent,
375                StatusEvent(status=msg, info='error'))
376            return
377
[a58706d]378        wx.PostEvent(self.parent.manager.parent,
379            StatusEvent(status="Conversion completed."))
380
[11794f2]381    def on_help(self, event):
[ba65aff]382        """
383        Show the File Converter documentation
384        """
[11794f2]385        tree_location = ("user/sasgui/perspectives/file_converter/"
386            "file_converter_help.html")
387        doc_viewer = DocumentationWindow(self, -1, tree_location,
388            "", "File Converter Help")
389
[fdbea3c]390    def validate_inputs(self):
391        msg = "You must select a"
[535e181]392        if self.q_input.GetPath() == '' and self.data_type != 'bsl':
[fdbea3c]393            msg += " Q Axis input file."
394        elif self.iq_input.GetPath() == '':
395            msg += "n Intensity input file."
396        elif self.output.GetPath() == '':
[35488b2]397            msg += " destination for the converted file."
[fdbea3c]398        if msg != "You must select a":
399            wx.PostEvent(self.parent.manager.parent,
400                StatusEvent(status=msg, info='error'))
401            return
402
403        return True
404
[af84162]405    def show_detector_window(self, event):
[ba65aff]406        """
[b7c21a7]407        Show the window for inputting Detector metadata
[ba65aff]408        """
[de0df2c]409        if self.meta_frames != []:
410            for frame in self.meta_frames:
411                frame.panel.on_close()
412        detector_frame = MetadataWindow(DetectorPanel,
413            parent=self.parent.manager.parent, manager=self,
[503cc34]414            metadata=self.detector, title='Detector Metadata')
[de0df2c]415        self.meta_frames.append(detector_frame)
416        self.parent.manager.put_icon(detector_frame)
417        detector_frame.Show(True)
[fdbea3c]418
[2a7722b]419    def show_sample_window(self, event):
[ba65aff]420        """
[b7c21a7]421        Show the window for inputting Sample metadata
[ba65aff]422        """
[2a7722b]423        if self.meta_frames != []:
424            for frame in self.meta_frames:
425                frame.panel.on_close()
426        sample_frame = MetadataWindow(SamplePanel,
427            parent=self.parent.manager.parent, manager=self,
[503cc34]428            metadata=self.sample, title='Sample Metadata')
[2a7722b]429        self.meta_frames.append(sample_frame)
430        self.parent.manager.put_icon(sample_frame)
431        sample_frame.Show(True)
432
[55bc56bc]433    def show_source_window(self, event):
[ba65aff]434        """
[b7c21a7]435        Show the window for inputting Source metadata
[ba65aff]436        """
[55bc56bc]437        if self.meta_frames != []:
438            for frame in self.meta_frames:
439                frame.panel.on_close()
440        source_frame = MetadataWindow(SourcePanel,
441            parent=self.parent.manager.parent, manager=self,
[503cc34]442            metadata=self.source, title="Source Metadata")
[55bc56bc]443        self.meta_frames.append(source_frame)
444        self.parent.manager.put_icon(source_frame)
445        source_frame.Show(True)
446
[a027549]447    def on_collapsible_pane(self, event):
[ba65aff]448        """
449        Resize the scrollable area to fit the metadata pane when it's
450        collapsed or expanded
451        """
[a027549]452        self.Freeze()
453        self.SetupScrolling()
454        self.parent.Layout()
455        self.Thaw()
456
[4b862c4]457    def datatype_changed(self, event):
[ba65aff]458        """
459        Update the UI and self.data_type when a data type radio button is
460        pressed
461        """
[4b862c4]462        event.Skip()
463        dtype = event.GetEventObject().GetName()
464        self.data_type = dtype
[535e181]465        if dtype == 'bsl':
[05595c4]466            self.q_input.SetPath("")
[535e181]467            self.q_input.Disable()
[e9e777c]468            self.output.SetWildcard("NXcanSAS HDF5 File (*.h5)|*.h5")
[535e181]469        else:
470            self.q_input.Enable()
[d6bf064]471            self.radiation_input.Enable()
472            self.metadata_section.Enable()
[0e11ec7]473            self.output.SetWildcard("CanSAS 1D (*.xml)|*.xml")
[4b862c4]474
[55775e8]475    def radiationtype_changed(self, event):
476        event.Skip()
477        rtype = event.GetEventObject().GetValue().lower()
[503cc34]478        self.source.radiation = rtype
[55775e8]479
[f2b3f28]480    def metadata_changed(self, event):
481        event.Skip()
482        textbox = event.GetEventObject()
483        attr = textbox.GetName()
484        value = textbox.GetValue().strip()
[fdbea3c]485
[503cc34]486        setattr(self, attr, value)
[f2b3f28]487
488
[77d92cd]489    def _do_layout(self):
[f2b3f28]490        vbox = wx.BoxSizer(wx.VERTICAL)
[a58706d]491
[f5c52b0]492        instructions = ("Select either single column ASCII files or 1D OTOKO "
493        "files containing the Q-Axis and Intensity-Axis data, or a 2D BSL file"
494        ", then chose where to save the converted file, and click Convert.\n"
495        "ASCII and OTOKO files will be converted to CanSAS XML, and OTKO files"
496        " to IGOR/DAT 2D Q_map files.\nCanSAS metadata can also be optionally "
497        "input below.")
498
[5afe77f]499        instruction_label = wx.StaticText(self, -1, instructions,
500            size=(_STATICBOX_WIDTH+40, -1))
501        instruction_label.Wrap(_STATICBOX_WIDTH+40)
[f2b3f28]502        vbox.Add(instruction_label, flag=wx.TOP | wx.LEFT | wx.RIGHT, border=5)
[77d92cd]503
[a58706d]504        section = wx.StaticBox(self, -1)
505        section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL)
506        section_sizer.SetMinSize((_STATICBOX_WIDTH, -1))
[77d92cd]507
508        input_grid = wx.GridBagSizer(5, 5)
509
[4b862c4]510        y = 0
511
[489bb46]512        data_type_label = wx.StaticText(self, -1, "Input Format: ")
513        input_grid.Add(data_type_label, (y,0), (1,1),
514            wx.ALIGN_CENTER_VERTICAL, 5)
515        radio_sizer = wx.BoxSizer(wx.HORIZONTAL)
516        ascii_btn = wx.RadioButton(self, -1, "ASCII", name="ascii",
517            style=wx.RB_GROUP)
518        ascii_btn.Bind(wx.EVT_RADIOBUTTON, self.datatype_changed)
519        radio_sizer.Add(ascii_btn)
520        otoko_btn = wx.RadioButton(self, -1, "OTOKO 1D", name="otoko")
521        otoko_btn.Bind(wx.EVT_RADIOBUTTON, self.datatype_changed)
522        radio_sizer.Add(otoko_btn)
523        input_grid.Add(radio_sizer, (y,1), (1,1), wx.ALL, 5)
524        bsl_btn = wx.RadioButton(self, -1, "BSL 2D", name="bsl")
525        bsl_btn.Bind(wx.EVT_RADIOBUTTON, self.datatype_changed)
526        radio_sizer.Add(bsl_btn)
527        y += 1
528
[13b9a63]529        q_label = wx.StaticText(self, -1, "Q-Axis Data: ")
[4b862c4]530        input_grid.Add(q_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5)
[a58706d]531
532        self.q_input = wx.FilePickerCtrl(self, -1,
533            size=(_STATICBOX_WIDTH-80, -1),
[785fa53]534            message="Chose the Q-Axis data file.",
535            style=wx.FLP_USE_TEXTCTRL | wx.FLP_CHANGE_DIR)
[4b862c4]536        input_grid.Add(self.q_input, (y,1), (1,1), wx.ALL, 5)
537        y += 1
[a58706d]538
[13b9a63]539        iq_label = wx.StaticText(self, -1, "Intensity-Axis Data: ")
[4b862c4]540        input_grid.Add(iq_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5)
[a58706d]541
542        self.iq_input = wx.FilePickerCtrl(self, -1,
543            size=(_STATICBOX_WIDTH-80, -1),
[785fa53]544            message="Chose the Intensity-Axis data file.",
545            style=wx.FLP_USE_TEXTCTRL | wx.FLP_CHANGE_DIR)
[4b862c4]546        input_grid.Add(self.iq_input, (y,1), (1,1), wx.ALL, 5)
547        y += 1
548
[55775e8]549        radiation_label = wx.StaticText(self, -1, "Radiation Type: ")
[489bb46]550        input_grid.Add(radiation_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5)
[d6bf064]551        self.radiation_input = wx.ComboBox(self, -1,
[55775e8]552            choices=["Neutron", "X-Ray", "Muon", "Electron"],
553            name="radiation", style=wx.CB_READONLY, value="Neutron")
[d6bf064]554        self.radiation_input.Bind(wx.EVT_COMBOBOX, self.radiationtype_changed)
555        input_grid.Add(self.radiation_input, (y,1), (1,1), wx.ALL, 5)
[55775e8]556        y += 1
557
[a58706d]558        output_label = wx.StaticText(self, -1, "Output File: ")
[4b862c4]559        input_grid.Add(output_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5)
[a58706d]560
[0e11ec7]561        self.output = FileInput(self,
562            wildcard="CanSAS 1D (*.xml)|*.xml")
563        input_grid.Add(self.output.GetCtrl(), (y,1), (1,1), wx.EXPAND | wx.ALL, 5)
[4b862c4]564        y += 1
[a58706d]565
[35488b2]566        self.convert_btn = wx.Button(self, wx.ID_OK, "Stop Converstion")
567        self.convert_btn.SetLabel("Convert")
568        input_grid.Add(self.convert_btn, (y,0), (1,1), wx.ALL, 5)
569        self.convert_btn.Bind(wx.EVT_BUTTON, self.on_convert)
[77d92cd]570
[11794f2]571        help_btn = wx.Button(self, -1, "HELP")
572        input_grid.Add(help_btn, (y,1), (1,1), wx.ALL, 5)
573        help_btn.Bind(wx.EVT_BUTTON, self.on_help)
574
[a58706d]575        section_sizer.Add(input_grid)
[77d92cd]576
[f2b3f28]577        vbox.Add(section_sizer, flag=wx.ALL, border=5)
578
[d6bf064]579        self.metadata_section = wx.CollapsiblePane(self, -1, "Metadata",
[fdbea3c]580            size=(_STATICBOX_WIDTH+40, -1), style=wx.WS_EX_VALIDATE_RECURSIVELY)
[d6bf064]581        metadata_pane = self.metadata_section.GetPane()
[f2b3f28]582        metadata_grid = wx.GridBagSizer(5, 5)
583
[d6bf064]584        self.metadata_section.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED,
[a027549]585            self.on_collapsible_pane)
586
[f2b3f28]587        y = 0
[503cc34]588        for item in self.properties:
[7c8ddb83]589            # Capitalise each word
590            label_txt = " ".join(
591                [s.capitalize() for s in item.replace('_', ' ').split(' ')])
592            if item == 'run':
593                label_txt = "Run Number"
[fdbea3c]594            label = wx.StaticText(metadata_pane, -1, label_txt,
[f2b3f28]595                style=wx.ALIGN_CENTER_VERTICAL)
[fdbea3c]596            input_box = wx.TextCtrl(metadata_pane, name=item,
[f2b3f28]597                size=(_STATICBOX_WIDTH-80, -1))
598            input_box.Bind(wx.EVT_TEXT, self.metadata_changed)
599            metadata_grid.Add(label, (y,0), (1,1),
600                wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
[fdbea3c]601            metadata_grid.Add(input_box, (y,1), (1,2), wx.EXPAND)
[f2b3f28]602            y += 1
603
[fdbea3c]604        detector_label = wx.StaticText(metadata_pane, -1,
605            "Detector:")
606        metadata_grid.Add(detector_label, (y, 0), (1,1), wx.ALL | wx.EXPAND, 5)
[af84162]607        detector_btn = wx.Button(metadata_pane, -1, "Enter Detector Metadata")
608        metadata_grid.Add(detector_btn, (y, 1), (1,1), wx.ALL | wx.EXPAND, 5)
609        detector_btn.Bind(wx.EVT_BUTTON, self.show_detector_window)
[fdbea3c]610        y += 1
611
[2a7722b]612        sample_label = wx.StaticText(metadata_pane, -1, "Sample: ")
613        metadata_grid.Add(sample_label, (y,0), (1,1), wx.ALL | wx.EXPAND, 5)
614        sample_btn = wx.Button(metadata_pane, -1, "Enter Sample Metadata")
615        metadata_grid.Add(sample_btn, (y,1), (1,1), wx.ALL | wx.EXPAND, 5)
616        sample_btn.Bind(wx.EVT_BUTTON, self.show_sample_window)
617        y += 1
618
[55bc56bc]619        source_label = wx.StaticText(metadata_pane, -1, "Source: ")
620        metadata_grid.Add(source_label, (y,0), (1,1), wx.ALL | wx.EXPAND, 5)
621        source_btn = wx.Button(metadata_pane, -1, "Enter Source Metadata")
622        source_btn.Bind(wx.EVT_BUTTON, self.show_source_window)
623        metadata_grid.Add(source_btn, (y,1), (1,1), wx.ALL | wx.EXPAND, 5)
624        y += 1
625
[fdbea3c]626        metadata_pane.SetSizer(metadata_grid)
[f2b3f28]627
[d6bf064]628        vbox.Add(self.metadata_section, proportion=0, flag=wx.ALL, border=5)
[77d92cd]629
630        vbox.Fit(self)
631        self.SetSizer(vbox)
632
633class ConverterWindow(widget.CHILD_FRAME):
[ba65aff]634    """Displays ConverterPanel"""
[77d92cd]635
636    def __init__(self, parent=None, title='File Converter', base=None,
[f5c52b0]637        manager=None, size=(PANEL_SIZE * 1.05, PANEL_SIZE / 1.1),
[77d92cd]638        *args, **kwargs):
639        kwargs['title'] = title
640        kwargs['size'] = size
641        widget.CHILD_FRAME.__init__(self, parent, *args, **kwargs)
642
643        self.manager = manager
644        self.panel = ConverterPanel(self, base=None)
645        self.Bind(wx.EVT_CLOSE, self.on_close)
646        self.SetPosition((wx.LEFT, PANEL_TOP))
647        self.Show(True)
648
649    def on_close(self, event):
650        if self.manager is not None:
651            self.manager.converter_frame = None
652        self.Destroy()
Note: See TracBrowser for help on using the repository browser.