[ccb560a] | 1 | import os, sys |
---|
[de9483d] | 2 | import wx |
---|
[81812d9] | 3 | from dataFitting import Data1D, Theory1D |
---|
| 4 | from danse.common.plottools.plottables import Data2D |
---|
| 5 | |
---|
[c7bc3e7] | 6 | from DataLoader.loader import Loader |
---|
[58eac5d] | 7 | |
---|
[de9483d] | 8 | def choose_data_file(parent, location=None): |
---|
| 9 | path = None |
---|
| 10 | if location==None: |
---|
| 11 | location = os.getcwd() |
---|
[c7bc3e7] | 12 | |
---|
| 13 | l = Loader() |
---|
| 14 | cards = l.get_wildcards() |
---|
| 15 | wlist = '|'.join(cards) |
---|
| 16 | |
---|
| 17 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) |
---|
[de9483d] | 18 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 19 | path = dlg.GetPath() |
---|
| 20 | mypath = os.path.basename(path) |
---|
| 21 | dlg.Destroy() |
---|
| 22 | |
---|
| 23 | return path |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | def load_ascii_1D(path): |
---|
| 27 | """ |
---|
| 28 | Load a 1D ascii file, with errors |
---|
| 29 | """ |
---|
[fc2b91a] | 30 | import numpy |
---|
[de9483d] | 31 | if path and os.path.isfile(path): |
---|
| 32 | |
---|
[fc2b91a] | 33 | file_x = numpy.zeros(0) |
---|
| 34 | file_y = numpy.zeros(0) |
---|
| 35 | file_dy = numpy.zeros(0) |
---|
[de9483d] | 36 | |
---|
| 37 | input_f = open(path,'r') |
---|
| 38 | buff = input_f.read() |
---|
| 39 | lines = buff.split('\n') |
---|
[fc2b91a] | 40 | |
---|
| 41 | has_dy = False |
---|
| 42 | |
---|
[de9483d] | 43 | for line in lines: |
---|
| 44 | try: |
---|
| 45 | toks = line.split() |
---|
| 46 | x = float(toks[0]) |
---|
| 47 | y = float(toks[1]) |
---|
| 48 | if len(toks)==3: |
---|
[fc2b91a] | 49 | has_dy = True |
---|
[de9483d] | 50 | err = float(toks[2]) |
---|
| 51 | else: |
---|
| 52 | err = 0.0 |
---|
[fc2b91a] | 53 | file_x = numpy.append(file_x, x) |
---|
| 54 | file_y = numpy.append(file_y, y) |
---|
| 55 | file_dy = numpy.append(file_dy, err) |
---|
[de9483d] | 56 | except: |
---|
| 57 | print "READ ERROR", line |
---|
| 58 | |
---|
[fc2b91a] | 59 | if has_dy==False: |
---|
| 60 | file_dy = None |
---|
| 61 | |
---|
[de9483d] | 62 | return file_x, file_y, file_dy |
---|
[fc2b91a] | 63 | return None, None, None |
---|
| 64 | |
---|
| 65 | def plot_data(parent, path, name="Loaded Data"): |
---|
[ccb560a] | 66 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
[7764b41] | 67 | |
---|
[4102709] | 68 | from DataLoader.loader import Loader |
---|
[1c9419f4] | 69 | import numpy |
---|
[4102709] | 70 | #Instantiate a loader |
---|
[81812d9] | 71 | L = Loader() |
---|
[4102709] | 72 | |
---|
| 73 | #Recieves data |
---|
[ccb560a] | 74 | try: |
---|
| 75 | output=L.load(path) |
---|
[81812d9] | 76 | |
---|
[ccb560a] | 77 | except: |
---|
| 78 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
| 79 | return |
---|
[acb1ad1] | 80 | filename = os.path.basename(path) |
---|
[cd84dca] | 81 | |
---|
[81812d9] | 82 | if not output.__class__.__name__=="list": |
---|
| 83 | try: |
---|
| 84 | dxl=output.dxl |
---|
| 85 | dxw=output.dxw |
---|
| 86 | except: |
---|
| 87 | dxl=None |
---|
| 88 | dxw=None |
---|
| 89 | if hasattr(output,'data'): |
---|
| 90 | new_plot = Data2D(image=output.data,err_image=output.err_data, |
---|
| 91 | xmin=output.xmin,xmax=output.xmax, |
---|
| 92 | ymin=output.ymin,ymax=output.ymax) |
---|
| 93 | new_plot.x_bins=output.x_bins |
---|
| 94 | new_plot.y_bins=output.y_bins |
---|
| 95 | #print "data_loader",output |
---|
| 96 | else: |
---|
[e8c96f5] | 97 | #print "output.dx, output.dy",output.dx, output.dy |
---|
[7a7bf55] | 98 | if output.dy ==None: |
---|
[e8c96f5] | 99 | #print "went here theory1D" |
---|
[81812d9] | 100 | new_plot = Theory1D(output.x,output.y, dxl, dxw) |
---|
| 101 | else: |
---|
| 102 | new_plot = Data1D(x=output.x,y=output.y,dy=output.dy, dxl=dxl, dxw=dxw) |
---|
| 103 | #print "dataloader",output[0],output[1] |
---|
| 104 | |
---|
| 105 | new_plot.source=output.source |
---|
[cd84dca] | 106 | new_plot.name = output.filename |
---|
[81812d9] | 107 | new_plot.interactive = True |
---|
[169460a] | 108 | new_plot.id = True |
---|
[81812d9] | 109 | new_plot.info= output |
---|
| 110 | if hasattr(output, "dxl"): |
---|
| 111 | new_plot.dxl = output.dxl |
---|
| 112 | if hasattr(output, "dxw"): |
---|
| 113 | new_plot.dxw = output.dxw |
---|
| 114 | #print "loader output.detector",output.source |
---|
| 115 | new_plot.detector =output.detector |
---|
| 116 | |
---|
| 117 | # If the data file does not tell us what the axes are, just assume... |
---|
| 118 | new_plot.xaxis(output._xaxis,output._xunit) |
---|
| 119 | new_plot.yaxis(output._yaxis,output._yunit) |
---|
[ab8f936] | 120 | new_plot.group_id = output.filename |
---|
| 121 | new_plot.id = output.filename |
---|
[81812d9] | 122 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename)) |
---|
| 123 | else: |
---|
[768656e] | 124 | i=1 |
---|
[81812d9] | 125 | for item in output: |
---|
[cd84dca] | 126 | |
---|
[81812d9] | 127 | try: |
---|
| 128 | dxl=item.dxl |
---|
| 129 | dxw=item.dxw |
---|
| 130 | except: |
---|
| 131 | dxl=None |
---|
| 132 | dxw=None |
---|
[7a7bf55] | 133 | if item.dy ==None: |
---|
[81812d9] | 134 | new_plot = Theory1D(item.x,item.y,dxl,dxw) |
---|
| 135 | else: |
---|
| 136 | new_plot = Data1D(x=item.x,y=item.y,dy=item.dy,dxl=dxl,dxw=dxw) |
---|
| 137 | |
---|
| 138 | new_plot.source=item.source |
---|
| 139 | new_plot.info=output |
---|
[cd84dca] | 140 | new_plot.name = str(item.run[0]) |
---|
[81812d9] | 141 | new_plot.interactive = True |
---|
[169460a] | 142 | |
---|
[81812d9] | 143 | #print "loader output.detector",output.source |
---|
| 144 | new_plot.detector =item.detector |
---|
| 145 | # If the data file does not tell us what the axes are, just assume... |
---|
| 146 | new_plot.xaxis(item._xaxis,item._xunit) |
---|
| 147 | new_plot.yaxis(item._yaxis,item._yunit) |
---|
[ab8f936] | 148 | new_plot.group_id = str(item.run[0]) |
---|
| 149 | new_plot.id = str(item.run[0]) |
---|
| 150 | |
---|
[81812d9] | 151 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename)) |
---|
[768656e] | 152 | i+=1 |
---|
[81812d9] | 153 | |
---|
| 154 | |
---|