Changeset f25328a5 in sasview for src/sas/sascalc


Ignore:
Timestamp:
Jul 20, 2017 11:18:55 AM (7 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.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
58a255b
Parents:
94e3572
Message:

Better error messages when 2D ASCII file is incorrectly formatted

File:
1 edited

Legend:

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

    r8bcf866 rf25328a5  
    4343        # Skip nUseRec lines 
    4444        current_line = 4 
    45         nUseRec = int(all_lines[current_line].strip()[0]) 
    46         current_line += nUseRec + 1 
     45        try: 
     46            nUseRec = int(all_lines[current_line].strip()[0]) 
     47            current_line += nUseRec + 1 
     48            # Read qx data 
     49            num_qs = int(all_lines[current_line].strip()) 
     50            current_line += 1 
     51            current_line, qx = _load_qs(all_lines, current_line, num_qs) 
    4752 
    48         # Read qx data 
    49         num_qs = int(all_lines[current_line].strip()) 
    50         current_line += 1 
    51         current_line, qx = _load_qs(all_lines, current_line, num_qs) 
    52  
    53         # Read qy data 
    54         num_qs = int(all_lines[current_line].strip()) 
    55         current_line += 1 
    56         current_line, qy = _load_qs(all_lines, current_line, num_qs) 
     53            # Read qy data 
     54            num_qs = int(all_lines[current_line].strip()) 
     55            current_line += 1 
     56            current_line, qy = _load_qs(all_lines, current_line, num_qs) 
     57        except ValueError as e: 
     58            err_msg = "File incorrectly formatted.\n" 
     59            if str(e).find('broadcast') != -1: 
     60                err_msg += "Incorrect number of q data points provided. " 
     61                err_msg += "Expected {}.".format(num_qs) 
     62            elif str(e).find('invalid literal') != -1: 
     63                err_msg += "Expected integer on line {}. Instead got '{}'".format(current_line + 1, 
     64                    all_lines[current_line]) 
     65            else: 
     66                err_msg += str(e) 
     67            raise ValueError(err_msg) 
    5768 
    5869        # dimensions: [width, height, scale] 
    59         dimensions = np.fromstring(all_lines[current_line], dtype=np.float32, sep=' ') 
    60         width = int(dimensions[0]) 
    61         height = int(dimensions[1]) 
     70        try: 
     71            dimensions = np.fromstring(all_lines[current_line], dtype=np.float32, sep=' ') 
     72            if len(dimensions) != 3: raise ValueError() 
     73            width = int(dimensions[0]) 
     74            height = int(dimensions[1]) 
     75        except ValueError as e: 
     76            err_msg = "File incorrectly formatted.\n" 
     77            err_msg += "Expected line {} to be of the form: <num_qx> <num_qy> <scale>.".format(current_line + 1) 
     78            err_msg += " Instead got '{}'.".format(all_lines[current_line]) 
     79            raise ValueError(err_msg) 
     80 
     81        if width > len(qx) or height > len(qy): 
     82            err_msg = "File incorrectly formatted.\n" 
     83            err_msg += ("Line {} says to use {}x{} points. " 
     84                "Only {}x{} provided.").format(current_line + 1, width, height, 
     85                len(qx), len(qy)) 
     86            raise ValueError(err_msg) 
    6287 
    6388        # More qx and/or qy points can be provided than are actually used 
     
    6994        # iflag = 2 => q axis and intensity data 
    7095        # iflag = 3 => q axis, intensity and error data 
    71         iflag = int(all_lines[current_line].strip()[0]) 
     96        try: 
     97            iflag = int(all_lines[current_line].strip()[0]) 
     98            if iflag <= 0 or iflag > 3: raise ValueError() 
     99        except: 
     100            err_msg = "File incorrectly formatted.\n" 
     101            iflag = all_lines[current_line].strip()[0] 
     102            err_msg += "Expected iflag on line {} to be 1, 2 or 3. Instead got '{}'.".format(current_line+1, iflag) 
     103            raise ValueError(err_msg) 
     104 
    72105        current_line += 1 
    73106 
Note: See TracChangeset for help on using the changeset viewer.