[11a0319] | 1 | # class Loader to load any king of file |
---|
| 2 | import wx |
---|
[8d6440f] | 3 | import string,numpy,logging |
---|
[11a0319] | 4 | class Reader: |
---|
| 5 | """ |
---|
| 6 | This class is loading values from given file or value giving by the user |
---|
| 7 | """ |
---|
[1b0b3ca] | 8 | ext=['.txt','.dat'] |
---|
[11a0319] | 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 | |
---|
[bb03739] | 17 | |
---|
[11a0319] | 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 | |
---|
[bb03739] | 23 | if not path == None: |
---|
| 24 | read_it = False |
---|
[1b0b3ca] | 25 | |
---|
| 26 | for item in self.ext: |
---|
[bb03739] | 27 | if path.lower().find(item)>=0: |
---|
| 28 | read_it = True |
---|
[aa749ac] | 29 | #print "this is the flag",read_it, path.lower() |
---|
[bb03739] | 30 | if read_it==False: |
---|
[aa749ac] | 31 | return None |
---|
[11a0319] | 32 | else: |
---|
[8d6440f] | 33 | try: |
---|
| 34 | input_f = open(path,'r') |
---|
| 35 | except : |
---|
| 36 | raise RuntimeError,"TXT3_Reader cannot open %s"%(path) |
---|
[bb03739] | 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: |
---|
[aa749ac] | 55 | pass |
---|
| 56 | #print "READ ERROR", line |
---|
[bb03739] | 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==[]): |
---|
[c125e0c] | 66 | raise ValueError, "TXT3_Reader can't read %s"%path |
---|
[bb03739] | 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 |
---|
[8d6440f] | 70 | logging.info("TXT3_Reader reading %s \n" %path) |
---|
[bb03739] | 71 | return self.x,self.y,self.dy |
---|
[11a0319] | 72 | |
---|
[aa749ac] | 73 | return None |
---|
[11a0319] | 74 | if __name__ == "__main__": |
---|
| 75 | read= Reader() |
---|
| 76 | #read= Reader(filename="testdata_line.txt") |
---|
| 77 | print read.load("test.dat") |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|