[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 |
---|
[7764b41] | 5 | class MetaData1D(Data1D): |
---|
| 6 | def __init__(self, output): |
---|
| 7 | self.datainfo = output |
---|
| 8 | Data1D.__init__(self,x=output.x, y=output.y, dy=output.dy) |
---|
| 9 | |
---|
| 10 | class MetaTheory1D(Theory1D): |
---|
| 11 | def __init__(self, output): |
---|
| 12 | self.datainfo = output |
---|
| 13 | Theory1D.__init__(self, x=output.x, y=output.y) |
---|
| 14 | |
---|
[de9483d] | 15 | |
---|
[7764b41] | 16 | class MetaData2D(Data2D): |
---|
| 17 | def __init__(self, output): |
---|
| 18 | self.datainfo = output |
---|
| 19 | Data2D.__init__(self,image= output.data,err_image=output.err_data, |
---|
| 20 | xmin=output.xmin,xmax=output.xmax,ymin=output.ymin,ymax=output.ymax) |
---|
| 21 | class MetaTheory2D(Data2D): |
---|
| 22 | def __init__(self, output): |
---|
| 23 | self.datainfo = output |
---|
| 24 | Data2D.__init__(self,image= output.data,err_image=output.err_data, |
---|
| 25 | xmin=output.xmin,xmax=output.xmax,ymin=output.ymin,ymax=output.ymax) |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | |
---|
[de9483d] | 29 | def choose_data_file(parent, location=None): |
---|
| 30 | path = None |
---|
| 31 | if location==None: |
---|
| 32 | location = os.getcwd() |
---|
[c7bc3e7] | 33 | |
---|
| 34 | l = Loader() |
---|
| 35 | cards = l.get_wildcards() |
---|
| 36 | wlist = '|'.join(cards) |
---|
| 37 | |
---|
| 38 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) |
---|
[de9483d] | 39 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 40 | path = dlg.GetPath() |
---|
| 41 | mypath = os.path.basename(path) |
---|
| 42 | dlg.Destroy() |
---|
| 43 | |
---|
| 44 | return path |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | def load_ascii_1D(path): |
---|
| 48 | """ |
---|
| 49 | Load a 1D ascii file, with errors |
---|
| 50 | """ |
---|
[fc2b91a] | 51 | import numpy |
---|
[de9483d] | 52 | if path and os.path.isfile(path): |
---|
| 53 | |
---|
[fc2b91a] | 54 | file_x = numpy.zeros(0) |
---|
| 55 | file_y = numpy.zeros(0) |
---|
| 56 | file_dy = numpy.zeros(0) |
---|
[de9483d] | 57 | |
---|
| 58 | input_f = open(path,'r') |
---|
| 59 | buff = input_f.read() |
---|
| 60 | lines = buff.split('\n') |
---|
[fc2b91a] | 61 | |
---|
| 62 | has_dy = False |
---|
| 63 | |
---|
[de9483d] | 64 | for line in lines: |
---|
| 65 | try: |
---|
| 66 | toks = line.split() |
---|
| 67 | x = float(toks[0]) |
---|
| 68 | y = float(toks[1]) |
---|
| 69 | if len(toks)==3: |
---|
[fc2b91a] | 70 | has_dy = True |
---|
[de9483d] | 71 | err = float(toks[2]) |
---|
| 72 | else: |
---|
| 73 | err = 0.0 |
---|
[fc2b91a] | 74 | file_x = numpy.append(file_x, x) |
---|
| 75 | file_y = numpy.append(file_y, y) |
---|
| 76 | file_dy = numpy.append(file_dy, err) |
---|
[de9483d] | 77 | except: |
---|
| 78 | print "READ ERROR", line |
---|
| 79 | |
---|
[fc2b91a] | 80 | if has_dy==False: |
---|
| 81 | file_dy = None |
---|
| 82 | |
---|
[de9483d] | 83 | return file_x, file_y, file_dy |
---|
[fc2b91a] | 84 | return None, None, None |
---|
| 85 | |
---|
| 86 | def plot_data(parent, path, name="Loaded Data"): |
---|
[ccb560a] | 87 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
[7764b41] | 88 | |
---|
[4102709] | 89 | from DataLoader.loader import Loader |
---|
[1c9419f4] | 90 | import numpy |
---|
[4102709] | 91 | #Instantiate a loader |
---|
| 92 | L=Loader() |
---|
| 93 | |
---|
| 94 | #Recieves data |
---|
[ccb560a] | 95 | try: |
---|
| 96 | output=L.load(path) |
---|
| 97 | except: |
---|
| 98 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
| 99 | return |
---|
[7764b41] | 100 | if hasattr(output,'data'): |
---|
| 101 | new_plot = MetaData2D(output) |
---|
[fc2b91a] | 102 | else: |
---|
[1c9419f4] | 103 | if output.dy==None: |
---|
[7764b41] | 104 | new_plot = MetaTheory1D(output) |
---|
[1c9419f4] | 105 | else: |
---|
[7764b41] | 106 | new_plot = MetaData1D(output) |
---|
[acb1ad1] | 107 | |
---|
| 108 | filename = os.path.basename(path) |
---|
| 109 | |
---|
[fc2b91a] | 110 | new_plot.name = name |
---|
| 111 | new_plot.interactive = True |
---|
| 112 | # If the data file does not tell us what the axes are, just assume... |
---|
[7764b41] | 113 | new_plot.xaxis("\\rm{Q}",'A^{-1}') |
---|
[fc2b91a] | 114 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
[acb1ad1] | 115 | new_plot.group_id = filename |
---|
[7764b41] | 116 | |
---|
[1c9419f4] | 117 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=filename)) |
---|