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 357b79b was
c125e0c,
checked in by Gervaise Alina <gervyh@…>, 16 years ago
|
unit test added
|
-
Property mode set to
100644
|
File size:
2.7 KB
|
Rev | Line | |
---|
[11a0319] | 1 | # class Loader to load any king of file |
---|
| 2 | import wx |
---|
| 3 | import string,numpy |
---|
| 4 | class Reader: |
---|
| 5 | """ |
---|
| 6 | This class is loading values from given file or value giving by the user |
---|
| 7 | """ |
---|
| 8 | |
---|
| 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 |
---|
| 25 | ext=['.txt','.dat'] |
---|
| 26 | for item in ext: |
---|
| 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: |
---|
[bb03739] | 33 | input_f = open(path,'r') |
---|
| 34 | buff = input_f.read() |
---|
| 35 | lines = buff.split('\n') |
---|
| 36 | self.x=[] |
---|
| 37 | self.y=[] |
---|
| 38 | self.dx = [] |
---|
| 39 | self.dy=[] |
---|
| 40 | for line in lines: |
---|
| 41 | toks = line.split() |
---|
| 42 | try: |
---|
| 43 | x = float(toks[0]) |
---|
| 44 | y = float(toks[1]) |
---|
| 45 | dy = float(toks[2]) |
---|
| 46 | |
---|
| 47 | self.x.append(x) |
---|
| 48 | self.y.append(y) |
---|
| 49 | self.dy.append(dy) |
---|
| 50 | |
---|
| 51 | except: |
---|
[aa749ac] | 52 | pass |
---|
| 53 | #print "READ ERROR", line |
---|
[bb03739] | 54 | |
---|
| 55 | self.dx = numpy.zeros(len(self.x)) |
---|
| 56 | # Sanity check |
---|
| 57 | if not len(self.x) == len(self.dx): |
---|
| 58 | raise ValueError, "x and dx have different length" |
---|
| 59 | if not len(self.y) == len(self.dy): |
---|
| 60 | raise ValueError, "y and dy have different length" |
---|
| 61 | |
---|
| 62 | if (self.x==[] or self.y==[])and (self.dy==[]): |
---|
[c125e0c] | 63 | raise ValueError, "TXT3_Reader can't read %s"%path |
---|
[bb03739] | 64 | else: |
---|
| 65 | #msg="txtReader Reading:\n"+"this x :"+ str(self.x) +"\n"+"this y:"+str(self.y)+"\n"+"this dy :"+str(self.dy)+"\n" |
---|
| 66 | #return msg |
---|
[c125e0c] | 67 | print "TXT3_Reader reading %s \n" %path |
---|
[bb03739] | 68 | return self.x,self.y,self.dy |
---|
[11a0319] | 69 | |
---|
[aa749ac] | 70 | return None |
---|
[11a0319] | 71 | if __name__ == "__main__": |
---|
| 72 | read= Reader() |
---|
| 73 | #read= Reader(filename="testdata_line.txt") |
---|
| 74 | print read.load("test.dat") |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | |
---|
Note: See
TracBrowser
for help on using the repository browser.