From 565aa167c9a683ec7812aa850c587188845d19f1 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Fri, 12 Oct 2012 20:03:53 +0800 Subject: [PATCH] TCS - Improve processing script Trying to simplify the graphing of stuff --- research/TCS/process.py | 90 ++++++++++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 15 deletions(-) diff --git a/research/TCS/process.py b/research/TCS/process.py index 31c98d2d..894e08f1 100755 --- a/research/TCS/process.py +++ b/research/TCS/process.py @@ -15,9 +15,37 @@ import odict #ordered dictionary import copy import Gnuplot, Gnuplot.funcutils +import string gnuplot = Gnuplot.Gnuplot() +def Reset(): + gnuplot = Gnuplot.Gnuplot() + +def FindDataFiles(directory=".", depth=1, result=None): + if result == None: + result = [] + + for f in os.listdir(directory): + if os.path.isdir(directory+"/"+str(f)): + if depth > 1: + result += FindDataFiles(directory+"/"+str(f), depth-1, result) + continue + s = f.split(".") + if (len(s) == 2 and s[1] == "dat"): + result.append(directory+"/"+str(f)) + + return result + +def BaseName(f): + a = f.split("/") + return a[len(a)-1] + + +def DirectoryName(f, start=0,back=1): + a = f.split("/") + return string.join(a[start:(len(a)-back)], "/") + def GetData(filename): input_file = open(filename, "r") data = [] @@ -109,7 +137,7 @@ def Derivative(data, a=1, b=2, sigma=None,step=1): return result[0:len(result)-1] -def MaxNormalise(data, u=1): +def MaxNormalise(data, u=2): result = copy.deepcopy(data) if (len(data) <= 0): return result @@ -165,21 +193,38 @@ def SaveData(filename, data): out.write(str(a[i])) if (i < len(a) - 1): out.write("\t") - out.write("\n") + out.write("\n") -def CalibrateData(data, ammeter_scale=1e-6): +def AverageAllData(directory, save=None) + data_sets = [] + if save == None: save = directory+"/average.dat": + for d in FindDataFiles(directory): + data_sets.append(GetData(d)) + + a = Average(data_sets) + SaveData(save, a) + return a + +def CalibrateData(original, ammeter_scale=1e-6): + data = copy.deepcopy(original) for i in range(0, len(data)): data[i][1] = 16.8 * float(data[i][1]) / 4000.0 data[i][2] = ammeter_scale * 0.170 * float(data[i][2]) / 268.0 data[i][3] = ammeter_scale * 0.170 * float(data[i][3]) / 268.0 return data -def ShowTCS(filename, calibrate=True, normalise=False, show_error=False, plot=gnuplot.plot,w="lp", step=1): +def ShowTCS(filename, calibrate=True, normalise=False, show_error=False, plot=gnuplot.plot,with_="lp", step=1, output=None, title=""): if type(filename) == type(""): data = GetData(filename) else: data = filename - filename = "data" + filename = "tcs data" + + if (title == ""): + title = BaseName(filename) + + if (len(data) <= 0): + return data if calibrate: data = CalibrateData(data) @@ -193,30 +238,39 @@ def ShowTCS(filename, calibrate=True, normalise=False, show_error=False, plot=gn data = MaxNormalise(data) gnuplot("set ylabel \"dI(E)/dE (normalised)\"") - gnuplot("set title \"S(E)\"") + if (output != None and type(output) == type("")): + gnuplot("set term png size 640,480") + gnuplot("set output \""+str(output)+"\"") + + gnuplot("set title \"Total Current Spectrum S(E)\"") gnuplot("set xlabel \"U ("+str(units[0])+")\"") d = Derivative(data, 1, 2, step=step) - plot(Gnuplot.Data(d, using="2:3", with_=w,title="S(E) : " + str(filename))) + plot(Gnuplot.Data(d, using="2:3", with_=with_,title=title)) if (show_error): error1 = Derivative(data, 1, 2, -3,step=step) error2 = Derivative(data, 1, 2, +3,step=step) - gnuplot.replot(Gnuplot.Data(error1, using="2:3", with_=w,title="Error : Low bound")) - gnuplot.replot(Gnuplot.Data(error2, using="2:3", with_=w, title="Error : Upper bound")) + gnuplot.replot(Gnuplot.Data(error1, using="2:3", with_=w,title="-sigma/2")) + gnuplot.replot(Gnuplot.Data(error2, using="2:3", with_=w, title="+sigma/2")) + if (output != None and type(output) == type("")): + gnuplot("set term wxt") return data -def ShowData(filename,calibrate=True, normalise=False, show_error=False, plot=gnuplot.plot,w="lp", step=1): +def ShowData(filename,calibrate=True, normalise=False, show_error=False, plot=gnuplot.plot,with_="lp", step=1, output=None, title=""): if type(filename) == type(""): data = GetData(filename) else: data = filename - filename = "data" + filename = "raw data" + + if (title == ""): + title = BaseName(filename) if len(data) <= 0: - return + return data if calibrate: data = CalibrateData(data) units = ["V", "uA"] @@ -229,13 +283,17 @@ def ShowData(filename,calibrate=True, normalise=False, show_error=False, plot=gn data = MaxNormalise(data) gnuplot("set ylabel \"I(E) (normalised)\"") - gnuplot("set title \"S(E)\"") + if (output != None and type(output) == type("")): + gnuplot("set term png size 640,480") + gnuplot("set output \""+str(output)+"\"") + + gnuplot("set title \"Sample Current I(E)\"") gnuplot("set xlabel \"U ("+str(units[0])+")\"") #d = Derivative(data, 1, 2, step=step) - plot(Gnuplot.Data(data, using="2:3", with_=w,title="S(E) : " + str(filename))) + plot(Gnuplot.Data(data, using="2:3", with_=with_,title=title)) if (show_error): error1 = copy.deepcopy(data) error2 = copy.deepcopy(data) @@ -245,7 +303,9 @@ def ShowData(filename,calibrate=True, normalise=False, show_error=False, plot=gn error2[i][2] += 0.50*float(data[i][3]) gnuplot.replot(Gnuplot.Data(error1, using="2:3", with_=w,title="Error : Low bound")) gnuplot.replot(Gnuplot.Data(error2, using="2:3", with_=w, title="Error : Upper bound")) - + + if (output != None and type(output) == type("")): + gnuplot("set term wxt") return data def main(): -- 2.20.1