Changeset 9c500ab in sasview for src/sas


Ignore:
Timestamp:
Aug 9, 2016 4:23:08 AM (8 years ago)
Author:
lewis
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:
dc8a553
Parents:
b7c21a7
Message:

Add more descriptive error messages

Location:
src/sas
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/file_converter/bsl_loader.py

    r535e181 r9c500ab  
    44import numpy as np 
    55 
     6class BSLParsingError(Exception): 
     7    pass 
     8 
    69class BSLLoader(CLoader): 
     10    """ 
     11    Loads 2D SAS data from a BSL file. 
     12    CLoader is a C extension (found in c_ext/bsl_loader.c) 
    713 
    8     # TODO: Change to __init__(self, filename, frame) 
    9     # and parse n_(pixels/rasters) from header file 
     14    See http://www.diamond.ac.uk/Beamlines/Soft-Condensed-Matter/small-angle/SAXS-Software/CCP13/BSL.html 
     15    for more info on the BSL file format. 
     16    """ 
     17 
    1018    def __init__(self, filename): 
     19        """ 
     20        Parses the BSL header file and sets instance variables apropriately 
     21 
     22        :param filename: Path to the BSL header file 
     23        """ 
    1124        header_file = open(filename, 'r') 
    1225        data_info = {} 
     
    1528 
    1629        [folder, filename] = os.path.split(filename) 
     30        # SAS data will be in file Xnn001.mdd 
     31        sasdata_filename = filename.replace('000.', '001.') 
     32        if sasdata_filename == filename: 
     33            err_msg = ("Invalid header filename {}.\n Should be of the format " 
     34                "Xnn000.XXX where X is any alphanumeric character and n is any" 
     35                " digit.").format(filename) 
     36            raise BSLParsingError(err_msg) 
    1737 
    1838        # First 2 lines are headers 
     
    2949                err_msg = "Invalid header file: {}".format(filename) 
    3050                break 
    31             # SAS data will be in file Xnn001.mdd 
    32             if data_filename != filename.replace('0.', '1.'): 
     51 
     52            if data_filename != sasdata_filename: 
     53                last_file = (metadata[9] == '0') 
     54                if last_file: # Reached last file we have metadata for 
     55                    is_valid = False 
     56                    err_msg = "No metadata for {} found in header file: {}" 
     57                    err_msg = err_msg.format(sasdata_filename, filename) 
     58                    break 
    3359                continue 
    3460            try: 
     
    4369                is_valid = False 
    4470                err_msg = "Invalid metadata in header file for {}" 
    45                 err_msg = err_msg.format(filename.replace('0.', '1.')) 
     71                err_msg = err_msg.format(sasdata_filename) 
    4672            break 
    4773 
    4874        if not is_valid: 
    49             raise Exception(err_msg) 
    50  
    51         if data_info['frames'] == 1: 
    52             # File is actually in OTOKO (1D) format 
    53             # Number of frames is 2nd indicator, 
    54             data_info['frames'] = data_info['rasters'] 
    55             data_info['rasters'] = data_info['pixels'] 
    56             data_info['pixels'] = 1 
     75            raise BSLParsingError(err_msg) 
    5776 
    5877        CLoader.__init__(self, data_info['filename'], data_info['frames'], 
  • src/sas/sasgui/perspectives/file_converter/converter_panel.py

    rb7c21a7 r9c500ab  
    186186        loader = OTOKOLoader(self.q_input.GetPath(), 
    187187            self.iq_input.GetPath()) 
    188         bsl_data = loader.load_bsl_data() 
    189         qdata = bsl_data.q_axis.data 
    190         iqdata = bsl_data.data_axis.data 
     188        otoko_data = loader.load_otoko_data() 
     189        qdata = otoko_data.q_axis.data 
     190        iqdata = otoko_data.data_axis.data 
    191191        if len(qdata) > 1: 
    192192            msg = ("Q-Axis file has multiple frames. Only 1 frame is " 
     
    212212        loader = BSLLoader(filename) 
    213213        frames = [0] 
     214        should_continue = True 
     215 
    214216        if loader.n_frames > 1: 
    215217            params = self.ask_frame_range(loader.n_frames) 
    216218            frames = params['frames'] 
     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 
    217242        frame_data = {} 
    218243 
     
    294319                    self.iq_input.GetPath()) 
    295320 
     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 
    296326                file_path = self.output.GetPath() 
    297327                self.convert_to_red2d(file_path, x_data, y_data, frame_data) 
     
    342372            'source': self.source 
    343373        } 
    344         import pdb; pdb.set_trace() 
    345374 
    346375        frame_data = {} 
  • src/sas/sasgui/perspectives/file_converter/otoko_loader.py

    rba65aff r9c500ab  
    11""" 
    22Here we handle loading of "OTOKO" data (for more info about this format see 
    3 the comment in load_bsl_data).  Given the paths of header and data files, we 
     3the comment in load_otoko_data).  Given the paths of header and data files, we 
    44aim to load the data into numpy arrays for use later. 
    55""" 
     
    2929        self.data_path = data_path 
    3030 
    31     def load_bsl_data(self): 
     31    def load_otoko_data(self): 
    3232        """ 
    3333        Loads "OTOKO" data, which is a format that stores each axis separately. 
     
    4242        http://www.diamond.ac.uk/Home/Beamlines/small-angle/SAXS-Software/CCP13/ 
    4343        XOTOKO.html 
    44  
    45         The BSL format, which is based on OTOKO, is also supported.  Find out more 
    46         about the BSL format at http://www.diamond.ac.uk/Home/Beamlines/small-angle 
    47         /SAXS-Software/CCP13/BSL.html. 
    4844        """ 
    49         q_axis    = self._load_bsl_axis(self.qaxis_path) 
    50         data_axis = self._load_bsl_axis(self.data_path) 
     45        q_axis    = self._load_otoko_axis(self.qaxis_path) 
     46        data_axis = self._load_otoko_axis(self.data_path) 
    5147 
    5248        return OTOKOData(q_axis, data_axis) 
    5349 
    54     def _load_bsl_axis(self, header_path): 
     50    def _load_otoko_axis(self, header_path): 
    5551        """ 
    5652        Loads an "OTOKO" axis, given the header file path.  Essentially, the 
     
    107103                    last_file  = int(indicators[9]) == 0 # We don't use this. 
    108104                ) 
     105                if binary_file_info.dimensions != 1: 
     106                    msg = "File {} has {} dimensions, expected 1. Is it a BSL file?" 
     107                    raise OTOKOParsingError(msg.format(filename.strip(), 
     108                        binary_file_info.dimensions)) 
     109 
    109110                binary_file_info_list.append(binary_file_info) 
    110111 
Note: See TracChangeset for help on using the changeset viewer.