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