1 | import os, sys,numpy |
---|
2 | import wx |
---|
3 | from dataFitting import Data1D |
---|
4 | from dataFitting import Data2D |
---|
5 | from DataLoader.loader import Loader |
---|
6 | |
---|
7 | def choose_data_file(parent, location=None): |
---|
8 | path = None |
---|
9 | if location==None: |
---|
10 | location = os.getcwd() |
---|
11 | |
---|
12 | l = Loader() |
---|
13 | cards = l.get_wildcards() |
---|
14 | wlist = '|'.join(cards) |
---|
15 | |
---|
16 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) |
---|
17 | if dlg.ShowModal() == wx.ID_OK: |
---|
18 | path = dlg.GetPath() |
---|
19 | mypath = os.path.basename(path) |
---|
20 | dlg.Destroy() |
---|
21 | |
---|
22 | return path |
---|
23 | |
---|
24 | |
---|
25 | def load_ascii_1D(path): |
---|
26 | """ |
---|
27 | Load a 1D ascii file, with errors |
---|
28 | """ |
---|
29 | if path and os.path.isfile(path): |
---|
30 | |
---|
31 | file_x = numpy.zeros(0) |
---|
32 | file_y = numpy.zeros(0) |
---|
33 | file_dy = numpy.zeros(0) |
---|
34 | file_dx = numpy.zeros(0) |
---|
35 | |
---|
36 | input_f = open(path,'r') |
---|
37 | buff = input_f.read() |
---|
38 | lines = buff.split('\n') |
---|
39 | |
---|
40 | has_dy = False |
---|
41 | has_dx = False |
---|
42 | |
---|
43 | for line in lines: |
---|
44 | try: |
---|
45 | toks = line.split() |
---|
46 | x = float(toks[0]) |
---|
47 | y = float(toks[1]) |
---|
48 | if len(toks)==3: |
---|
49 | has_dy = True |
---|
50 | errdy = float(toks[2]) |
---|
51 | else: |
---|
52 | errdy = 0.0 |
---|
53 | if len(toks)==4: |
---|
54 | has_dx = True |
---|
55 | errdx = float(toks[3]) |
---|
56 | else: |
---|
57 | errdx = 0.0 |
---|
58 | file_x = numpy.append(file_x, x) |
---|
59 | file_y = numpy.append(file_y, y) |
---|
60 | file_dy = numpy.append(file_dy, dyerr) |
---|
61 | file_dx = numpy.append(file_dx, dxerr) |
---|
62 | except: |
---|
63 | print "READ ERROR", line |
---|
64 | |
---|
65 | if has_dy==False: |
---|
66 | file_dy = None |
---|
67 | if has_dx==False: |
---|
68 | file_dx = None |
---|
69 | |
---|
70 | return file_x, file_y, file_dy, file_dx |
---|
71 | return None, None, None, None |
---|
72 | |
---|
73 | def plot_data(parent, path): |
---|
74 | """ |
---|
75 | Use the DataLoader loader to created data to plot. |
---|
76 | @param path: the path of the data to load |
---|
77 | """ |
---|
78 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
79 | from DataLoader.loader import Loader |
---|
80 | |
---|
81 | #Instantiate a loader |
---|
82 | L = Loader() |
---|
83 | |
---|
84 | #Recieves data |
---|
85 | try: |
---|
86 | output=L.load(path) |
---|
87 | if output==None: |
---|
88 | msg="Could not open this page" |
---|
89 | wx.PostEvent(parent, StatusEvent(status=msg)) |
---|
90 | return |
---|
91 | except: |
---|
92 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
93 | return |
---|
94 | |
---|
95 | |
---|
96 | filename = os.path.basename(path) |
---|
97 | |
---|
98 | if not output.__class__.__name__=="list": |
---|
99 | ## Creating a Data2D with output |
---|
100 | if hasattr(output,'data'): |
---|
101 | new_plot = Data2D(image=None,err_image=None) |
---|
102 | |
---|
103 | else: |
---|
104 | msg="Loading 1D data: " |
---|
105 | wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename))) |
---|
106 | new_plot = Data1D(x=[], y=[], dx=None,dy= None) |
---|
107 | |
---|
108 | new_plot.copy_from_datainfo(output) |
---|
109 | output.clone_without_data(clone=new_plot) |
---|
110 | |
---|
111 | ## data 's name |
---|
112 | if output.filename==None or output.filename=="": |
---|
113 | output.filename = str(filename) |
---|
114 | ## name of the data allow to differentiate data when plotted |
---|
115 | name= output.filename |
---|
116 | if not name in parent.indice_load_data.keys(): |
---|
117 | parent.indice_load_data[name]=0 |
---|
118 | else: |
---|
119 | ## create a copy of the loaded data |
---|
120 | parent.indice_load_data[name]+=1 |
---|
121 | name = name +"[%i]"%parent.indice_load_data[name] |
---|
122 | |
---|
123 | new_plot.name = name |
---|
124 | ## allow to highlight data when plotted |
---|
125 | new_plot.interactive = True |
---|
126 | ## when 2 data have the same id override the 1 st plotted |
---|
127 | new_plot.id = name |
---|
128 | ## info is a reference to output of dataloader that can be used |
---|
129 | ## to save data 1D as cansas xml file |
---|
130 | new_plot.info= output |
---|
131 | ##group_id specify on which panel to plot this data |
---|
132 | new_plot.group_id = name |
---|
133 | new_plot.is_data =True |
---|
134 | ##post data to plot |
---|
135 | if hasattr(new_plot,"title"): |
---|
136 | title= str(new_plot.title) |
---|
137 | if title =="": |
---|
138 | title=str(name) |
---|
139 | else: |
---|
140 | title = str(name) |
---|
141 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title= title )) |
---|
142 | |
---|
143 | ## the output of the loader is a list , some xml files contain more than one data |
---|
144 | else: |
---|
145 | i=1 |
---|
146 | for item in output: |
---|
147 | try: |
---|
148 | dx=item.dx |
---|
149 | dxl=item.dxl |
---|
150 | dxw=item.dxw |
---|
151 | except: |
---|
152 | dx=None |
---|
153 | dxl=None |
---|
154 | dxw=None |
---|
155 | |
---|
156 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy) |
---|
157 | new_plot.copy_from_datainfo(item) |
---|
158 | item.clone_without_data(clone=new_plot) |
---|
159 | new_plot.dxl = dxl |
---|
160 | new_plot.dxw = dxw |
---|
161 | |
---|
162 | name= str(item.run[0]) |
---|
163 | if not name in parent.indice_load_data.keys(): |
---|
164 | parent.indice_load_data[name]=0 |
---|
165 | else: |
---|
166 | ## create a copy of the loaded data |
---|
167 | |
---|
168 | #TODO: this is a very annoying feature. We should make this |
---|
169 | # an option. Excel doesn't do this. Why should we? |
---|
170 | # What is the requirement for this feature, and are the |
---|
171 | # counter arguments stronger? Is this feature developed |
---|
172 | # to please at least 80% of the users or a special few? |
---|
173 | parent.indice_load_data[name]+=1 |
---|
174 | name = name +"(copy %i)"%parent.indice_load_data[name] |
---|
175 | |
---|
176 | new_plot.name = name |
---|
177 | new_plot.interactive = True |
---|
178 | |
---|
179 | new_plot.group_id = name |
---|
180 | new_plot.id = name |
---|
181 | new_plot.info= item |
---|
182 | new_plot.is_data =True |
---|
183 | if hasattr(item,"title"): |
---|
184 | title= item.title |
---|
185 | if title=="": |
---|
186 | title=str(name) |
---|
187 | else: |
---|
188 | title= name |
---|
189 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
190 | i+=1 |
---|
191 | |
---|
192 | |
---|