Changeset 952afaa in sasview for DataLoader/readers


Ignore:
Timestamp:
May 12, 2010 2:29:49 PM (14 years ago)
Author:
Jae Cho <jhjcho@…>
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, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
59a2dab
Parents:
3a295d2
Message:

refactored; speed optimized…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • DataLoader/readers/red2d_reader.py

    rc7c5ef8 r952afaa  
    4343            raise ValueError, \ 
    4444            "Specified file %s is not a regular file" % filename 
    45          
     45 
    4646        # Read file 
    4747        f = open(filename,'r') 
    4848        buf = f.read() 
    49          
     49        f.close()      
    5050        # Instantiate data object 
    5151        output = Data2D() 
     
    9494            data_conv_i(1.0, output.I_unit)             
    9595         
    96         #Set the space for data 
    97         data = numpy.zeros(0) 
    98         qx_data = numpy.zeros(0) 
    99         qy_data = numpy.zeros(0) 
    100         q_data = numpy.zeros(0) 
    101         dqx_data = numpy.zeros(0) 
    102         dqy_data = numpy.zeros(0) 
    103         mask = numpy.zeros(0,dtype=bool) 
    104          
    105         #Read Header and 2D data 
     96               
     97        # Remove the last lines before the for loop if the lines are empty 
     98        # to calculate the exact number of data points 
     99        count = 0 
     100        while (len(lines[len(lines)-(count+1)].lstrip().rstrip()) < 1): 
     101            del lines[len(lines)-(count+1)] 
     102            count = count + 1 
     103 
     104        #Read Header and find the dimensions of 2D data 
     105        line_num = 0 
    106106        for line in lines:       
     107            line_num += 1 
    107108            ## Reading the header applies only to IGOR/NIST 2D q_map data files 
    108109            # Find setup info line 
     
    159160            ## Read and get data.    
    160161            if dataStarted == True: 
    161                 line_toks = line.split()                
     162                line_toks = line.split()               
    162163                if len(line_toks) == 0: 
    163164                    #empty line 
    164165                    continue 
    165                 elif len(line_toks) < 3:  
    166                     #Q-map 2d require 3 or more columns of data           
    167                     raise ValueError,"Igor_Qmap_Reader: Can't read this file: Not a proper file format" 
    168  
    169                 # defaults  
    170                 dqx_value  = None 
    171                 dqy_value  = None 
    172                 qz_value   = None 
    173                 mask_value = 0 
    174                  
    175                 ##Read and get data values 
    176                 qx_value =  float(line_toks[0]) 
    177                 qy_value =  float(line_toks[1]) 
    178                 value    =  float(line_toks[2]) 
    179                  
    180                 try: 
    181                     #Read qz_value if exist on 4th column 
    182                     qz_value = float(line_toks[3])                     
    183                 except: 
    184                     # Found a non-float entry, skip it: Not required. 
    185                     pass                             
    186                 try: 
    187                     #Read qz_value if exist on 5th column 
    188                     dqx_value = float(line_toks[4]) 
    189                 except: 
    190                     # Found a non-float entry, skip it: Not required. 
    191                     pass 
    192                 try: 
    193                     #Read qz_value if exist on 6th column 
    194                     dqy_value = float(line_toks[5]) 
    195                 except: 
    196                     # Found a non-float entry, skip it: Not required. 
    197                     pass                 
    198                 try: 
    199                     #Read beam block mask if exist on 7th column 
    200                     mask_value = float(line_toks[6]) 
    201                 except: 
    202                     # Found a non-float entry, skip it 
    203                     pass  
    204                                               
    205                 # get data   
    206                 data    = numpy.append(data, value) 
    207                 qx_data = numpy.append(qx_data, qx_value) 
    208                 qy_data = numpy.append(qy_data, qy_value) 
    209                  
    210                 # optional data 
    211                 if dqx_value != None and numpy.isfinite(dqx_value):                
    212                     dqx_data = numpy.append(dqx_data, dqx_value) 
    213                 if dqy_value != None and numpy.isfinite(dqy_value):  
    214                     dqy_data = numpy.append(dqy_data, dqy_value)  
    215                      
    216                 # default data 
    217                 if qz_value == None or not numpy.isfinite(qz_value):  
    218                     qz_value = 0   
    219                 if not numpy.isfinite(mask_value):  
    220                     mask_value = 1              
    221                 q_data  = numpy.append(q_data,numpy.sqrt(qx_value**2+qy_value**2+qz_value**2)) 
    222                 # Note: For convenience, mask = False stands for masked, while mask = True for unmasked 
    223                 mask    = numpy.append(mask,(mask_value>=1)) 
    224                  
    225         f.close()         
     166                # the number of columns must be stayed same  
     167                col_num = len(line_toks) 
     168                break 
     169 
     170         
     171        # Make numpy array to remove header lines using index 
     172        lines_array = numpy.array(lines) 
     173 
     174        # index for lines_array 
     175        lines_index = numpy.arange(len(lines)) 
     176         
     177        # get the data lines 
     178        data_lines = lines_array[lines_index>=(line_num-1)] 
     179        # Now we get the total number of rows (i.e., # of data points) 
     180        row_num = len(data_lines) 
     181        # make it as list again to control the separators 
     182        data_list = " ".join(data_lines.tolist()) 
     183        # split all data to one big list w/" "separator 
     184        data_list = data_list.split() 
     185  
     186        # Check if the size is consistent with data, otherwise try the tab(\t) separator 
     187        # (this may be removed once get the confidence the former working all cases). 
     188        if len(data_list) != (len(data_lines)) * col_num: 
     189            data_list = "\t".join(data_lines.tolist()) 
     190            data_list = data_list.split() 
     191             
     192        # Change it(string) into float 
     193        data_list = map(float,data_list) 
     194        # numpy array form 
     195        data_array = numpy.array(data_list) 
     196 
     197        # Redimesion based on the row_num and col_num, otherwise raise an error. 
     198        try: 
     199            data_point = data_array.reshape(row_num,col_num).transpose() 
     200        except: 
     201            raise ValueError, "red2d_reader: Can't read this file: Not a proper file format" 
     202         
     203        ## Get the all data: Let's HARDcoding; Todo find better way 
     204        # Defaults 
     205        dqx_data = numpy.zeros(0) 
     206        dqy_data = numpy.zeros(0) 
     207        qz_data = numpy.zeros(row_num) 
     208        mask = numpy.ones(row_num,dtype=bool) 
     209        # Get from the array 
     210        qx_data = data_point[0] 
     211        qy_data = data_point[1] 
     212        data = data_point[2] 
     213        if col_num >3: qz_data = data_point[3] 
     214        if col_num >4: dqx_data = data_point[4] 
     215        if col_num >5: dqy_data = data_point[5] 
     216        if col_num >6: mask[data_point[6]<1] = False 
     217        q_data = numpy.sqrt(qx_data*qx_data+qy_data*qy_data+qz_data*qz_data) 
     218            
     219        # Extra protection(it is needed for some data files):  
    226220        # If all mask elements are False, put all True 
    227221        if not mask.any(): mask[mask==False] = True    
     
    315309    print reader.read("../test/exp18_14_igor_2dqxqy.dat")  
    316310         
     311 
Note: See TracChangeset for help on using the changeset viewer.