source: sasview/guiframe/data_loader.py @ 6c0568b

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 6c0568b was 6d920cd, checked in by Gervaise Alina <gervyh@…>, 16 years ago

print statement removed

  • Property mode set to 100644
File size: 5.5 KB
Line 
1import os, sys,numpy
2import wx
3from dataFitting import Data1D, Theory1D
4from danse.common.plottools.plottables import Data2D
5
6from DataLoader.loader import Loader
7
8def choose_data_file(parent, location=None):
9    path = None
10    if location==None:
11        location = os.getcwd()
12   
13    l = Loader()
14    cards = l.get_wildcards()
15    wlist = '|'.join(cards)
16   
17    dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN)
18    if dlg.ShowModal() == wx.ID_OK:
19        path = dlg.GetPath()
20        mypath = os.path.basename(path)
21    dlg.Destroy()
22   
23    return path
24
25
26def load_ascii_1D(path):
27    """
28        Load a 1D ascii file, with errors
29    """
30    if path and os.path.isfile(path):
31   
32        file_x = numpy.zeros(0)
33        file_y = numpy.zeros(0)
34        file_dy = 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       
42        for line in lines:
43            try:
44                toks = line.split()
45                x = float(toks[0])
46                y = float(toks[1])
47                if len(toks)==3:
48                    has_dy = True
49                    err = float(toks[2])
50                else:
51                    err = 0.0
52                file_x  = numpy.append(file_x, x)
53                file_y  = numpy.append(file_y, y)
54                file_dy = numpy.append(file_dy, err)
55            except:
56                print "READ ERROR", line
57   
58        if has_dy==False:
59            file_dy = None
60           
61        return file_x, file_y, file_dy
62    return None, None, None
63
64def plot_data(parent, path):
65    """
66        Use the DataLoader loader to created data to plot.
67        @param path: the path of the data to load
68    """
69    from sans.guicomm.events import NewPlotEvent, StatusEvent
70    from DataLoader.loader import  Loader
71   
72    #Instantiate a loader
73    L = Loader()
74   
75    #Recieves data
76    try:
77        output=L.load(path)
78       
79    except:
80        wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value))
81        return
82    filename = os.path.basename(path)
83   
84    if not  output.__class__.__name__=="list":
85        try:
86            dxl=output.dxl
87            dxw=output.dxw
88        except:
89            dxl=None
90            dxw=None
91        if hasattr(output,'data'):
92            temp = output.err_data
93            temp[temp==0]=1
94
95            wx.PostEvent(parent, StatusEvent(status="Loading 2D error bars of value 1 \
96            were added to : %s"% output.filename))
97            new_plot = Data2D(image=output.data,err_image=temp,
98                              xmin=output.xmin,xmax=output.xmax,
99                              ymin=output.ymin,ymax=output.ymax)
100            new_plot.x_bins=output.x_bins
101            new_plot.y_bins=output.y_bins
102        else:
103           
104            if output.dy ==None :
105                new_plot = Theory1D(output.x,output.y, dxl, dxw)
106            elif len(output.dy[output.dy==0])==len(output.dy):
107                output.dy[output.dy==0]=1 
108                wx.PostEvent(parent, StatusEvent(status="Loading 1D error bars of value 1 \
109                    were added to : %s"%output.filename))
110                new_plot = Theory1D(output.x,output.y,output.dy, dxl, dxw)
111            else:
112                new_plot = Data1D(x=output.x,y=output.y,dx=output.dx,dy=output.dy, dxl=dxl, dxw=dxw)
113        if output.filename==None:
114            output.filename=str(filename)
115        new_plot.source=output.source
116        new_plot.name = output.filename
117        new_plot.interactive = True
118        new_plot.id = True
119        new_plot.info= output
120        if hasattr(output, "dxl"):
121            new_plot.dxl = output.dxl
122        if hasattr(output, "dxw"):
123            new_plot.dxw = output.dxw
124        new_plot.detector =output.detector
125       
126        # If the data file does not tell us what the axes are, just assume...
127        new_plot.xaxis(output._xaxis,output._xunit)
128        new_plot.yaxis(output._yaxis,output._yunit)
129        new_plot.group_id = output.filename
130        new_plot.id = output.filename
131        try:
132            title=output.filename
133        except:
134            title= filename
135       
136        wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title)))
137    else:
138        i=1
139        for item in output:
140           
141            try:
142                dxl=item.dxl
143                dxw=item.dxw
144            except:
145                dxl=None
146                dxw=None
147            if item.dy ==None:
148                new_plot = Theory1D(item.x,item.y,dxl,dxw)
149            else:
150                new_plot = Data1D(x=item.x,y=item.y,dx=item.dx,dy=item.dy,dxl=dxl,dxw=dxw)
151           
152            new_plot.source=item.source
153            new_plot.name = str(item.run[0])
154            new_plot.interactive = True
155            new_plot.detector =item.detector
156            # If the data file does not tell us what the axes are, just assume...
157            new_plot.xaxis(item._xaxis,item._xunit)
158            new_plot.yaxis(item._yaxis,item._yunit)
159            new_plot.group_id = str(item.run[0])
160            new_plot.id = str(item.run[0])
161            new_plot.info= item
162           
163            if hasattr(item,"title"):
164                title= item.title
165            else:
166                title= str(item.run[0])
167            wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title)))
168            i+=1
169           
170           
Note: See TracBrowser for help on using the repository browser.