source: sasview/DataLoader/plugins/TXT3_Reader.py @ 8176aad

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 8176aad was 16d8e5f, checked in by Gervaise Alina <gervyh@…>, 16 years ago

ReadInfo? module added in readers….every reader should return a read info object

  • Property mode set to 100644
File size: 3.8 KB
Line 
1# class Loader  to load any king of file
2import wx
3import string,numpy
4import logging
5class ReaderInfo:
6    """
7        This class is a container of data. It stores values read from files
8    """
9    ## Wavelength [A]
10    wavelength = 0.0
11    ## Number of x bins
12    xbins = 128
13    ## Number of y bins
14    ybins = 128
15    ## Beam center X [pixel number]
16    center_x = 65
17    ## Beam center Y [pixel number]
18    center_y = 65
19    ## Distance from sample to detector [m]
20    distance = 11.0
21    ## Qx values [A-1]
22    x_vals = []
23    ## Qy xalues [A-1]
24    y_vals = []
25    ## Qx min
26    xmin = 0.0
27    ## Qx max
28    xmax = 1.0
29    ## Qy min
30    ymin = 0.0
31    ## Qy max
32    ymax = 1.0
33    ## Image
34    image = None
35    ## Pixel size
36    pixel_size = 0.5
37    ## x coordinate
38    x=[]
39    ##y coordinate
40    y=[]
41    ##dx error on y
42    dx=None
43    ## dy error on y
44    dy=None
45    ## Error on each pixel
46    error = None
47    ##type of data
48    type=""
49    ##z
50    Z=None
51class Reader:
52    """
53        This class is loading values from given file or value giving by the user
54    """
55    ext=['.txt','.dat'] 
56    def _init_(self,filename=None,x=None,y=None,dx=None,dy=None):
57        # variable to store loaded values
58        self.x = x
59        self.y = y
60        self.dx = dx
61        self.dy = dy
62        self.filename = filename
63       
64       
65    def read(self,path, format=None):
66        """ Store the values loaded from file in local variables
67            can read 3 columns of data
68        """
69       
70        if not path == None:
71            read_it = False
72           
73            for item in self.ext:
74                if path.lower().find(item)>=0:
75                    read_it = True
76            #print "this is the flag",read_it, path.lower()
77            if read_it==False:
78                return None
79            else:
80                try:
81                    input_f =  open(path,'r')
82                except :
83                    raise  RuntimeError,"TXT3_Reader cannot open %s"%(path)
84                buff = input_f.read()
85                lines = buff.split('\n')
86                self.x=[]
87                self.y=[]
88                self.dx = [] 
89                self.dy=[]
90                for line in lines:
91                    toks = line.split()
92                    try: 
93                        x = float(toks[0])
94                        y = float(toks[1]) 
95                        dy = float(toks[2])
96                       
97                        self.x.append(x)
98                        self.y.append(y)
99                        self.dy.append(dy)
100                   
101                    except:
102                        pass
103                        #print "READ ERROR", line
104           
105                    self.dx = numpy.zeros(len(self.x))
106                    # Sanity check
107                    if not len(self.x) == len(self.dx):
108                        raise ValueError, "x and dx have different length"
109                    if not len(self.y) == len(self.dy):
110                        raise ValueError, "y and dy have different length"
111               
112                if (self.x==[] or self.y==[])and (self.dy==[]):
113                    raise ValueError, "TXT3_Reader can't read %s"%path
114                else:
115                   
116                    logging.info ("TXT3_Reader reading %s \n" %(path))
117                     
118                    output = ReaderInfo()
119                    output.x    = self.x
120                    output.y    = self.y
121                    output.dy   = self.dy
122                    output.type = "1D"
123                   
124                    return output
125               
126        return None
127if __name__ == "__main__": 
128    read= Reader()
129    #read= Reader(filename="testdata_line.txt")
130    print read.load("test.dat")
131   
132   
133           
Note: See TracBrowser for help on using the repository browser.