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 93f0a586 was
8d6440f,
checked in by Gervaise Alina <gervyh@…>, 16 years ago
|
readers printing in a log file text_log.txt now.testload modified
|
-
Property mode set to
100644
|
File size:
2.6 KB
|
Line | |
---|
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 | should be able to read only 2 columns of data |
---|
8 | """ |
---|
9 | ext=['.txt','.dat'] |
---|
10 | def _init_(self,x=None,y=None,dx=None,dy=None): |
---|
11 | # variable to store loaded values |
---|
12 | self.x = x |
---|
13 | self.y = y |
---|
14 | self.dx = dx |
---|
15 | self.dy = dy |
---|
16 | |
---|
17 | def read(self,path, format=None): |
---|
18 | """ Store the values loaded from file in local variables """ |
---|
19 | if not path == None: |
---|
20 | read_it = False |
---|
21 | |
---|
22 | for item in self.ext: |
---|
23 | if path.lower().find(item)>=0: |
---|
24 | read_it = True |
---|
25 | #print "this is the flag",read_it, path.lower() |
---|
26 | if read_it==False: |
---|
27 | return None |
---|
28 | else: |
---|
29 | try: |
---|
30 | input_f = open(path,'r') |
---|
31 | except : |
---|
32 | raise RuntimeError,"TXT2_Reader cannot open %s"%(path) |
---|
33 | buff = input_f.read() |
---|
34 | lines = buff.split('\n') |
---|
35 | self.x=[] |
---|
36 | self.y=[] |
---|
37 | self.dx = [] |
---|
38 | self.dy=[] |
---|
39 | for line in lines: |
---|
40 | toks = line.split() |
---|
41 | try: |
---|
42 | x = float(toks[0]) |
---|
43 | self.x.append(x) |
---|
44 | except: |
---|
45 | pass |
---|
46 | #print "READ ERROR", line |
---|
47 | try: |
---|
48 | y = float(toks[1]) |
---|
49 | self.y.append(y) |
---|
50 | except: |
---|
51 | pass |
---|
52 | #print "READ ERROR", line |
---|
53 | try: |
---|
54 | dy=float(toks[2]) |
---|
55 | self.dy.append(dy) |
---|
56 | |
---|
57 | except: |
---|
58 | pass |
---|
59 | #print "READ ERROR", line |
---|
60 | |
---|
61 | if(( self.x==[])or (self.y==[])) or (self.dy !=[]): |
---|
62 | #print "went here" |
---|
63 | #return value |
---|
64 | raise ValueError, "TXT2_Reader can't read %s"%path |
---|
65 | else: |
---|
66 | #msg="R1 reading: \n"+"this x :"+ str(self.x) +"\n"+"this y:"+str(self.y)+"\n"+"this dy :"+str(self.dy)+"\n" |
---|
67 | #return msg |
---|
68 | logging.info("TXT2_Reader reading %s\n" %path) |
---|
69 | return self.x,self.y,self.dy |
---|
70 | return None |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | if __name__ == "__main__": |
---|
75 | read= Reader() |
---|
76 | read.load("testdata_line.txt") |
---|
77 | |
---|
78 | |
---|
Note: See
TracBrowser
for help on using the repository browser.