[6d920cd] | 1 | import os, sys,numpy |
---|
[de9483d] | 2 | import wx |
---|
[ff3f900b] | 3 | from dataFitting import Data1D |
---|
| 4 | from dataFitting import Data2D |
---|
[c7bc3e7] | 5 | from DataLoader.loader import Loader |
---|
[58eac5d] | 6 | |
---|
[de9483d] | 7 | def choose_data_file(parent, location=None): |
---|
| 8 | path = None |
---|
| 9 | if location==None: |
---|
| 10 | location = os.getcwd() |
---|
[c7bc3e7] | 11 | |
---|
| 12 | l = Loader() |
---|
| 13 | cards = l.get_wildcards() |
---|
| 14 | wlist = '|'.join(cards) |
---|
| 15 | |
---|
| 16 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) |
---|
[de9483d] | 17 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 18 | path = dlg.GetPath() |
---|
| 19 | mypath = os.path.basename(path) |
---|
| 20 | dlg.Destroy() |
---|
| 21 | |
---|
| 22 | return path |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | def load_ascii_1D(path): |
---|
| 26 | """ |
---|
| 27 | Load a 1D ascii file, with errors |
---|
| 28 | """ |
---|
| 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) |
---|
[bc3dd65d] | 34 | file_dx = numpy.zeros(0) |
---|
[de9483d] | 35 | |
---|
| 36 | input_f = open(path,'r') |
---|
| 37 | buff = input_f.read() |
---|
| 38 | lines = buff.split('\n') |
---|
[fc2b91a] | 39 | |
---|
| 40 | has_dy = False |
---|
[bc3dd65d] | 41 | has_dx = False |
---|
[fc2b91a] | 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 |
---|
[bc3dd65d] | 50 | errdy = float(toks[2]) |
---|
[de9483d] | 51 | else: |
---|
[bc3dd65d] | 52 | errdy = 0.0 |
---|
| 53 | if len(toks)==4: |
---|
| 54 | has_dx = True |
---|
| 55 | errdx = float(toks[3]) |
---|
| 56 | else: |
---|
| 57 | errdx = 0.0 |
---|
[fc2b91a] | 58 | file_x = numpy.append(file_x, x) |
---|
| 59 | file_y = numpy.append(file_y, y) |
---|
[bc3dd65d] | 60 | file_dy = numpy.append(file_dy, dyerr) |
---|
| 61 | file_dx = numpy.append(file_dx, dxerr) |
---|
[de9483d] | 62 | except: |
---|
| 63 | print "READ ERROR", line |
---|
| 64 | |
---|
[fc2b91a] | 65 | if has_dy==False: |
---|
| 66 | file_dy = None |
---|
[bc3dd65d] | 67 | if has_dx==False: |
---|
| 68 | file_dx = None |
---|
[fc2b91a] | 69 | |
---|
[bc3dd65d] | 70 | return file_x, file_y, file_dy, file_dx |
---|
[e5bfd57] | 71 | return None, None, None, None |
---|
[fc2b91a] | 72 | |
---|
[6d920cd] | 73 | def plot_data(parent, path): |
---|
| 74 | """ |
---|
| 75 | Use the DataLoader loader to created data to plot. |
---|
| 76 | @param path: the path of the data to load |
---|
| 77 | """ |
---|
[ccb560a] | 78 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
[4102709] | 79 | from DataLoader.loader import Loader |
---|
[f2776f6] | 80 | |
---|
[4102709] | 81 | #Instantiate a loader |
---|
[81812d9] | 82 | L = Loader() |
---|
[4102709] | 83 | |
---|
| 84 | #Recieves data |
---|
[ccb560a] | 85 | try: |
---|
| 86 | output=L.load(path) |
---|
[ff3f900b] | 87 | if output==None: |
---|
| 88 | msg="Could not open this page" |
---|
| 89 | wx.PostEvent(parent, StatusEvent(status=msg)) |
---|
| 90 | return |
---|
[ccb560a] | 91 | except: |
---|
| 92 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
| 93 | return |
---|
[ff3f900b] | 94 | |
---|
| 95 | |
---|
[acb1ad1] | 96 | filename = os.path.basename(path) |
---|
[cd84dca] | 97 | |
---|
[81812d9] | 98 | if not output.__class__.__name__=="list": |
---|
[12aa9b5] | 99 | ## Creating a Data2D with output |
---|
[81812d9] | 100 | if hasattr(output,'data'): |
---|
[ff3f900b] | 101 | new_plot = Data2D(image=None,err_image=None) |
---|
| 102 | |
---|
[81812d9] | 103 | else: |
---|
[7c427a6] | 104 | msg="Loading 1D data: " |
---|
| 105 | wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename))) |
---|
[ff3f900b] | 106 | new_plot = Data1D(x=[], y=[], dx=None,dy= None) |
---|
| 107 | |
---|
| 108 | new_plot.copy_from_datainfo(output) |
---|
| 109 | output.clone_without_data(clone=new_plot) |
---|
| 110 | |
---|
[25ccf33] | 111 | ## data 's name |
---|
[ff3f900b] | 112 | if output.filename==None or output.filename=="": |
---|
| 113 | output.filename = str(filename) |
---|
[12aa9b5] | 114 | ## name of the data allow to differentiate data when plotted |
---|
[35eeea8] | 115 | name= output.filename |
---|
[25ccf33] | 116 | if not name in parent.indice_load_data.keys(): |
---|
| 117 | parent.indice_load_data[name]=0 |
---|
| 118 | else: |
---|
| 119 | ## create a copy of the loaded data |
---|
| 120 | parent.indice_load_data[name]+=1 |
---|
[ff3f900b] | 121 | name = name +"[%i]"%parent.indice_load_data[name] |
---|
| 122 | |
---|
[35eeea8] | 123 | new_plot.name = name |
---|
[12aa9b5] | 124 | ## allow to highlight data when plotted |
---|
[81812d9] | 125 | new_plot.interactive = True |
---|
[12aa9b5] | 126 | ## when 2 data have the same id override the 1 st plotted |
---|
[35eeea8] | 127 | new_plot.id = name |
---|
[12aa9b5] | 128 | ## info is a reference to output of dataloader that can be used |
---|
| 129 | ## to save data 1D as cansas xml file |
---|
[81812d9] | 130 | new_plot.info= output |
---|
[12aa9b5] | 131 | ##group_id specify on which panel to plot this data |
---|
[35eeea8] | 132 | new_plot.group_id = name |
---|
[5ee2306] | 133 | new_plot.is_data =True |
---|
[12aa9b5] | 134 | ##post data to plot |
---|
[ff3f900b] | 135 | if hasattr(new_plot,"title"): |
---|
| 136 | title= str(new_plot.title) |
---|
[d4ccbb1] | 137 | if title =="": |
---|
| 138 | title=str(name) |
---|
[ff3f900b] | 139 | else: |
---|
| 140 | title = str(name) |
---|
| 141 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title= title )) |
---|
[8bd764d] | 142 | |
---|
[12aa9b5] | 143 | ## the output of the loader is a list , some xml files contain more than one data |
---|
[81812d9] | 144 | else: |
---|
[768656e] | 145 | i=1 |
---|
[81812d9] | 146 | for item in output: |
---|
| 147 | try: |
---|
[bc3dd65d] | 148 | dx=item.dx |
---|
[81812d9] | 149 | dxl=item.dxl |
---|
| 150 | dxw=item.dxw |
---|
| 151 | except: |
---|
[bc3dd65d] | 152 | dx=None |
---|
[81812d9] | 153 | dxl=None |
---|
| 154 | dxw=None |
---|
[ff3f900b] | 155 | |
---|
| 156 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy) |
---|
| 157 | new_plot.copy_from_datainfo(item) |
---|
[1abcb04] | 158 | item.clone_without_data(clone=new_plot) |
---|
[ff3f900b] | 159 | new_plot.dxl = dxl |
---|
| 160 | new_plot.dxw = dxw |
---|
| 161 | |
---|
[35eeea8] | 162 | name= str(item.run[0]) |
---|
[25ccf33] | 163 | if not name in parent.indice_load_data.keys(): |
---|
| 164 | parent.indice_load_data[name]=0 |
---|
| 165 | else: |
---|
| 166 | ## create a copy of the loaded data |
---|
[675e0ab] | 167 | |
---|
| 168 | #TODO: this is a very annoying feature. We should make this |
---|
| 169 | # an option. Excel doesn't do this. Why should we? |
---|
| 170 | # What is the requirement for this feature, and are the |
---|
| 171 | # counter arguments stronger? Is this feature developed |
---|
| 172 | # to please at least 80% of the users or a special few? |
---|
[25ccf33] | 173 | parent.indice_load_data[name]+=1 |
---|
| 174 | name = name +"(copy %i)"%parent.indice_load_data[name] |
---|
| 175 | |
---|
[35eeea8] | 176 | new_plot.name = name |
---|
[81812d9] | 177 | new_plot.interactive = True |
---|
[ff3f900b] | 178 | |
---|
[35eeea8] | 179 | new_plot.group_id = name |
---|
| 180 | new_plot.id = name |
---|
[6d920cd] | 181 | new_plot.info= item |
---|
[5ee2306] | 182 | new_plot.is_data =True |
---|
[18eba35] | 183 | if hasattr(item,"title"): |
---|
| 184 | title= item.title |
---|
[d4ccbb1] | 185 | if title=="": |
---|
| 186 | title=str(name) |
---|
[18eba35] | 187 | else: |
---|
[35eeea8] | 188 | title= name |
---|
[18eba35] | 189 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
[768656e] | 190 | i+=1 |
---|
[81812d9] | 191 | |
---|
| 192 | |
---|