Changeset b99ac227 in sasview for DataLoader/readers


Ignore:
Timestamp:
Aug 6, 2008 10:54:02 AM (16 years ago)
Author:
Mathieu Doucet <doucetm@…>
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:
17a6843
Parents:
99d1af6
Message:

Updates and tests for readers

Location:
DataLoader/readers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • DataLoader/readers/IgorReader.py

    r68f6ae2 rb99ac227  
     1""" 
     2    IGOR 2D reduced file reader 
     3""" 
     4 
     5""" 
     6This software was developed by the University of Tennessee as part of the 
     7Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
     8project funded by the US National Science Foundation.  
     9 
     10If you use DANSE applications to do scientific research that leads to  
     11publication, we ask that you acknowledge the use of the software with the  
     12following sentence: 
     13 
     14"This work benefited from DANSE software developed under NSF award DMR-0520547."  
     15 
     16copyright 2008, University of Tennessee 
     17""" 
     18 
    119import os, sys 
    2 import pylab 
    3 from copy import deepcopy 
    4 import math,logging 
     20import numpy 
     21import math, logging 
     22from DataLoader.data_info import Data2D, Detector 
     23 
     24# Look for unit converter 
     25has_converter = True 
     26try: 
     27    from data_util.nxsunit import Converter 
     28except: 
     29    has_converter = False 
     30 
    531class Reader: 
    632    """ Simple data reader for Igor data files """ 
    7     ext=['.ASC'] 
    8     def __init__(self, filename=None): 
    9         """ Init 
    10             @param filename: Name of Igor data file to read 
    11         """ 
    12         self.file = filename 
    13         self.x = [] 
    14         self.y = [] 
    15         self.image = [] 
    16          
    17         # Detector info 
    18         self.wavelength = 0.0 
    19         self.distance   = 0.0 
    20         self.center_x   = 63.5 
    21         self.center_y   = 63.5 
    22          
     33    ## File type 
     34    type = ["IGOR 2D files (*.ASC)|*.ASC"] 
     35    ## Extension 
     36    ext=['.ASC', '.asc'] 
     37 
    2338    def read(self,filename=None): 
    2439        """ Read file """ 
    25         # Check if the file is there 
    26         self.file=filename 
    27         if not os.path.isfile(self.file): 
     40        if not os.path.isfile(filename): 
    2841            raise ValueError, \ 
    29             "Specified file %s is not a regular file" % self.file 
     42            "Specified file %s is not a regular file" % filename 
    3043         
    3144        # Read file 
    32         f = open(self.file,'r') 
     45        f = open(filename,'r') 
    3346        buf = f.read() 
     47         
     48        # Instantiate data object 
     49        output = Data2D() 
     50        output.filename = os.path.basename(filename) 
     51        detector = Detector() 
     52        if len(output.detector)>0: print str(output.detector[0]) 
     53        output.detector.append(detector) 
     54         
     55        size_x = 128 
     56        size_y = 128 
     57        output.data = numpy.zeros([size_x,size_y]) 
     58        output.err_data = numpy.zeros([size_x,size_y]) 
     59         
     60        data_conv_q = None 
     61        data_conv_i = None 
     62         
     63        if has_converter == True and output.Q_unit != '1/A': 
     64            data_conv_q = Converter('1/A') 
     65            # Test it 
     66            data_conv_q(1.0, output.Q_unit) 
     67             
     68        if has_converter == True and output.I_unit != '1/cm': 
     69            data_conv_i = Converter('1/cm') 
     70            # Test it 
     71            data_conv_i(1.0, output.I_unit)             
     72     
    3473         
    3574        # Get content 
     
    3978        lines = buf.split('\n') 
    4079        itot = 0 
    41         self.x = [] 
    42         self.y = [] 
    43         self.image = [] 
     80        x = [] 
     81        y = [] 
    4482         
    4583        ncounts = 0 
    46          
    47         #x = pylab.arange(0, 128, 1) 
    48         #y = pylab.arange(0, 128, 1) 
    49         x = pylab.arange(-.5, .5, 1.0/128) 
    50         y = pylab.arange(-.5, .5, 1.0/128) 
    51         X, Y = pylab.meshgrid(x, y) 
    52         Z = deepcopy(X) 
    5384         
    5485        xmin = None 
     
    79110                    raise ValueError,"IgorReader: can't read this file, missing distance" 
    80111                 
     112                # Distance in meters 
     113                try: 
     114                    transmission = float(line_toks[4]) 
     115                except: 
     116                    raise ValueError,"IgorReader: can't read this file, missing transmission" 
     117                 
    81118            if line.count("LAMBDA")>0: 
    82119                isInfo = True 
     
    109146                    value = float(line) 
    110147                except: 
     148                    # Found a non-float entry, skip it 
    111149                    continue 
    112150                 
     
    118156                    i_x += 1 
    119157                     
    120                 Z[i_y][i_x] = value 
     158                output.data[i_y][i_x] = value 
    121159                ncounts += 1  
    122160                 
     
    126164                theta = (i_x-center_x+1)*0.5 / distance / 100.0 
    127165                qx = 4.0*math.pi/wavelength * math.sin(theta/2.0) 
     166 
     167                if has_converter == True and output.Q_unit != '1/A': 
     168                    qx = data_conv_q(qx, units=output.Q_unit) 
     169 
    128170                if xmin==None or qx<xmin: 
    129171                    xmin = qx 
     
    133175                theta = (i_y-center_y+1)*0.5 / distance / 100.0 
    134176                qy = 4.0*math.pi/wavelength * math.sin(theta/2.0) 
     177 
     178                if has_converter == True and output.Q_unit != '1/A': 
     179                    qy = data_conv_q(qy, units=output.Q_unit) 
     180                 
    135181                if ymin==None or qy<ymin: 
    136182                    ymin = qy 
     
    138184                    ymax = qy 
    139185                 
    140                  
    141                 if not qx in self.x: 
    142                     self.x.append(qx) 
    143                 if not qy in self.y: 
    144                     self.y.append(qy) 
     186                if not qx in x: 
     187                    x.append(qx) 
     188                if not qy in y: 
     189                    y.append(qy) 
    145190                 
    146191                itot += 1 
     
    153198        ystep = 4.0*math.pi/wavelength * math.sin(theta/2.0) 
    154199         
    155         # Store q max  
    156         if xmax>ymax: 
    157             self.qmax = xmax 
     200        # Store all data ###################################### 
     201        # Store wavelength 
     202        if has_converter==True and output.source.wavelength_unit != 'A': 
     203            conv = Converter('A') 
     204            wavelength = conv(wavelength, units=output.source.wavelength_unit) 
     205        output.source.wavelength = wavelength 
     206 
     207        # Store distance 
     208        if has_converter==True and detector.distance_unit != 'm': 
     209            conv = Converter('m') 
     210            distance = conv(distance, units=detector.distance_unit) 
     211        detector.distance = distance 
     212   
     213        # Store transmission 
     214        output.sample.transmission = transmission 
     215         
     216        # Store pixel size 
     217        pixel = 5.0 
     218        if has_converter==True and detector.pixel_size_unit != 'mm': 
     219            conv = Converter('mm') 
     220            pixel = conv(pixel, units=detector.pixel_size_unit) 
     221        detector.pixel_size.x = pixel 
     222        detector.pixel_size.y = pixel 
     223   
     224        # Store beam center in distance units 
     225        detector.beam_center.x = center_x*pixel 
     226        detector.beam_center.y = center_y*pixel 
     227   
     228         
     229        # Store limits of the image (2D array) 
     230        xmin    =xmin-xstep/2.0 
     231        xmax    =xmax+xstep/2.0 
     232        ymin    =ymin-ystep/2.0 
     233        ymax    =ymax+ystep/2.0 
     234        if has_converter == True and output.Q_unit != '1/A': 
     235            xmin = data_conv_q(xmin, units=output.Q_unit) 
     236            xmax = data_conv_q(xmax, units=output.Q_unit) 
     237            ymin = data_conv_q(ymin, units=output.Q_unit) 
     238            ymax = data_conv_q(ymax, units=output.Q_unit) 
     239        output.xmin = xmin 
     240        output.xmax = xmax 
     241        output.ymin = ymin 
     242        output.ymax = ymax 
     243         
     244        # Store x and y axis bin centers 
     245        output.x_bins     = x 
     246        output.y_bins     = y 
     247         
     248        # Units 
     249        if data_conv_q is not None: 
     250            output.xaxis("\\rm{Q}", output.Q_unit) 
     251            output.yaxis("\\rm{Q}", output.Q_unit) 
    158252        else: 
    159             self.qmax = ymax 
    160          
    161         #config.printEVT("Read %g points from file %s" % (ncounts, self.file)) 
    162    
    163         self.wavelength = wavelength 
    164         self.distance   = distance*1000.0 
    165         self.center_x = center_x 
    166         self.center_y = center_y 
    167          
    168         from readInfo import ReaderInfo    
    169         output = ReaderInfo() 
    170         output.Z=Z 
    171         output.xmin    =xmin-xstep/2.0 
    172         output.xmax    = xmax+xstep/2.0 
    173         output.ymin    =ymin-ystep/2.0 
    174         output.ymax    =ymax+ystep/2.0 
    175         output.x          = x 
    176         output.y          = y 
    177         output.wavelength = wavelength 
    178         output.distance   = distance*1000.0 
    179         output.center_x = center_x 
    180         output.center_y = center_y 
    181         output.type    ="2D" 
    182          
    183         logging.info("IgorReader reading %s"%self.file) 
     253            output.xaxis("\\rm{Q}", 'A^{-1}') 
     254            output.yaxis("\\rm{Q}", 'A^{-1}') 
     255             
     256        if data_conv_i is not None: 
     257            output.zaxis("\\{I(Q)}", output.I_unit) 
     258        else: 
     259            output.zaxis("\\rm{I(Q)}","cm^{-1}") 
     260     
     261         
    184262        return output 
    185       
     263     
     264if __name__ == "__main__":  
     265    reader = Reader() 
     266    print reader.read("../test/MAR07232_rest.ASC")  
     267     
  • DataLoader/readers/abs_reader.py

    r99d1af6 rb99ac227  
    5353                detector = Detector() 
    5454                output.detector.append(detector) 
    55                 self.filename = output.filename = basename 
     55                output.filename = basename 
    5656                 
    5757                is_info = False 
     
    111111                        try: 
    112112                            value = float(line_toks[5]) 
    113                             if has_converter==True and output.sample.thickness_unit != 'mm': 
    114                                 conv = Converter('mm') 
     113                            if has_converter==True and output.sample.thickness_unit != 'cm': 
     114                                conv = Converter('cm') 
    115115                                output.sample.thickness = conv(value, units=output.sample.thickness_unit) 
    116116                            else: 
  • DataLoader/readers/danse_reader.py

    r99d1af6 rb99ac227  
    11""" 
    22    DANSE/SANS file reader 
     3""" 
     4 
     5""" 
     6This software was developed by the University of Tennessee as part of the 
     7Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
     8project funded by the US National Science Foundation.  
     9 
     10If you use DANSE applications to do scientific research that leads to  
     11publication, we ask that you acknowledge the use of the software with the  
     12following sentence: 
     13 
     14"This work benefited from DANSE software developed under NSF award DMR-0520547."  
     15 
     16copyright 2008, University of Tennessee 
    317""" 
    418 
     
    2438    type = ["DANSE files (*.sans)|*.sans"] 
    2539    ## Extension 
    26     ext  = ['.sans']     
     40    ext  = ['.sans', '.SANS']     
    2741         
    2842    def read(self, filename=None): 
     
    140154            xmax = None 
    141155             
    142             #x = numpy.zeros(size_x) 
    143             #y = numpy.zeros(size_y) 
    144             #X, Y = pylab.meshgrid(x, y) 
    145             #Z = copy.deepcopy(X) 
    146             #E = copy.deepcopy(X) 
    147             itot = 0 
    148             i_x = 0 
    149             i_y = 0 
    150              
    151156            # Qx and Qy vectors 
    152157            for i_x in range(size_x): 
     
    179184             
    180185            # Store the data in the 2D array 
     186            itot = 0 
     187            i_x  = 0 
     188            i_y  = -1 
     189             
    181190            for i_pt in range(len(data)): 
    182191                try: 
     
    188197                 
    189198                # Get bin number 
    190                 if math.fmod(itot, size_x)==0: 
     199                if math.fmod(i_pt, size_x)==0: 
    191200                    i_x = 0 
    192201                    i_y += 1 
     
    194203                    i_x += 1 
    195204                     
    196                 output.data[size_y-1-i_y][i_x] = value 
     205                output.data[i_y][i_x] = value        
     206                #output.data[size_y-1-i_y][i_x] = value 
    197207                if fversion>1.0: 
    198                     output.err_data[size_y-1-i_y][i_x] = error[i_pt] 
    199                  
    200                 itot += 1 
     208                    output.err_data[i_y][i_x] = error[i_pt] 
     209                    #output.err_data[size_y-1-i_y][i_x] = error[i_pt] 
     210                 
    201211                 
    202212            # Store all data ###################################### 
Note: See TracChangeset for help on using the changeset viewer.