import os, sys import wx from DataLoader.loader import Loader def choose_data_file(parent, location=None): path = None if location==None: location = os.getcwd() l = Loader() cards = l.get_wildcards() wlist = '|'.join(cards) dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() mypath = os.path.basename(path) dlg.Destroy() return path def load_ascii_1D(path): """ Load a 1D ascii file, with errors """ import numpy if path and os.path.isfile(path): file_x = numpy.zeros(0) file_y = numpy.zeros(0) file_dy = numpy.zeros(0) input_f = open(path,'r') buff = input_f.read() lines = buff.split('\n') has_dy = False for line in lines: try: toks = line.split() x = float(toks[0]) y = float(toks[1]) if len(toks)==3: has_dy = True err = float(toks[2]) else: err = 0.0 file_x = numpy.append(file_x, x) file_y = numpy.append(file_y, y) file_dy = numpy.append(file_dy, err) except: print "READ ERROR", line if has_dy==False: file_dy = None return file_x, file_y, file_dy return None, None, None def plot_data(parent, path, name="Loaded Data"): from sans.guicomm.events import NewPlotEvent, StatusEvent from DataLoader.loader import Loader #Instantiate a loader L=Loader() #Recieves data try: output=L.load(path) except: wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) return if type(output).__name__=='list': for i in range(len(output)): _plot_single_data(parent, output[i], path, name, str(i)) else: _plot_single_data(parent, output, path, name) def _plot_single_data(parent, output, path, name="Loaded Data", index=None): """ Plot a single data set @param output: data set to plot """ from sans.guicomm.events import NewPlotEvent from sans.guitools.plottables import Data1D, Theory1D if output.dy==None: new_plot = Theory1D(output.x, output.y) else: new_plot = Data1D(output.x, output.y, dy=output.dy) filename = os.path.basename(path) if index is not None: filename += "(%s)" % index new_plot.name = name new_plot.interactive = True # If the data file does not tell us what the axes are, just assume... new_plot.xaxis("\\rm{Q}", 'A^{-1}') new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") new_plot.group_id = filename title = filename if len(output.title) > 0: title = filename+'['+output.title+']' wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=title))