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