source: sasview/DataLoader/plugins/TXT3_Reader.py @ 93f0a586

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 93f0a586 was 8d6440f, checked in by Gervaise Alina <gervyh@…>, 16 years ago

readers printing in a log file text_log.txt now.testload modified

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