source: sasview/DataLoader/readers/TXT2_Reader.py @ 83ca047

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 83ca047 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: 2.8 KB
Line 
1# class Loader  to load any king of file
2import wx
3import string,numpy,logging
4class Reader:
5    """
6        This class is loading values from given file or value giving by the user
7        should be able to read only 2 columns of data
8    """
9    ext=['.txt','.dat'] 
10    def _init_(self,x=None,y=None,dx=None,dy=None):
11        # variable to store loaded values
12        self.x = x
13        self.y = y
14        self.dx = dx
15        self.dy = dy
16       
17    def read(self,path, format=None):
18        """ Store the values loaded from file in local variables """
19        if not path == None:
20            read_it = False
21             
22            for item in self.ext:
23                if path.lower().find(item)>=0:
24                    read_it = True
25            #print "this is the flag",read_it, path.lower()
26            if read_it==False:
27                return None
28            else:
29                try:
30                    input_f =  open(path,'r')
31                except :
32                    raise  RuntimeError,"TXT2_Reader cannot open %s"%(path)
33                buff = input_f.read()
34                lines = buff.split('\n')
35                self.x=[]
36                self.y=[]
37                self.dx = [] 
38                self.dy=[]
39                for line in lines:
40                    toks = line.split()
41                    try:
42                        x = float(toks[0])
43                        self.x.append(x)
44                    except:
45                        pass
46                        #print "READ ERROR", line
47                    try:
48                        y = float(toks[1])
49                        self.y.append(y)
50                    except:
51                        pass
52                        #print "READ ERROR", line
53                    try:
54                        dy=float(toks[2])
55                        self.dy.append(dy)
56   
57                    except:
58                        pass
59                        #print "READ ERROR", line
60   
61                if(( self.x==[])or (self.y==[])) or (self.dy !=[]):
62                    #print "went here"
63                    #return value
64                    raise ValueError, "TXT2_Reader can't read %s"%path
65                else:
66                    #msg="R1 reading: \n"+"this x :"+ str(self.x) +"\n"+"this y:"+str(self.y)+"\n"+"this dy :"+str(self.dy)+"\n"
67                    #return msg
68                    from readInfo import ReaderInfo   
69                    output = ReaderInfo()
70                    output.x=self.x
71                    output.y=self.y
72                   
73                    logging.info("TXT2_Reader reading  %s\n" %path)
74                    return output
75        return None
76   
77       
78   
79if __name__ == "__main__": 
80    read= Reader()
81    read.load("testdata_line.txt")
82   
83           
Note: See TracBrowser for help on using the repository browser.