Changeset dcb91cf in sasview for src/sas/sascalc/dataloader


Ignore:
Timestamp:
Aug 21, 2017 10:16:13 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:
7b15990
Parents:
44daa56
Message:

Make suggested changes

Location:
src/sas/sascalc/dataloader
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/file_reader_base_class.py

    r83ee7258 rdcb91cf  
    8484        :param msg: Error message 
    8585        """ 
    86         if isinstance(self.current_datainfo, DataInfo): 
     86        if len(self.output) > 0: 
     87            self.output[-1].errors.append(msg) 
     88        elif isinstance(self.current_datainfo, DataInfo): 
    8789            self.current_datainfo.errors.append(msg) 
    8890        else: 
     
    120122                if data.dlam is not None: 
    121123                    data.dlam = np.asarray([data.dlam[i] for i in ind]).astype(np.float64) 
    122                 if len(data.x > 0): 
     124                if len(data.x) > 0: 
    123125                    data.xmin = np.min(data.x) 
    124126                    data.xmax = np.max(data.x) 
  • src/sas/sascalc/dataloader/loader.py

    r3a81cd9 rdcb91cf  
    6969        readers if no reader was registered for the file's extension. 
    7070        """ 
     71        # Gets set to a string if the file has an associated reader that fails 
     72        msg_from_reader = None 
    7173        try: 
    7274            return super(Registry, self).load(path, format=format) 
    7375        except NoKnownLoaderException as nkl_e: 
    74             pass  # try the ASCII reader 
     76            pass  # Try the ASCII reader 
    7577        except FileContentsException as fc_exc: 
    76             # File has an associated reader but it failed 
    77             raise RuntimeError(fc_exc.message) 
     78            # File has an associated reader but it failed. 
     79            # Save the error message to display later, but try the 3 default loaders 
     80            msg_from_reader = fc_exc.message 
    7881        except Exception: 
    7982            pass 
    8083 
    81         # File has no associated reader - try the ASCII reader 
     84        # File has no associated reader, or the associated reader failed. 
     85        # Try the ASCII reader 
    8286        try: 
    8387            ascii_loader = ascii_reader.Reader() 
     
    8690            pass  # Loader specific error to try the cansas XML reader 
    8791        except FileContentsException as e: 
    88             raise RuntimeError(e.message) 
     92            if msg_from_reader is None: 
     93                raise RuntimeError(e.message) 
    8994 
    9095        # ASCII reader failed - try CanSAS xML reader 
     
    95100            pass  # Loader specific error to try the NXcanSAS reader 
    96101        except FileContentsException as e: 
    97             raise RuntimeError(e.message) 
    98         except Exception as csr: 
     102            if msg_from_reader is None: 
     103                raise RuntimeError(e.message) 
     104        except Exception: 
    99105            pass 
    100106 
     
    106112            logging.error("No default loader can load the data") 
    107113            # No known reader available. Give up and throw an error 
    108             msg = "\n\tUnknown data format: %s.\n\tThe file is not a " % path 
    109             msg += "known format that can be loaded by SasView.\n" 
    110             raise NoKnownLoaderException(msg) 
     114            if msg_from_reader is None: 
     115                msg = "\nUnknown data format: {}.\nThe file is not a ".format(path) 
     116                msg += "known format that can be loaded by SasView.\n" 
     117                raise NoKnownLoaderException(msg) 
     118            else: 
     119                # Associated reader and default readers all failed. 
     120                # Show error message from associated reader 
     121                raise RuntimeError(msg_from_reader) 
    111122        except FileContentsException as e: 
    112             raise RuntimeError(e.message) 
     123            err_msg = msg_from_reader if msg_from_reader is not None else e.message 
     124            raise RuntimeError(err_msg) 
    113125 
    114126    def find_plugins(self, dir): 
  • src/sas/sascalc/dataloader/readers/cansas_reader.py

    r248ff73 rdcb91cf  
    157157                    raise DataReaderException(invalid_xml) # Handled by base class 
    158158                except FileContentsException as fc_exc: 
    159                     if not self.extension in self.ext: # If the file has no associated loader 
    160                         raise DefaultReaderException(msg) 
    161159                    msg = "CanSAS Reader could not load the file {}".format(xml_file) 
    162160                    if fc_exc.message is not None: # Propagate error messages from earlier 
    163161                        msg = fc_exc.message 
     162                    if not self.extension in self.ext: # If the file has no associated loader 
     163                        raise DefaultReaderException(msg) 
    164164                    raise FileContentsException(msg) 
    165165                    pass 
     
    179179            self.set_xml_file(xml_file) 
    180180        except etree.XMLSyntaxError: # File isn't valid XML so can't be loaded 
    181             msg = "Cansas cannot load {}.\n Invalid XML syntax".format(xml_file) 
     181            msg = "SasView cannot load {}.\nInvalid XML syntax".format(xml_file) 
    182182            raise FileContentsException(msg) 
    183183 
     
    207207            return True # Why is this required? 
    208208        # If we get to this point then file isn't valid CanSAS 
     209        logger.warning("File doesn't meet CanSAS schema. Trying to load anyway.") 
    209210        raise FileContentsException("The file is not valid CanSAS") 
    210211 
  • src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py

    r8dec7e7 rdcb91cf  
    8282                        raise DefaultReaderException(msg) 
    8383                    raise FileContentsException(e.message) 
    84                 # Read in all child elements of top level SASroot 
    85                 self.read_children(self.raw_data, []) 
    86                 # Add the last data set to the list of outputs 
    87                 self.add_data_set() 
    88                 # Close the data file 
    89                 self.raw_data.close() 
    90         # Return data set(s) 
    91         return self.output 
     84                try: 
     85                    # Read in all child elements of top level SASroot 
     86                    self.read_children(self.raw_data, []) 
     87                    # Add the last data set to the list of outputs 
     88                    self.add_data_set() 
     89                except Exception as exc: 
     90                    raise FileContentsException(exc.message) 
     91                finally: 
     92                    # Close the data file 
     93                    self.raw_data.close() 
     94 
     95                for dataset in self.output: 
     96                    if isinstance(dataset, Data1D): 
     97                        if dataset.x.size < 5: 
     98                            self.output = [] 
     99                            raise FileContentsException("Fewer than 5 data points found.") 
    92100 
    93101    def reset_class_variables(self): 
Note: See TracChangeset for help on using the changeset viewer.