[ccb560a] | 1 | import os, sys |
---|
[de9483d] | 2 | import wx |
---|
[7764b41] | 3 | from danse.common.plottools.plottables import Data1D, Theory1D, Data2D |
---|
[c7bc3e7] | 4 | from DataLoader.loader import Loader |
---|
[58eac5d] | 5 | |
---|
[de9483d] | 6 | def choose_data_file(parent, location=None): |
---|
| 7 | path = None |
---|
| 8 | if location==None: |
---|
| 9 | location = os.getcwd() |
---|
[c7bc3e7] | 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) |
---|
[de9483d] | 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 | """ |
---|
[fc2b91a] | 28 | import numpy |
---|
[de9483d] | 29 | if path and os.path.isfile(path): |
---|
| 30 | |
---|
[fc2b91a] | 31 | file_x = numpy.zeros(0) |
---|
| 32 | file_y = numpy.zeros(0) |
---|
| 33 | file_dy = numpy.zeros(0) |
---|
[de9483d] | 34 | |
---|
| 35 | input_f = open(path,'r') |
---|
| 36 | buff = input_f.read() |
---|
| 37 | lines = buff.split('\n') |
---|
[fc2b91a] | 38 | |
---|
| 39 | has_dy = False |
---|
| 40 | |
---|
[de9483d] | 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: |
---|
[fc2b91a] | 47 | has_dy = True |
---|
[de9483d] | 48 | err = float(toks[2]) |
---|
| 49 | else: |
---|
| 50 | err = 0.0 |
---|
[fc2b91a] | 51 | file_x = numpy.append(file_x, x) |
---|
| 52 | file_y = numpy.append(file_y, y) |
---|
| 53 | file_dy = numpy.append(file_dy, err) |
---|
[de9483d] | 54 | except: |
---|
| 55 | print "READ ERROR", line |
---|
| 56 | |
---|
[fc2b91a] | 57 | if has_dy==False: |
---|
| 58 | file_dy = None |
---|
| 59 | |
---|
[de9483d] | 60 | return file_x, file_y, file_dy |
---|
[fc2b91a] | 61 | return None, None, None |
---|
| 62 | |
---|
| 63 | def plot_data(parent, path, name="Loaded Data"): |
---|
[ccb560a] | 64 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
[7764b41] | 65 | |
---|
[4102709] | 66 | from DataLoader.loader import Loader |
---|
[1c9419f4] | 67 | import numpy |
---|
[4102709] | 68 | #Instantiate a loader |
---|
| 69 | L=Loader() |
---|
| 70 | |
---|
| 71 | #Recieves data |
---|
[ccb560a] | 72 | try: |
---|
| 73 | output=L.load(path) |
---|
| 74 | except: |
---|
| 75 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
| 76 | return |
---|
[7764b41] | 77 | if hasattr(output,'data'): |
---|
[48c7477] | 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) |
---|
[49815a2] | 81 | new_plot.x_bins=output.x_bins |
---|
| 82 | new_plot.y_bins=output.y_bins |
---|
[978967c] | 83 | #print "data_loader",output |
---|
[fc2b91a] | 84 | else: |
---|
[1c9419f4] | 85 | if output.dy==None: |
---|
[48c7477] | 86 | new_plot = Theory1D(output.x,output.y) |
---|
[1c9419f4] | 87 | else: |
---|
[48c7477] | 88 | new_plot = Data1D(x=output.x,y=output.y,dy=output.dy) |
---|
[978967c] | 89 | |
---|
[acb1ad1] | 90 | filename = os.path.basename(path) |
---|
[ce64735] | 91 | new_plot.source=output.source |
---|
[978967c] | 92 | new_plot.name = filename |
---|
[fc2b91a] | 93 | new_plot.interactive = True |
---|
[ce64735] | 94 | #print "loader output.detector",output.source |
---|
[743d75c] | 95 | new_plot.detector =output.detector |
---|
[fc2b91a] | 96 | # If the data file does not tell us what the axes are, just assume... |
---|
[978967c] | 97 | new_plot.xaxis(output._xaxis,output._xunit) |
---|
| 98 | new_plot.yaxis(output._yaxis,output._yunit) |
---|
| 99 | #new_plot.xaxis("\\rm{Q}",'A^{-1}') |
---|
| 100 | #new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 101 | |
---|
[acb1ad1] | 102 | new_plot.group_id = filename |
---|
[7764b41] | 103 | |
---|
[1c9419f4] | 104 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename)) |
---|