import csv import numpy as np # 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 = "C:/Users/Tim/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 = [] chan_2_mean = [] mean_amplitude_for_position_chan_1 =[] ################################################################### Functions ######################################################################## # Prints the headers in the file. def headers(): for rows in data: header_1 = rows[0] print header_1 # 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() # files the lists with the data from the respective channels. print len(chan_1) # 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) # calculates the normaised area for each chan_1 repetision and position b = np.arange(len(chan_1)) for j in b: chan_1_avg_i = np.trapz(chan_1[i], dx=1.0/len(chan_1[0])/np.trapz(chan_2[i], dx=1.0/len(chan_1[i]))) chan_1_area_norm.append(chan_1_avg_i) # Removes the extra vaue at the beginning mir_pos = mir_pos[1:] print mir_pos print chan_1_mean #values = 0 #for values in chan_1_mean #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(","))