pos = readtable("rvpackard.txt");
d1 = readtable("RVPackard_20251022_GFET013_GFET016.csv");
d2 = readtable("RVPackard_20251023_GFET013_GFET016.csv");
d3 = readtable("RVPackard_20251025_2_GFET013_GFET016.csv");
d4 = readtable("RVPackard_20251025_GFET013_GFET016.csv");
d5 = readtable("RVPackard_20251026_GFET013_GFET016.csv");

data = [d1;d2;d3;d4;d5];
data = sortrows(data,'Datetime');

data_gfet013 = data(ismember(data.SensorID,'GliderFET013'),:);
data_gfet016 = data(ismember(data.SensorID,'GliderFET016'),:);
% QC
igood = data.Vrse < -.4 & data.Vrse > -.7;
data = data(igood,:);
%%
pos.Datetime = datetime(pos.unixtime, 'ConvertFrom', 'posixtime');
%%
% Convert datetime to numeric for interpolation
pos_time_numeric = posixtime(pos.Datetime);  % or datenum(pos.datetime)
data_time_numeric = posixtime(data.Datetime);
data.unixtime = data_time_numeric; % unix
% Interpolate latitude and longitude
data.latitude = interp1(pos_time_numeric, pos.latitude, data_time_numeric, 'linear');
data.longitude = interp1(pos_time_numeric, pos.longitude, data_time_numeric, 'linear');
%% Timetable
% Convert data to timetable and resample to 5 minutes
% Convert to timetable
data_tt = data;
SID = char(data_tt.SensorID);
SID = string(SID(:,end-1:end));
data_tt.SensorID = str2double(SID);
data_tt.COM = [];
data_tt = table2timetable(data_tt, 'RowTimes', 'Datetime');
data_tt_13 = data_tt(data_tt.SensorID == 13,:);
data_tt_16 = data_tt(data_tt.SensorID == 16,:);
% Resample to n-minute intervals using mean (or other method)
n=60;
data_tt_13 = retime(data_tt_13, 'regular', 'mean', 'TimeStep', minutes(n));
data_tt_16 = retime(data_tt_16, 'regular', 'mean', 'TimeStep', minutes(n));


data_tt = [data_tt_13; data_tt_16];
data_tt = sortrows(data_tt,"Datetime");
%% Create figure with color by date
fig = figure;
geobasemap("satellite")
geoscatter(data_tt.latitude, data_tt.longitude, 50, data_tt.unixtime, 'filled')
title(['Position Data Over Time, ', num2str(n), ' min resample'])
% Colorbar with datetime labels
c = colorbar;
c.Ticks = linspace(min(data_tt.unixtime), max(data_tt.unixtime), 6);
% Convert unix timestamps to datetime, then format
c.TickLabels = datestr(datetime(c.Ticks, 'ConvertFrom', 'posixtime'), 'mm/dd HH:MM');
saveas(fig,['map_',num2str(n),'min_resample.jpg'])
%% Create figure by vrse
fig = figure;
geobasemap("satellite")
geoscatter(data_tt_13.latitude, data_tt_13.longitude, 50, data_tt_13.Vrse, 'filled')
title(['Location by vrse GFET013, ', num2str(n), ' min resample'])
colorbar
saveas(fig,['GFET013_Vrse_UW_map_',num2str(n),' min_resample.jpg'])
%% Create figure by vrse
fig = figure;
geobasemap("satellite")
geoscatter(data_tt_16.latitude, data_tt_16.longitude, 50, data_tt_16.Vrse, 'filled')
title(['Location by vrse GFET016, ', num2str(n), ' min resample'])
colorbar
saveas(fig,['GFET016_Vrse_UW_map_',num2str(n),' min_resample.jpg'])