+def FullWidthAtHalfMax(data, u=1):
+ maxval = max(data, key = lambda e : e[u])
+ peak = data.index(maxval)
+ maxval = maxval[0]
+ lhs = None
+ rhs = None
+ for i in range(1, len(data)/2):
+ if lhs == None:
+ if (peak-i > 0 and data[peak-i] < 0.50*maxval):
+ lhs = data[peak-i][u]
+ if rhs == None:
+ if (peak+i < len(data) and data[peak+i] < 0.50*maxval):
+ rhs = peak+i
+ if lhs != None and rhs != None:
+ break
+ if rhs == None or lhs == None:
+ return abs(data[len(data)-1][0] - data[0][0])
+ else:
+ return abs(rhs - lhs)
+
+def SaveData(filename, data):
+ out = open(filename, "w", 0)
+ for a in data:
+ for i in range(0, len(a)):
+ out.write(str(a[i]))
+ if (i < len(a) - 1):
+ out.write("\t")
+ out.write("\n")
+
+def AverageAllData(directory, save=None, normalise=True):
+ data_sets = []
+ if save == None: save = directory+"/average.dat"
+ for f in FindDataFiles(directory):
+ d = GetData(f)
+ if normalise:
+ d = MaxNormalise(d)
+ data_sets.append(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, raw=True,calibrate=True, normalise=False, show_error=False, plot=gnuplot.plot,with_="lp", step=1, output=None, title="", master_title="", smooth=0, show_peak=False, inflection=1):
+
+ if raw == False:
+ calibrate = False
+ normalise = False
+
+ 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 (smooth > 0):
+ if type(smooth) == type([]):
+ for i in range(smooth[0]):
+ data = Smooth(data, m=smooth[1])
+ else:
+ data = Smooth(data, m=smooth)
+
+
+ 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)+"\"")
+
+ if master_title == "":
+ master_title = "Total Current Spectrum S(E)"
+ if type(filename) == type("") and plot == gnuplot.plot:
+ if filename != "tcs data":
+ p = ReadParameters(filename)
+ if "Sample" in p:
+ master_title += "\\nSample: "+p["Sample"]
+
+ gnuplot("set title \""+str(master_title)+"\"")
+ gnuplot("set xlabel \"U ("+str(units[0])+")\"")
+
+
+ if raw:
+ d = Derivative(data, 1, 2, step=step)
+ else:
+ d = data
+
+ ymax = 0.01 + 1.2 * max(d, key=lambda e : e[2])[2]
+ ymin = -0.01 + 1.2 * min(d, key=lambda e : e[2])[2]
+ gnuplot("set yrange ["+str(ymin)+":"+str(ymax)+"]")
+
+ plotList = []
+ plotList.append(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)
+ plotList.append(Gnuplot.Data(error1, using="2:3", with_=w,title="-sigma/2"))
+ plotList.append(Gnuplot.Data(error2, using="2:3", with_=w, title="+sigma/2"))
+
+ if (show_peak):
+ peak = SmoothPeakFind(d, ap=DoNothing, stop=1, inflection=inflection)
+ plotList += PlotPeaks(peak,with_="l lt -1", plot=None)
+
+
+
+ if (plot != None):
+ plot(*plotList)
+ time.sleep(0.2)
+
+ if (output != None and type(output) == type("")):
+ gnuplot("set term wxt")
+
+ if (plot == None):
+ return plotList
+ return data
+
+def ShowData(filename,calibrate=True, normalise=False, show_error=False, plot=gnuplot.plot,with_="lp", step=1, output=None, title="", master_title="Sample Current I(E)", smooth=0):
+ 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 (smooth > 0):
+ data = Smooth(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 \""+str(master_title)+"\"")
+ gnuplot("set xlabel \"U ("+str(units[0])+")\"")
+
+
+ #d = Derivative(data, 1, 2, step=step)
+
+ plotList = []
+
+ plotList.append(Gnuplot.Data(data, using="2:3", with_=with_,title=title))
+ time.sleep(0.1)
+ 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])
+ plotList.append(Gnuplot.Data(error1, using="2:3", with_=w,title="Error : Low bound"))
+ plotList.append(Gnuplot.Data(error2, using="2:3", with_=w, title="Error : Upper bound"))
+
+ if plot != None: