source: sasview/guiframe/data_loader.py @ cd84dca

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

change on xml loading title

  • Property mode set to 100644
File size: 4.8 KB
Line 
1import os, sys
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    import numpy
31    if path and os.path.isfile(path):
32   
33        file_x = numpy.zeros(0)
34        file_y = numpy.zeros(0)
35        file_dy = numpy.zeros(0)
36       
37        input_f = open(path,'r')
38        buff = input_f.read()
39        lines = buff.split('\n')
40       
41        has_dy = 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                    err = float(toks[2])
51                else:
52                    err = 0.0
53                file_x  = numpy.append(file_x, x)
54                file_y  = numpy.append(file_y, y)
55                file_dy = numpy.append(file_dy, err)
56            except:
57                print "READ ERROR", line
58   
59        if has_dy==False:
60            file_dy = None
61           
62        return file_x, file_y, file_dy
63    return None, None, None
64
65def plot_data(parent, path, name="Loaded Data"):
66    from sans.guicomm.events import NewPlotEvent, StatusEvent
67   
68    from DataLoader.loader import  Loader
69    import numpy
70    #Instantiate a loader
71    L = Loader()
72   
73    #Recieves data
74    try:
75        output=L.load(path)
76       
77    except:
78        wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value))
79        return
80    filename = os.path.basename(path)
81   
82    if not  output.__class__.__name__=="list":
83        try:
84            dxl=output.dxl
85            dxw=output.dxw
86        except:
87            dxl=None
88            dxw=None
89        if hasattr(output,'data'):
90            new_plot = Data2D(image=output.data,err_image=output.err_data,
91                              xmin=output.xmin,xmax=output.xmax,
92                              ymin=output.ymin,ymax=output.ymax)
93            new_plot.x_bins=output.x_bins
94            new_plot.y_bins=output.y_bins
95            #print "data_loader",output
96        else:
97            #print "output.dx, output.dy",output.dx, output.dy
98            if output.dy ==None:
99                #print "went here theory1D"
100                new_plot = Theory1D(output.x,output.y, dxl, dxw)
101            else:
102                new_plot = Data1D(x=output.x,y=output.y,dy=output.dy, dxl=dxl, dxw=dxw)
103        #print "dataloader",output[0],output[1]
104       
105        new_plot.source=output.source
106        new_plot.name = output.filename
107        new_plot.interactive = True
108        new_plot.info= output
109        if hasattr(output, "dxl"):
110            new_plot.dxl = output.dxl
111        if hasattr(output, "dxw"):
112            new_plot.dxw = output.dxw
113        #print "loader output.detector",output.source
114        new_plot.detector =output.detector
115       
116        # If the data file does not tell us what the axes are, just assume...
117        new_plot.xaxis(output._xaxis,output._xunit)
118        new_plot.yaxis(output._yaxis,output._yunit)
119        new_plot.group_id = filename
120        wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename))
121    else:
122        i=1
123        for item in output:
124           
125            try:
126                dxl=item.dxl
127                dxw=item.dxw
128            except:
129                dxl=None
130                dxw=None
131            if item.dy ==None:
132                new_plot = Theory1D(item.x,item.y,dxl,dxw)
133            else:
134                new_plot = Data1D(x=item.x,y=item.y,dy=item.dy,dxl=dxl,dxw=dxw)
135           
136            new_plot.source=item.source
137            new_plot.info=output
138            new_plot.name = str(item.run[0])
139            new_plot.interactive = True
140           
141            #print "loader output.detector",output.source
142            new_plot.detector =item.detector
143            # If the data file does not tell us what the axes are, just assume...
144            new_plot.xaxis(item._xaxis,item._xunit)
145            new_plot.yaxis(item._yaxis,item._yunit)
146            new_plot.group_id = filename
147            wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename))
148            i+=1
149           
150           
Note: See TracBrowser for help on using the repository browser.