# EBP 2016 Data Parsing # Created by Mo Washburn for EBP 2019 Project # This file builds the data sequence for use in ChucK ## # Library dependencies import numpy as np import pandas as pd import matplotlib.pyplot as plt import csv import os index = 0 WRITE_SEQUENCE = True PRESSURE_DATA_OUTPUT = 'C:/data/pressure_data_output.csv' RIGHT_GAIN_OUTPUT = 'C:/data/right_gain_output.csv' LEFT_GAIN_OUTPUT = 'C:/data/left_gain_output.csv' ROOT_ADJUST_OUTPUT = 'C:/data/root_adjust_output.csv' THIRD_ADJUST_OUTPUT = 'C:/data/third_adjust_output.csv' FIFTH_ADJUST_OUTPUT = 'C:/data/fifth_adjust_output.csv' file_location = 'C:/Users/dawsh/PycharmProjects/Elijah_Balloon_Space_Music/Sample_Data_Testing.xlsx' sheet = pd.ExcelFile(file_location).parse(sheet_name='Atmospheric Data') # Isolate desired values in source file and read them in as columnar data. # Re-formats horizontal data in source format as vertical. temp_read = sheet.loc[sheet['Reading'] == 'Temp'].astype(str) temp_read.reset_index(drop=True, inplace=True) alt_read = sheet.loc[sheet['Reading'] == 'Alt'].astype(str) alt_read.reset_index(drop=True, inplace=True) pressure_read = sheet.loc[sheet['Reading'] == 'Pressure'].astype(str) pressure_read.reset_index(drop=True, inplace=True) ozone_read = sheet.loc[sheet['Reading'] == 'O3'].astype(str) ozone_read.reset_index(drop=True, inplace=True) # Initialize arrays for data insertion sorted_temp = np.empty((1536, 1), dtype=float) sorted_alt = np.empty((1536, 1), dtype=float) sorted_pressure = np.empty((1536, 1), dtype=float) sorted_ozone = np.empty((1536, 1), dtype=float) structure = np.empty((1536, 4), dtype=float) # Loop through each column of data and insert into array for index, row in temp_read.iterrows(): element = row.iloc[1] sorted_temp[index] = (float(element[0:5])) for index, row in alt_read.iterrows(): element = row.iloc[1] sorted_alt[index] = (float(element[0:10])) for index, row in pressure_read.iterrows(): element = row.iloc[1] sorted_pressure[index] = (float(element[0:10])) for index, row in ozone_read.iterrows(): element = row.iloc[1] sorted_ozone[index] = (float(element[0:10])) pop_pressure = np.delete(sorted_pressure, np.arange(600), axis=0) mean_ozone = np.mean(sorted_ozone, dtype=float) std_ozone = np.std(sorted_ozone, dtype=float, ddof=1) mean_temp = np.mean(sorted_temp, dtype=float) std_temp = np.std(sorted_temp, dtype=float, ddof=1) mean_pressure = np.mean(sorted_pressure, dtype=float) std_pressure = np.std(sorted_pressure, dtype=float, ddof=1) sorted_ozone_var = np.empty((1536, 1), dtype=float) sorted_temp_var = np.empty((1536, 1), dtype=float) sorted_pressure_var = np.empty((1536, 1), dtype=float) for index, element in enumerate(sorted_ozone): sorted_ozone_var[index] = ((element-mean_ozone)/std_ozone) for index, element in enumerate(sorted_temp): sorted_temp_var[index] = ((element-mean_temp)/std_temp) for index, element in enumerate(sorted_pressure): sorted_pressure_var[index] = ((element-mean_pressure)/std_pressure) # Reshaping arrays for proper input shape structure = np.array((sorted_alt, sorted_temp, sorted_pressure, sorted_ozone), dtype=float) structure = np.reshape(structure, (4, 1536)) test_data = np.transpose(structure) # New data frame for desired content in desired format sorted_sheet = pd.DataFrame(data=test_data, index=None, columns=['Altitude', 'Temp', 'Pressure', 'Ozone'], dtype=float) ''' TIME TO BUILD A SEQUENCE =) We'll need to be able to look at the entire dataframe to see trends. So, let's print it out to excel. ''' sorted_sheet.to_excel('Sample_Data_Testing_Parsing.xlsx', sheet_name='Parsed_Data') ''' Introduction to graphing plots with Python. Here we will display the four data columns vs the sensor reading number, effectively showing how the data changes over the flight (or w.r.t time). ''' quarter, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(20.0, 9.0)) quarter.suptitle('Complete Flight Data Graphs') ax1.plot(np.arange(0, sorted_alt.size), sorted_alt, 'g', linewidth=0.5, label='Altitude') ax1.title.set_text('Altitude Over Time') ax1.set_xlabel('Reading No.') ax1.set_ylabel('Altitude(m)') ax2.plot(sorted_alt, sorted_temp, 'r', label='Temperature') ax2.axvline(x=np.amax(sorted_alt)) ax2.title.set_text('Temperature vs Altitude') ax2.set_xlabel('Altitude(m)') ax2.set_ylabel('Temperature(C)') ax3.plot(sorted_alt, sorted_pressure, 'k', label='Pressure') ax3.axvline(x=np.amax(sorted_alt)) ax3.title.set_text('Pressure vs Altitude') ax3.set_xlabel('Altitude(m)') ax3.set_ylabel('Pressure(u)') ax4.scatter(sorted_alt, sorted_ozone, s=1, marker='^', linewidth=0.05, label='Ozone') ax4.axvline(x=np.amax(sorted_alt)) ax4.title.set_text('Ozone vs Altitude') ax4.set_xlabel('Altitude(m)') ax4.set_ylabel('Ozone Abundance(mPa)') # Splicing the graphs in half in order to observe trends during flight and during descent spliced_alt1 = sorted_alt[0:np.argmax(sorted_alt)] spliced_alt2 = sorted_alt[np.argmax(sorted_alt):] spliced_temp1 = sorted_temp[0:np.argmax(sorted_alt)] spliced_temp2 = sorted_temp[np.argmax(sorted_alt):] spliced_ozone1 = sorted_ozone[0:np.argmax(sorted_alt)] spliced_ozone2 = sorted_ozone[np.argmax(sorted_alt):] spliced_pressure1 = sorted_pressure[0:np.argmax(sorted_alt)] spliced_pressure2 = sorted_pressure[np.argmax(sorted_alt):] spliced_ozone1_var = sorted_ozone_var[0:np.argmax(sorted_alt)] spliced_ozone2_var = sorted_ozone_var[np.argmax(sorted_alt):] spliced_temp1_var = sorted_temp_var[0:np.argmax(sorted_alt)] spliced_temp2_var = sorted_temp_var[np.argmax(sorted_alt):] spliced_pressure1_var = sorted_pressure_var[0:np.argmax(sorted_alt)] spliced_pressure2_var = sorted_pressure_var[np.argmax(sorted_alt):] # Spliced Temperature Figure with Ozone vs Temp option figure_2, (ax5, ax6) = plt.subplots(1, 2, sharex=False, figsize=(20.0, 9.0)) figure_2.suptitle('Spliced Temperature Graph') ax5.plot(spliced_alt1, spliced_temp1, 'g', label='Temperature') ax5.title.set_text('Ascent Temperature') ax5.set_xlabel('Altitude(m)') ax5.set_ylabel('Temp(C)') ax6.plot(spliced_alt2, spliced_temp2, 'r', label='Temperature') ax6.invert_xaxis() ax6.title.set_text('Descent Temperature') ax6.set_xlabel('Altitude(m)') ax6.set_ylabel('Temp(C)') ''' ax5.plot(spliced_alt1, spliced_pressure1, 'g', label='Pressure') ax5.title.set_text('Ascent Pressure') ax5.set_xlabel('Altitude(m)') ax5.set_ylabel('Pressure(mPa)') ax6.plot(spliced_alt2, spliced_pressure2, 'r', label='Pressure') ax6.invert_xaxis() ax6.title.set_text('Descent Pressure') ax6.set_xlabel('Altitude(m)') ax6.set_ylabel('Pressure(mPa)') ''' ''' ax7.plot(sorted_temp, sorted_ozone, 'b') ax7.title.set_text('Ozone vs Temperature') ax7.set_xlabel('Temperature(C)') # STANDARD DEVIATION HARMONY EXPERIMENT ozone_figure, (ax8, ax9, ax10, ax11) = plt.subplots(1, 4, sharex=False, figsize=(20.0, 9.0)) ozone_figure.suptitle('Spliced Ozone Graph Analysis') ax8.plot(spliced_alt1, spliced_ozone1, marker='.', linewidth=0.1, label='Flight') ax8.title.set_text('Ozone During Ascent') ax8.set_xlabel('Altitude(m)') ax9.plot(spliced_alt1, spliced_ozone1_var, marker='.') ax9.title.set_text('Ozone Std Dev Ascent') ax9.set_xlabel('Altitude(m)') ax10.plot(spliced_alt2, spliced_ozone2, marker='.', linewidth=0.1, label='Descent') ax10.invert_xaxis() ax10.title.set_text('Ozone During Descent') ax10.set_xlabel('Altitude(m)') ax11.plot(spliced_alt2, spliced_ozone2_var, marker='.') ax11.invert_xaxis() ax11.title.set_text('Ozone Std Dev Descent') ax11.set_xlabel('Altitude(m)') temp_figure, (ax12, ax13, ax14, ax15) = plt.subplots(1, 4, sharex=False, figsize=(20.0, 9.0)) temp_figure.suptitle('Spliced Temp Graph Analysis') ax12.plot(spliced_alt1, spliced_temp1, marker='.', linewidth=0.1, label='Flight') ax12.title.set_text('Ascent Temperature') ax12.set_xlabel('Altitude(m)') ax13.plot(spliced_alt1, spliced_temp1_var, marker='.') ax13.title.set_text('Temp Std Dev Ascent') ax13.set_xlabel('Altitude(m)') ax14.plot(spliced_alt2, spliced_temp2, 'r', label='Descent') ax14.invert_xaxis() ax14.title.set_text('Descent Temperature') ax14.set_xlabel('Altitude(m)') ax15.plot(spliced_alt2, spliced_temp2_var, 'k', marker='.') ax15.invert_xaxis() ax15.title.set_text('Temp Std Dev Descent') ax15.set_xlabel('Altitude(m)') pressure_figure, (ax16, ax17, ax18, ax19) = plt.subplots(1, 4, sharex=False, figsize=(20.0, 9.0)) pressure_figure.suptitle('Spliced Pressure Graph Analysis') ax16.plot(spliced_alt1, spliced_pressure1, label='Flight') ax16.title.set_text('Ascent Pressure') ax16.set_xlabel('Altitude(m)') ax17.plot(spliced_alt1, spliced_pressure1_var, marker='.', linewidth=0.1) ax17.title.set_text('Pressure Std Dev Ascent') ax17.set_xlabel('Altitude(m)') ax18.plot(spliced_alt2, spliced_pressure2, label='Descent') ax18.invert_xaxis() ax18.title.set_text('Descent Pressure') ax18.set_xlabel('Altitude(m)') ax19.plot(spliced_alt2, spliced_pressure2_var, 'k', marker='.', linewidth=0.1) ax19.invert_xaxis() ax19.title.set_text('Pressure Std Dev Descent') ax19.set_xlabel('Altitude(m)') ''' plt.show() # TEMP-DRIVEN GAIN FLUCTUATION EXPERIMENT leftGain = np.empty((1536, 1), dtype=float) rightGain = np.empty((1536, 1), dtype=float) for index, element in enumerate(sorted_temp): if element > 0: rightGain[index] = (element/29.4) leftGain[index] = float(1.0-rightGain[index]) elif element < 0: leftGain[index] = abs(element/18.5) rightGain[index] = float(1.0-leftGain[index]) else: leftGain[index] = 0.0 rightGain[index] = 0.0 # STANDARD DEVIATION HARMONY EXPERIMENT adjust_root = np.empty((1536, 1), dtype=float) adjust_3rd = np.empty((1536, 1), dtype=float) adjust_5th = np.empty((1536, 1), dtype=float) for index, element in enumerate(sorted_ozone_var): if element > 1: adjust_root[index] = (element-1)/10 elif element < -1: adjust_root[index] = (element+1)/10 else: adjust_root[index] = 0 for index, element in enumerate(sorted_temp_var): if element > 1: adjust_3rd[index] = (element-1)/10 elif element < -1: adjust_3rd[index] = (element+1)/10 else: adjust_3rd[index] = 0 for index, element in enumerate(sorted_pressure_var): if element > 1: adjust_5th[index] = (element-1)/10 elif element < -1: adjust_5th[index] = (element+1)/10 else: adjust_5th[index] = 0 adjust_output = np.array((adjust_root, adjust_3rd, adjust_5th), dtype=float, ndmin=2) adjust_output = np.reshape(adjust_output, (3, 1536)) adjust_output = adjust_output.transpose() adjust_frame = pd.DataFrame(data=adjust_output, index=None, columns=['Root Adjustment', '3rd Adjustment', '5th Adjustment']) adjust_frame.to_excel('Standard_Deviation_Harmony_Experiment.xlsx', sheet_name='Freq Adjustments') if WRITE_SEQUENCE: # TEMP-DRIVEN GAIN FLUCTUATION EXPERIMENT with open(RIGHT_GAIN_OUTPUT, 'w+') as right: file = csv.writer(right) for index, value in enumerate(rightGain): file.writerow(value) right.seek(0, os.SEEK_END) right.truncate() with open(LEFT_GAIN_OUTPUT, 'w+') as left: file = csv.writer(left) for index, value in enumerate(leftGain): file.writerow(value) left.seek(0, os.SEEK_END) left.truncate() # STANDARD DEVIATION HARMONY EXPERIMENT with open(ROOT_ADJUST_OUTPUT, 'w+') as r: file = csv.writer(r) for value in adjust_root: file.writerow(value) r.seek(0, os.SEEK_END) r.truncate() with open(THIRD_ADJUST_OUTPUT, 'w+') as t: file = csv.writer(t) for value in adjust_3rd: file.writerow(value) t.seek(0, os.SEEK_END) t.truncate() with open(FIFTH_ADJUST_OUTPUT, 'w+') as f: file = csv.writer(f) for value in adjust_5th: file.writerow(value) f.seek(0, os.SEEK_END) f.truncate() # PRESSURE-DRIVEN PITCH CHANGE with open(PRESSURE_DATA_OUTPUT, 'w+') as p: file = csv.writer(p) for value in pop_pressure: file.writerow(value/100000) p.seek(0, os.SEEK_END) p.truncate()