Changeset fafe52a in sasview for src/sas/sascalc/dataloader/readers


Ignore:
Timestamp:
Jul 31, 2017 4:28:48 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:
2f85af7
Parents:
9df816b
Message:

Refactor Anton Paar SAXS reader to use FileReader? class

Location:
src/sas/sascalc/dataloader/readers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/anton_paar_saxs_reader.py

    ra235f715 rfafe52a  
    99 
    1010from sas.sascalc.dataloader.readers.xml_reader import XMLreader 
    11 from sas.sascalc.dataloader.data_info import plottable_1D, Data1D, Sample, Source 
     11from sas.sascalc.dataloader.data_info import plottable_1D, Data1D, DataInfo, Sample, Source 
    1212from sas.sascalc.dataloader.data_info import Process, Aperture, Collimation, TransmissionSpectrum, Detector 
    13  
     13from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DataReaderException 
    1414 
    1515class Reader(XMLreader): 
    1616    """ 
    17     A class for reading in CanSAS v2.0 data files. The existing iteration opens Mantid generated HDF5 formatted files 
    18     with file extension .h5/.H5. Any number of data sets may be present within the file and any dimensionality of data 
    19     may be used. Currently 1D and 2D SAS data sets are supported, but future implementations will include 1D and 2D 
    20     SESANS data. This class assumes a single data set for each sasentry. 
    21  
    22     :Dependencies: 
    23         The CanSAS HDF5 reader requires h5py v2.5.0 or later. 
     17    A class for reading in Anton Paar .pdh files 
    2418    """ 
    2519 
     
    3024    ## Raw file contents to be processed 
    3125    raw_data = None 
    32     ## Data set being modified 
    33     current_dataset = None 
    3426    ## For recursion and saving purposes, remember parent objects 
    3527    parent_list = None 
     
    4234    ## Flag to bypass extension check 
    4335    allow_all = False 
    44     ## List of files to return 
    45     output = None 
    4636 
    4737    def reset_state(self): 
    48         self.current_dataset = Data1D(np.empty(0), np.empty(0), 
    49                                             np.empty(0), np.empty(0)) 
     38        self.current_dataset = plottable_1D(np.empty(0), np.empty(0), np.empty(0), np.empty(0)) 
     39        self.current_datainfo = DataInfo() 
    5040        self.datasets = [] 
    5141        self.raw_data = None 
     
    6353        self.lower = 5 
    6454 
    65     def read(self, filename): 
     55    def get_file_contents(self): 
    6656        """ 
    6757            This is the general read method that all SasView data_loaders must have. 
     
    7363        ## Reinitialize the class when loading a new data file to reset all class variables 
    7464        self.reset_state() 
    75         ## Check that the file exists 
    76         if os.path.isfile(filename): 
    77             basename = os.path.basename(filename) 
    78             _, extension = os.path.splitext(basename) 
    79             # If the file type is not allowed, return empty list 
    80             if extension in self.ext or self.allow_all: 
    81                 ## Load the data file 
    82                 input_f = open(filename, 'r') 
    83                 buff = input_f.read() 
    84                 self.raw_data = buff.splitlines() 
    85                 self.read_data() 
    86         return self.output 
     65        buff = self.f_open.read() 
     66        self.raw_data = buff.splitlines() 
     67        self.read_data() 
    8768 
    8869    def read_data(self): 
     70        correctly_loaded = True 
     71        error_message = "" 
     72 
    8973        q_unit = "1/nm" 
    9074        i_unit = "1/um^2" 
    91         self.current_dataset.title = self.raw_data[0] 
    92         self.current_dataset.meta_data["Keywords"] = self.raw_data[1] 
    93         line3 = self.raw_data[2].split() 
    94         line4 = self.raw_data[3].split() 
    95         line5 = self.raw_data[4].split() 
    96         self.data_points = int(line3[0]) 
    97         self.lower = 5 
    98         self.upper = self.lower + self.data_points 
    99         self.source.radiation = 'x-ray' 
    100         normal = float(line4[3]) 
    101         self.current_dataset.source.radiation = "x-ray" 
    102         self.current_dataset.source.name = "Anton Paar SAXSess Instrument" 
    103         self.current_dataset.source.wavelength = float(line4[4]) 
    104         xvals = [] 
    105         yvals = [] 
    106         dyvals = [] 
    107         for i in range(self.lower, self.upper): 
    108             index = i - self.lower 
    109             data = self.raw_data[i].split() 
    110             xvals.insert(index, normal * float(data[0])) 
    111             yvals.insert(index, normal * float(data[1])) 
    112             dyvals.insert(index, normal * float(data[2])) 
     75        try: 
     76            self.current_datainfo.title = self.raw_data[0] 
     77            self.current_datainfo.meta_data["Keywords"] = self.raw_data[1] 
     78            line3 = self.raw_data[2].split() 
     79            line4 = self.raw_data[3].split() 
     80            line5 = self.raw_data[4].split() 
     81            self.data_points = int(line3[0]) 
     82            self.lower = 5 
     83            self.upper = self.lower + self.data_points 
     84            self.source.radiation = 'x-ray' 
     85            normal = float(line4[3]) 
     86            self.current_datainfo.source.radiation = "x-ray" 
     87            self.current_datainfo.source.name = "Anton Paar SAXSess Instrument" 
     88            self.current_datainfo.source.wavelength = float(line4[4]) 
     89            xvals = [] 
     90            yvals = [] 
     91            dyvals = [] 
     92            for i in range(self.lower, self.upper): 
     93                index = i - self.lower 
     94                data = self.raw_data[i].split() 
     95                xvals.insert(index, normal * float(data[0])) 
     96                yvals.insert(index, normal * float(data[1])) 
     97                dyvals.insert(index, normal * float(data[2])) 
     98        except Exception as e: 
     99            error_message = "Couldn't load {}.\n".format(self.f_open.name) 
     100            error_message += e.message 
     101            raise FileContentsException(error_message) 
    113102        self.current_dataset.x = np.append(self.current_dataset.x, xvals) 
    114103        self.current_dataset.y = np.append(self.current_dataset.y, yvals) 
    115104        self.current_dataset.dy = np.append(self.current_dataset.dy, dyvals) 
    116105        if self.data_points != self.current_dataset.x.size: 
    117             self.errors.add("Not all data was loaded properly.") 
    118         if self.current_dataset.dx.size != self.current_dataset.x.size: 
    119             dxvals = np.zeros(self.current_dataset.x.size) 
    120             self.current_dataset.dx = dxvals 
     106            error_message += "Not all data points could be loaded.\n" 
     107            correctly_loaded = False 
    121108        if self.current_dataset.x.size != self.current_dataset.y.size: 
    122             self.errors.add("The x and y data sets are not the same size.") 
     109            error_message += "The x and y data sets are not the same size.\n" 
     110            correctly_loaded = False 
    123111        if self.current_dataset.y.size != self.current_dataset.dy.size: 
    124             self.errors.add("The y and dy datasets are not the same size.") 
    125         self.current_dataset.errors = self.errors 
     112            error_message += "The y and dy datasets are not the same size.\n" 
     113            correctly_loaded = False 
     114 
    126115        self.current_dataset.xaxis("Q", q_unit) 
    127116        self.current_dataset.yaxis("Intensity", i_unit) 
    128117        xml_intermediate = self.raw_data[self.upper:] 
    129118        xml = ''.join(xml_intermediate) 
    130         self.set_xml_string(xml) 
    131         dom = self.xmlroot.xpath('/fileinfo') 
    132         self._parse_child(dom) 
    133         self.output.append(self.current_dataset) 
     119        try: 
     120            self.set_xml_string(xml) 
     121            dom = self.xmlroot.xpath('/fileinfo') 
     122            self._parse_child(dom) 
     123        except Exception as e: 
     124            # Data loaded but XML metadata has an error 
     125            error_message += "Data points have been loaded but there was an " 
     126            error_message += "error reading XML metadata: " + e.message 
     127            correctly_loaded = False 
     128        self.send_to_output() 
     129        if not correctly_loaded: 
     130            raise DataReaderException(error_message) 
    134131 
    135132    def _parse_child(self, dom, parent=''): 
     
    146143                self._parse_child(node, key) 
    147144                if key == "SampleDetector": 
    148                     self.current_dataset.detector.append(self.detector) 
     145                    self.current_datainfo.detector.append(self.detector) 
    149146                    self.detector = Detector() 
    150147            else: 
    151148                if key == "value": 
    152149                    if parent == "Wavelength": 
    153                         self.current_dataset.source.wavelength = value 
     150                        self.current_datainfo.source.wavelength = value 
    154151                    elif parent == "SampleDetector": 
    155152                        self.detector.distance = value 
    156153                    elif parent == "Temperature": 
    157                         self.current_dataset.sample.temperature = value 
     154                        self.current_datainfo.sample.temperature = value 
    158155                    elif parent == "CounterSlitLength": 
    159156                        self.detector.slit_length = value 
     
    161158                    value = value.replace("_", "") 
    162159                    if parent == "Wavelength": 
    163                         self.current_dataset.source.wavelength_unit = value 
     160                        self.current_datainfo.source.wavelength_unit = value 
    164161                    elif parent == "SampleDetector": 
    165162                        self.detector.distance_unit = value 
     
    169166                        self.current_dataset.yaxis(self.current_dataset._yaxis, value) 
    170167                    elif parent == "Temperature": 
    171                         self.current_dataset.sample.temperature_unit = value 
     168                        self.current_datainfo.sample.temperature_unit = value 
    172169                    elif parent == "CounterSlitLength": 
    173170                        self.detector.slit_length_unit = value 
  • src/sas/sascalc/dataloader/readers/xml_reader.py

    rbc570f4 rfafe52a  
    9393        except etree.XMLSyntaxError as xml_error: 
    9494            logger.info(xml_error) 
    95         except Exception: 
     95            raise xml_error 
     96        except Exception as exc: 
    9697            self.xml = None 
    9798            self.xmldoc = None 
    9899            self.xmlroot = None 
     100            raise exc 
    99101 
    100102    def set_schema(self, schema): 
Note: See TracChangeset for help on using the changeset viewer.