Ignore:
Timestamp:
Jul 31, 2017 5:25:56 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:
a78433dd
Parents:
fafe52a
Message:

Refactor red_2d_reader to use FileReader? class

File:
1 edited

Legend:

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

    r959eb01 r2f85af7  
    55#This software was developed by the University of Tennessee as part of the 
    66#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    7 #project funded by the US National Science Foundation.  
     7#project funded by the US National Science Foundation. 
    88#See the license text in license.txt 
    99#copyright 2008, University of Tennessee 
     
    1212import numpy as np 
    1313import math 
    14 from sas.sascalc.dataloader.data_info import Data2D, Detector 
     14from sas.sascalc.dataloader.data_info import plottable_2D, DataInfo, Detector 
     15from sas.sascalc.dataloader.file_reader_base_class import FileReader 
     16from sas.sascalc.dataloader.loader_exceptions import FileContentsException 
    1517 
    1618# Look for unit converter 
     
    2022except: 
    2123    has_converter = False 
    22      
    23      
     24 
     25 
    2426def check_point(x_point): 
    2527    """ 
     
    3133    except: 
    3234        return 0 
    33        
    34          
    35 class Reader: 
     35 
     36 
     37class Reader(FileReader): 
    3638    """ Simple data reader for Igor data files """ 
    3739    ## File type 
     
    4143    ## Extension 
    4244    ext = ['.DAT', '.dat'] 
    43      
     45 
    4446    def write(self, filename, data): 
    4547        """ 
    4648        Write to .dat 
    47          
     49 
    4850        :param filename: file name to write 
    4951        :param data: data2D 
     
    5153        import time 
    5254        # Write the file 
    53         fd = open(filename, 'w') 
    54         t = time.localtime() 
    55         time_str = time.strftime("%H:%M on %b %d %y", t) 
    56          
    57         header_str = "Data columns are Qx - Qy - I(Qx,Qy)\n\nASCII data" 
    58         header_str += " created at %s \n\n" % time_str 
    59         # simple 2D header 
    60         fd.write(header_str) 
    61         # write qx qy I values 
    62         for i in range(len(data.data)): 
    63             fd.write("%g  %g  %g\n" % (data.qx_data[i], 
    64                                         data.qy_data[i], 
    65                                        data.data[i])) 
    66         # close 
    67         fd.close() 
    68  
    69     def read(self, filename=None): 
    70         """ Read file """ 
    71         if not os.path.isfile(filename): 
    72             raise ValueError, \ 
    73             "Specified file %s is not a regular file" % filename 
    74  
     55        try: 
     56            fd = open(filename, 'w') 
     57            t = time.localtime() 
     58            time_str = time.strftime("%H:%M on %b %d %y", t) 
     59 
     60            header_str = "Data columns are Qx - Qy - I(Qx,Qy)\n\nASCII data" 
     61            header_str += " created at %s \n\n" % time_str 
     62            # simple 2D header 
     63            fd.write(header_str) 
     64            # write qx qy I values 
     65            for i in range(len(data.data)): 
     66                fd.write("%g  %g  %g\n" % (data.qx_data[i], 
     67                                            data.qy_data[i], 
     68                                           data.data[i])) 
     69        finally: 
     70            fd.close() 
     71 
     72    def get_file_contents(self): 
    7573        # Read file 
    76         f = open(filename, 'r') 
    77         buf = f.read() 
    78         f.close() 
     74        buf = self.f_open.read() 
     75        self.f_open.close() 
    7976        # Instantiate data object 
    80         output = Data2D() 
    81         output.filename = os.path.basename(filename) 
    82         detector = Detector() 
    83         if len(output.detector) > 0: 
    84             print str(output.detector[0]) 
    85         output.detector.append(detector) 
    86                  
     77        self.current_dataset = plottable_2D() 
     78        self.current_datainfo = DataInfo() 
     79        self.current_datainfo.filename = os.path.basename(self.f_open.name) 
     80        self.current_datainfo.detector.append(Detector()) 
     81 
    8782        # Get content 
    88         dataStarted = False 
    89          
     83        data_started = False 
     84 
    9085        ## Defaults 
    9186        lines = buf.split('\n') 
    9287        x = [] 
    9388        y = [] 
    94          
     89 
    9590        wavelength = None 
    9691        distance = None 
    9792        transmission = None 
    98          
     93 
    9994        pixel_x = None 
    10095        pixel_y = None 
    101          
    102         isInfo = False 
    103         isCenter = False 
    104  
    105         data_conv_q = None 
    106         data_conv_i = None 
    107          
    108         # Set units: This is the unit assumed for Q and I in the data file. 
    109         if has_converter == True and output.Q_unit != '1/A': 
    110             data_conv_q = Converter('1/A') 
    111             # Test it 
    112             data_conv_q(1.0, output.Q_unit) 
    113              
    114         if has_converter == True and output.I_unit != '1/cm': 
    115             data_conv_i = Converter('1/cm') 
    116             # Test it 
    117             data_conv_i(1.0, output.I_unit) 
    118          
    119                
     96 
     97        is_info = False 
     98        is_center = False 
     99 
    120100        # Remove the last lines before the for loop if the lines are empty 
    121101        # to calculate the exact number of data points 
     
    133113            ## Reading the header applies only to IGOR/NIST 2D q_map data files 
    134114            # Find setup info line 
    135             if isInfo: 
    136                 isInfo = False 
     115            if is_info: 
     116                is_info = False 
    137117                line_toks = line.split() 
    138118                # Wavelength in Angstrom 
     
    141121                    # Units 
    142122                    if has_converter == True and \ 
    143                     output.source.wavelength_unit != 'A': 
     123                    self.current_datainfo.source.wavelength_unit != 'A': 
    144124                        conv = Converter('A') 
    145125                        wavelength = conv(wavelength, 
    146                                           units=output.source.wavelength_unit) 
     126                                          units=self.current_datainfo.source.wavelength_unit) 
    147127                except: 
    148128                    #Not required 
     
    152132                    distance = float(line_toks[3]) 
    153133                    # Units 
    154                     if has_converter == True and detector.distance_unit != 'm': 
     134                    if has_converter == True and self.current_datainfo.detector[0].distance_unit != 'm': 
    155135                        conv = Converter('m') 
    156                         distance = conv(distance, units=detector.distance_unit) 
     136                        distance = conv(distance, 
     137                            units=self.current_datainfo.detector[0].distance_unit) 
    157138                except: 
    158139                    #Not required 
    159140                    pass 
    160                  
     141 
    161142                # Distance in meters 
    162143                try: 
     
    165146                    #Not required 
    166147                    pass 
    167                                              
     148 
    168149            if line.count("LAMBDA") > 0: 
    169                 isInfo = True 
    170                  
     150                is_info = True 
     151 
    171152            # Find center info line 
    172             if isCenter: 
    173                 isCenter = False 
     153            if is_center: 
     154                is_center = False 
    174155                line_toks = line.split() 
    175156                # Center in bin number 
     
    178159 
    179160            if line.count("BCENT") > 0: 
    180                 isCenter = True 
     161                is_center = True 
    181162            # Check version 
    182163            if line.count("Data columns") > 0: 
     
    185166            # Find data start 
    186167            if line.count("ASCII data") > 0: 
    187                 dataStarted = True 
     168                data_started = True 
    188169                continue 
    189170 
    190171            ## Read and get data. 
    191             if dataStarted == True: 
     172            if data_started == True: 
    192173                line_toks = line.split() 
    193174                if len(line_toks) == 0: 
    194175                    #empty line 
    195176                    continue 
    196                 # the number of columns must be stayed same  
     177                # the number of columns must be stayed same 
    197178                col_num = len(line_toks) 
    198179                break 
     
    202183        # index for lines_array 
    203184        lines_index = np.arange(len(lines)) 
    204          
     185 
    205186        # get the data lines 
    206187        data_lines = lines_array[lines_index >= (line_num - 1)] 
     
    211192        # split all data to one big list w/" "separator 
    212193        data_list = data_list.split() 
    213   
     194 
    214195        # Check if the size is consistent with data, otherwise 
    215196        #try the tab(\t) separator 
     
    231212            data_point = data_array.reshape(row_num, col_num).transpose() 
    232213        except: 
    233             msg = "red2d_reader: Can't read this file: Not a proper file format" 
    234             raise ValueError, msg 
     214            msg = "red2d_reader can't read this file: Incorrect number of data points provided." 
     215            raise FileContentsException(msg) 
    235216        ## Get the all data: Let's HARDcoding; Todo find better way 
    236217        # Defaults 
     
    255236        #if col_num > (6 + ver): mask[data_point[(6 + ver)] < 1] = False 
    256237        q_data = np.sqrt(qx_data*qx_data+qy_data*qy_data+qz_data*qz_data) 
    257             
    258         # Extra protection(it is needed for some data files):  
     238 
     239        # Extra protection(it is needed for some data files): 
    259240        # If all mask elements are False, put all True 
    260241        if not mask.any(): 
    261242            mask[mask == False] = True 
    262    
     243 
    263244        # Store limits of the image in q space 
    264245        xmin = np.min(qx_data) 
     
    267248        ymax = np.max(qy_data) 
    268249 
    269         # units 
    270         if has_converter == True and output.Q_unit != '1/A': 
    271             xmin = data_conv_q(xmin, units=output.Q_unit) 
    272             xmax = data_conv_q(xmax, units=output.Q_unit) 
    273             ymin = data_conv_q(ymin, units=output.Q_unit) 
    274             ymax = data_conv_q(ymax, units=output.Q_unit) 
    275              
    276250        ## calculate the range of the qx and qy_data 
    277251        x_size = math.fabs(xmax - xmin) 
    278252        y_size = math.fabs(ymax - ymin) 
    279          
     253 
    280254        # calculate the number of pixels in the each axes 
    281255        npix_y = math.floor(math.sqrt(len(data))) 
    282256        npix_x = math.floor(len(data) / npix_y) 
    283          
     257 
    284258        # calculate the size of bins 
    285259        xstep = x_size / (npix_x - 1) 
    286260        ystep = y_size / (npix_y - 1) 
    287          
     261 
    288262        # store x and y axis bin centers in q space 
    289263        x_bins = np.arange(xmin, xmax + xstep, xstep) 
    290264        y_bins = np.arange(ymin, ymax + ystep, ystep) 
    291         
     265 
    292266        # get the limits of q values 
    293267        xmin = xmin - xstep / 2 
     
    295269        ymin = ymin - ystep / 2 
    296270        ymax = ymax + ystep / 2 
    297          
     271 
    298272        #Store data in outputs 
    299273        #TODO: Check the lengths 
    300         output.data = data 
     274        self.current_dataset.data = data 
    301275        if (err_data == 1).all(): 
    302             output.err_data = np.sqrt(np.abs(data)) 
    303             output.err_data[output.err_data == 0.0] = 1.0 
     276            self.current_dataset.err_data = np.sqrt(np.abs(data)) 
     277            self.current_dataset.err_data[self.current_dataset.err_data == 0.0] = 1.0 
    304278        else: 
    305             output.err_data = err_data 
    306              
    307         output.qx_data = qx_data 
    308         output.qy_data = qy_data 
    309         output.q_data = q_data 
    310         output.mask = mask 
    311          
    312         output.x_bins = x_bins 
    313         output.y_bins = y_bins 
    314                 
    315         output.xmin = xmin 
    316         output.xmax = xmax 
    317         output.ymin = ymin 
    318         output.ymax = ymax 
    319          
    320         output.source.wavelength = wavelength 
    321          
     279            self.current_dataset.err_data = err_data 
     280 
     281        self.current_dataset.qx_data = qx_data 
     282        self.current_dataset.qy_data = qy_data 
     283        self.current_dataset.q_data = q_data 
     284        self.current_dataset.mask = mask 
     285 
     286        self.current_dataset.x_bins = x_bins 
     287        self.current_dataset.y_bins = y_bins 
     288 
     289        self.current_dataset.xmin = xmin 
     290        self.current_dataset.xmax = xmax 
     291        self.current_dataset.ymin = ymin 
     292        self.current_dataset.ymax = ymax 
     293 
     294        self.current_datainfo.source.wavelength = wavelength 
     295 
    322296        # Store pixel size in mm 
    323         detector.pixel_size.x = pixel_x 
    324         detector.pixel_size.y = pixel_y 
    325          
     297        self.current_datainfo.detector[0].pixel_size.x = pixel_x 
     298        self.current_datainfo.detector[0].pixel_size.y = pixel_y 
     299 
    326300        # Store the sample to detector distance 
    327         detector.distance = distance 
    328          
     301        self.current_datainfo.detector[0].distance = distance 
     302 
    329303        # optional data: if all of dq data == 0, do not pass to output 
    330304        if len(dqx_data) == len(qx_data) and dqx_data.any() != 0: 
     
    338312                    cos_th = qx_data / diag 
    339313                    sin_th = qy_data / diag 
    340                     output.dqx_data = np.sqrt((dqx_data * cos_th) * \ 
     314                    self.current_dataset.dqx_data = np.sqrt((dqx_data * cos_th) * \ 
    341315                                                 (dqx_data * cos_th) \ 
    342316                                                 + (dqy_data * sin_th) * \ 
    343317                                                  (dqy_data * sin_th)) 
    344                     output.dqy_data = np.sqrt((dqx_data * sin_th) * \ 
     318                    self.current_dataset.dqy_data = np.sqrt((dqx_data * sin_th) * \ 
    345319                                                 (dqx_data * sin_th) \ 
    346320                                                 + (dqy_data * cos_th) * \ 
    347321                                                  (dqy_data * cos_th)) 
    348322                else: 
    349                     output.dqx_data = dqx_data 
    350                     output.dqy_data = dqy_data 
     323                    self.current_dataset.dqx_data = dqx_data 
     324                    self.current_dataset.dqy_data = dqy_data 
    351325 
    352326        # Units of axes 
    353         if data_conv_q is not None: 
    354             output.xaxis("\\rm{Q_{x}}", output.Q_unit) 
    355             output.yaxis("\\rm{Q_{y}}", output.Q_unit) 
    356         else: 
    357             output.xaxis("\\rm{Q_{x}}", 'A^{-1}') 
    358             output.yaxis("\\rm{Q_{y}}", 'A^{-1}') 
    359         if data_conv_i is not None: 
    360             output.zaxis("\\rm{Intensity}", output.I_unit) 
    361         else: 
    362             output.zaxis("\\rm{Intensity}", "cm^{-1}") 
    363      
     327        self.current_dataset.xaxis("\\rm{Q_{x}}", 'A^{-1}') 
     328        self.current_dataset.yaxis("\\rm{Q_{y}}", 'A^{-1}') 
     329        self.current_dataset.zaxis("\\rm{Intensity}", "cm^{-1}") 
     330 
    364331        # Store loading process information 
    365         output.meta_data['loader'] = self.type_name 
    366  
    367         return output 
     332        self.current_datainfo.meta_data['loader'] = self.type_name 
     333 
     334        self.send_to_output() 
Note: See TracChangeset for help on using the changeset viewer.