Changeset 26d4864 in sasview for src/sas/sascalc/dataloader


Ignore:
Timestamp:
Mar 18, 2016 11:08:53 AM (8 years ago)
Author:
krzywon
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:
b2ac7b0
Parents:
6a9c0e5a
Message:

Move the unit conversion from the script to the data reader

File:
1 edited

Legend:

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

    rb699768 r26d4864  
    8787#                print output                 
    8888                self.filename = output.filename = basename 
    89                  
    90                  
    91                 data_conv_z = None 
    92                 data_conv_P = None 
    93 #                print "passing" 
    94                 if has_converter == True and output.x_unit != 'A': 
    95                     data_conv_z = Converter('nm') 
    96                     # Test it 
    97                     data_conv_z(1.0, output.x_unit) 
    98 #                    print data_conv_z 
    99 #                    print data_conv_z(1.0, output.x_unit) 
    100                 if has_converter == True and output.y_unit != ' ': 
    101                     data_conv_P = Converter('a.u.') 
    102                     # Test it 
    103                     data_conv_P(1.0, output.y_unit) 
    104 #                    print data_conv_P 
    105 #                    print data_conv_P(1.0, output.y_unit) 
    106                 # The first good line of data will define whether 
    107                 # we have 2-column or 3-column ascii 
    108 #                print output.x_unit 
    109 #                print output.y_unit 
    110                  
    111 #                print "past output" 
    112                  
    113 #                has_error_dx = None 
    114 #                has_error_dy = None 
    115                  
     89 
    11690#                #Initialize counters for data lines and header lines. 
    11791#                is_data = False  # Has more than 5 lines 
     
    141115                for line in lines: 
    142116                    # Initial try for CSV (split on ,) 
    143 #                    print line 
    144117                    line=line.strip() 
    145 #                    print line 
    146118                    toks = line.split('\t') 
    147 #                    print toks 
    148119                    if len(toks)==2: 
    149120                        paramnames.append(toks[0]) 
    150 #                        print paramnames 
    151121                        paramvals.append(toks[1]) 
    152 #                        print paramvals 
    153122                    if len(toks)>5: 
    154123                        zvals.append(toks[0]) 
     
    160129                    else: 
    161130                        continue 
    162 #                print varheaders 
    163 #                print paramnames 
    164 #                print paramvals 
    165 #                print zvals   
    166 #                print len(zvals) 
    167                  
     131 
    168132                x=[] 
    169133                y=[] 
     
    172136                dy=[] 
    173137                dlam=[] 
     138                lam_header = lamvals[0].split() 
     139                data_conv_z = None 
     140                default_z_unit = "A" 
     141                data_conv_P = None 
     142                default_p_unit = " " 
     143                lam_unit = lam_header[1].replace("[","").replace("]","") 
     144                for i,x_val in output.x: 
     145                    output.x[i], output.x_unit = self._unit_conversion(x_val, lam_unit, default_z_unit) 
     146                for i,y_val in output.y: 
     147                    output.y[i], output.y_unit = self._unit_conversion(y_val, " ", default_p_unit) 
    174148                varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] 
    175 #                print varheader 
    176                 valrange=range(len(zvals)-1) 
    177 #                print valrange 
     149                valrange=range(1, len(zvals)) 
    178150                for i in valrange: 
    179                     x.append(float(zvals[i+1])) 
    180                     y.append(float(Pvals[i+1])) 
    181                     lam.append(float(lamvals[i+1])) 
    182                     dy.append(float(dPvals[i+1])) 
    183                     dx.append(float(dzvals[i+1])) 
    184                     dlam.append(float(dlamvals[i+1])) 
    185                      
     151                    x.append(float(zvals[i])) 
     152                    y.append(float(Pvals[i])) 
     153                    lam.append(float(lamvals[i])) 
     154                    dy.append(float(dPvals[i])) 
     155                    dx.append(float(dzvals[i])) 
     156                    dlam.append(float(dlamvals[i])) 
     157 
    186158                x,y,lam,dy,dx,dlam = [ 
    187159                   numpy.asarray(v, 'double') 
     
    189161                ] 
    190162 
    191 #                print x 
    192 #                print y 
    193 #                print dx 
    194 #                print dy 
    195 #                print len(x) 
    196 #                print len(y) 
    197 #                print len(dx) 
    198 #                print len(dy) 
    199                  
    200                  
    201163                input_f.close() 
    202                 # Sanity check 
    203 #                if has_error_dy == True and not len(y) == len(dy): 
    204 #                    msg = "sesans_reader: y and dy have different length" 
    205 #                    raise RuntimeError, msg 
    206 #                if has_error_dx == True and not len(x) == len(dx): 
    207 #                    msg = "sesans_reader: y and dy have different length" 
    208 #                    raise RuntimeError, msg 
    209 #                # If the data length is zero, consider this as 
    210 #                # though we were not able to read the file. 
    211 #                if len(x) == 0: 
    212 #                    raise RuntimeError, "sesans_reader: could not load file" 
    213 #                print "alive" 
    214                 #Let's re-order the data to make cal. 
    215                 # curve look better some cases 
    216 #                ind = numpy.lexsort((ty, tx)) 
    217 #                for i in ind: 
    218 #                    x[i] = tx[ind[i]] 
    219 #                    y[i] = ty[ind[i]] 
    220 #                    if has_error_dy == True: 
    221 #                        dy[i] = tdy[ind[i]] 
    222 #                    if has_error_dx == True: 
    223 #                        dx[i] = tdx[ind[i]] 
    224                 # Zeros in dx, dy 
    225 #                if has_error_dx: 
    226 #                    dx[dx == 0] = _ZERO 
    227 #                if has_error_dy: 
    228 #                    dy[dy == 0] = _ZERO 
     164 
    229165                #Data 
    230166                output.x = x #[x != 0] 
    231 #                print output.x 
    232167                output.y = y #[x != 0] 
    233 #                print output.y 
    234 #                output.dy = dy[x != 0] if has_error_dy == True\ 
    235 #                    else numpy.zeros(len(output.y)) 
    236 #                output.dx = dx[x != 0] if has_error_dx == True\ 
    237 #                    else numpy.zeros(len(output.x)) 
    238168                output.dy = dy 
    239169                output.dx = dx 
     
    241171                output.dlam = dlam 
    242172 
    243  
    244 #                print "still alive"                 
    245 #                if data_conv_z is not None: 
    246 #                    output.xaxis("\\rm{z}", output.x_unit) 
    247 #                else: 
    248 #                    output.xaxis("\\rm{z}", 'nm') 
    249 #                if data_conv_P is not None: 
    250 #                    output.yaxis("\\rm{P/P0}", output.y_unit) 
    251 #                else: 
    252 #                    output.yaxis("\\rm{P/P0}", "a.u.") 
    253                 output.xaxis("\\rm{z}", 'nm')    
    254                 output.yaxis("\\rm{P/P0}", " ") 
     173                output.xaxis("\rm{z}", output.x_unit) 
     174                output.yaxis("\\rm{P/P0}", output.y_unit) 
    255175                # Store loading process information 
    256176                output.meta_data['loader'] = self.type_name 
     
    261181                output.vars=varheader 
    262182 
    263 #                print "sesans_reader end" 
    264                  
    265183                if len(output.x) < 1: 
    266184                    raise RuntimeError, "%s is empty" % path 
    267 #                print output 
    268 #                print output.lam 
    269185                return output 
    270              
     186 
    271187        else: 
    272188            raise RuntimeError, "%s is not a file" % path 
    273189        return None 
     190 
     191    def _unit_conversion(self, value, value_unit, default_unit): 
     192        if has_converter == True and value_unit != default_unit: 
     193            data_conv_q = Converter(value_unit) 
     194            value = data_conv_q(value, units=default_unit) 
     195            new_unit = default_unit 
     196        else: 
     197            new_unit = value_unit 
     198        return value, new_unit 
Note: See TracChangeset for help on using the changeset viewer.