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