%% OpenAndFixFile
% Open and read the csv file. The Raven software will let you export the
% Raven data as a csv file. It's formatted incorrectly, but you can read
% it. Change the file below to your actual Raven .csv file. Not every
% single column is plotted, but some representative data are. You can plot
% additional data by looking in the FIP_Data table at the headings and
% making the substitutions in the relevant locations.
% Note that bILBA in a column label indicated Ignition, Lift-off, Burnout,
% Apogee in a binary format (one-hot binary encoding).
fid  = fopen('E190_9SEP2017_J825R_1_Raven.csv','r');
f=fread(fid,'*char')';
fclose(fid);

% Fix the ,,-to-,,, problem and write temporary file
goodFile = replace(f,",,",",,,");
fid  = fopen('temp.csv','w');
fprintf(fid,'%s',goodFile);
fclose(fid);

% Read the temporary file into a table
FIP_Data=readtable('temp.csv');
clearvars f goodFile fid

%% GetPlotAccelData
timeAccel=transpose(FIP_Data.Time_AxialAccel_Gs_(isfinite(FIP_Data.Time_AxialAccel_Gs_)));
dtA = mean(diff(timeAccel))
accelZ=transpose(FIP_Data.AxialAccel_Gs_(isfinite(FIP_Data.AxialAccel_Gs_)));
timeAccelLat=transpose(FIP_Data.Time_LateralAccel_Gs_(isfinite(FIP_Data.Time_LateralAccel_Gs_)));
dtAL = mean(diff(timeAccelLat))
accelX=transpose(FIP_Data.LateralAccel_Gs_(isfinite(FIP_Data.LateralAccel_Gs_)));
plot(timeAccel,accelZ,timeAccelLat,accelX)
title('Accelerometer Data')
xlabel('Time (s)')
ylabel('Acceleration (g)')
legend('a_x', 'a_y')

%% GetPlotBaroAtmData
timeBaro=transpose(FIP_Data.Time_Baro_Atm_(isfinite(FIP_Data.Time_Baro_Atm_)));
dtB = mean(diff(timeBaro))
Baro=transpose(FIP_Data.Baro_Atm_(isfinite(FIP_Data.Baro_Atm_)));
plot(timeBaro,Baro)
title('Barometric Pressure Data')
xlabel('Time (s)')
ylabel('Barometric Pressure (atm)')
legend('Pressure')

%% GetPlotCurrentDraw
timeCurrent=transpose(FIP_Data.Time_CurrentDraw_A_(isfinite(FIP_Data.Time_CurrentDraw_A_)));
dtI = mean(diff(timeBaro))
Current=transpose(FIP_Data.CurrentDraw_A_(isfinite(FIP_Data.CurrentDraw_A_)));
plot(timeCurrent,Current)
title('Current Draw Data')
xlabel('Time (s)')
ylabel('Current Draw (A)')
legend('Current')

%% GetPlotTemperature
timeTemperature=transpose(FIP_Data.Time_Temperature_F_(isfinite(FIP_Data.Time_Temperature_F_)));
dtT = mean(diff(timeTemperature))
temperature=transpose(FIP_Data.Temperature_F_(isfinite(FIP_Data.Temperature_F_)));
plot(timeTemperature,temperature)
title('Temperature Data')
xlabel('Time (s)')
ylabel('Temperature (F)')
legend('Temperature')

%% GetPlotVelocityData
timeVelC=transpose(FIP_Data.Time_Velocity_Accel_Ft_Sec_(isfinite(FIP_Data.Time_Velocity_Accel_Ft_Sec_)));
dtVC = mean(diff(timeVelC)) % The velocity calculated every 50 ms (coarse scale)
velC=transpose(FIP_Data.Velocity_Accel_Ft_Sec_(isfinite(FIP_Data.Velocity_Accel_Ft_Sec_)));
timeVelF=transpose(FIP_Data.Time__Velocity_Accel_Ft_Sec__(isfinite(FIP_Data.Time__Velocity_Accel_Ft_Sec__)));
dtVF = mean(diff(timeVelF)) % The velocity calculated every 2.5 ms (fine scale)
velF=transpose(FIP_Data.x_Velocity_Accel_Ft_Sec__(isfinite(FIP_Data.x_Velocity_Accel_Ft_Sec__)));
plot(timeVelC,velC,timeVelF,velF)
title('Velocity Data')
xlabel('Time (s)')
ylabel('Velocity (ft/s)')
legend('v_c', 'v_f')

%% GetPlotAltitudeData
% to be fixed
timeAltA=transpose(FIP_Data.Time__Altitude_Accel_Ft__(isfinite(FIP_Data.Time__Altitude_Accel_Ft__)));
dtAA = mean(diff(timeAltA)) % The altitude calculated from accelerometer
altA=transpose(FIP_Data.x_Altitude_Accel_Ft__(isfinite(FIP_Data.x_Altitude_Accel_Ft__)));
timeAltB=transpose(FIP_Data.Time__Altitude_Baro_Ft_AGL__(isfinite(FIP_Data.Time__Altitude_Baro_Ft_AGL__)));
dtAB = mean(diff(timeAltB)) % The altitude calculated from barometer
altB=transpose(FIP_Data.x_Altitude_Baro_Ft_AGL__(isfinite(FIP_Data.x_Altitude_Baro_Ft_AGL__)));
plot(timeAltA,altA,timeAltB,altB)
title('Altitude Data')
xlabel('Time (s)')
ylabel('Altitude AGL (ft)')
legend('x_a', 'x_b')
