1 | import os, sys |
---|
2 | import wx |
---|
3 | from DataLoader.loader import Loader |
---|
4 | |
---|
5 | def choose_data_file(parent, location=None): |
---|
6 | path = None |
---|
7 | if location==None: |
---|
8 | location = os.getcwd() |
---|
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) |
---|
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 | |
---|
23 | def load_ascii_1D(path): |
---|
24 | """ |
---|
25 | Load a 1D ascii file, with errors |
---|
26 | """ |
---|
27 | import numpy |
---|
28 | if path and os.path.isfile(path): |
---|
29 | |
---|
30 | file_x = numpy.zeros(0) |
---|
31 | file_y = numpy.zeros(0) |
---|
32 | file_dy = numpy.zeros(0) |
---|
33 | |
---|
34 | input_f = open(path,'r') |
---|
35 | buff = input_f.read() |
---|
36 | lines = buff.split('\n') |
---|
37 | |
---|
38 | has_dy = False |
---|
39 | |
---|
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: |
---|
46 | has_dy = True |
---|
47 | err = float(toks[2]) |
---|
48 | else: |
---|
49 | err = 0.0 |
---|
50 | file_x = numpy.append(file_x, x) |
---|
51 | file_y = numpy.append(file_y, y) |
---|
52 | file_dy = numpy.append(file_dy, err) |
---|
53 | except: |
---|
54 | print "READ ERROR", line |
---|
55 | |
---|
56 | if has_dy==False: |
---|
57 | file_dy = None |
---|
58 | |
---|
59 | return file_x, file_y, file_dy |
---|
60 | return None, None, None |
---|
61 | |
---|
62 | def plot_data(parent, path, name="Loaded Data"): |
---|
63 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
64 | #from sans.guitools.plottables import Data1D, Theory1D |
---|
65 | from danse.common.plottools.plottables import Data1D, Theory1D, Data2D |
---|
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(data): |
---|
78 | new_plot = Data2D(image= output.data) |
---|
79 | new_plot.setValues(output) |
---|
80 | else: |
---|
81 | if output.dy==None: |
---|
82 | new_plot = Theory1D(output.x, output.y) |
---|
83 | else: |
---|
84 | new_plot = Data1D(output.x, output.y, dy=output.dy) |
---|
85 | |
---|
86 | filename = os.path.basename(path) |
---|
87 | |
---|
88 | new_plot.name = name |
---|
89 | new_plot.interactive = True |
---|
90 | |
---|
91 | # If the data file does not tell us what the axes are, just assume... |
---|
92 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
93 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
94 | new_plot.group_id = filename |
---|
95 | |
---|
96 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename)) |
---|