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