TCS - Improve processing script
authorSam Moore <sam@daedalus.(none)>
Fri, 12 Oct 2012 12:03:53 +0000 (20:03 +0800)
committerSam Moore <sam@daedalus.(none)>
Fri, 12 Oct 2012 12:03:53 +0000 (20:03 +0800)
Trying to simplify the graphing of stuff

research/TCS/process.py

index 31c98d2..894e08f 100755 (executable)
@@ -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():    

UCC git Repository :: git.ucc.asn.au