exportpdf = 1;
gdata = readtable("Diel_GPP_log.csv");

mission = 7;
filename = char(gdata.Filename(mission));
load(filename, 's'); % .mat file
pdffilename = ['Mission_',num2str(gdata.MissionID(mission)),'_Profiles_164_588_PIcurve.pdf'];
idiel = 164:588;
% idiel = [164:313 360:588];
iupper = 1:5;
s.o2satconc = reshape(calcO2sat(s.tc(:), s.psal(:)), size(s.pres));
s.o2anom = s.doxy - s.o2satconc;
sdn = s.sdn(idiel)-7/24; % GMT to local
dt = datetime(datevec(sdn));
dt.TimeZone = 'America/Los_Angeles';

tt = timetable(dt,mean(s.o2anom(iupper,idiel),1)',mean(s.o2satper(iupper,idiel))',max(s.par(iupper,idiel))','VariableNames',{'meano2anom','meano2sat','PAR'});
tthourly = retime(tt,'hourly','mean');
ttdaily = retime(tt,'daily',@trapz);
PAR_int = ttdaily.PAR;
ttdaily = retime(tt,'daily',@range);
AmpGPP = ttdaily.meano2sat;
dO2anom = diff(tthourly.meano2anom);
dO2anom(dO2anom < -0.5 | dO2anom > 1) = NaN;
PAR = tthourly.PAR(1:end-1)+diff(tthourly.PAR)./2;
hr = hour(tthourly.dt(1:end-1));
imorn = hr < 15;

PARbin = [0:100:400 600:200:1000 1250 1500 2000 2500 3000];
dO2bin = bin_data_to_X_gpt(PAR, dO2anom, PARbin);
%%

if exportpdf == 1
    fig1 = figure('Visible','off');
else
    fig1 = figure('Visible','on');
end
tl = tiledlayout(1,2,"TileSpacing","tight");
title(tl,'Composite')
subtitle(tl, 'June 22 - July 13')
nexttile
plot(PAR, dO2anom, 'k.');
hold on
plot(PARbin(2:end), dO2bin, 'ko-', 'markerfacecolor', 'r');

xlabel('PAR')
ylabel('dO_2/dt')
title('PI curve')


nexttile
plot(PAR_int,AmpGPP,'o', 'markerfacecolor', 'r')
hold on
ls = lsline;
ls.Color = 'k';
xlabel('Daily Integrated PAR')
ylabel('O_2 Sat Amplitude (GPP)')
title('GPP vs PAR')
if exportpdf == 1
    exportgraphics(fig1,pdffilename, 'ContentType', 'vector', 'Append', true);
end

