%% LoadFile
% The easiest way to read in the XTRA or BASE .csv is to read it in as
% a table. Switch the file name below to your desired AIM XTRA or BASE .csv
% file.
AIM_Data=readtable('E190_9SEP2017_J825R_1_XTRA.csv');
% You have to actually look at the column headings to make sure
% you have the correct column names.
% Because the AIM XTRA uses different sample rates for the different
% sensors, most columns (except for the z-axis accelerometer and associated
% time) will have NaNs for much of the column. They show up once the actual
% column data cease. The easiest way to remove them is with a conditional
% using isfinite.

% Table data are column vectors.
% You should transpose most table data before using.

% The data in a column heading may be accessed by appending with a dot, ".",
% and either the column name, e.g., .time, or the column number in
% parentheses, e.g., .(1). The examples below shows how to plot two columns
% using either method of addressing.

%% PlotDotName
plot(AIM_Data.time,AIM_Data.GyroZ)
title('Test Plot Table Columns .name')
xlabel('time (s)')
ylabel('\omega_z (°/s)')
%% PlotDotNumber
plot(AIM_Data.(1),AIM_Data.(4))
title('Test Plot Table Columns .(#)')
xlabel('time (s)')
ylabel('\omega_z (°/s)')

%% GetGyroData
% Getting IMU data. Note the transposition and NaN removal.
timeGyro=transpose(AIM_Data.time(isfinite(AIM_Data.time)));
gyroX=transpose(AIM_Data.GyroX(isfinite(AIM_Data.GyroX)));
gyroY=transpose(AIM_Data.GyroY(isfinite(AIM_Data.GyroY)));
gyroZ=transpose(AIM_Data.GyroZ(isfinite(AIM_Data.GyroZ)));
plot(timeGyro,gyroZ, timeGyro,gyroX, timeGyro,gyroY)
title('Gyro Data')
xlabel('time (s)')
ylabel('Rotation Rate (°/s)')
legend('\omega_z', '\omega_x', '\omega_y')

%% GetAccelData
timeAccel=transpose(AIM_Data.time_5(isfinite(AIM_Data.time_5)));
accelZ=transpose(AIM_Data.acceleration(isfinite(AIM_Data.acceleration)));
timeAccelLat=transpose(AIM_Data.time_6(isfinite(AIM_Data.time_6)));
accelX=transpose(AIM_Data.lat_XAccel_(isfinite(AIM_Data.lat_XAccel_)));
accelY=transpose(AIM_Data.lat_YAccel_(isfinite(AIM_Data.lat_YAccel_)));
plot(timeAccel,accelZ, timeAccelLat,accelX, timeAccelLat,accelY)
title('Accelerometer Data')
xlabel('time (s)')
ylabel('Acceleration')
legend('a_z', 'a_x', 'a_y')

%% GetAltimeterdata
timeAlt=transpose(AIM_Data.time_7(isfinite(AIM_Data.time_7)));
Pdata=transpose(AIM_Data.pressure(isfinite(AIM_Data.pressure)));
altMSL=transpose(AIM_Data.PressureMSL(isfinite(AIM_Data.PressureMSL)));
altAGL=transpose(AIM_Data.PressureAGL(isfinite(AIM_Data.PressureAGL)));
plot(timeAlt,altMSL, timeAlt,altAGL)
title('Pressure Altimeter Data')
xlabel('time (s)')
ylabel('Altitude MSL (m), Altitude AGL (m)')
legend('MSL', 'AGL')

%% GetMagnetometerData
timeMag=transpose(AIM_Data.time_9(isfinite(AIM_Data.time_9)));
magX=transpose(AIM_Data.MagX(isfinite(AIM_Data.MagX)));
magY=transpose(AIM_Data.MagY(isfinite(AIM_Data.MagY)));
magZ=transpose(AIM_Data.MagZ(isfinite(AIM_Data.MagZ)));
plot(timeMag,magX, timeMag,magY, timeMag,magZ)
title('Raw Magnetometer Data')
xlabel('time (s)')
ylabel('Magnetic Field Strength')
legend('m_x', 'm_y', 'm_z')

%% GetGPSdata
timeGPS=transpose(AIM_Data.time_8(isfinite(AIM_Data.time_8)));
lat=transpose(AIM_Data.lat(isfinite(AIM_Data.lat)));
long=transpose(AIM_Data.long(isfinite(AIM_Data.long)));
GPSMSL=transpose(AIM_Data.GPSMSL(isfinite(AIM_Data.GPSMSL)));
plot(timeGPS,lat, 'g*', timeGPS,long, 'b*', timeGPS,GPSMSL, 'r*')
title('GPS Data')
xlabel('time (s)')
ylabel('Latitude (°), Longitude (°), Altitude (m MSL)')
legend('lat', 'long', 'alt')

%% DisplayVoltages
% Below is the procedure for calculating the temperatures of the
% thermistors from the voltages in Channels C & D. Note the transposition
% and the isfinite to remove NaNs.
VoltA=transpose(AIM_Data.AVoltage(isfinite(AIM_Data.AVoltage)));
TtimeA=transpose(AIM_Data.time_1(isfinite(AIM_Data.time_1)));
VoltB=transpose(AIM_Data.BVoltage(isfinite(AIM_Data.BVoltage)));
TtimeB=transpose(AIM_Data.time_2(isfinite(AIM_Data.time_2)));
VoltC=transpose(AIM_Data.CVoltage(isfinite(AIM_Data.CVoltage)));
TtimeC=transpose(AIM_Data.time_3(isfinite(AIM_Data.time_3)));
VoltD=transpose(AIM_Data.DVoltage(isfinite(AIM_Data.DVoltage)));
TtimeD=transpose(AIM_Data.time_4(isfinite(AIM_Data.time_4)));
plot(TtimeA,VoltA,TtimeB,VoltB,TtimeC,VoltC,TtimeD,VoltD)
title('Voltage vs. Time Profile')
xlabel('time (s)')
ylabel('Voltage (V)')
legend('Channel A','Channel B','Channel C','Channel D')


