1 | import os, sys,numpy |
---|
2 | import wx |
---|
3 | from dataFitting import Data1D, Theory1D |
---|
4 | from danse.common.plottools.plottables 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 | |
---|
88 | except: |
---|
89 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
90 | return |
---|
91 | if output==None: |
---|
92 | msg="Could not open this page" |
---|
93 | wx.PostEvent(parent, StatusEvent(status=msg)) |
---|
94 | return |
---|
95 | filename = os.path.basename(path) |
---|
96 | |
---|
97 | if not output.__class__.__name__=="list": |
---|
98 | ## smearing info |
---|
99 | try: |
---|
100 | dxl = output.dxl |
---|
101 | dxw = output.dxw |
---|
102 | except: |
---|
103 | dxl = None |
---|
104 | dxw = None |
---|
105 | |
---|
106 | |
---|
107 | ## Creating a Data2D with output |
---|
108 | if hasattr(output,'data'): |
---|
109 | |
---|
110 | new_plot = Data2D(image=output.data,err_image=output.err_data, |
---|
111 | xmin=output.xmin,xmax=output.xmax, |
---|
112 | ymin=output.ymin,ymax=output.ymax) |
---|
113 | new_plot.x_bins=output.x_bins |
---|
114 | new_plot.y_bins=output.y_bins |
---|
115 | |
---|
116 | ##Creating Data1D with output |
---|
117 | else: |
---|
118 | ##dy values checked |
---|
119 | dy= output.dy |
---|
120 | if dy ==None: |
---|
121 | dy= numpy.zeros(len(output.y)) |
---|
122 | |
---|
123 | msg="Loading 1D data: " |
---|
124 | wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename))) |
---|
125 | new_plot = Data1D(x=output.x, y=output.y, dx=output.dx, |
---|
126 | dy= dy, dxl=dxl, dxw=dxw) |
---|
127 | |
---|
128 | ## source will request in dataLoader .manipulation module |
---|
129 | new_plot.source=output.source |
---|
130 | ## data 's name |
---|
131 | if output.filename==None: |
---|
132 | output.filename=str(filename) |
---|
133 | ## name of the data allow to differentiate data when plotted |
---|
134 | name= output.filename |
---|
135 | if not name in parent.indice_load_data.keys(): |
---|
136 | parent.indice_load_data[name]=0 |
---|
137 | else: |
---|
138 | ## create a copy of the loaded data |
---|
139 | parent.indice_load_data[name]+=1 |
---|
140 | name = name +"(copy %i)"%parent.indice_load_data[name] |
---|
141 | |
---|
142 | new_plot.name = name |
---|
143 | ## allow to highlight data when plotted |
---|
144 | new_plot.interactive = True |
---|
145 | ## when 2 data have the same id override the 1 st plotted |
---|
146 | new_plot.id = name |
---|
147 | ## info is a reference to output of dataloader that can be used |
---|
148 | ## to save data 1D as cansas xml file |
---|
149 | new_plot.info= output |
---|
150 | ## detector used by dataLoader.manipulation module |
---|
151 | new_plot.detector =output.detector |
---|
152 | ## If the data file does not tell us what the axes are, just assume... |
---|
153 | new_plot.xaxis(output._xaxis,output._xunit) |
---|
154 | new_plot.yaxis(output._yaxis,output._yunit) |
---|
155 | ##group_id specify on which panel to plot this data |
---|
156 | new_plot.group_id = name |
---|
157 | new_plot.is_data =True |
---|
158 | ##post data to plot |
---|
159 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(name))) |
---|
160 | |
---|
161 | ## the output of the loader is a list , some xml files contain more than one data |
---|
162 | else: |
---|
163 | i=1 |
---|
164 | for item in output: |
---|
165 | try: |
---|
166 | dx=item.dx |
---|
167 | dxl=item.dxl |
---|
168 | dxw=item.dxw |
---|
169 | except: |
---|
170 | dx=None |
---|
171 | dxl=None |
---|
172 | dxw=None |
---|
173 | dy = item.dy |
---|
174 | if dy ==None: |
---|
175 | dy= numpy.zeros(len(item.y)) |
---|
176 | |
---|
177 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy,dxl=dxl,dxw=dxw) |
---|
178 | new_plot.source=item.source |
---|
179 | |
---|
180 | name= str(item.run[0]) |
---|
181 | if not name in parent.indice_load_data.keys(): |
---|
182 | parent.indice_load_data[name]=0 |
---|
183 | else: |
---|
184 | ## create a copy of the loaded data |
---|
185 | parent.indice_load_data[name]+=1 |
---|
186 | name = name +"(copy %i)"%parent.indice_load_data[name] |
---|
187 | |
---|
188 | new_plot.name = name |
---|
189 | new_plot.interactive = True |
---|
190 | new_plot.detector =item.detector |
---|
191 | # If the data file does not tell us what the axes are, just assume... |
---|
192 | new_plot.xaxis(item._xaxis,item._xunit) |
---|
193 | new_plot.yaxis(item._yaxis,item._yunit) |
---|
194 | new_plot.group_id = name |
---|
195 | new_plot.id = name |
---|
196 | new_plot.info= item |
---|
197 | new_plot.is_data =True |
---|
198 | if hasattr(item,"title"): |
---|
199 | title= item.title |
---|
200 | else: |
---|
201 | title= name |
---|
202 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
203 | i+=1 |
---|
204 | |
---|
205 | |
---|