+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,with_="lp", step=1, output=None, title=""):
+ if type(filename) == type(""):
+ data = GetData(filename)
+ else:
+ data = filename
+ filename = "tcs data"
+
+ if (title == ""):
+ title = BaseName(filename)
+
+ if (len(data) <= 0):
+ return data
+
+ if calibrate:
+ data = CalibrateData(data)
+ units = ["V", "uA / V"]
+ else:
+ units = ["DAC counts", "ADC counts / DAC counts"]
+
+ if not normalise:
+ gnuplot("set ylabel \"dI(E)/dE ("+str(units[1])+")\"")
+ else:
+ data = MaxNormalise(data)
+ gnuplot("set ylabel \"dI(E)/dE (normalised)\"")
+
+ 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_=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="-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,with_="lp", step=1, output=None, title=""):
+ if type(filename) == type(""):
+ data = GetData(filename)
+ else:
+ data = filename
+ filename = "raw data"
+
+ if (title == ""):
+ title = BaseName(filename)
+
+ if len(data) <= 0:
+ return data
+ if calibrate:
+ data = CalibrateData(data)
+ units = ["V", "uA"]
+ else:
+ units = ["DAC counts", "ADC counts"]
+
+ if not normalise:
+ gnuplot("set ylabel \"I(E) ("+str(units[1])+")\"")
+ else:
+ data = MaxNormalise(data)
+ gnuplot("set ylabel \"I(E) (normalised)\"")
+
+ 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_=with_,title=title))
+ if (show_error):
+ error1 = copy.deepcopy(data)
+ error2 = copy.deepcopy(data)
+ for i in range(len(data)):
+ #print str(data[i])
+ error1[i][2] -= 0.50*float(data[i][3])
+ 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():
+ return 0
+ if (len(sys.argv) < 2):
+ sys.stderr.write(sys.argv[0] + " - Require arguments (filename)\n")
+ return 1
+
+ tcs = []
+ gnuplot("set style data lp")
+ gnuplot("set key outside right")
+ #gnuplot("set title \"Au on Si (50min 3.5A 3-6 e-8mbar)\"")
+ #gnuplot("set xlabel \"E (DAC Counts)\"")
+ #gnuplot("set ylabel \"S(E) (ADC/DAC Counts)\"")
+ #gnuplot("set term postscript colour")
+ #gnuplot("set output \"test.eps\"")
+ for i in range(1, len(sys.argv)):
+ if (len(tcs[i-1]) > 0):
+ gnuplot.replot(Gnuplot.Data(tcs[i-1], title=sys.argv[i], with_="lp"))
+
+ # Now average the data
+
+
+
+ avg = Average(tcs)
+ for a in avg:
+ sys.stdout.write(str(a[0]) + "\t" + str(a[1]) + "\t" + str(a[1]) + "\n")
+ gnuplot.replot(Gnuplot.Data(avg, title="Average", with_="l lw 2"))
+
+ sys.stdout.write("Save averaged data as (blank for no save): ")
+ filename = sys.stdin.readline().strip(" \r\n\t")
+ if (filename != ""):
+ SaveData(filename, avg)
+