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
Rev | Line | |
---|
[ee5479d8] | 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 | |
---|
| 17 | def read(self,path, format=None): |
---|
| 18 | """ Store the values loaded from file in local variables |
---|
| 19 | can read 3 columns of data |
---|
| 20 | """ |
---|
| 21 | if not path == None: |
---|
| 22 | input_f = open(path,'r') |
---|
| 23 | buff = input_f.read() |
---|
| 24 | lines = buff.split('\n') |
---|
| 25 | self.x=[] |
---|
| 26 | self.y=[] |
---|
| 27 | self.dx = [] |
---|
| 28 | self.dy=[] |
---|
| 29 | value="can't read" |
---|
| 30 | for line in lines: |
---|
| 31 | toks = line.split() |
---|
| 32 | try: |
---|
| 33 | x = float(toks[0]) |
---|
| 34 | y = float(toks[1]) |
---|
| 35 | dy = float(toks[2]) |
---|
| 36 | |
---|
| 37 | self.x.append(x) |
---|
| 38 | self.y.append(y) |
---|
| 39 | self.dy.append(dy) |
---|
| 40 | |
---|
| 41 | except: |
---|
| 42 | print "READ ERROR", line |
---|
| 43 | |
---|
| 44 | self.dx = numpy.zeros(len(self.x)) |
---|
| 45 | # Sanity check |
---|
| 46 | if not len(self.x) == len(self.dx): |
---|
| 47 | raise ValueError, "x and dx have different length" |
---|
| 48 | if not len(self.y) == len(self.dy): |
---|
| 49 | raise ValueError, "y and dy have different length" |
---|
| 50 | |
---|
| 51 | if (self.x==[] or self.y==[])and (self.dy==[]): |
---|
| 52 | raise ValueError, "txtReader can't read" |
---|
| 53 | else: |
---|
| 54 | #msg="txtReader Reading:\n"+"this x :"+ str(self.x) +"\n"+"this y:"+str(self.y)+"\n"+"this dy :"+str(self.dy)+"\n" |
---|
| 55 | #return msg |
---|
| 56 | print "TXT3_Reader reading: \n" |
---|
| 57 | return self.x,self.y,self.dy |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | if __name__ == "__main__": |
---|
| 61 | read= Reader() |
---|
| 62 | #read= Reader(filename="testdata_line.txt") |
---|
| 63 | print read.load("test.dat") |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
Note: See
TracBrowser
for help on using the repository browser.