[de9483d] | 1 | import os |
---|
| 2 | import wx |
---|
| 3 | |
---|
| 4 | def choose_data_file(parent, location=None): |
---|
| 5 | path = None |
---|
| 6 | if location==None: |
---|
| 7 | location = os.getcwd() |
---|
| 8 | dlg = wx.FileDialog(parent, "Choose a file", location, "", "*.txt", wx.OPEN) |
---|
| 9 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 10 | path = dlg.GetPath() |
---|
| 11 | mypath = os.path.basename(path) |
---|
| 12 | dlg.Destroy() |
---|
| 13 | |
---|
| 14 | return path |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | def load_ascii_1D(path): |
---|
| 18 | """ |
---|
| 19 | Load a 1D ascii file, with errors |
---|
| 20 | """ |
---|
[fc2b91a] | 21 | import numpy |
---|
[de9483d] | 22 | if path and os.path.isfile(path): |
---|
| 23 | |
---|
[fc2b91a] | 24 | file_x = numpy.zeros(0) |
---|
| 25 | file_y = numpy.zeros(0) |
---|
| 26 | file_dy = numpy.zeros(0) |
---|
[de9483d] | 27 | |
---|
| 28 | input_f = open(path,'r') |
---|
| 29 | buff = input_f.read() |
---|
| 30 | lines = buff.split('\n') |
---|
[fc2b91a] | 31 | |
---|
| 32 | has_dy = False |
---|
| 33 | |
---|
[de9483d] | 34 | for line in lines: |
---|
| 35 | try: |
---|
| 36 | toks = line.split() |
---|
| 37 | x = float(toks[0]) |
---|
| 38 | y = float(toks[1]) |
---|
| 39 | if len(toks)==3: |
---|
[fc2b91a] | 40 | has_dy = True |
---|
[de9483d] | 41 | err = float(toks[2]) |
---|
| 42 | else: |
---|
| 43 | err = 0.0 |
---|
[fc2b91a] | 44 | file_x = numpy.append(file_x, x) |
---|
| 45 | file_y = numpy.append(file_y, y) |
---|
| 46 | file_dy = numpy.append(file_dy, err) |
---|
[de9483d] | 47 | except: |
---|
| 48 | print "READ ERROR", line |
---|
| 49 | |
---|
[fc2b91a] | 50 | if has_dy==False: |
---|
| 51 | file_dy = None |
---|
| 52 | |
---|
[de9483d] | 53 | return file_x, file_y, file_dy |
---|
[fc2b91a] | 54 | return None, None, None |
---|
| 55 | |
---|
| 56 | def plot_data(parent, path, name="Loaded Data"): |
---|
| 57 | from sans.guicomm.events import NewPlotEvent |
---|
| 58 | from sans.guitools.plottables import Data1D, Theory1D |
---|
| 59 | x, y, dy = load_ascii_1D(path) |
---|
| 60 | |
---|
| 61 | if dy==None: |
---|
| 62 | new_plot = Theory1D(x, y) |
---|
| 63 | else: |
---|
| 64 | new_plot = Data1D(x, y, dy=dy) |
---|
| 65 | new_plot.name = name |
---|
| 66 | new_plot.interactive = True |
---|
| 67 | |
---|
| 68 | # If the data file does not tell us what the axes are, just assume... |
---|
| 69 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 70 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 71 | |
---|
| 72 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title="Loaded data")) |
---|