1 | # class Loader to load any king of file |
---|
2 | import wx |
---|
3 | import string,numpy |
---|
4 | import logging |
---|
5 | class 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 |
---|
75 | if __name__ == "__main__": |
---|
76 | read= Reader() |
---|
77 | #read= Reader(filename="testdata_line.txt") |
---|
78 | print read.load("test.dat") |
---|
79 | |
---|
80 | |
---|
81 | |
---|