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

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 6f73a08 was fb8d4050, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Allow multiple data sets to be loaded

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[ccb560a]1import os, sys
[de9483d]2import wx
[c7bc3e7]3from DataLoader.loader import Loader
[de9483d]4
5def choose_data_file(parent, location=None):
6    path = None
7    if location==None:
8        location = os.getcwd()
[c7bc3e7]9   
10    l = Loader()
11    cards = l.get_wildcards()
12    wlist = '|'.join(cards)
13   
14    dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN)
[de9483d]15    if dlg.ShowModal() == wx.ID_OK:
16        path = dlg.GetPath()
17        mypath = os.path.basename(path)
18    dlg.Destroy()
19   
20    return path
21
22
23def load_ascii_1D(path):
24    """
25        Load a 1D ascii file, with errors
26    """
[fc2b91a]27    import numpy
[de9483d]28    if path and os.path.isfile(path):
29   
[fc2b91a]30        file_x = numpy.zeros(0)
31        file_y = numpy.zeros(0)
32        file_dy = numpy.zeros(0)
[de9483d]33       
34        input_f = open(path,'r')
35        buff = input_f.read()
36        lines = buff.split('\n')
[fc2b91a]37       
38        has_dy = False
39       
[de9483d]40        for line in lines:
41            try:
42                toks = line.split()
43                x = float(toks[0])
44                y = float(toks[1])
45                if len(toks)==3:
[fc2b91a]46                    has_dy = True
[de9483d]47                    err = float(toks[2])
48                else:
49                    err = 0.0
[fc2b91a]50                file_x  = numpy.append(file_x, x)
51                file_y  = numpy.append(file_y, y)
52                file_dy = numpy.append(file_dy, err)
[de9483d]53            except:
54                print "READ ERROR", line
55   
[fc2b91a]56        if has_dy==False:
57            file_dy = None
58           
[de9483d]59        return file_x, file_y, file_dy
[fc2b91a]60    return None, None, None
61
62def plot_data(parent, path, name="Loaded Data"):
[ccb560a]63    from sans.guicomm.events import NewPlotEvent, StatusEvent
[4102709]64    from DataLoader.loader import  Loader
65    #Instantiate a loader
66    L=Loader()
67   
68    #Recieves data
[ccb560a]69    try:
70        output=L.load(path)
71    except:
72        wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value))
73        return
74       
[fb8d4050]75    if type(output).__name__=='list':
76        for i in range(len(output)):
77            _plot_single_data(parent, output[i], path, name, str(i))
78    else:
79        _plot_single_data(parent, output, path, name)
80       
81def _plot_single_data(parent, output, path, name="Loaded Data", index=None):
82    """
83        Plot a single data set
84        @param output: data set to plot
85    """
86    from sans.guicomm.events import NewPlotEvent
87    from sans.guitools.plottables import Data1D, Theory1D
88   
[49b3ddd]89    if output.dy==None:
90        new_plot = Theory1D(output.x, output.y)
[fc2b91a]91    else:
[49b3ddd]92        new_plot = Data1D(output.x, output.y, dy=output.dy)
[acb1ad1]93       
94    filename = os.path.basename(path)
[fb8d4050]95    if index is not None:
96        filename += "(%s)" % index
[acb1ad1]97       
[fc2b91a]98    new_plot.name = name
99    new_plot.interactive = True
100   
101    # If the data file does not tell us what the axes are, just assume...
102    new_plot.xaxis("\\rm{Q}", 'A^{-1}')
103    new_plot.yaxis("\\rm{Intensity} ","cm^{-1}")
[acb1ad1]104    new_plot.group_id = filename
[fc2b91a]105       
[fb8d4050]106    title = filename
107    if len(output.title) > 0:
108        title = filename+'['+output.title+']'
109
110    wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=title))
Note: See TracBrowser for help on using the repository browser.