1 | import os, sys |
---|
2 | import wx |
---|
3 | from danse.common.plottools.plottables import Data1D, Theory1D, Data2D |
---|
4 | from DataLoader.loader import Loader |
---|
5 | |
---|
6 | def choose_data_file(parent, location=None): |
---|
7 | path = None |
---|
8 | if location==None: |
---|
9 | location = os.getcwd() |
---|
10 | |
---|
11 | l = Loader() |
---|
12 | cards = l.get_wildcards() |
---|
13 | wlist = '|'.join(cards) |
---|
14 | |
---|
15 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) |
---|
16 | if dlg.ShowModal() == wx.ID_OK: |
---|
17 | path = dlg.GetPath() |
---|
18 | mypath = os.path.basename(path) |
---|
19 | dlg.Destroy() |
---|
20 | |
---|
21 | return path |
---|
22 | |
---|
23 | |
---|
24 | def load_ascii_1D(path): |
---|
25 | """ |
---|
26 | Load a 1D ascii file, with errors |
---|
27 | """ |
---|
28 | import numpy |
---|
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 | |
---|
35 | input_f = open(path,'r') |
---|
36 | buff = input_f.read() |
---|
37 | lines = buff.split('\n') |
---|
38 | |
---|
39 | has_dy = False |
---|
40 | |
---|
41 | for line in lines: |
---|
42 | try: |
---|
43 | toks = line.split() |
---|
44 | x = float(toks[0]) |
---|
45 | y = float(toks[1]) |
---|
46 | if len(toks)==3: |
---|
47 | has_dy = True |
---|
48 | err = float(toks[2]) |
---|
49 | else: |
---|
50 | err = 0.0 |
---|
51 | file_x = numpy.append(file_x, x) |
---|
52 | file_y = numpy.append(file_y, y) |
---|
53 | file_dy = numpy.append(file_dy, err) |
---|
54 | except: |
---|
55 | print "READ ERROR", line |
---|
56 | |
---|
57 | if has_dy==False: |
---|
58 | file_dy = None |
---|
59 | |
---|
60 | return file_x, file_y, file_dy |
---|
61 | return None, None, None |
---|
62 | |
---|
63 | def plot_data(parent, path, name="Loaded Data"): |
---|
64 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
65 | |
---|
66 | from DataLoader.loader import Loader |
---|
67 | import numpy |
---|
68 | #Instantiate a loader |
---|
69 | L=Loader() |
---|
70 | |
---|
71 | #Recieves data |
---|
72 | try: |
---|
73 | output=L.load(path) |
---|
74 | except: |
---|
75 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
76 | return |
---|
77 | if hasattr(output,'data'): |
---|
78 | new_plot = Data2D(image=output.data,err_image=output.err_data, |
---|
79 | xmin=output.xmin,xmax=output.xmax, |
---|
80 | ymin=output.ymin,ymax=output.ymax) |
---|
81 | new_plot.x_bins=output.x_bins |
---|
82 | new_plot.y_bins=output.y_bins |
---|
83 | #print "data_loader",output |
---|
84 | else: |
---|
85 | if output.dy==None: |
---|
86 | new_plot = Theory1D(output.x,output.y) |
---|
87 | else: |
---|
88 | new_plot = Data1D(x=output.x,y=output.y,dy=output.dy) |
---|
89 | |
---|
90 | filename = os.path.basename(path) |
---|
91 | new_plot.source=output |
---|
92 | new_plot.name = filename |
---|
93 | new_plot.interactive = True |
---|
94 | # If the data file does not tell us what the axes are, just assume... |
---|
95 | new_plot.xaxis(output._xaxis,output._xunit) |
---|
96 | new_plot.yaxis(output._yaxis,output._yunit) |
---|
97 | #new_plot.xaxis("\\rm{Q}",'A^{-1}') |
---|
98 | #new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
99 | |
---|
100 | new_plot.group_id = filename |
---|
101 | |
---|
102 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename)) |
---|