load('24403401_sat.mat','s')

%

s.o2satconc = reshape(calcO2sat(s.tc(:), s.psal(:)), size(s.pres));
s.o2anom = s.doxy - s.o2satconc;
idiel = 275:length(s.o2satper);
date = datetime(datevec(s.sdn(idiel) - 7/24));
date.TimeZone = "America/Los_Angeles";

%

figure(1)
tl = tiledlayout(3,1);
title(tl, [datestr(date(1)), ' to ', datestr(date(end))])
subtitle(tl,[num2str(length(s.o2anom(1,idiel))), ' profiles'])
ylabel(tl, 'O_2 Sat Anom')
xlabel(tl, 'Time Local')
nexttile
iupper = 1; % surface
meano2anom = mean(s.o2anom(iupper,idiel),1);

plot(date,meano2anom,'.') 


if length(iupper) > 1
    title(['mean depth ', num2str(mean(s.depth(iupper(1),idiel))), ' - ', num2str(mean(s.depth(iupper(end),idiel))), ' m'])
else
    title(['depth: ', num2str(mean(s.depth(iupper(1),idiel))),' m'])
end

nexttile
iupper = 7; % 5ish m
meano2anom = mean(s.o2anom(iupper,idiel),1);

plot(date,meano2anom,'.') 
if length(iupper) > 1
    title(['mean depth ', num2str(mean(s.depth(iupper(1),idiel))), ' - ', num2str(mean(s.depth(iupper(end),idiel))), ' m'])
else
    title(['depth: ', num2str(mean(s.depth(iupper(1),idiel))),' m'])
end

nexttile
iupper = 12; % 5ish m
meano2anom = mean(s.o2anom(iupper,idiel),1);

plot(date,meano2anom,'.') 
if length(iupper) > 1
    title(['mean depth ', num2str(mean(s.depth(iupper(1),idiel))), ' - ', num2str(mean(s.depth(iupper(end),idiel))), ' m'])
else
    title(['depth: ', num2str(mean(s.depth(iupper(1),idiel))),' m'])
end

%

iupper = 1:10;
figure(2)
tl = tiledlayout(1,1);
title(tl, [datestr(date(1)), ' to ', datestr(date(end))])
subtitle(tl,[num2str(length(s.o2anom(1,idiel))), ' profiles over ', num2str(height(num2str(unique(day(date))))), ' days'])

nexttile
pcolor(date,s.depth(iupper,idiel(1)),s.o2anom(iupper,idiel))
xticklabels([])
shading interp
cb1 = colorbar;
% caxis([3 9])
ylabel(cb1,'O_2 anom')
ylabel('depth')
if numel(date(1):.25:date(end)+1) < 15
    xticks(date(1):.25:date(end)+1)
else
    xticks(date(1):.5:date(end)+1)
end
set(gca,'YDir','reverse')
xtickformat('HH:mm')
xtickangle(45)
xlabel(tl,'Local time (hours:minutes)')

%

figure(3)
detrendmeano2anom = detrend(meano2anom,1);
plot(date',detrendmeano2anom)