
% Close all existing plots
close all

% Import the data
% Note: this function is one of matlab's automatic import scripts
[Date,Time,Callsign,Lat,Long,Alt,Invalid] = import_aprs('packets_processed.csv');

% Select data for only valid packets
% Logical Indexing FTW
Date = Date(Invalid == 0);
Time = Time(Invalid == 0);
Callsign = Callsign(Invalid == 0);
Lat = Lat(Invalid == 0);
Long = Long(Invalid == 0);
Alt = Alt(Invalid == 0);

% Select data for only packets from altitudes above 1000ft
% (to exlude some erroneous points from on the ground)
Lat = Lat(Alt > 1000);
Long = Long(Alt > 1000);
Alt = Alt(Alt > 1000);

% Convert altitude to meters
Alt = Alt * 0.3048; % [m]

% Plot Lat, Long, and Altitude in a 3d plot
plot3(Lat,Long,Alt)
hold on
% plot the evenly spaced red points along the track for velocity reference
plot3(Lat(1:3:end),Long(1:3:end),Alt (1:3:end),'.r')
% plot the green points on the ground to see the ground track
plot3(Lat(1:3:end),Long(1:3:end),0*Alt (1:3:end),'.g')

% label the plot
xlabel('Lattitude [deg]')
ylabel('Longitude [deg]')
zlabel('Altitude [m]')
title('Balloon Absolute Position')

% add gridlines
grid on


%% Relative plot

% create a new figure
figure()

% convert the lat/long to km
% these values are approximate (for 40deg latitude)! Should do actual
% conversion
lat_m = Lat * 111.03; % km
long_m = Long * 85.39; % km

% convert it to a measure of relative position with (0,0) at launch site
lat_m_rel = lat_m - lat_m(1);
long_m_rel = long_m - long_m(1);

% plot the relative Lat/Long/Alt in 3d
plot3(lat_m_rel,long_m_rel,Alt)
hold on
% plot the evenly spaced red points along the track for velocity reference
plot3(lat_m_rel(1:3:end),long_m_rel(1:3:end),Alt (1:3:end),'.r')
% plot the green points on the ground to see the ground track
plot3(lat_m_rel(1:3:end),long_m_rel(1:3:end),0*Alt (1:3:end),'.g')

% label the plot
grid on
xlabel('Lattitude [km]')
ylabel('Longitude [km]')
zlabel('Altitude [m]')
title('Balloon Relative Position')



