[4a0536a] | 1 | # class Loader to load any king of file |
---|
| 2 | import wx |
---|
| 3 | import string,numpy |
---|
| 4 | class Load: |
---|
| 5 | """ |
---|
| 6 | This class is loading values from given file or value giving by the user |
---|
| 7 | """ |
---|
| 8 | |
---|
| 9 | def _init_(self,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=None |
---|
| 16 | |
---|
| 17 | def set_filename(self,path=None): |
---|
| 18 | """ |
---|
| 19 | Store path into a variable.If the user doesn't give a path as a parameter a pop-up |
---|
| 20 | window appears to select the file. |
---|
| 21 | @param path: the path given by the user |
---|
| 22 | """ |
---|
| 23 | if path == None: |
---|
| 24 | dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt", wx.OPEN) |
---|
| 25 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 26 | path = dlg.GetPath() |
---|
| 27 | dlg.Destroy() |
---|
| 28 | self.filename = path |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | def get_filename(self): |
---|
| 32 | """ return the file's path""" |
---|
| 33 | return self.filename |
---|
| 34 | def set_values(self): |
---|
| 35 | """ Store the values loaded from file in local variables """ |
---|
| 36 | if not self.filename == None: |
---|
| 37 | input_f = open(self.filename,'r') |
---|
| 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 | try: |
---|
| 46 | toks = line.split() |
---|
| 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 | self.dx = numpy.zeros(len(self.x)) |
---|
| 55 | except: |
---|
| 56 | print "READ ERROR", line |
---|
| 57 | |
---|
| 58 | |
---|
| 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 | |
---|
| 66 | |
---|
| 67 | def get_values(self): |
---|
| 68 | """ Return x, y, dx, dy """ |
---|
| 69 | return self.x,self.y,self.dx,self.dy |
---|
| 70 | |
---|
| 71 | def load_data(self,data): |
---|
| 72 | """ Return plottable """ |
---|
| 73 | #load data |
---|
| 74 | data.x = self.x |
---|
| 75 | data.y = self.y |
---|
| 76 | data.dx = self.dx |
---|
| 77 | data.dy =self.dy |
---|
| 78 | #Load its View class |
---|
| 79 | #plottable.reset_view() |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | if __name__ == "__main__": |
---|
| 83 | load= Load() |
---|
| 84 | load.set_filename("testdata_line.txt") |
---|
| 85 | print load.get_filename() |
---|
| 86 | load.set_values() |
---|
| 87 | print load.get_values() |
---|
| 88 | |
---|
| 89 | |
---|