import csv import numpy as np import matplotlib.pyplot as plt import math # Avaiable Variable: #1) no_rep = number of repatitions for each position #2) no_of_pos = the number of positions in the scan #3) headers() = the headers in the file #Opens and reads the file. filename = "/home/to/Documents/Phd/rhul_thz_from_Raspberry/data/2015_09_18/20150918_123033_scan.csv" data_file = open(filename, "rb") data = csv.reader(data_file, delimiter = '\t') ################################################################## Arrays ############################################################################## mir_pos = [] chan_1 = [] chan_2 = [] chan_1_area_norm = [] mean_area_for_positions =[] error_of_mean_area = [] ################################################################### Functions ######################################################################## # Prints the headers in the file. def headers(): data_file = open(filename, "rb") data = csv.reader(data_file, delimiter = '\t') for rows in data: header_1 = rows[0] return header_1 data_file = open(filename, "rb") data = csv.reader(data_file, delimiter = '\t') def aqisition_rate(): data_file = open(filename, "rb") data = csv.reader(data_file, delimiter = '\t') for rows in data: header_3 = rows[0] if header_3 == "ACQ_RTE": return float(rows[1]) data_file = open(filename, "rb") data = csv.reader(data_file, delimiter = '\t') def rep(): data_file = open(filename, "rb") data = csv.reader(data_file, delimiter = '\t') for rows in data: header_3 = rows[0] if header_3 == "SCN_REP": return int(rows[1]) data_file = open(filename, "rb") data = csv.reader(data_file, delimiter = '\t') # Prints out the number of position in the mirror def no_of_pos_mir(): del mir_pos[0] no_of_pos = len(mir_pos) return no_of_pos # A genera function to return the number of position/elements in a list def no_of_pos(axis): no_of_pos_1 = len(axis) return no_of_pos_1 #Coverts the list of lists for each channe to seperate np array for each positions. def convert_list_to_array_of_float(chan,i): chan[i] = chan[i].split(",")[1:-1] #removes problem with [ attachd for first and last entry chan[i] = map(float,chan[i]) # converts every elemenet in the list to a float chan[i] = np.array(chan[i]) # Converts each list in the total chan_1 list to a seperate np array return chan[i] # Calculates the mean for a givem channel and position index def mean_calc(chan,i): chan_avg_i = np.mean(chan[i]) return chan_avg_i # Finds the data for channels 1 and 2. def channel_data_to_list(): for thing in data: header = thing[0] # Creates an array of ALL channel_1 data if header == "CH1_DAT": chan_1.append(thing[1]) # Creates an array of All Channe_2 data if header == "CH2_DAT": chan_2.append(thing[1]) if header == "INT_MIR": mir_pos.append(thing[1]) ##################################################### Code ################################################################################## channel_data_to_list() # Sorts the lists in lists with the data from the respective channels. # Converts each list to an array a = np.arange(len(chan_1)) for i in a: convert_list_to_array_of_float(chan_1,i) convert_list_to_array_of_float(chan_2,i) ############################################################### Code for area analysis ################################################################################## #print chan_1[0] # Calculates the normaised area for each chan_1 repetision and position b = np.arange(len(chan_1)) for i in b: chan_1_avg = np.trapz(chan_1[i], dx=1.0/len(chan_1[i])/np.trapz(chan_2[i], dx=1.0/len(chan_1[i]))) chan_1_area_norm.append(chan_1_avg) # Calculates the mean area for each position start_value= int(0) a = rep() hard_rep = rep() while a < len(chan_1_area_norm) + hard_rep: mean_value_temp = [] for p in range(start_value,a): mean_value_temp.append(chan_1_area_norm[p]) mean_area_for_positions.append(np.mean(mean_value_temp)) start_value = start_value + hard_rep a = a + hard_rep ####################### Calculates the standard error of the mean counter = 0 a = rep() start_value= int(0) while a < len(chan_1_area_norm) + hard_rep: mean_value_temp_1 = [] sum_temp =[] for p in range(start_value,a): mean_value_temp_1.append(chan_1_area_norm[p]) for i in mean_value_temp_1: sum_temp.append((i-mean_area_for_positions[counter])**2/(rep()*(rep()-1))) error_of_mean_area.append(math.sqrt(sum(sum_temp))) start_value = start_value + hard_rep a = a + hard_rep counter = counter + 1 print error_of_mean_area #error_area.append(np.array(chan_1_area_norm).std()/math.sqrt(rep())) ########################################################### Code for maximum anaylsis ######################### ################################################# Plotting ####################################################### # Removes the extra vaue at the beginning mir_pos = mir_pos[1:] x = np.arange(len(chan_1[0])) canvus = plt.figure(facecolor='white') #Plots the data straight from the channel. data_plot = canvus.add_subplot(2,2,1, axisbg='white') data_plot.plot((x/aqisition_rate()),chan_1[0], label ="Channel 1") data_plot.plot(x/aqisition_rate(),chan_2[0], label =" Channel 2 ") data_plot.set_xlabel("Time / sec") data_plot.set_ylabel("Signal / V") data_plot.set_title("Channel Data", y=1.04) plt.legend() plt.grid() interfer = canvus.add_subplot(2,2,2, axisbg='white') interfer.errorbar(mir_pos,mean_area_for_positions, yerr = error_of_mean_area, label ="Channel 1") interfer.set_xlabel("Mirror Position / mm") interfer.set_ylabel("Amplitude / Abt. Units") interfer.set_title("Scan Interferrogram", y=1.04) #interfer.set_ylim(0, 2*max(mean_area_for_positions)) plt.legend() plt.grid() plt.show() fft = canvus.add_subplot(2,1,2, axisbg='white') fft.plot(mir_pos, mean_area_for_positions, label ="Channel 1") fft.set_xlabel("Frequency / Hz") fft.set_ylabel("Amplitude / Abt. Units") fft.set_title("Scan FFT", y=1.04) plt.legend() plt.grid() plt.show() #mean_area_final =[] #values = rep()-1 #while values < len(mean_area_for_positions): # mean_area_final.append(mean_area_for_positions[values]) # values =values + hard_rep #print mean_area_final #t#est = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27] #while a < len(test) + hard_rep: # mean_value_temp = [] # for p in range(start_value,a): # mean_value_temp.append(test[p]) # print mean_value_temp # mean_area_for_positions.append(np.mean(mean_value_temp)) # start_value = start_value + hard_rep # a = a + hard_rep #for rows in data: # header = rows[0] # Defines the numer of repatitions at each position # if header == "SCN_REP": # no_rep = rows[1] # Creates an array of mirror positions # if header == "INT_MIR": # mir_pos.append(rows[1]) # Creates an array of ALL channel_1 data # if header == "CH1_DAT": # chan_1.append(rows[1]) # Creates an array of All Channe_2 data # if header == "CH2_DAT": # chan_2.append(rows[1]) #irint no_rep # Fixes problem with extra value at start of mirror position. #a = np.arange(len(chan_1)) #for i in a: #chan_1[i] = chan_1[i].split(",")[1:len(chan_1[i]-1)] #print chan_1[0], len(chan_1[0]) # len(chan_1[0].split(',')) #for i in a: # chan_1[i] = chan_1[i].split(",")[1:-1] #removes problem with [ attachd for first and last entry # chan_1[i] = map(float,chan_1[i]) # converts every elemenet in the list to a float # chan_1[i] = np.array(chan_1[i]) #chan_1_fl = map(float,chan_1[0]) #a = np.array(chan_1_fl) #print a #print np.mean(chan_1[0].split(","))